install 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. ynh_script_progression --message="Starting CouchDB..." --weight=2
  42. flohmarkt_ynh_start_couchdb
  43. # get flohmarkt
  44. # suspecting that this deletes all other sources for YNH_APP_ID
  45. ynh_script_progression --message="Downloading flohmarkt..." --weight=2
  46. ynh_setup_source --dest_dir="$flohmarkt_app_dir"
  47. # setup python environment for flohmarkt
  48. ynh_script_progression --message="Creating python venv..." --weight=2
  49. ynh_secure_remove "$flohmarkt_venv_dir"
  50. flohmarkt_ynh_create_venv
  51. flohmarkt_ynh_venv_requirements
  52. # JwtSecret
  53. # workaround for https://github.com/YunoHost/issues/issues/2379
  54. jwtsecret=$(ynh_string_random -l 200 -f 'a-zA-Z0-9/+'; ynh_string_random -l 142 -f 'a-zA-Z0-9/+'; echo -n '==')
  55. ynh_app_setting_set --app=$app --key=jwtsecret --value="$jwtsecret"
  56. password_couchdb_flohmarkt=$(ynh_string_random --length=31 --filter='A-Za-z0-9_.:,')
  57. ynh_app_setting_set --app=$app --key=password_couchdb_flohmarkt --value="$password_couchdb_flohmarkt"
  58. # generate flohmarkt.conf
  59. ynh_script_progression --message="Adding flohmarkt.conf configuration..." --weight=2
  60. ynh_add_config --template="../conf/flohmarkt.conf" --destination="$flohmarkt_app_dir/flohmarkt.conf"
  61. # setup couchdb
  62. ynh_script_progression --message="Setup couchdb database and user for flohmarkt..." --weight=2
  63. flohmarkt_ynh_initialize_couchdb
  64. # SETUP LOGROTATE
  65. ynh_script_progression --message="Configuring log rotation..." --weight=2
  66. # Use logrotate to manage application logfile(s)
  67. ynh_use_logrotate --logfile=$flohmarkt_logfile
  68. # NGINX CONFIGURATION
  69. ynh_script_progression --message="Configuring NGINX web server..." --weight=3
  70. # Create a dedicated NGINX config
  71. ynh_add_nginx_config
  72. # systemd.service
  73. ynh_script_progression --message="Configuring a systemd service..." --weight=1
  74. # Create a dedicated systemd config
  75. ynh_add_systemd_config --service=$flohmarkt_filename
  76. # integrate into yunohost
  77. ynh_script_progression --message="Integrating service in YunoHost..." --weight=1
  78. yunohost service add $flohmarkt_filename --description="A decentral federated small advertisement platform" --log="$flohmarkt_logfile"
  79. # logfile contains possibly the secret setup URL
  80. ynh_script_progression --message="Setting permissions on logfile..." --weight=2
  81. mkdir -m755 -p "$flohmarkt_log_dir"
  82. touch "$flohmarkt_logfile"
  83. chown root:$app "$flohmarkt_logfile"
  84. chmod 660 "$flohmarkt_logfile"
  85. # set file permissions for install dir
  86. ynh_script_progression --message="Setting permissions on install directory..." --weight=2
  87. flohmarkt_ynh_set_permission
  88. # SETUP FAIL2BAN
  89. ynh_script_progression --message="Configuring Fail2Ban..." --weight=3
  90. ynh_add_fail2ban_config --logpath="$flohmarkt_logfile" --failregex='INFO: +<HOST>:\d+ - "POST /token HTTP/\d+\.\d+" 403 Forbidden' --max_retry=5
  91. # symlink data_dir and install_dir to make it easier to find the
  92. # files for a certain domain/path
  93. ynh_script_progression --message="Creating symlinks..." --weight=1
  94. flohmarkt_ynh_create_symlinks
  95. # start service
  96. ynh_script_progression --message="Debug before starting flohmarkt..." --weight=1
  97. ls -l $flohmarkt_logfile /bin/bash /usr/bin/bash || true
  98. ynh_script_progression --message="Starting flohmarkt..." --weight=10
  99. flohmarkt_ynh_start_service
  100. # integrate urlwatch
  101. ynh_script_progression --message="Configure urlwatch and its cron job..." --weight=1
  102. flohmarkt_ynh_urlwatch_cron
  103. ynh_script_progression --message="Installation of $id completed" --last