From 2fca6c3e68833b758c87bacf616aec2eeb3f19f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20Pi=C3=A9dallu?= Date: Fri, 19 Jan 2024 16:41:23 +0100 Subject: [PATCH] Cleanup, fixes --- scripts/_common.sh | 15 --------------- scripts/install | 29 +++++++++++++---------------- scripts/remove | 18 +++++------------- scripts/restore | 2 +- scripts/upgrade | 23 ++++++++--------------- 5 files changed, 27 insertions(+), 60 deletions(-) diff --git a/scripts/_common.sh b/scripts/_common.sh index bd82a93..a187a4a 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -4,8 +4,6 @@ # COMMON VARIABLES #================================================= - - #================================================= # PERSONAL HELPERS #================================================= @@ -30,19 +28,6 @@ _yunomonitor_remove_nginx_config() { # EXPERIMENTAL HELPERS #================================================= -ynh_export () { - local ynh_arg="" - for var in $@; - do - ynh_arg=$(echo $var | awk '{print toupper($0)}') - if [ "$var" == "path" ]; then - ynh_arg="PATH" - fi - ynh_arg="YNH_APP_ARG_$ynh_arg" - export $var="${!ynh_arg}" - done -} - #================================================= # FUTURE OFFICIAL HELPERS #================================================= diff --git a/scripts/install b/scripts/install index 0bf635b..5f2e057 100755 --- a/scripts/install +++ b/scripts/install @@ -10,7 +10,7 @@ source _common.sh source /usr/share/yunohost/helpers #================================================= -# STOREĀ SETTINGS +# INITIALIZE AND STORE SETTINGS #================================================= domain=$(yunohost domain list --json | jq -r '.["main"]') @@ -19,10 +19,6 @@ path=/.well-known/yunomonitor/ ynh_app_setting_set --app="$app" --key="domain" --value="$domain" ynh_app_setting_set --app="$app" --key="path" --value="$path" -# FIXME: required? -ynh_export monitored_servers monitoring_servers mails sms_api - - #================================================= # DOWNLOAD, CHECK AND UNPACK SOURCE #================================================= @@ -49,7 +45,7 @@ _yunomonitor_add_nginx_config ynh_script_progression --message="Building Yunomonitor..." pushd "$install_dir" - python3 -m pip install pycryptodome + python3 -m pip install pycryptodome pyyaml popd #================================================= @@ -69,15 +65,17 @@ systemctl start "$app.timer" #================================================= ynh_script_progression --message="Adding a configuration file..." -echo "mails: [$mails]" > $install_dir/conf/$app.yml -echo "sms_apis: [$sms_api]" >> $install_dir/conf/$app.yml -echo "monitored_servers: [$monitored_servers]" >> $install_dir/conf/$app.yml -echo "monitoring_servers: [$monitoring_servers]" >> $install_dir/conf/$app.yml -echo "cachet_apis: []" >> $install_dir/conf/$app.yml -sed -i 's/,/","/g' $install_dir/conf/$app.yml -sed -i 's/\[/["/g' $install_dir/conf/$app.yml -sed -i 's/\]/"]/g' $install_dir/conf/$app.yml -sed -i 's/\[""\]/[]/g' $install_dir/conf/$app.yml +python3 -c ' +import sys +import yaml +print(yaml.dumps({ + "mails": sys.argv[1].split(","), + "sms_apis": sys.argv[2].split(","), + "monitored_servers": sys.argv[3].split(","), + "monitoring_servers": sys.argv[4].split(","), +})) +' "$mails" "$sms_api" "$monitored_servers" "$monitoring_servers" \ +> "$install_dir/conf/$app.yml" # Calculate and store the config file checksum into the app settings ynh_store_file_checksum --file="$install_dir/conf/$app.yml" @@ -88,7 +86,6 @@ ynh_store_file_checksum --file="$install_dir/conf/$app.yml" echo "method level server message target" > "$install_dir/conf/ignore_alert.csv" - # Set permissions to app files chmod u+x "$install_dir/yunomonitor.py" chmod u+w "$install_dir/conf" diff --git a/scripts/remove b/scripts/remove index 01b179c..1dd02fa 100755 --- a/scripts/remove +++ b/scripts/remove @@ -9,24 +9,16 @@ source _common.sh source /usr/share/yunohost/helpers -#================================================= -# STANDARD REMOVE -#================================================= -# STOP AND REMOVE SERVICE -#================================================= - -# Remove the service from the list of services known by YunoHost (added from `yunohost service add`) -if ynh_exec_warn_less yunohost service status $app >/dev/null -then - ynh_script_progression --message="Removing $app service integration..." --weight=3 - yunohost service remove "$app" -fi - #================================================= # REMOVE SYSTEM CONFIGURATIONS #================================================= ynh_script_progression --message="Removing system configurations related to $app..." --weight=1 +# Remove the service from the list of services known by YunoHost (added from `yunohost service add`) +if ynh_exec_warn_less yunohost service status "$app" >/dev/null; then + yunohost service remove "$app" +fi + ynh_systemd_action --service_name="$app" --action="stop" --log_path="systemd" --timeout=15 ynh_secure_remove --file="/etc/systemd/system/$app.timer" diff --git a/scripts/restore b/scripts/restore index 6e86890..0af8da9 100755 --- a/scripts/restore +++ b/scripts/restore @@ -35,7 +35,7 @@ chmod u+w "$install_dir/well-known" ynh_script_progression --message="Building Yunomonitor..." pushd "$install_dir" - python3 -m pip install pycryptodome + python3 -m pip install pycryptodome popd #================================================= diff --git a/scripts/upgrade b/scripts/upgrade index 33016f7..de70d05 100755 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -9,8 +9,6 @@ source _common.sh source /usr/share/yunohost/helpers -upgrade_type=$(ynh_check_app_version_changed) - #================================================= # STANDARD UPGRADE STEPS #================================================= @@ -18,25 +16,22 @@ upgrade_type=$(ynh_check_app_version_changed) #================================================= ynh_script_progression --message="Ensuring downward compatibility..." --weight=1 -if ! [ -f $install_dir/conf/ignore_alert.csv ]; then - echo "method level server message target" > $install_dir/conf/ignore_alert.csv +if ! [ -f "$install_dir/conf/ignore_alert.csv" ]; then + echo "method level server message target" > "$install_dir/conf/ignore_alert.csv" fi #================================================= # DOWNLOAD, CHECK AND UNPACK SOURCE #================================================= +ynh_script_progression --message="Upgrading source files..." --weight=1 -if [ "$upgrade_type" == "UPGRADE_APP" ] -then - ynh_script_progression --message="Upgrading source files..." --weight=1 - - # Download, check integrity, uncompress and patch the source from app.src - ynh_setup_source --dest_dir="$install_dir" -fi +# Download, check integrity, uncompress and patch the source from app.src +ynh_setup_source --dest_dir="$install_dir" chmod 750 "$install_dir" chmod -R o-rwx "$install_dir" -chown -R $app:www-data "$install_dir" +chown -R "$app:www-data" "$install_dir" + chmod u+x "$install_dir/yunomonitor.py" chmod u+w "$install_dir/conf" chmod u+w "$install_dir/well-known" @@ -47,15 +42,13 @@ chmod u+w "$install_dir/well-known" ynh_script_progression --message="Building Yunomonitor..." pushd "$install_dir" - python3 -m pip install pycryptodome + python3 -m pip install pycryptodome pyyaml popd #================================================= # STORE THE CONFIG FILE CHECKSUM #================================================= -### Verify the checksum of a file, stored by `ynh_store_file_checksum` in the install script. -### And create a backup of this file if the checksum is different. So the file will be backed up if the admin had modified it. ynh_backup_if_checksum_is_different --file="$install_dir/conf/$app.yml" # Recalculate and store the checksum of the file for the next upgrade. ynh_store_file_checksum --file="$install_dir/conf/$app.yml"