_common.sh 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  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. # Redisgn of ynh_handle_getopts_args for flohmarkt to be tested as `flohmarkt_ynh_handle_getopts_args`
  45. # Internal helper design to allow helpers to use getopts to manage their arguments
  46. #
  47. # [internal]
  48. #
  49. # example: function my_helper()
  50. # {
  51. # local -A args_array=( [a]=arg1= [b]=arg2= [c]=arg3 )
  52. # local arg1
  53. # local arg2
  54. # local arg3
  55. # ynh_handle_getopts_args "$@"
  56. #
  57. # [...]
  58. # }
  59. # my_helper --arg1 "val1" -b val2 -c
  60. #
  61. # usage: ynh_handle_getopts_args "$@"
  62. # | arg: $@ - Simply "$@" to tranfert all the positionnal arguments to the function
  63. #
  64. # This helper need an array, named "args_array" with all the arguments used by the helper
  65. # that want to use ynh_handle_getopts_args
  66. # Be carreful, this array has to be an associative array, as the following example:
  67. # local -A args_array=( [a]=arg1 [b]=arg2= [c]=arg3 )
  68. # Let's explain this array:
  69. # a, b and c are short options, -a, -b and -c
  70. # arg1, arg2 and arg3 are the long options associated to the previous short ones. --arg1, --arg2 and --arg3
  71. # For each option, a short and long version has to be defined.
  72. # Let's see something more significant
  73. # local -A args_array=( [u]=user [f]=finalpath= [d]=database )
  74. #
  75. # NB: Because we're using 'declare' without -g, the array will be declared as a local variable.
  76. #
  77. # Please keep in mind that the long option will be used as a variable to store the values for this option.
  78. # For the previous example, that means that $finalpath will be fill with the value given as argument for this option.
  79. #
  80. # Also, in the previous example, finalpath has a '=' at the end. That means this option need a value.
  81. # So, the helper has to be call with --finalpath /final/path, --finalpath=/final/path or -f /final/path,
  82. # the variable $finalpath will get the value /final/path
  83. # If there's many values for an option, -f /final /path, the value will be separated by a ';' $finalpath=/final;/path
  84. # For an option without value, like --user in the example, the helper can be called only with --user or -u. $user
  85. # will then get the value 1.
  86. #
  87. # To keep a retrocompatibility, a package can still call a helper, using getopts, with positional arguments.
  88. # The "legacy mode" will manage the positional arguments and fill the variable in the same order than they are given
  89. # in $args_array. e.g. for `my_helper "val1" val2`, arg1 will be filled with val1, and arg2 with val2.
  90. #
  91. # @@ TODO: explain $legacy_args and '--'
  92. # @@ '--' start processing the rest of the arguments as positional parameters in legacy mode
  93. # @@ $legacy_args The arguments positional parameters will be assign to
  94. # Needs to be composed of array keys of args_array. If a key for a predefined variable
  95. # is used multiple times the assigned values will be concatenated delimited by ';'.
  96. # If the long option variable to contain the data is predefined as an array (e.g. using
  97. # `local -a arg1` then multiple values will be assigned to its cells.
  98. # If the last positional parameter defined in legacy_args is defined as an array all
  99. # the leftover positional parameters will be assigned to its cells.
  100. #
  101. # Requires YunoHost version 3.2.2 or higher.
  102. flohmarkt_ynh_handle_getopts_args() {
  103. ## TODO: replace use of term 'legacy arg' throughout comments by 'positional parameter' if this works
  104. # Manage arguments only if there's some provided
  105. set +o xtrace # set +x
  106. if [ $# -eq 0 ]; then
  107. ynh_print_warn --message="ynh_handle_getopts_args called without arguments"
  108. return
  109. fi
  110. # Store arguments in an array to keep each argument separated
  111. local arguments=("$@")
  112. # For each option in the array, reduce to short options for getopts (e.g. for [u]=user, --user will be -u)
  113. # And built parameters string for getopts
  114. # ${!args_array[@]} is the list of all option_flags in the array (An option_flag is 'u' in [u]=user, user is a value)
  115. local getopts_parameters=""
  116. local option_flag=""
  117. ## go through all possible options and replace arguments with short versions
  118. echo "debug: arguments = '${arguments[@]}"
  119. echo "debug: args_array = '${!args_array[@]}'"
  120. for option_flag in "${!args_array[@]}"; do
  121. echo "debug: option_flag = $option_flag"
  122. # Concatenate each option_flags of the array to build the string of arguments for getopts
  123. # Will looks like 'abcd' for -a -b -c -d
  124. # If the value of an option_flag finish by =, it's an option with additionnal values. (e.g. --user bob or -u bob)
  125. # Check the last character of the value associate to the option_flag
  126. echo "debug: compare to '${args_array[$option_flag]: -1}'"
  127. if [ "${args_array[$option_flag]: -1}" = "=" ]; then
  128. # For an option with additionnal values, add a ':' after the letter for getopts.
  129. getopts_parameters="${getopts_parameters}${option_flag}:"
  130. else
  131. getopts_parameters="${getopts_parameters}${option_flag}"
  132. fi
  133. echo "debug: getopts_parameters = ${getopts_parameters}"
  134. # Check each argument given to the function
  135. local arg=""
  136. # ${#arguments[@]} is the size of the array
  137. ## for one possible option: look at each argument supplied:
  138. for arg in $(seq 0 $((${#arguments[@]} - 1))); do
  139. echo "debug: arg = '$arg', argument = '${arguments[arg]}'"
  140. # Replace long option with = (match the beginning of the argument)
  141. arguments[arg]="$(printf '%s\n' "${arguments[arg]}" | sed "s/^--${args_array[$option_flag]}/-${option_flag}/")"
  142. echo "debug: sed - printf '%s\n' \"${arguments[arg]}\" | sed \"s/^--${args_array[$option_flag]}/-${option_flag} /\""
  143. echo "debug: arg = '$arg', argument = '${arguments[arg]}'"
  144. # And long option without = (match the whole line)
  145. arguments[arg]="$(printf '%s\n' "${arguments[arg]}" | sed "s/^--${args_array[$option_flag]%=}$/-${option_flag}/")"
  146. echo "debug: sed - printf '%s\n' \"${arguments[arg]}\" | sed \"s/^--${args_array[$option_flag]%=}$/-${option_flag} /\""
  147. echo "debug: arg = '$arg', argument = '${arguments[arg]}'"
  148. done
  149. echo "debug: arguments = '${arguments[@]}'"
  150. done
  151. echo 'debug: ================= end first loop ================='
  152. # Parse the first argument, return the number of arguments to be shifted off the arguments array
  153. # The function call is necessary here to allow `getopts` to use $@
  154. parse_arg() {
  155. echo "debug: ========= parse_arg started ======== , arguments='$@', getopts_parameters: '$getopts_parameters'"
  156. for ME in "$@"; do
  157. echo "debug: '$ME'"
  158. done
  159. # Initialize the index of getopts
  160. OPTIND=1
  161. # getopts will fill $parameter with the letter of the option it has read.
  162. local parameter=""
  163. getopts ":$getopts_parameters" parameter || true
  164. echo "debug: after getopts - parameter='$parameter', OPTIND='$OPTIND', OPTARG='$OPTARG'"
  165. if [ "$parameter" = "?" ]; then
  166. ynh_die --message="Invalid argument: -${OPTARG:-}"
  167. echo "debug: Invalid argument: -${OPTARG:-}"
  168. exit 255
  169. elif [ "$parameter" = ":" ]; then
  170. ynh_die --message="-$OPTARG parameter requires an argument."
  171. echo "-$OPTARG parameter requires an argument."
  172. exit 255
  173. else
  174. # Use the long option, corresponding to the short option read by getopts, as a variable
  175. # (e.g. for [u]=user, 'user' will be used as a variable)
  176. # Also, remove '=' at the end of the long option
  177. # The variable name will be stored in 'option_var'
  178. option_var="${args_array[$parameter]%=}"
  179. # if there's a '=' at the end of the long option name, this option takes values
  180. if [ "${args_array[$parameter]: -1}" != "=" ]; then
  181. # no argument expected for option - set option variable to '1'
  182. # 'eval ${option_var}' will use the content of 'option_var'
  183. echo "debug: option_var='${option_var}', option_value='1'"
  184. option_value=1
  185. return 1
  186. else
  187. # remove leading and trailing spaces from OPTARG
  188. OPTARG="$( printf '%s' ${OPTARG} | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
  189. echo "debug: option_var='${option_var}', OPTARG='${OPTARG}'"
  190. option_value="${OPTARG}"
  191. return 2
  192. fi
  193. fi
  194. }
  195. # iterate over the arguments: if first argument starts with a '-' feed arguments to getopts
  196. # if first argument doesn't start with a '-' enter mode to read positional parameters
  197. local argument
  198. local positional_mode=0 # state is getopts mode at the beginning, not positional parameters
  199. local positional_count=0 # counter for legacy arguments
  200. local option_var='' # the variable name to be filled
  201. # Try to use legacy_args as a list of option_flag of the array args_array
  202. # Otherwise, fill it with getopts_parameters to get the option_flag.
  203. # (But an associative arrays isn't always sorted in the correct order...)
  204. # Remove all ':' in getopts_parameters, if used.
  205. legacy_args=${legacy_args:-${getopts_parameters//:/}}
  206. while [ ${#arguments} -ne 0 ]; do
  207. local shift_value=0
  208. local option_value='' # the value to be filled into ${!option_var}
  209. echo 'debug: ======= start while loop ======='
  210. argument=${arguments[0]}
  211. echo "debug: argument='$argument'"
  212. # if state once changed to positional parameter mode, all the rest of the arguments will
  213. # be interpreted in positional parameter mode even if they start with a '-'
  214. if [ $positional_mode == 0 ] && [ "${argument}" == '--' ];then
  215. echo "debug: found '--', start positional parameter mode"
  216. positional_mode=1
  217. shift_value=1
  218. elif [ $positional_mode == 0 ] && [ "${argument:0:1}" == '-' ]; then
  219. echo "debug: getopts, arguments='${arguments[@]}', starting parse_arg"
  220. parse_arg "${arguments[@]}"
  221. shift_value=$?
  222. else
  223. positional_mode=1 # set state to positional parameter mode
  224. # @@ set -x
  225. # @@ echo "! Helper used in positional parameter mode !" >/dev/null
  226. # @@ set +x
  227. echo "debug: positional parameter, argument='$argument'"
  228. # Get the option_flag from getopts_parameters by using the option_flag according to the
  229. # position of the argument.
  230. option_flag=${legacy_args:$positional_count:1}
  231. # increment counter for legacy_args if still args left. If no args left check if the
  232. # last arg is a predefined array and let it cells be filled. Otherwise complain and
  233. # return.
  234. echo "debug: positional_counter='$positional_count', max positional_counter='$(( ${#legacy_args} -1 ))'"
  235. if [[ $positional_count -le $((${#legacy_args} - 1)) ]]; then
  236. # set counter to for next option_flag to fill
  237. positional_count=$((positional_count+1))
  238. echo "debug: incremented positional_counter to '$positional_count'"
  239. # Use the long option, corresponding to the option_flag, as a variable
  240. # (e.g. for [u]=user, 'user' will be used as a variable)
  241. # Also, remove '=' at the end of the long option
  242. # The variable name will be stored in 'option_var'
  243. option_var="${args_array[$option_flag]%=}"
  244. elif [[ $positional_count -ge $((${#legacy_args} - 1)) ]] &&
  245. ! declare -p ${option_var} | grep '^declare -a'
  246. then
  247. # no more legacy_args to fill - legacy behaviour: complain and return
  248. ynh_print_warn --message="Too many arguments ! \"${arguments[$i]}\" will be ignored."
  249. return
  250. else
  251. echo "debug: array found - keep going"
  252. fi
  253. # Store each value given as argument in the corresponding variable
  254. # The values will be stored in the same order than $args_array
  255. # → eval ${option_var}+='"${arguments[$i]}"'
  256. # I'll store it later (see below), but why do they use in the old
  257. # routine '+=' to append if legacy mode would exclusively write one
  258. # value to each option and then stop filling variables at all?
  259. option_value=$argument
  260. # shift off one positional parameter
  261. shift_value=1
  262. fi
  263. # fill option_var with value found
  264. # if ${option_var} is an array, fill mutiple values as array cells
  265. # otherwise concatenate them seperated by ';'
  266. echo "debug: option_var '$option_var', option_value '$option_value'"
  267. if declare -p $option_var | grep '^declare -a ' > /dev/null; then
  268. # hurray it's an array
  269. echo "debug: hurray! '$option_var' is an array."
  270. eval ${option_var}+='("${option_value}")'
  271. elif [[ -z ${!option_var} ]]; then
  272. eval ${option_var}='"${option_value}"'
  273. else
  274. eval ${option_var}+='";${option_value}"'
  275. fi
  276. echo "debug: option_var '$option_var', option_value '${!option_var}'"
  277. # shift value off arguments array
  278. echo "debug: shifting '$shift_value' off arguments"
  279. arguments=("${arguments[@]:${shift_value}}")
  280. done
  281. # the former subroutine did this - no idea if it is expected somewhere
  282. unset legacy_args
  283. # re-enable trace
  284. set -o xtrace # set -x
  285. }
  286. # local copy of ynh_local_curl() to test some improvement
  287. # https://github.com/YunoHost/issues/issues/2396
  288. # https://codeberg.org/flohmarkt/flohmarkt_ynh/issues/51
  289. flohmarkt_ynh_local_curl() {
  290. # Curl abstraction to help with POST requests to local pages (such as installation forms)
  291. #
  292. # usage: ynh_local_curl "page" "key1=value1" "key2=value2" ...
  293. # | arg: -l --line_match: check answer for a regex to return true
  294. # | arg: -P --put: PUT instead of POST, requires --data (see below)
  295. # | arg: -H --header: add a header to the request (can be used multiple times)
  296. # | arg: -n --no_sleep: don't sleep 2 seconds https://github.com/YunoHost/yunohost/pull/547
  297. # | arg: -d --data: data to be used with PUT: one long string
  298. # | arg: or to be POSTed: multiple 'key=value'
  299. # | arg: -p --page: either the PAGE part in 'https://$domain/$path/PAGE' or an a
  300. # | arg: URL like 'http://doma.in/path/file.ext'
  301. # | arg: page - positional parameter legacy version of '--page'
  302. # | arg: key1=value1 - (Optional, POST only) legacy version of '--data' as positional parameter
  303. # | arg: key2=value2 - (Optional, POST only) Another POST key and corresponding value
  304. # | arg: ... - (Optional, POST only) More POST keys and values
  305. #
  306. # example: ynh_local_curl "/install.php?installButton" "foo=$var1" "bar=$var2"
  307. # → will open a POST request to "https://$domain/$path/install.php?installButton" posting "foo=$var1" and "bar=$var2"
  308. # example: ynh_local_curl -P --header "Accept: application/json" -H "Content-Type: application/json" \
  309. # --data "{\"members\":{\"names\": [\"${app}\"],\"roles\": [\"editor\"]}}" -l '"ok":true' \
  310. # "http://localhost:5984/"
  311. # → will open a POST request to "http://localhost:5984/" adding headers with "Accept: application/json"
  312. # and "Content-Type: application/json" sending the data from the "--data" argument. ynh_local_curl will
  313. # return with an error if the servers response does not match the extended regex '"ok":true'.
  314. #
  315. # For multiple calls, cookies are persisted between each call for the same app.
  316. #
  317. # `$domain` and `$path_url` need to be defined externally if the first form for the 'page' argument is used.
  318. #
  319. # Requires YunoHost version 2.6.4 or higher.
  320. # Declare an array to define the options of this helper.
  321. local legacy_args=pd
  322. local -A args_array=( [l]=line_match= [P]=put [H]=header= [n]=no_sleep [p]=page= [d]=data= )
  323. local line_match
  324. local put
  325. # @@ todo if the headers contain ';' somewhere it might be a problem to split them
  326. # apart correctly later, because all values are stored in $header seperated by
  327. # ';' like 'header1: value;header2: value'.
  328. # might be a good improvement to 'ynh_handle_getopts_args' to act differently if
  329. # e.g. $header had been defined as an array: https://stackoverflow.com/questions/14525296/how-do-i-check-if-variable-is-an-array
  330. local header
  331. local no_sleep
  332. local page
  333. local -a data
  334. # Manage arguments with getopts
  335. flohmarkt_ynh_handle_getopts_args "$@"
  336. # @@ debug
  337. set +x
  338. local V
  339. for V in line_match put header no_sleep page data; do
  340. declare -p $V
  341. done
  342. return
  343. # Define url of page to curl
  344. local local_page=$(ynh_normalize_url_path $1)
  345. local full_path=$path_url$local_page
  346. if [ "${path_url}" == "/" ]; then
  347. full_path=$local_page
  348. fi
  349. local full_page_url=https://localhost$full_path
  350. # Concatenate all other arguments with '&' to prepare POST data
  351. local POST_data=""
  352. local arg=""
  353. for arg in "${@:2}"; do
  354. POST_data="${POST_data}${arg}&"
  355. done
  356. if [ -n "$POST_data" ]; then
  357. # Add --data arg and remove the last character, which is an unecessary '&'
  358. POST_data="--data ${POST_data::-1}"
  359. fi
  360. # https://github.com/YunoHost/yunohost/pull/547
  361. # Wait untils nginx has fully reloaded (avoid curl fail with http2) unless disabled
  362. if ! [[ $no_sleep == 1 ]]; then
  363. sleep 2
  364. fi
  365. local cookiefile=/tmp/ynh-$app-cookie.txt
  366. touch $cookiefile
  367. chown root $cookiefile
  368. chmod 700 $cookiefile
  369. # @@ debug disabled
  370. # # Temporarily enable visitors if needed...
  371. # local visitors_enabled=$(ynh_permission_has_user "main" "visitors" && echo yes || echo no)
  372. # if [[ $visitors_enabled == "no" ]]; then
  373. # ynh_permission_update --permission "main" --add "visitors"
  374. # fi
  375. # Curl the URL
  376. curl --silent --show-error --insecure --location --header "Host: $domain" --resolve $domain:443:127.0.0.1 $POST_data "$full_page_url" --cookie-jar $cookiefile --cookie $cookiefile
  377. # @@ debug disabled
  378. # if [[ $visitors_enabled == "no" ]]; then
  379. # ynh_permission_update --permission "main" --remove "visitors"
  380. # fi
  381. }
  382. # create symlinks containing domain and path for install, data and log directories
  383. flohmarkt_ynh_create_symlinks() {
  384. ynh_script_progression --message="Creating symlinks..." --weight=1
  385. ln -s "$flohmarkt_install" "$flohmarkt_sym_install"
  386. ln -s "$flohmarkt_data_dir" "$flohmarkt_sym_data_dir"
  387. ln -s "$flohmarkt_log_dir" "$flohmarkt_sym_log_dir"
  388. true
  389. }
  390. # set file permissions and owner for installation
  391. flohmarkt_ynh_set_permission() {
  392. # install dir - only root needs to write and $app reads
  393. chown root:$app -R "$flohmarkt_install"
  394. chmod g-w,o-rwx -R "$flohmarkt_install"
  395. }
  396. # start flohmarkt service
  397. flohmarkt_ynh_start_service() {
  398. ynh_systemd_action --service_name=$flohmarkt_filename --action="start" \
  399. --line_match='INFO: *Application startup complete.' --log_path="$flohmarkt_logfile" \
  400. --timeout=30
  401. }
  402. # stop flohmarkt service
  403. flohmarkt_ynh_stop_service() {
  404. ynh_systemd_action --service_name=$flohmarkt_filename --action="stop"
  405. }
  406. # start couchdb and wait for success
  407. flohmarkt_ynh_start_couchdb() {
  408. ynh_systemd_action --service_name=couchdb --action="start" --timeout=30 \
  409. --log_path="/var/log/couchdb/couchdb.log" \
  410. --line_match='Apache CouchDB has started on http://127.0.0.1'
  411. }
  412. # stop couchdb
  413. flohmarkt_ynh_stop_couchdb() {
  414. ynh_systemd_action --service_name=couchdb --action="stop" --timeout=30 \
  415. --log_path="/var/log/couchdb/couchdb.log" \
  416. --line_match='SIGTERM received - shutting down'
  417. }
  418. # install or upgrade couchdb
  419. flohmarkt_ynh_up_inst_couchdb() {
  420. echo "\
  421. couchdb couchdb/mode select standalone
  422. couchdb couchdb/mode seen true
  423. couchdb couchdb/bindaddress string 127.0.0.1
  424. couchdb couchdb/bindaddress seen true
  425. couchdb couchdb/cookie string $couchdb_magic_cookie
  426. couchdb couchdb/adminpass password $password_couchdb_admin
  427. couchdb couchdb/adminpass seen true
  428. couchdb couchdb/adminpass_again password $password_couchdb_admin
  429. couchdb couchdb/adminpass_again seen true" | debconf-set-selections
  430. DEBIAN_FRONTEND=noninteractive # apt-get install -y --force-yes couchdb
  431. ynh_install_extra_app_dependencies \
  432. --repo="deb https://apache.jfrog.io/artifactory/couchdb-deb/ $(lsb_release -c -s) main" \
  433. --key="https://couchdb.apache.org/repo/keys.asc" \
  434. --package="couchdb"
  435. }
  436. flohmarkt_ynh_dump_couchdb() {
  437. ../settings/scripts/couchdb-dump/couchdb-dump.sh -b -H 127.0.0.1 -d "${app}" \
  438. -q -u admin -p "${password_couchdb_admin}" -f "${YNH_CWD}/${app}.json"
  439. }
  440. flohmarkt_ynh_import_couchdb() {
  441. ls -l ../settings/scripts/couchdb-dump/couchdb-dump.sh ${YNH_CWD}/${app}.json
  442. ../settings/scripts/couchdb-dump/couchdb-dump.sh -r -c -H 127.0.0.1 -d "${app}" \
  443. -q -u admin -p "${password_couchdb_admin}" -f "${YNH_CWD}/${app}.json"
  444. }
  445. flohmarkt_ynh_delete_couchdb_user() {
  446. # https://codeberg.org/flohmarkt/flohmarkt_ynh/issues/46 - more than one revision?
  447. local couchdb_user_revision=$( curl -sX GET "http://127.0.0.1:5984/_users/org.couchdb.user%3A${app}" \
  448. --user "admin:${password_couchdb_admin}" | jq -r ._rev )
  449. curl -s -X DELETE "http://127.0.0.1:5984/_users/org.couchdb.user%3A${app}?rev=${couchdb_user_revision}" \
  450. --user "admin:${password_couchdb_admin}"
  451. }
  452. flohmarkt_ynh_delete_couchdb_db() {
  453. curl -s -X DELETE "http://127.0.0.1:5984/${app}" --user "admin:${password_couchdb_admin}"
  454. }
  455. flohmarkt_ynh_create_couchdb_user() {
  456. curl -s -X PUT "http://127.0.0.1:5984/_users/org.couchdb.user:${app}" --user "admin:${password_couchdb_admin}"\
  457. -H "Accept: application/json" -H "Content-Type: application/json" \
  458. -d "{\"name\": \"${app}\", \"password\": \"${password_couchdb_flohmarkt}\", \"roles\": [], \"type\": \"user\"}"
  459. # @@ check answer something like
  460. # {"ok":true,"id":"org.couchdb.user:flohmarkt","rev":"35-9865694604ab384388eea0f978a6e728"}
  461. }
  462. flohmarkt_ynh_couchdb_user_permissions() {
  463. curl -s -X PUT "http://127.0.0.1:5984/${app}/_security" --user "admin:${password_couchdb_admin}"\
  464. -H "Accept: application/json" -H "Content-Type: application/json" \
  465. -d "{\"members\":{\"names\": [\"${app}\"],\"roles\": [\"editor\"]}}"
  466. }
  467. flohmarkt_ynh_exists_couchdb_user() {
  468. if [[ $( curl -sX GET "http://127.0.0.1:5984/_users/org.couchdb.user%3A${app}" \
  469. --user "admin:${password_couchdb_admin}" | jq .error ) == '"not_found"' ]]
  470. then
  471. false
  472. else
  473. true
  474. fi
  475. }
  476. flohmarkt_ynh_exists_couchdb_db() {
  477. if [[ $( curl -sX GET "http://127.0.0.1:5984/${app}" --user "admin:${password_couchdb_admin}" \
  478. | jq .error ) == '"not_found"' ]]
  479. then
  480. false
  481. else
  482. true
  483. fi
  484. }
  485. # check whether old couchdb user or database exist before creating the new ones
  486. flohmarkt_ynh_check_old_couchdb() {
  487. if flohmarkt_ynh_exists_couchdb_user; then
  488. ynh_die --ret_code=100 --message="CouchDB user '$app' exists already. Stopping install."
  489. elif flohmarkt_ynh_exists_couchdb_db; then
  490. ynh_die --ret_code=100 --message="CouchDB database '$app' exists already. Stopping install."
  491. fi
  492. }
  493. flohmarkt_ynh_restore_couchdb() {
  494. flohmarkt_ynh_check_old_couchdb
  495. flohmarkt_ynh_import_couchdb
  496. flohmarkt_ynh_create_couchdb_user
  497. flohmarkt_ynh_couchdb_user_permissions
  498. }
  499. # create venv
  500. flohmarkt_ynh_create_venv() {
  501. python3 -m venv --without-pip "$flohmarkt_venv_dir"
  502. }
  503. # install requirements.txt in venv
  504. flohmarkt_ynh_venv_requirements() {
  505. (
  506. set +o nounset
  507. source "$flohmarkt_venv_dir/bin/activate"
  508. set -o nounset
  509. set -x
  510. $flohmarkt_venv_dir/bin/python3 -m ensurepip
  511. $flohmarkt_venv_dir/bin/pip3 install -r "$flohmarkt_app_dir/requirements.txt"
  512. )
  513. }
  514. # move files and directories to their new places
  515. flohmarkt_ynh_upgrade_path_ynh5() {
  516. # flohmarkt and couchdb are already stopped in upgrade script
  517. # move app_dir into new 'app' folder
  518. mv "$flohmarkt_install/flohmarkt" "$flohmarkt_app_dir"
  519. # yunohost seems to move the venv dir automatically, but this
  520. # doesn't work, because the paths inside the venv are not adjusted
  521. # delete the old, not working venv and create a new one:
  522. ynh_secure_remove --file="$flohmarkt_venv_dir"
  523. flohmarkt_ynh_create_venv
  524. flohmarkt_ynh_venv_requirements
  525. # remove old $install_dir
  526. ynh_secure_remove --file="$flohmarkt_old_install"
  527. # move logfile directory
  528. mkdir -p "$flohmarkt_log_dir"
  529. # remove systemd.service - will be generated newly by upgrade
  530. # ynh_remove_systemd_config --service="$flohmarkt_old_service"
  531. ynh_systemd_action --action=stop --service_name="$flohmarkt_old_service"
  532. ynh_systemd_action --action=disable --service_name="$flohmarkt_old_service"
  533. ynh_secure_remove --file="/etc/systemd/system/multi-user.target.wants/flohmarkt.service"
  534. ynh_secure_remove --file="/etc/systemd/system/flohmarkt.service"
  535. # funktioniert nicht? issue?
  536. #ynh_systemd_action --action=daemon-reload
  537. # DEBUG + systemctl daemon-reload flohmarkt
  538. # WARNING Too many arguments.
  539. systemctl daemon-reload
  540. # unit flohmarkt is automatically appended and therefor this fails:
  541. #ynh_systemd_action --action=reset-failed
  542. systemctl reset-failed
  543. # create symlinks
  544. ln -s "$flohmarkt_install" "$flohmarkt_sym_install"
  545. ln -s "$flohmarkt_data_dir" "$flohmarkt_sym_data_dir"
  546. }
  547. #=================================================
  548. # EXPERIMENTAL HELPERS
  549. #=================================================
  550. #=================================================
  551. # FUTURE OFFICIAL HELPERS
  552. #=================================================