install 5.4 KB

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