install 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 [[ -e /opt/couchdb ]] || pgrep epmd > /dev/null || pgrep beam.smp || dpkg-query -W couchdb > /dev/null 2>&1;
  8. then
  9. ynh_die --message="CouchDB already installed on this host - will not proceed."
  10. exit 1
  11. fi
  12. # INITIALIZE AND STORE SETTINGS
  13. # todo: do we need to store the password un-encrypted somewhere on the system?
  14. # → there's no way to get the admin password later if sometimes in the future deleting
  15. # the app will delete its specific database only (instead of the whole couchdb server as
  16. # of now in the earliest versions of the integration).
  17. ynh_app_setting_set --app=$app --key=password_couchdb_admin --value="$password_couchdb_admin"
  18. # get port, admin_pw for already installed couchdb
  19. # skip the installation steps below
  20. ynh_script_progression --message="Installing CouchDB..." --weight=60
  21. # A CouchDB node has an Erlang magic cookie value set at startup.
  22. # This value must match for all nodes in the cluster. If they do not match,
  23. # attempts to connect the node to the cluster will be rejected.
  24. couchdb_magic_cookie=$(ynh_string_random --length=23 --filter='A-Za-z0-9_')
  25. ynh_app_setting_set --app=$app --key=erlang_cookie --value="$couchdb_magic_cookie"
  26. echo "\
  27. couchdb couchdb/mode select standalone
  28. couchdb couchdb/mode seen true
  29. couchdb couchdb/bindaddress string 127.0.0.1
  30. couchdb couchdb/bindaddress seen true
  31. couchdb couchdb/cookie string $couchdb_magic_cookie
  32. couchdb couchdb/adminpass password $password_couchdb_admin
  33. couchdb couchdb/adminpass seen true
  34. couchdb couchdb/adminpass_again password $password_couchdb_admin
  35. couchdb couchdb/adminpass_again seen true" | debconf-set-selections
  36. DEBIAN_FRONTEND=noninteractive # apt-get install -y --force-yes couchdb
  37. ynh_install_extra_app_dependencies \
  38. --repo="deb https://apache.jfrog.io/artifactory/couchdb-deb/ $(lsb_release -c -s) main" \
  39. --key="https://couchdb.apache.org/repo/keys.asc" \
  40. --package="couchdb"
  41. # add couchdb configuration
  42. ynh_script_progression --message="Adding a configuration file..." --weight=2
  43. # customize couchdb config
  44. ynh_add_config --template="../conf/05-flohmarkt.ini" --destination="/opt/couchdb/etc/local.d/05-flohmarkt.ini"
  45. chown root:couchdb /opt/couchdb/etc/local.d/05-flohmarkt.ini
  46. chmod 640 /opt/couchdb/etc/local.d/05-flohmarkt.ini
  47. # @@ todo need to create a couchdb user and set the files to be readable/executable by it
  48. chmod 750 "$install_dir"
  49. chmod -R o-rwx "$install_dir"
  50. chown -R "$app:$app" "$install_dir"
  51. # restart couchdb to pick up changes
  52. systemctl restart couchdb
  53. systemctl status couchdb
  54. # INTEGRATE SERVICE IN YUNOHOST
  55. # would this work? couchdb brings its own systemd.service...
  56. # ynh_script_progression --message="Integrating service in YunoHost..." --weight=1
  57. # yunohost service add $app --description="war mal couchdb" --log="/var/log/$app/$app.log"
  58. # get flohmarkt
  59. ynh_setup_source --dest_dir="$install_dir/$app/"
  60. # setup python environment for flohmarkt
  61. ynh_secure_remove "$install_dir/venv"
  62. python3 -m venv --without-pip "$install_dir/venv"
  63. # install python dependencies
  64. (
  65. set +o nounset
  66. source "$install_dir/venv/bin/activate"
  67. set -o nounset
  68. set -x
  69. $install_dir/venv/bin/python3 -m ensurepip
  70. $install_dir/venv/bin/pip3 install -r "$install_dir/$app/requirements.txt"
  71. )
  72. # JwtSecret
  73. jwtsecret=$(ynh_string_random --length=344 --filter='a-zA-Z0-9/+')
  74. password_couchdb_flohmarkt=$(ynh_string_random --length=31 --filter='A-Za-z0-9_.:,')
  75. ynh_app_setting_set --app=$app --key=erlang_cookie --value="$password_couchdb_flohmarkt"
  76. # generate flohmarkt.conf
  77. ynh_add_config --template="../conf/flohmarkt.conf" --destination="$install_dir/$app/flohmarkt.conf"
  78. # setup couchdb
  79. (
  80. set +o nounset
  81. source "$install_dir/venv/bin/activate"
  82. set -o nounset
  83. cd "$install_dir/$app"
  84. # initialize_couchdb seems to re-try on connect problems endlessly blocking the yunohost api
  85. # give it 45 seconds to finish and then fail
  86. # https://codeberg.org/ChriChri/flohmarkt_ynh/issues/13
  87. timeout 45 python3 initialize_couchdb.py $password_couchdb_admin $password_couchdb_flohmarkt
  88. )
  89. # SETUP LOGROTATE
  90. ynh_script_progression --message="Configuring log rotation..." --weight=2
  91. # Use logrotate to manage application logfile(s)
  92. # @@ how does this know where the logfiles are?
  93. ynh_use_logrotate
  94. # NGINX CONFIGURATION
  95. ynh_script_progression --message="Configuring NGINX web server..." --weight=3
  96. # Create a dedicated NGINX config
  97. ynh_add_nginx_config
  98. # systemd.service
  99. ynh_script_progression --message="Configuring a systemd service..." --weight=1
  100. # Create a dedicated systemd config
  101. ynh_add_systemd_config
  102. # integrate into yunohost
  103. ynh_script_progression --message="Integrating service in YunoHost..." --weight=1
  104. yunohost service add $app --description="A decentral federated small advertisement platform" --log="/var/log/$app/$app.log"
  105. # start service
  106. ynh_systemd_action --service_name=$app --action="start"
  107. # SETUP FAIL2BAN
  108. # no need for couchdb, because it will not listen externally
  109. # ynh_script_progression --message="Configuring Fail2Ban..." --weight=3
  110. #
  111. # # Create a dedicated Fail2Ban config
  112. # ynh_add_fail2ban_config --logpath="/var/log/couchdb/couchdb.log" --failregex="[warning] .*couch_httpd_auth: Authentication failed for user .+ from <HOST>" --max_retry=5
  113. # @@ logrotation
  114. ynh_script_progression --message="Installation of $app completed" --last
  115. # qed