_common.sh 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. #!/bin/bash
  2. #=================================================
  3. # COMMON VARIABLES
  4. #=================================================
  5. ## new filenames starting 0.00~ynh5
  6. # make a filename/service name from domain/path
  7. if [[ "$path" == /* ]]; then
  8. url_path="${path:1}"
  9. fi
  10. if [[ "__${url_path}__" == '____' ]]; then
  11. flohmarkt_filename="$domain"
  12. else
  13. flohmarkt_filename="$domain-${url_path}"
  14. fi
  15. # this filename is used for logfile name and systemd.service name
  16. # and for symlinking install_dir and data_dir
  17. flohmarkt_filename="${YNH_APP_ID}_${flohmarkt_filename//[^A-Za-z0-9._-]/_}"
  18. # directory flohmarkts software is installed to
  19. # contains ./venv and ./src as sub-directories
  20. flohmarkt_install="$install_dir"
  21. flohmarkt_sym_install="$( dirname $flohmarkt_install )/$flohmarkt_filename"
  22. flohmarkt_venv_dir="${flohmarkt_install}/venv"
  23. flohmarkt_app_dir="${flohmarkt_install}/app"
  24. # directory containing logfiles
  25. flohmarkt_log_dir="/var/log/${app}"
  26. flohmarkt_sym_log_dir="/var/log/${flohmarkt_filename}"
  27. # filename for logfiles - ¡ojo! if not ends with .log will be interpreted
  28. # as a directory by ynh_use_logrotate
  29. # https://github.com/YunoHost/issues/issues/2383
  30. flohmarkt_logfile="${flohmarkt_log_dir}/app.log"
  31. # flohmarkt data_dir
  32. flohmarkt_data_dir="$data_dir"
  33. flohmarkt_sym_data_dir="$( dirname $flohmarkt_data_dir )/$flohmarkt_filename"
  34. ## old filenames before 0.00~ynh5 - for reference and needed to
  35. # migrate (see below)
  36. flohmarkt_old_install="/opt/flohmarkt"
  37. flohmarkt_old_venv_dir="${flohmarkt_old_install}/venv"
  38. flohmarkt_old_app_dir="${flohmarkt_old_install}/flohmarkt"
  39. flohmarkt_old_log_dir="/var/log/flohmarkt/"
  40. flohmarkt_old_service="flohmarkt"
  41. #=================================================
  42. # PERSONAL HELPERS
  43. #=================================================
  44. # set file permissions and owner for installation
  45. flohmarkt_ynh_set_permission() {
  46. # install dir - only root needs to write and $app reads
  47. chown root:$app -R "$flohmarkt_install"
  48. chmod g-w,o-rwx -R "$flohmarkt_install"
  49. }
  50. # start flohmarkt service
  51. flohmarkt_ynh_start_service() {
  52. ynh_systemd_action --service_name=$flohmarkt_filename --action="start" \
  53. --line_match='INFO: *Application startup complete.' --log_path="$flohmarkt_logfile" \
  54. --timeout=30
  55. }
  56. # stop flohmarkt service
  57. flohmarkt_ynh_stop_service() {
  58. ynh_systemd_action --service_name=$flohmarkt_filename --action="stop"
  59. }
  60. # start couchdb and wait for success
  61. flohmarkt_ynh_start_couchdb() {
  62. ynh_systemd_action --service_name=couchdb --action="start" --timeout=30 \
  63. --log_path="/var/log/couchdb/couchdb.log" \
  64. --line_match='Apache CouchDB has started on http://127.0.0.1'
  65. }
  66. # stop couchdb
  67. flohmarkt_ynh_stop_couchdb() {
  68. ynh_systemd_action --service_name=couchdb --action="stop" --timeout=30 \
  69. --log_path="/var/log/couchdb/couchdb.log" \
  70. --line_match='SIGTERM received - shutting down'
  71. }
  72. # install or upgrade couchdb
  73. flohmarkt_ynh_up_inst_couchdb() {
  74. echo "\
  75. couchdb couchdb/mode select standalone
  76. couchdb couchdb/mode seen true
  77. couchdb couchdb/bindaddress string 127.0.0.1
  78. couchdb couchdb/bindaddress seen true
  79. couchdb couchdb/cookie string $couchdb_magic_cookie
  80. couchdb couchdb/adminpass password $password_couchdb_admin
  81. couchdb couchdb/adminpass seen true
  82. couchdb couchdb/adminpass_again password $password_couchdb_admin
  83. couchdb couchdb/adminpass_again seen true" | debconf-set-selections
  84. DEBIAN_FRONTEND=noninteractive # apt-get install -y --force-yes couchdb
  85. ynh_install_extra_app_dependencies \
  86. --repo="deb https://apache.jfrog.io/artifactory/couchdb-deb/ $(lsb_release -c -s) main" \
  87. --key="https://couchdb.apache.org/repo/keys.asc" \
  88. --package="couchdb"
  89. }
  90. flohmarkt_ynh_dump_couchdb() {
  91. ../settings/scripts/couchdb-dump/couchdb-dump.sh -b -H 127.0.0.1 -d "${app}" \
  92. -q -u admin -p "${password_couchdb_admin}" -f "${YNH_CWD}/${app}.json"
  93. }
  94. flohmarkt_ynh_import_couchdb() {
  95. ls -l ../settings/scripts/couchdb-dump/couchdb-dump.sh ${YNH_CWD}/${app}.json
  96. ../settings/scripts/couchdb-dump/couchdb-dump.sh -r -c -H 127.0.0.1 -d "${app}" \
  97. -q -u admin -p "${password_couchdb_admin}" -f "${YNH_CWD}/${app}.json"
  98. }
  99. flohmarkt_ynh_delete_couchdb_user() {
  100. # https://codeberg.org/flohmarkt/flohmarkt_ynh/issues/46 - more than one revision?
  101. local couchdb_user_revision=$( curl -sX GET "http://127.0.0.1:5984/_users/org.couchdb.user%3A${app}" \
  102. --user "admin:${password_couchdb_admin}" | jq -r ._rev )
  103. curl -s -X DELETE "http://127.0.0.1:5984/_users/org.couchdb.user%3A${app}?rev=${couchdb_user_revision}" \
  104. --user "admin:${password_couchdb_admin}"
  105. }
  106. flohmarkt_ynh_delete_couchdb_db() {
  107. curl -s -X DELETE "http://127.0.0.1:5984/${app}" --user "admin:${password_couchdb_admin}"
  108. }
  109. flohmarkt_ynh_create_couchdb_user() {
  110. curl -s -X PUT "http://127.0.0.1:5984/_users/org.couchdb.user:${app}" --user "admin:${password_couchdb_admin}"\
  111. -H "Accept: application/json" -H "Content-Type: application/json" \
  112. -d "{\"name\": \"${app}\", \"password\": \"${password_couchdb_flohmarkt}\", \"roles\": [], \"type\": \"user\"}"
  113. # @@ check answer something like
  114. # {"ok":true,"id":"org.couchdb.user:flohmarkt","rev":"35-9865694604ab384388eea0f978a6e728"}
  115. }
  116. flohmarkt_ynh_couchdb_user_permissions() {
  117. curl -s -X PUT "http://127.0.0.1:5984/${app}/_security" --user "admin:${password_couchdb_admin}"\
  118. -H "Accept: application/json" -H "Content-Type: application/json" \
  119. -d "{\"members\":{\"names\": [\"${app}\"],\"roles\": [\"editor\"]}}"
  120. }
  121. flohmarkt_ynh_exists_couchdb_user() {
  122. if [[ $( curl -sX GET "http://127.0.0.1:5984/_users/org.couchdb.user%3A${app}" \
  123. --user "admin:${password_couchdb_admin}" | jq .error ) == '"not_found"' ]]
  124. then
  125. false
  126. else
  127. true
  128. fi
  129. }
  130. flohmarkt_ynh_exists_couchdb_db() {
  131. if [[ $( curl -sX GET "http://127.0.0.1:5984/flohmarkt__22" --user "admin:${password_couchdb_admin}" \
  132. | jq .error ) == '"not_found"' ]]
  133. then
  134. false
  135. else
  136. true
  137. fi
  138. }
  139. # check whether old couchdb user or database exist before creating the new ones
  140. flohmarkt_ynh_check_old_couchdb() {
  141. if flohmarkt_ynh_exists_couchdb_user; then
  142. ynh_die --ret_code=100 --message="CouchDB user '$app' exists already. Stopping install."
  143. elif flohmarkt_ynh_exists_couchdb_db; then
  144. ynh_die --ret_code=100 --message="CouchDB database '$app' exists already. Stopping install."
  145. fi
  146. }
  147. flohmarkt_ynh_restore_couchdb() {
  148. flohmarkt_ynh_check_old_couchdb
  149. flohmarkt_ynh_import_couchdb
  150. flohmarkt_ynh_create_couchdb_user
  151. flohmarkt_ynh_couchdb_user_permissions
  152. }
  153. # create venv
  154. flohmarkt_ynh_create_venv() {
  155. python3 -m venv --without-pip "$flohmarkt_venv_dir"
  156. }
  157. # install requirements.txt in venv
  158. flohmarkt_ynh_venv_requirements() {
  159. (
  160. set +o nounset
  161. source "$flohmarkt_venv_dir/bin/activate"
  162. set -o nounset
  163. set -x
  164. $flohmarkt_venv_dir/bin/python3 -m ensurepip
  165. $flohmarkt_venv_dir/bin/pip3 install -r "$flohmarkt_app_dir/requirements.txt"
  166. )
  167. }
  168. # move files and directories to their new places
  169. flohmarkt_ynh_upgrade_path_ynh5() {
  170. # flohmarkt and couchdb are already stopped in upgrade script
  171. # move app_dir into new 'app' folder
  172. mv "$flohmarkt_install/flohmarkt" "$flohmarkt_app_dir"
  173. # yunohost seems to move the venv dir automatically, but this
  174. # doesn't work, because the paths inside the venv are not adjusted
  175. # delete the old, not working venv and create a new one:
  176. ynh_secure_remove --file="$flohmarkt_venv_dir"
  177. flohmarkt_ynh_create_venv
  178. flohmarkt_ynh_venv_requirements
  179. # remove old $install_dir
  180. ynh_secure_remove --file="$flohmarkt_old_install"
  181. # move logfile directory
  182. mkdir -p "$flohmarkt_log_dir"
  183. # remove systemd.service - will be generated newly by upgrade
  184. # ynh_remove_systemd_config --service="$flohmarkt_old_service"
  185. ynh_systemd_action --action=stop --service_name="$flohmarkt_old_service"
  186. ynh_systemd_action --action=disable --service_name="$flohmarkt_old_service"
  187. ynh_secure_remove --file="/etc/systemd/system/multi-user.target.wants/flohmarkt.service"
  188. ynh_secure_remove --file="/etc/systemd/system/flohmarkt.service"
  189. # funktioniert nicht? issue?
  190. #ynh_systemd_action --action=daemon-reload
  191. # DEBUG + systemctl daemon-reload flohmarkt
  192. # WARNING Too many arguments.
  193. systemctl daemon-reload
  194. # unit flohmarkt is automatically appended and therefor this fails:
  195. #ynh_systemd_action --action=reset-failed
  196. systemctl reset-failed
  197. # create symlinks
  198. ln -s "$flohmarkt_install" "$flohmarkt_sym_install"
  199. ln -s "$flohmarkt_data_dir" "$flohmarkt_sym_data_dir"
  200. }
  201. #=================================================
  202. # EXPERIMENTAL HELPERS
  203. #=================================================
  204. #=================================================
  205. # FUTURE OFFICIAL HELPERS
  206. #=================================================