_common.sh 9.3 KB

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