install 5.8 KB

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