install 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #!/bin/bash
  2. # IMPORT GENERIC HELPERS
  3. source _common.sh
  4. source /usr/share/yunohost/helpers
  5. # https://codeberg.org/ChriChri/flohmarkt_ynh/issues/9
  6. # check if couchdb is already installed
  7. # if there's a couchdb configuration file for flohmarkt we'll assume that couchdb
  8. # had been installed by flohmarkt and we thus can savely do whatever we want to it
  9. # with the resulting damage hopefully only influencing flohmarkt instances
  10. if [[ -e /opt/couchdb ]] || pgrep epmd > /dev/null || pgrep beam.smp || dpkg-query -W couchdb > /dev/null 2>&1 \
  11. && ![[ -e /opt/couchdb/etc/local.d/05-flohmarkt.ini ]];
  12. then
  13. ynh_die --message="CouchDB already installed on this host - will not proceed."
  14. exit 1
  15. fi
  16. # INITIALIZE AND STORE SETTINGS
  17. # todo: do we need to store the password un-encrypted somewhere on the system?
  18. # → there's no way to get the admin password later if sometimes in the future deleting
  19. # the app will delete its specific database only (instead of the whole couchdb server as
  20. # of now in the earliest versions of the integration).
  21. ynh_app_setting_set --app=$app --key=password_couchdb_admin --value="$password_couchdb_admin"
  22. # get port, admin_pw for already installed couchdb
  23. # skip the installation steps below
  24. ynh_script_progression --message="Installing CouchDB..." --weight=60
  25. # A CouchDB node has an Erlang magic cookie value set at startup.
  26. # This value must match for all nodes in the cluster. If they do not match,
  27. # attempts to connect the node to the cluster will be rejected.
  28. couchdb_magic_cookie=$(ynh_string_random --length=23 --filter='A-Za-z0-9_')
  29. ynh_app_setting_set --app=$app --key=couchdb_magic_cookie --value="$couchdb_magic_cookie"
  30. echo "\
  31. couchdb couchdb/mode select standalone
  32. couchdb couchdb/mode seen true
  33. couchdb couchdb/bindaddress string 127.0.0.1
  34. couchdb couchdb/bindaddress seen true
  35. couchdb couchdb/cookie string $couchdb_magic_cookie
  36. couchdb couchdb/adminpass password $password_couchdb_admin
  37. couchdb couchdb/adminpass seen true
  38. couchdb couchdb/adminpass_again password $password_couchdb_admin
  39. couchdb couchdb/adminpass_again seen true" | debconf-set-selections
  40. DEBIAN_FRONTEND=noninteractive # apt-get install -y --force-yes couchdb
  41. ynh_install_extra_app_dependencies \
  42. --repo="deb https://apache.jfrog.io/artifactory/couchdb-deb/ $(lsb_release -c -s) main" \
  43. --key="https://couchdb.apache.org/repo/keys.asc" \
  44. --package="couchdb"
  45. # add couchdb configuration
  46. ynh_script_progression --message="Adding a configuration file..." --weight=2
  47. # customize couchdb config
  48. ynh_add_config --template="../conf/05-flohmarkt.ini" --destination="/opt/couchdb/etc/local.d/05-flohmarkt.ini"
  49. chown root:couchdb /opt/couchdb/etc/local.d/05-flohmarkt.ini
  50. chmod 640 /opt/couchdb/etc/local.d/05-flohmarkt.ini
  51. # restart couchdb to pick up changes
  52. systemctl restart couchdb
  53. systemctl status couchdb
  54. # get flohmarkt
  55. ynh_setup_source --dest_dir="$flohmarkt_install"
  56. # setup python environment for flohmarkt
  57. ynh_secure_remove "$flohmarkt_venv_dir"
  58. python3 -m venv --without-pip "$flohmarkt_venv_dir"
  59. # install python dependencies
  60. (
  61. set +o nounset
  62. source "$flohmarkt_venv_dir/bin/activate"
  63. set -o nounset
  64. set -x
  65. $flohmarkt_venv_dir/bin/python3 -m ensurepip
  66. $flohmarkt_venv_dir/bin/pip3 install -r "$flohmarkt_install/requirements.txt"
  67. )
  68. # JwtSecret
  69. # workaround for https://github.com/YunoHost/issues/issues/2379
  70. jwtsecret=$(ynh_string_random -l 200 -f 'a-zA-Z0-9/+'; ynh_string_random -l 142 -f 'a-zA-Z0-9/+'; echo -n '==')
  71. ynh_app_setting_set --app=$app --key=jwtsecret --value="$jwtsecret"
  72. password_couchdb_flohmarkt=$(ynh_string_random --length=31 --filter='A-Za-z0-9_.:,')
  73. ynh_app_setting_set --app=$app --key=password_couchdb_flohmarkt --value="$password_couchdb_flohmarkt"
  74. # generate flohmarkt.conf
  75. ynh_add_config --template="../conf/flohmarkt.conf" --destination="$flohmarkt_install/flohmarkt.conf"
  76. # setup couchdb
  77. (
  78. set +o nounset
  79. source "$flohmarkt_venv_dir/bin/activate"
  80. set -o nounset
  81. cd "$flohmarkt_install"
  82. # initialize_couchdb seems to re-try on connect problems endlessly blocking the yunohost api
  83. # give it 45 seconds to finish and then fail
  84. # https://codeberg.org/ChriChri/flohmarkt_ynh/issues/13
  85. timeout 45 python3 initialize_couchdb.py $password_couchdb_admin $password_couchdb_flohmarkt
  86. )
  87. # SETUP LOGROTATE
  88. ynh_script_progression --message="Configuring log rotation..." --weight=2
  89. # Use logrotate to manage application logfile(s)
  90. ynh_use_logrotate
  91. # NGINX CONFIGURATION
  92. ynh_script_progression --message="Configuring NGINX web server..." --weight=3
  93. # Create a dedicated NGINX config
  94. ynh_add_nginx_config
  95. # systemd.service
  96. ynh_script_progression --message="Configuring a systemd service..." --weight=1
  97. # Create a dedicated systemd config
  98. ynh_add_systemd_config
  99. # integrate into yunohost
  100. ynh_script_progression --message="Integrating service in YunoHost..." --weight=1
  101. yunohost service add $app --description="A decentral federated small advertisement platform" --log="/var/log/$app/$app.log"
  102. # logfile contains possibly the secret setup URL
  103. mkdir -m755 -p "/var/log/$app"
  104. touch "/var/log/$app/$app.log"
  105. chmod 640 "/var/log/$app/$app.log"
  106. # start service
  107. ynh_systemd_action --service_name=$app --action="start"
  108. # SETUP FAIL2BAN
  109. ynh_script_progression --message="Configuring Fail2Ban..." --weight=3
  110. ynh_add_fail2ban_config --logpath="/var/log/$app/$app.log" --failregex='INFO: +<HOST>:\d+ - "POST /token HTTP/\d+\.\d+" 403 Forbidden' --max_retry=5
  111. ynh_script_progression --message="Installation of $app completed" --last