2024-02-21 16:50:15 +01:00
|
|
|
#!/bin/bash
|
|
|
|
|
2024-05-29 11:19:09 +02:00
|
|
|
|
|
|
|
|
2024-02-21 16:50:15 +01:00
|
|
|
#=================================================
|
|
|
|
# COMMON VARIABLES
|
|
|
|
#=================================================
|
|
|
|
|
2024-05-06 07:47:13 +02:00
|
|
|
## new filenames starting 0.00~ynh5
|
|
|
|
# make a filename/service name from domain/path
|
|
|
|
if [[ "$path" == /* ]]; then
|
|
|
|
url_path="${path:1}"
|
|
|
|
fi
|
|
|
|
if [[ "__${url_path}__" == '____' ]]; then
|
|
|
|
flohmarkt_filename="$domain"
|
|
|
|
else
|
|
|
|
flohmarkt_filename="$domain-${url_path}"
|
|
|
|
fi
|
|
|
|
# this filename is used for logfile name and systemd.service name
|
2024-05-07 12:18:36 +02:00
|
|
|
# and for symlinking install_dir and data_dir
|
|
|
|
flohmarkt_filename="${YNH_APP_ID}_${flohmarkt_filename//[^A-Za-z0-9._-]/_}"
|
2024-04-30 14:12:49 +02:00
|
|
|
# directory flohmarkts software is installed to
|
2024-05-06 07:47:13 +02:00
|
|
|
# contains ./venv and ./src as sub-directories
|
2024-05-07 12:18:36 +02:00
|
|
|
flohmarkt_install="$install_dir"
|
|
|
|
flohmarkt_sym_install="$( dirname $flohmarkt_install )/$flohmarkt_filename"
|
2024-05-06 07:47:13 +02:00
|
|
|
flohmarkt_venv_dir="${flohmarkt_install}/venv"
|
|
|
|
flohmarkt_app_dir="${flohmarkt_install}/app"
|
|
|
|
# directory containing logfiles
|
2024-05-15 13:38:45 +02:00
|
|
|
flohmarkt_log_dir="/var/log/${app}"
|
|
|
|
flohmarkt_sym_log_dir="/var/log/${flohmarkt_filename}"
|
2024-05-06 07:47:13 +02:00
|
|
|
# filename for logfiles - ¡ojo! if not ends with .log will be interpreted
|
|
|
|
# as a directory by ynh_use_logrotate
|
|
|
|
# https://github.com/YunoHost/issues/issues/2383
|
2024-05-15 13:38:45 +02:00
|
|
|
flohmarkt_logfile="${flohmarkt_log_dir}/app.log"
|
2024-05-07 12:18:36 +02:00
|
|
|
# flohmarkt data_dir
|
|
|
|
flohmarkt_data_dir="$data_dir"
|
|
|
|
flohmarkt_sym_data_dir="$( dirname $flohmarkt_data_dir )/$flohmarkt_filename"
|
2024-05-06 07:47:13 +02:00
|
|
|
|
|
|
|
## old filenames before 0.00~ynh5 - for reference and needed to
|
|
|
|
# migrate (see below)
|
2024-05-08 11:18:05 +02:00
|
|
|
flohmarkt_old_install="/opt/flohmarkt"
|
|
|
|
flohmarkt_old_venv_dir="${flohmarkt_old_install}/venv"
|
|
|
|
flohmarkt_old_app_dir="${flohmarkt_old_install}/flohmarkt"
|
|
|
|
flohmarkt_old_log_dir="/var/log/flohmarkt/"
|
|
|
|
flohmarkt_old_service="flohmarkt"
|
2024-04-30 14:12:49 +02:00
|
|
|
|
2024-02-21 16:50:15 +01:00
|
|
|
#=================================================
|
|
|
|
# PERSONAL HELPERS
|
|
|
|
#=================================================
|
|
|
|
|
2024-05-29 11:19:09 +02:00
|
|
|
# debug output for ynh_handle_getopts_args and ynh_local_curl
|
|
|
|
# otherwise not needed TODO delete after development of the two is done
|
2024-05-28 10:54:23 +02:00
|
|
|
flohmarkt_debug=0
|
2024-05-26 14:49:18 +02:00
|
|
|
flohmarkt_print_debug() {
|
2024-05-28 10:54:23 +02:00
|
|
|
if [[ $flohmarkt_debug -eq 1 ]]; then echo "flohmarkt_debug: $*" >&2; fi
|
2024-05-26 14:49:18 +02:00
|
|
|
}
|
2024-05-29 11:19:09 +02:00
|
|
|
# source own development version of ynh_handle_getopts_args and ynh_local_curl
|
|
|
|
source ynh_handle_getopts_args
|
|
|
|
source ynh_local_curl
|
|
|
|
# TODO delete above when local versions not needed anymore
|
2024-05-19 13:51:00 +02:00
|
|
|
|
|
|
|
# create symlinks containing domain and path for install, data and log directories
|
|
|
|
flohmarkt_ynh_create_symlinks() {
|
|
|
|
ynh_script_progression --message="Creating symlinks..." --weight=1
|
|
|
|
ln -s "$flohmarkt_install" "$flohmarkt_sym_install"
|
|
|
|
ln -s "$flohmarkt_data_dir" "$flohmarkt_sym_data_dir"
|
|
|
|
ln -s "$flohmarkt_log_dir" "$flohmarkt_sym_log_dir"
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2024-05-08 16:14:04 +02:00
|
|
|
# set file permissions and owner for installation
|
|
|
|
flohmarkt_ynh_set_permission() {
|
|
|
|
# install dir - only root needs to write and $app reads
|
|
|
|
chown root:$app -R "$flohmarkt_install"
|
|
|
|
chmod g-w,o-rwx -R "$flohmarkt_install"
|
|
|
|
}
|
|
|
|
|
2024-05-10 09:15:22 +02:00
|
|
|
# start flohmarkt service
|
2024-05-08 16:14:04 +02:00
|
|
|
flohmarkt_ynh_start_service() {
|
|
|
|
ynh_systemd_action --service_name=$flohmarkt_filename --action="start" \
|
|
|
|
--line_match='INFO: *Application startup complete.' --log_path="$flohmarkt_logfile" \
|
|
|
|
--timeout=30
|
|
|
|
}
|
|
|
|
|
2024-05-10 09:15:22 +02:00
|
|
|
# stop flohmarkt service
|
2024-05-08 16:14:04 +02:00
|
|
|
flohmarkt_ynh_stop_service() {
|
2024-05-10 09:15:22 +02:00
|
|
|
ynh_systemd_action --service_name=$flohmarkt_filename --action="stop"
|
|
|
|
}
|
|
|
|
|
|
|
|
# start couchdb and wait for success
|
|
|
|
flohmarkt_ynh_start_couchdb() {
|
|
|
|
ynh_systemd_action --service_name=couchdb --action="start" --timeout=30 \
|
|
|
|
--log_path="/var/log/couchdb/couchdb.log" \
|
|
|
|
--line_match='Apache CouchDB has started on http://127.0.0.1'
|
|
|
|
}
|
|
|
|
|
|
|
|
# stop couchdb
|
|
|
|
flohmarkt_ynh_stop_couchdb() {
|
|
|
|
ynh_systemd_action --service_name=couchdb --action="stop" --timeout=30 \
|
|
|
|
--log_path="/var/log/couchdb/couchdb.log" \
|
|
|
|
--line_match='SIGTERM received - shutting down'
|
|
|
|
}
|
|
|
|
|
|
|
|
# install or upgrade couchdb
|
|
|
|
flohmarkt_ynh_up_inst_couchdb() {
|
|
|
|
echo "\
|
|
|
|
couchdb couchdb/mode select standalone
|
|
|
|
couchdb couchdb/mode seen true
|
|
|
|
couchdb couchdb/bindaddress string 127.0.0.1
|
|
|
|
couchdb couchdb/bindaddress seen true
|
|
|
|
couchdb couchdb/cookie string $couchdb_magic_cookie
|
|
|
|
couchdb couchdb/adminpass password $password_couchdb_admin
|
|
|
|
couchdb couchdb/adminpass seen true
|
|
|
|
couchdb couchdb/adminpass_again password $password_couchdb_admin
|
|
|
|
couchdb couchdb/adminpass_again seen true" | debconf-set-selections
|
|
|
|
DEBIAN_FRONTEND=noninteractive # apt-get install -y --force-yes couchdb
|
|
|
|
ynh_install_extra_app_dependencies \
|
|
|
|
--repo="deb https://apache.jfrog.io/artifactory/couchdb-deb/ $(lsb_release -c -s) main" \
|
|
|
|
--key="https://couchdb.apache.org/repo/keys.asc" \
|
|
|
|
--package="couchdb"
|
2024-05-08 16:14:04 +02:00
|
|
|
}
|
|
|
|
|
2024-05-14 14:13:41 +02:00
|
|
|
flohmarkt_ynh_dump_couchdb() {
|
|
|
|
../settings/scripts/couchdb-dump/couchdb-dump.sh -b -H 127.0.0.1 -d "${app}" \
|
2024-05-14 20:40:51 +02:00
|
|
|
-q -u admin -p "${password_couchdb_admin}" -f "${YNH_CWD}/${app}.json"
|
|
|
|
}
|
|
|
|
|
|
|
|
flohmarkt_ynh_import_couchdb() {
|
|
|
|
ls -l ../settings/scripts/couchdb-dump/couchdb-dump.sh ${YNH_CWD}/${app}.json
|
|
|
|
../settings/scripts/couchdb-dump/couchdb-dump.sh -r -c -H 127.0.0.1 -d "${app}" \
|
|
|
|
-q -u admin -p "${password_couchdb_admin}" -f "${YNH_CWD}/${app}.json"
|
2024-05-14 14:13:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
flohmarkt_ynh_delete_couchdb_user() {
|
2024-05-29 11:19:09 +02:00
|
|
|
local couchdb_user_revision=$( ynh_local_curl -n -m GET -u admin -p "$password_couchdb_admin" \
|
2024-05-30 08:50:08 +02:00
|
|
|
"http://127.0.0.1:5984/_users/org.couchdb.user%3A${app}" | jq -r ._rev )
|
2024-05-29 11:19:09 +02:00
|
|
|
ynh_local_curl -n -m DELETE -u admin -p ${password_couchdb_admin} -l '"ok":true' \
|
2024-05-30 08:50:08 +02:00
|
|
|
"http://127.0.0.1:5984/_users/org.couchdb.user%3A${app}?rev=${couchdb_user_revision}"
|
2024-05-14 14:13:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
flohmarkt_ynh_create_couchdb_user() {
|
2024-05-29 11:19:09 +02:00
|
|
|
ynh_local_curl -n -m PUT -u admin -p "${password_couchdb_admin}" \
|
2024-05-14 14:13:41 +02:00
|
|
|
-H "Accept: application/json" -H "Content-Type: application/json" \
|
2024-05-29 11:19:09 +02:00
|
|
|
-d '{' \
|
|
|
|
-d "\"name\": \"${app}\", \"password\": \"${password_couchdb_flohmarkt}\"," \
|
|
|
|
-d '"roles": [], "type": "user"}' \
|
|
|
|
--line_match='"ok":true' \
|
|
|
|
"http://127.0.0.1:5984/_users/org.couchdb.user:${app}"
|
2024-05-14 14:13:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
flohmarkt_ynh_couchdb_user_permissions() {
|
2024-05-29 11:19:09 +02:00
|
|
|
ynh_local_curl -n -m PUT -u admin -p "$password_couchdb_admin" \
|
2024-05-14 14:13:41 +02:00
|
|
|
-H "Accept: application/json" -H "Content-Type: application/json" \
|
2024-05-29 11:19:09 +02:00
|
|
|
-d "{\"members\":{\"names\": [\"${app}\"],\"roles\": [\"editor\"]}}" \
|
|
|
|
--line_match='ok' \
|
|
|
|
"http://127.0.0.1:5984/${app}/_security"
|
2024-05-14 14:13:41 +02:00
|
|
|
}
|
|
|
|
|
2024-05-15 20:48:18 +02:00
|
|
|
flohmarkt_ynh_exists_couchdb_user() {
|
2024-05-29 11:19:09 +02:00
|
|
|
ynh_local_curl -n -m GET -u admin -p "$password_couchdb_admin" -l "\"_id\":\"org.couchdb.user:$app\"" \
|
|
|
|
"http://127.0.0.1:5984/_users/org.couchdb.user%3A${app}"
|
2024-05-15 20:48:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
flohmarkt_ynh_exists_couchdb_db() {
|
2024-05-29 11:19:09 +02:00
|
|
|
ynh_local_curl -n -m GET -u admin -p "$password_couchdb_admin" -l "\"db_name\":\"testing\"" \
|
|
|
|
"http://127.0.0.1:5984/${app}"
|
|
|
|
}
|
|
|
|
|
|
|
|
flohmarkt_ynh_delete_couchdb_db() {
|
|
|
|
ynh_local_curl -n -m DELETE -u admin -p "$password_couchdb_admin" \
|
|
|
|
--line_match='"ok":true' "http://127.0.0.1:5984/${app}"
|
2024-05-15 20:48:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# check whether old couchdb user or database exist before creating the new ones
|
|
|
|
flohmarkt_ynh_check_old_couchdb() {
|
|
|
|
if flohmarkt_ynh_exists_couchdb_user; then
|
|
|
|
ynh_die --ret_code=100 --message="CouchDB user '$app' exists already. Stopping install."
|
|
|
|
elif flohmarkt_ynh_exists_couchdb_db; then
|
|
|
|
ynh_die --ret_code=100 --message="CouchDB database '$app' exists already. Stopping install."
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2024-05-14 14:13:41 +02:00
|
|
|
flohmarkt_ynh_restore_couchdb() {
|
2024-05-15 20:48:18 +02:00
|
|
|
flohmarkt_ynh_check_old_couchdb
|
2024-05-14 14:13:41 +02:00
|
|
|
|
|
|
|
flohmarkt_ynh_import_couchdb
|
|
|
|
flohmarkt_ynh_create_couchdb_user
|
|
|
|
flohmarkt_ynh_couchdb_user_permissions
|
|
|
|
}
|
|
|
|
|
2024-05-08 11:18:05 +02:00
|
|
|
# create venv
|
|
|
|
flohmarkt_ynh_create_venv() {
|
|
|
|
python3 -m venv --without-pip "$flohmarkt_venv_dir"
|
|
|
|
}
|
|
|
|
|
|
|
|
# install requirements.txt in venv
|
|
|
|
flohmarkt_ynh_venv_requirements() {
|
|
|
|
(
|
|
|
|
set +o nounset
|
|
|
|
source "$flohmarkt_venv_dir/bin/activate"
|
|
|
|
set -o nounset
|
|
|
|
set -x
|
|
|
|
$flohmarkt_venv_dir/bin/python3 -m ensurepip
|
|
|
|
$flohmarkt_venv_dir/bin/pip3 install -r "$flohmarkt_app_dir/requirements.txt"
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-05-06 07:47:13 +02:00
|
|
|
# move files and directories to their new places
|
|
|
|
flohmarkt_ynh_upgrade_path_ynh5() {
|
|
|
|
# flohmarkt and couchdb are already stopped in upgrade script
|
2024-05-08 11:18:05 +02:00
|
|
|
|
|
|
|
# move app_dir into new 'app' folder
|
|
|
|
mv "$flohmarkt_install/flohmarkt" "$flohmarkt_app_dir"
|
|
|
|
|
|
|
|
# yunohost seems to move the venv dir automatically, but this
|
|
|
|
# doesn't work, because the paths inside the venv are not adjusted
|
|
|
|
# delete the old, not working venv and create a new one:
|
|
|
|
ynh_secure_remove --file="$flohmarkt_venv_dir"
|
|
|
|
flohmarkt_ynh_create_venv
|
|
|
|
flohmarkt_ynh_venv_requirements
|
|
|
|
# remove old $install_dir
|
|
|
|
ynh_secure_remove --file="$flohmarkt_old_install"
|
|
|
|
|
|
|
|
# move logfile directory
|
|
|
|
mkdir -p "$flohmarkt_log_dir"
|
|
|
|
|
|
|
|
# remove systemd.service - will be generated newly by upgrade
|
|
|
|
# ynh_remove_systemd_config --service="$flohmarkt_old_service"
|
|
|
|
ynh_systemd_action --action=stop --service_name="$flohmarkt_old_service"
|
|
|
|
ynh_systemd_action --action=disable --service_name="$flohmarkt_old_service"
|
|
|
|
ynh_secure_remove --file="/etc/systemd/system/multi-user.target.wants/flohmarkt.service"
|
|
|
|
ynh_secure_remove --file="/etc/systemd/system/flohmarkt.service"
|
|
|
|
# funktioniert nicht? issue?
|
|
|
|
#ynh_systemd_action --action=daemon-reload
|
|
|
|
# DEBUG + systemctl daemon-reload flohmarkt
|
|
|
|
# WARNING Too many arguments.
|
|
|
|
systemctl daemon-reload
|
|
|
|
# unit flohmarkt is automatically appended and therefor this fails:
|
|
|
|
#ynh_systemd_action --action=reset-failed
|
|
|
|
systemctl reset-failed
|
|
|
|
|
|
|
|
# create symlinks
|
|
|
|
ln -s "$flohmarkt_install" "$flohmarkt_sym_install"
|
|
|
|
ln -s "$flohmarkt_data_dir" "$flohmarkt_sym_data_dir"
|
2024-05-06 07:47:13 +02:00
|
|
|
}
|
|
|
|
|
2024-02-21 16:50:15 +01:00
|
|
|
#=================================================
|
|
|
|
# EXPERIMENTAL HELPERS
|
|
|
|
#=================================================
|
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# FUTURE OFFICIAL HELPERS
|
|
|
|
#=================================================
|