install 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. # update couchdb dpkg
  31. flohmarkt_ynh_up_inst_couchdb
  32. # stop couchdb
  33. flohmarkt_ynh_stop_couchdb
  34. # add couchdb configuration
  35. ynh_script_progression --message="Adding a configuration file..." --weight=2
  36. # customize couchdb config
  37. ynh_add_config --template="../conf/05-flohmarkt.ini" --destination="/opt/couchdb/etc/local.d/05-flohmarkt.ini"
  38. chown root:couchdb /opt/couchdb/etc/local.d/05-flohmarkt.ini
  39. chmod 640 /opt/couchdb/etc/local.d/05-flohmarkt.ini
  40. # restart couchdb to pick up changes
  41. flohmarkt_ynh_start_couchdb
  42. # get flohmarkt
  43. # suspecting that this deletes all other sources for YNH_APP_ID
  44. ynh_setup_source --dest_dir="$flohmarkt_app_dir"
  45. # setup python environment for flohmarkt
  46. ynh_secure_remove "$flohmarkt_venv_dir"
  47. flohmarkt_ynh_create_venv
  48. flohmarkt_ynh_venv_requirements
  49. # JwtSecret
  50. # workaround for https://github.com/YunoHost/issues/issues/2379
  51. jwtsecret=$(ynh_string_random -l 200 -f 'a-zA-Z0-9/+'; ynh_string_random -l 142 -f 'a-zA-Z0-9/+'; echo -n '==')
  52. ynh_app_setting_set --app=$app --key=jwtsecret --value="$jwtsecret"
  53. password_couchdb_flohmarkt=$(ynh_string_random --length=31 --filter='A-Za-z0-9_.:,')
  54. ynh_app_setting_set --app=$app --key=password_couchdb_flohmarkt --value="$password_couchdb_flohmarkt"
  55. # generate flohmarkt.conf
  56. ynh_add_config --template="../conf/flohmarkt.conf" --destination="$flohmarkt_app_dir/flohmarkt.conf"
  57. # setup couchdb
  58. (
  59. set +o nounset
  60. source "$flohmarkt_venv_dir/bin/activate"
  61. set -o nounset
  62. cd "$flohmarkt_app_dir"
  63. # initialize_couchdb seems to re-try on connect problems endlessly blocking the yunohost api
  64. # give it 45 seconds to finish and then fail
  65. # https://codeberg.org/ChriChri/flohmarkt_ynh/issues/13
  66. timeout 45 python3 initialize_couchdb.py $password_couchdb_admin
  67. )
  68. # SETUP LOGROTATE
  69. ynh_script_progression --message="Configuring log rotation..." --weight=2
  70. # Use logrotate to manage application logfile(s)
  71. ynh_use_logrotate --logfile=$flohmarkt_logfile
  72. # NGINX CONFIGURATION
  73. ynh_script_progression --message="Configuring NGINX web server..." --weight=3
  74. # Create a dedicated NGINX config
  75. ynh_add_nginx_config
  76. # systemd.service
  77. ynh_script_progression --message="Configuring a systemd service..." --weight=1
  78. # Create a dedicated systemd config
  79. ynh_add_systemd_config --service=$flohmarkt_filename
  80. # integrate into yunohost
  81. ynh_script_progression --message="Integrating service in YunoHost..." --weight=1
  82. yunohost service add $flohmarkt_filename --description="A decentral federated small advertisement platform" --log="$flohmarkt_logfile"
  83. # logfile contains possibly the secret setup URL
  84. ynh_script_progression --message="Setting permissions on logfile..." --weight=2
  85. mkdir -m755 -p "$flohmarkt_log_dir"
  86. touch "$flohmarkt_logfile"a
  87. chown root:$app "$flohmarkt_logfile"
  88. chmod 660 "$flohmarkt_logfile"
  89. # set file permissions for install dir
  90. ynh_script_progression --message="Setting permissions on install directory..." --weight=2
  91. flohmarkt_ynh_set_permission
  92. # SETUP FAIL2BAN
  93. ynh_script_progression --message="Configuring Fail2Ban..." --weight=3
  94. ynh_add_fail2ban_config --logpath="$flohmarkt_logfile" --failregex='INFO: +<HOST>:\d+ - "POST /token HTTP/\d+\.\d+" 403 Forbidden' --max_retry=5
  95. # symlink data_dir and install_dir to make it easier to find the
  96. # files for a certain domain/path
  97. ynh_script_progression --message="Creating symlinks..." --weight=1
  98. ln -s "$flohmarkt_install" "$flohmarkt_sym_install"
  99. ln -s "$flohmarkt_data_dir" "$flohmarkt_sym_data_dir"
  100. # start service
  101. ynh_script_progression --message="Debug before starting flohmarkt..." --weight=1
  102. ps axuf
  103. ls -l $flohmarkt_logfile /bin/bash /usr/bin/bash || true
  104. ynh_script_progression --message="Starting flohmarkt..." --weight=10
  105. flohmarkt_ynh_start_service
  106. ynh_script_progression --message="Installation of $id completed" --last