_common.sh 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. # debug output for ynh_handle_getopts_args and ynh_local_curl
  45. # otherwise not needed TODO delete after development of the two is done
  46. flohmarkt_debug=0
  47. flohmarkt_print_debug() {
  48. set +x
  49. if [[ $flohmarkt_debug -eq 1 ]]; then echo "flohmarkt_debug: $*" >&2; fi
  50. set -x
  51. }
  52. # source own development version of ynh_handle_getopts_args and ynh_local_curl
  53. source ynh_handle_getopts_args
  54. source ynh_local_curl
  55. # TODO delete above when local versions not needed anymore
  56. # create symlinks containing domain and path for install, data and log directories
  57. flohmarkt_ynh_create_symlinks() {
  58. ynh_script_progression --message="Creating symlinks..." --weight=1
  59. ln -s "$flohmarkt_install" "$flohmarkt_sym_install"
  60. ln -s "$flohmarkt_data_dir" "$flohmarkt_sym_data_dir"
  61. ln -s "$flohmarkt_log_dir" "$flohmarkt_sym_log_dir"
  62. true
  63. }
  64. # set file permissions and owner for installation
  65. flohmarkt_ynh_set_permission() {
  66. # install dir - only root needs to write and $app reads
  67. chown root:$app -R "$flohmarkt_install"
  68. chmod g-w,o-rwx -R "$flohmarkt_install"
  69. }
  70. # start flohmarkt service
  71. flohmarkt_ynh_start_service() {
  72. ynh_systemd_action --service_name=$flohmarkt_filename --action="start" \
  73. --line_match='INFO: *Application startup complete.' --log_path="$flohmarkt_logfile" \
  74. --timeout=30
  75. }
  76. # stop flohmarkt service
  77. flohmarkt_ynh_stop_service() {
  78. ynh_systemd_action --service_name=$flohmarkt_filename --action="stop"
  79. }
  80. # start couchdb and wait for success
  81. flohmarkt_ynh_start_couchdb() {
  82. ynh_systemd_action --service_name=couchdb --action="start" --timeout=30 \
  83. --log_path="/var/log/couchdb/couchdb.log" \
  84. --line_match='Apache CouchDB has started on http://127.0.0.1'
  85. }
  86. # stop couchdb
  87. flohmarkt_ynh_stop_couchdb() {
  88. ynh_systemd_action --service_name=couchdb --action="stop" --timeout=30 \
  89. --log_path="/var/log/couchdb/couchdb.log" \
  90. --line_match='SIGTERM received - shutting down'
  91. }
  92. # install or upgrade couchdb
  93. flohmarkt_ynh_up_inst_couchdb() {
  94. echo "\
  95. couchdb couchdb/mode select standalone
  96. couchdb couchdb/mode seen true
  97. couchdb couchdb/bindaddress string 127.0.0.1
  98. couchdb couchdb/bindaddress seen true
  99. couchdb couchdb/cookie string $couchdb_magic_cookie
  100. couchdb couchdb/adminpass password $password_couchdb_admin
  101. couchdb couchdb/adminpass seen true
  102. couchdb couchdb/adminpass_again password $password_couchdb_admin
  103. couchdb couchdb/adminpass_again seen true" | debconf-set-selections
  104. DEBIAN_FRONTEND=noninteractive # apt-get install -y --force-yes couchdb
  105. ynh_install_extra_app_dependencies \
  106. --repo="deb https://apache.jfrog.io/artifactory/couchdb-deb/ $(lsb_release -c -s) main" \
  107. --key="https://couchdb.apache.org/repo/keys.asc" \
  108. --package="couchdb"
  109. }
  110. flohmarkt_ynh_dump_couchdb() {
  111. ../settings/scripts/couchdb-dump/couchdb-dump.sh -b -H 127.0.0.1 -d "${app}" \
  112. -q -u admin -p "${password_couchdb_admin}" -f "${YNH_CWD}/${app}.json"
  113. }
  114. flohmarkt_ynh_import_couchdb() {
  115. ls -l ../settings/scripts/couchdb-dump/couchdb-dump.sh ${YNH_CWD}/${app}.json
  116. ../settings/scripts/couchdb-dump/couchdb-dump.sh -r -c -H 127.0.0.1 -d "${app}" \
  117. -q -u admin -p "${password_couchdb_admin}" -f "${YNH_CWD}/${app}.json"
  118. }
  119. flohmarkt_ynh_delete_couchdb_user() {
  120. local couchdb_user_revision=$( ynh_local_curl -n -m GET -u admin -p "$password_couchdb_admin" \
  121. "http://localhost:5984/_users/org.couchdb.user%3A${app}" | jq -r ._rev )
  122. ynh_local_curl -n -m DELETE -u admin -p ${password_couchdb_admin} -l '"ok":true' \
  123. "http://localhost:5984/_users/org.couchdb.user%3A${app}?rev=${couchdb_user_revision}"
  124. }
  125. flohmarkt_ynh_create_couchdb_user() {
  126. ynh_local_curl -n -m PUT -u admin -p "${password_couchdb_admin}" \
  127. -H "Accept: application/json" -H "Content-Type: application/json" \
  128. -d '{' \
  129. -d "\"name\": \"${app}\", \"password\": \"${password_couchdb_flohmarkt}\"," \
  130. -d '"roles": [], "type": "user"}' \
  131. --line_match='"ok":true' \
  132. "http://127.0.0.1:5984/_users/org.couchdb.user:${app}"
  133. }
  134. flohmarkt_ynh_couchdb_user_permissions() {
  135. ynh_local_curl -n -m PUT -u admin -p "$password_couchdb_admin" \
  136. -H "Accept: application/json" -H "Content-Type: application/json" \
  137. -d "{\"members\":{\"names\": [\"${app}\"],\"roles\": [\"editor\"]}}" \
  138. --line_match='ok' \
  139. "http://127.0.0.1:5984/${app}/_security"
  140. }
  141. flohmarkt_ynh_exists_couchdb_user() {
  142. ynh_local_curl -n -m GET -u admin -p "$password_couchdb_admin" -l "\"_id\":\"org.couchdb.user:$app\"" \
  143. "http://127.0.0.1:5984/_users/org.couchdb.user%3A${app}"
  144. }
  145. flohmarkt_ynh_exists_couchdb_db() {
  146. ynh_local_curl -n -m GET -u admin -p "$password_couchdb_admin" -l "\"db_name\":\"testing\"" \
  147. "http://127.0.0.1:5984/${app}"
  148. }
  149. flohmarkt_ynh_delete_couchdb_db() {
  150. ynh_local_curl -n -m DELETE -u admin -p "$password_couchdb_admin" \
  151. --line_match='"ok":true' "http://127.0.0.1:5984/${app}"
  152. }
  153. # check whether old couchdb user or database exist before creating the new ones
  154. flohmarkt_ynh_check_old_couchdb() {
  155. if flohmarkt_ynh_exists_couchdb_user; then
  156. ynh_die --ret_code=100 --message="CouchDB user '$app' exists already. Stopping install."
  157. elif flohmarkt_ynh_exists_couchdb_db; then
  158. ynh_die --ret_code=100 --message="CouchDB database '$app' exists already. Stopping install."
  159. fi
  160. }
  161. flohmarkt_ynh_restore_couchdb() {
  162. flohmarkt_ynh_check_old_couchdb
  163. flohmarkt_ynh_import_couchdb
  164. flohmarkt_ynh_create_couchdb_user
  165. flohmarkt_ynh_couchdb_user_permissions
  166. }
  167. # create venv
  168. flohmarkt_ynh_create_venv() {
  169. python3 -m venv --without-pip "$flohmarkt_venv_dir"
  170. }
  171. # install requirements.txt in venv
  172. flohmarkt_ynh_venv_requirements() {
  173. (
  174. set +o nounset
  175. source "$flohmarkt_venv_dir/bin/activate"
  176. set -o nounset
  177. set -x
  178. $flohmarkt_venv_dir/bin/python3 -m ensurepip
  179. $flohmarkt_venv_dir/bin/pip3 install -r "$flohmarkt_app_dir/requirements.txt"
  180. )
  181. }
  182. # move files and directories to their new places
  183. flohmarkt_ynh_upgrade_path_ynh5() {
  184. # flohmarkt and couchdb are already stopped in upgrade script
  185. # move app_dir into new 'app' folder
  186. mv "$flohmarkt_install/flohmarkt" "$flohmarkt_app_dir"
  187. # yunohost seems to move the venv dir automatically, but this
  188. # doesn't work, because the paths inside the venv are not adjusted
  189. # delete the old, not working venv and create a new one:
  190. ynh_secure_remove --file="$flohmarkt_venv_dir"
  191. flohmarkt_ynh_create_venv
  192. flohmarkt_ynh_venv_requirements
  193. # remove old $install_dir
  194. ynh_secure_remove --file="$flohmarkt_old_install"
  195. # move logfile directory
  196. mkdir -p "$flohmarkt_log_dir"
  197. # remove systemd.service - will be generated newly by upgrade
  198. # ynh_remove_systemd_config --service="$flohmarkt_old_service"
  199. ynh_systemd_action --action=stop --service_name="$flohmarkt_old_service"
  200. ynh_systemd_action --action=disable --service_name="$flohmarkt_old_service"
  201. ynh_secure_remove --file="/etc/systemd/system/multi-user.target.wants/flohmarkt.service"
  202. ynh_secure_remove --file="/etc/systemd/system/flohmarkt.service"
  203. # funktioniert nicht? issue?
  204. #ynh_systemd_action --action=daemon-reload
  205. # DEBUG + systemctl daemon-reload flohmarkt
  206. # WARNING Too many arguments.
  207. systemctl daemon-reload
  208. # unit flohmarkt is automatically appended and therefor this fails:
  209. #ynh_systemd_action --action=reset-failed
  210. systemctl reset-failed
  211. # create symlinks
  212. ln -s "$flohmarkt_install" "$flohmarkt_sym_install"
  213. ln -s "$flohmarkt_data_dir" "$flohmarkt_sym_data_dir"
  214. }
  215. #=================================================
  216. # EXPERIMENTAL HELPERS
  217. #=================================================
  218. #=================================================
  219. # FUTURE OFFICIAL HELPERS
  220. #=================================================