From f37594eb5df701e70ce884354e4f5ad3371b3d8b Mon Sep 17 00:00:00 2001 From: Maniack Crudelis Date: Thu, 23 Apr 2020 20:43:03 +0200 Subject: [PATCH] Update config-panel and actions --- actions.json | 22 --- actions.toml | 27 +++ config_panel.json | 100 ---------- config_panel.toml | 80 ++++++++ scripts/actions/reset_default_app | 94 +++++++++ scripts/actions/reset_default_config | 113 ++++++----- scripts/config | 283 ++++++++++++++------------- 7 files changed, 405 insertions(+), 314 deletions(-) delete mode 100644 actions.json create mode 100644 actions.toml delete mode 100644 config_panel.json create mode 100644 config_panel.toml create mode 100755 scripts/actions/reset_default_app diff --git a/actions.json b/actions.json deleted file mode 100644 index 862ff5d..0000000 --- a/actions.json +++ /dev/null @@ -1,22 +0,0 @@ -[{ - "id": "reset_default_unattended", - "name": "Reset the 50unattended-upgrades config file and restore a default one.", - "command": "/bin/bash scripts/actions/reset_default_config \"50unattended-upgrades\"", - "user": "root", - "accepted_return_codes": [0], - "description": { - "en": "Reset the unattended-upgrades config file 50unattended-upgrades.", - "fr": "Réinitialise le fichier de configuration unattended-upgrades 50unattended-upgrades." - } -}, -{ - "id": "reset_default_periodic", - "name": "Reset the 02periodic apt config file and restore a default one.", - "command": "/bin/bash scripts/actions/reset_default_config \"02periodic\"", - "user": "root", - "accepted_return_codes": [0], - "description": { - "en": "Reset the config file 02periodic.", - "fr": "Réinitialise le fichier de configuration 02periodic." - } -}] diff --git a/actions.toml b/actions.toml new file mode 100644 index 0000000..7691e45 --- /dev/null +++ b/actions.toml @@ -0,0 +1,27 @@ +[reset_default_unattended] +name = "Reset the 50unattended-upgrades config file and restore a default one." +command = "/bin/bash scripts/actions/reset_default_config \"50unattended-upgrades\"" +# user = "root" # optional +# cwd = "/" # optional +# accepted_return_codes = [0, 1, 2, 3] # optional +accepted_return_codes = [0] +description = "Reset the unattended-upgrades config file 50unattended-upgrades." + +[reset_default_periodic] +name = "Reset the 02periodic apt config file and restore a default one." +command = "/bin/bash scripts/actions/reset_default_config \"02periodic\"" +# user = "root" # optional +# cwd = "/" # optional +# accepted_return_codes = [0, 1, 2, 3] # optional +accepted_return_codes = [0] +description = "Reset the config file 02periodic." + + +[reset_default_app] +name = "Reset the app with a default configuration." +command = "/bin/bash scripts/actions/reset_default_app" +# user = "root" # optional +# cwd = "/" # optional +# accepted_return_codes = [0, 1, 2, 3] # optional +accepted_return_codes = [0] +description = "Reset the app to its default configuration to try to fix potential issues.
This action won't remove any data added to the app.
However, if you have modified any configuration, it will be overwritten." diff --git a/config_panel.json b/config_panel.json deleted file mode 100644 index c4878c5..0000000 --- a/config_panel.json +++ /dev/null @@ -1,100 +0,0 @@ -{ - "name": "Unattended-upgrades configuration panel", - "version": "0.1", - "panel": [{ - "name": "Unattended-upgrades configuration", - "id": "main", - "sections": [{ - "name": "50unattended-upgrades configuration file", - "id": "unattended_configuration", - "options": [{ - "name": "Choose the sources of packages to automatically upgrade.", - "help": "We can't use a choices field for now. In the meantime please choose between one of this values:
Security only, Security and updates.", - "id": "upgrade_level", - "type": "text", - "//": "\"choices\" : [\"Security only\", \"Security and updates\"]", - "default" : "Security only" - }, - { - "name": "Would you like to update YunoHost packages automatically ?", - "id": "ynh_update", - "type": "bool", - "default": true - }, - { - "name": "Would you like to receive an email from Unattended-Upgrades ?", - "help": "We can't use a choices field for now. In the meantime please choose between one of this values:
If an upgrade has been done, Only if there was an error, Never.", - "id": "unattended_mail", - "type": "text", - "//": "\"choices\" : [\"If an upgrade has been done\", \"Only if there was an error\", \"Never\"]", - "default" : "If an upgrade has been done" - }] - }, - { - "name": "apticron cron file", - "id": "apticron_configuration", - "options": [{ - "name": "Would you like to receive an email to inform which upgrades need to be done ?", - "id": "previous_apticron", - "type": "bool", - "default": true - }, - { - "name": "When do you want to receive this email ?", - "help": "Choose an hour between 12 and 23.
", - "id": "previous_apticron_hour", - "type": "number", - "default": 20 - }, - { - "name": "Would you like to receive an email to verify if there any upgrades left after each auto upgrade ?", - "id": "after_apticron", - "type": "bool", - "default": true - }, - { - "name": "When do you want to receive this email ?", - "help": "Choose an hour between 0 and 10.", - "id": "after_apticron_hour", - "type": "number", - "default": 2 - }] - }, - { - "name": "02periodic apt config file", - "id": "periodic_configuration", - "options": [{ - "name": "Choose the level of verbosity of Unattended-Upgrades mail", - "help": "We can't use a choices field for now. In the meantime please choose between one of this values:
1, 2, 3.", - "help": "1: Progress report only.
2: Progress report and command outputs.
3: Progress report and command outputs and trace.", - "id": "unattended_verbosity", - "type": "text", - "//": "\"choices\" : [\"1\", \"2\", \"3\"]", - "default" : "1" - }] - }, - { - "name": "Overwriting config files", - "id": "overwrite_files", - "options": [{ - "name": "Overwrite the config file 02periodic ?", - "help": "If the file is overwritten, a backup will be created.", - "id": "overwrite_periodic", - "type": "bool", - "default": true - }] - }, - { - "name": "Global configuration", - "id": "global_config", - "options": [{ - "name": "Send HTML email to admin ?", - "help": "Allow app scripts to send HTML mails instead of plain text.", - "id": "email_type", - "type": "bool", - "default": true - }] - }] - } -] -} diff --git a/config_panel.toml b/config_panel.toml new file mode 100644 index 0000000..726ea6e --- /dev/null +++ b/config_panel.toml @@ -0,0 +1,80 @@ +version = "0.1" +name = "Unattended-upgrades configuration panel" + +[main] +name = "Unattended-upgrades configuration" + + + [main.unattended_configuration] + name = "50unattended-upgrades configuration file" + + [main.unattended_configuration.upgrade_level] + ask = "Choose the sources of packages to automatically upgrade." + choices = ["Security only", "Security and updates"] + default = "Security only" + + [main.unattended_configuration.ynh_update] + ask = "Would you like to update YunoHost packages automatically ?" + type = "boolean" + default = true + + [main.unattended_configuration.unattended_mail] + ask = "Would you like to receive an email from Unattended-Upgrades ?" + choices = ["If an upgrade has been done", "Only if there was an error", "Never"] + default = "If an upgrade has been done" + + + [main.apticron_configuration] + name = "apticron cron file" + + [main.apticron_configuration.previous_apticron] + ask = "Would you like to receive an email to inform which upgrades need to be done ?" + type = "boolean" + default = true + + [main.apticron_configuration.previous_apticron_hour] + ask = "When do you want to receive this email ?" + type = "number" + default = "20" + help = "Choose an hour between 12 and 23." + + [main.apticron_configuration.after_apticron] + ask = "Would you like to receive an email to verify if there any upgrades left after each auto upgrade ?" + type = "boolean" + default = true + + [main.apticron_configuration.after_apticron_hour] + ask = "When do you want to receive this email ?" + type = "number" + default = "2" + help = "Choose an hour between 0 and 10." + + + [main.periodic_configuration] + name = "02periodic apt config file" + + [main.periodic_configuration.unattended_verbosity] + ask = "Choose the level of verbosity of Unattended-Upgrades mail." + choices = ["1", "2", "3"] + default = "1" + help = "1: Progress report only.
2: Progress report and command outputs.
3: Progress report and command outputs and trace." + + + [main.overwrite_files] + name = "Overwriting config files" + + [main.overwrite_files.overwrite_periodic] + ask = "Overwrite the config file 02periodic ?" + type = "boolean" + default = true + help = "If the file is overwritten, a backup will be created." + + + [main.global_config] + name = "Global configuration" + + [main.global_config.email_type] + ask = "Send HTML email to admin ?" + type = "boolean" + default = true + help = "Allow app scripts to send HTML mails instead of plain text." diff --git a/scripts/actions/reset_default_app b/scripts/actions/reset_default_app new file mode 100755 index 0000000..5be79e4 --- /dev/null +++ b/scripts/actions/reset_default_app @@ -0,0 +1,94 @@ +#!/bin/bash + +#================================================= +# GENERIC STARTING +#================================================= +# IMPORT GENERIC HELPERS +#================================================= + +# Load common variables for all scripts. +source scripts/_variables + +source scripts/_common.sh +source /usr/share/yunohost/helpers + +#================================================= +# MANAGE SCRIPT FAILURE +#================================================= + +# Exit if an error occurs during the execution of the script +ynh_abort_if_errors + +#================================================= +# RETRIEVE ARGUMENTS +#================================================= + +app=$YNH_APP_INSTANCE_NAME + +unattended_verbosity=$(ynh_app_setting_get --app=$app --key=unattended_verbosity) +upgrade_level="$(ynh_app_setting_get --app=$app --key=upgrade_level)" +ynh_update=$(ynh_app_setting_get --app=$app --key=ynh_update) +unattended_mail="$(ynh_app_setting_get --app=$app --key=unattended_mail)" +previous_apticron=$(ynh_app_setting_get --app=$app --key=previous_apticron) +after_apticron=$(ynh_app_setting_get --app=$app --key=after_apticron) + +#================================================= +# SPECIFIC ACTION +#================================================= +# RESET APTICRON CONFIGURATION +#================================================= +ynh_script_progression --message="Resetting Apticron..." + +apticron_config="/etc/apticron/apticron.conf" +cp "conf/apticron.conf.backup" "$apticron_config" +ynh_replace_string --match_string="# CUSTOM_SUBJECT=.*" \ + --replace_string="&\nCUSTOM_SUBJECT=\'[apticron] \$SYSTEM: \$NUM_PACKAGES package update(s)\'" --target_file="$apticron_config" +ynh_replace_string --match_string="# CUSTOM_NO_UPDATES_SUBJECT=.*" \ + --replace_string="&\nCUSTOM_NO_UPDATES_SUBJECT=\'[apticron] \$SYSTEM: Up to date \\\\o/\'" --target_file="$apticron_config" + +#================================================= +# RESET UNATTENDED-UPGRADES CONFIGURATION +#================================================= +ynh_script_progression --message="Resetting unattended-upgrades..." + +yunohost app action run $app reset_default_unattended + +#================================================= +# RESET APT PERIODIC CONFIGURATION +#================================================= +ynh_script_progression --message="Resetting apt periodic..." + +yunohost app action run $app reset_default_periodic + +#================================================= +# RESET APTICRON CRON FILE +#================================================= +ynh_script_progression --message="Resetting apticron cron file..." + +apticron_cron="/etc/cron.d/apticron" +cp "conf/apticron.crond.backup" "$apticron_cron" + +# Copy and comment the current cron +ynh_replace_string --match_string="^.* root if.*" --replace_string="#&\n&" --target_file="$apticron_cron" +# Modify the time to set at 20:00 every day +ynh_replace_string --match_string="^[[:digit:]].*\( root if.*\)" --replace_string="0 20 * * *\1" --target_file="$apticron_cron" +# Copy the new cron and set the time to 2:00 every night +ynh_replace_string --match_string="^0 20\(.*\)" --replace_string="&\n0 2\1" --target_file="$apticron_cron" + +if [ $previous_apticron -eq 0 ] +then + # Comment the first cron + ynh_replace_string --match_string="^0 20 .*" --replace_string="#&" --target_file="$apticron_cron" +fi + +if [ $after_apticron -eq 0 ] +then + # Comment the second cron + ynh_replace_string --match_string="^0 2 .*" --replace_string="#&" --target_file="$apticron_cron" +fi + +#================================================= +# END OF SCRIPT +#================================================= + +ynh_script_progression --message="Execution completed" --last diff --git a/scripts/actions/reset_default_config b/scripts/actions/reset_default_config index 92e79bd..a30c30f 100755 --- a/scripts/actions/reset_default_config +++ b/scripts/actions/reset_default_config @@ -9,16 +9,24 @@ source scripts/_common.sh source /usr/share/yunohost/helpers +#================================================= +# MANAGE SCRIPT FAILURE +#================================================= + +# Exit if an error occurs during the execution of the script +ynh_abort_if_errors + #================================================= # RETRIEVE ARGUMENTS #================================================= -ynh_script_progression --message="Retrieve arguments from the manifest" --weight=2 +ynh_script_progression --message="Loading installation settings..." --weight=2 -app=${YNH_APP_INSTANCE_NAME:-$YNH_APP_ID} -upgrade_level="$(ynh_app_setting_get $app upgrade_level)" -ynh_update="$(ynh_app_setting_get $app ynh_update)" -unattended_mail="$(ynh_app_setting_get $app unattended_mail)" -unattended_verbosity="$(ynh_app_setting_get $app unattended_verbosity)" +app=$YNH_APP_INSTANCE_NAME + +upgrade_level="$(ynh_app_setting_get --app=$app --key=upgrade_level)" +ynh_update=$(ynh_app_setting_get --app=$app --key=ynh_update) +unattended_mail="$(ynh_app_setting_get --app=$app --key=unattended_mail)" +unattended_verbosity=$(ynh_app_setting_get --app=$app --key=unattended_verbosity) #================================================= # SORT OUT THE CONFIG FILE TO HANDLE @@ -27,9 +35,9 @@ unattended_verbosity="$(ynh_app_setting_get $app unattended_verbosity)" file="$1" if [ "$file" = "50unattended-upgrades" ]; then - config_file="/etc/apt/apt.conf.d/50unattended-upgrades" + config_file="/etc/apt/apt.conf.d/50unattended-upgrades" elif [ "$file" = "02periodic" ]; then - config_file="/etc/apt/apt.conf.d/02periodic" + config_file="/etc/apt/apt.conf.d/02periodic" fi #================================================= @@ -37,71 +45,72 @@ fi #================================================= # RESET THE CONFIG FILE #================================================= -ynh_script_progression --message="Reset the config file $config_file" --weight=9 +ynh_script_progression --message="Resetting the config file $config_file..." --weight=9 # Verify the checksum and backup the file if it's different -ynh_backup_if_checksum_is_different "$config_file" +ynh_backup_if_checksum_is_different --file="$config_file" +# Reset the config /etc/apt/apt.conf.d/50unattended-upgrades if [ "$file" = "50unattended-upgrades" ] then - # Get the default file and overwrite the current config - cp /etc/apt/50unattended-upgrades.backup "$config_file" + # Get the default file and overwrite the current config + cp /etc/apt/50unattended-upgrades.backup "$config_file" - # Recreate the default config - distro_codename=$(lsb_release -cs) - # Allow security update - ynh_replace_string "//\(.*\"o=Debian,n=${distro_codename},l=Debian-Security\";\)" "\1" "$config_file" - # Allow other updates - if [ "$upgrade_level" = "Security and updates" ] - then - ynh_replace_string "//\(.*\"o=Debian,n=$distro_codename\";\)" "\1" "$config_file" - ynh_replace_string "//\(.*\"o=Debian,n=$distro_codename-updates\";\)" "\1" "$config_file" - fi + # Recreate the default config + # Allow security update + ynh_replace_string --match_string="//\(.*\"o=Debian,n=$(ynh_get_debian_release),l=Debian-Security\";\)" --replace_string="\1" --target_file="$config_file" + # Allow other updates + if [ "$upgrade_level" = "Security and updates" ] + then + ynh_replace_string --match_string="//\(.*\"o=Debian,n=$(ynh_get_debian_release)\";\)" --replace_string="\1" --target_file="$config_file" + ynh_replace_string --match_string="//\(.*\"o=Debian,n=$(ynh_get_debian_release)-updates\";\)" --replace_string="\1" --target_file="$config_file" + fi - # Add YunoHost upgrade source - if [ $ynh_update -eq 1 ] - then - ynh_replace_string "origin=Debian,codename=\${distro_codename},label=Debian-Security\";" \ - "&\n\n //YunoHost upgrade\n \"o=YunoHost,n=$distro_codename\";" "$config_file" - fi + # Add YunoHost upgrade source + if [ $ynh_update -eq 1 ] + then + ynh_replace_string --match_string="origin=Debian,codename=\$(ynh_get_debian_release),label=Debian-Security\";" \ + --replace_string="&\n\n //YunoHost upgrade\n \"o=YunoHost,n=$(ynh_get_debian_release)\";" --target_file="$config_file" + fi - # Allow MinimalSteps upgrading to reduce risk in case of reboot - ynh_replace_string "//\(Unattended-Upgrade::MinimalSteps\).*" "\1 \"true\";" "$config_file" + # Allow MinimalSteps upgrading to reduce risk in case of reboot + ynh_replace_string --match_string="//\(Unattended-Upgrade::MinimalSteps\).*" --replace_string="\1 \"true\";" --target_file="$config_file" - # Configure Unattended Upgrades mailing - if [ "$unattended_mail" = "If an upgrade has been done" ] - then - # Allow mail to root - ynh_replace_string "//\(Unattended-Upgrade::Mail \)" "\1" "$config_file" + # Configure Unattended Upgrades mailing + if [ "$unattended_mail" = "If an upgrade has been done" ] + then + # Allow mail to root + ynh_replace_string --match_string="//\(Unattended-Upgrade::Mail \)" --replace_string="\1" --target_file="$config_file" - # Send mail even if there's no errors - ynh_replace_string "//\(Unattended-Upgrade::MailOnlyOnError \).*" "\1\"false\";" "$config_file" + # Send mail even if there's no errors + ynh_replace_string --match_string="//\(Unattended-Upgrade::MailOnlyOnError \).*" --replace_string="\1\"false\";" --target_file="$config_file" - elif [ "$unattended_mail" = "Only if there was an error" ] - then - # Allow mail to root - ynh_replace_string "//\(Unattended-Upgrade::Mail \)" "\1" "$config_file" + elif [ "$unattended_mail" = "Only if there was an error" ] + then + # Allow mail to root + ynh_replace_string --match_string="//\(Unattended-Upgrade::Mail \)" --replace_string="\1" --target_file="$config_file" - # Send mail only if there's an error - ynh_replace_string "//\(Unattended-Upgrade::MailOnlyOnError \).*" "\1\"true\";" "$config_file" + # Send mail only if there's an error + ynh_replace_string --match_string="//\(Unattended-Upgrade::MailOnlyOnError \).*" --replace_string="\1\"true\";" --target_file="$config_file" - else # "Never" - # Comment "Unattended-Upgrade::Mail" if it isn't already commented - ynh_replace_string "^\(Unattended-Upgrade::Mail \)" "//\1" "$config_file" - fi + else # "Never" + # Comment "Unattended-Upgrade::Mail" if it isn't already commented + ynh_replace_string --match_string="^\(Unattended-Upgrade::Mail \)" --replace_string="//\1" --target_file="$config_file" + fi fi +# Reset the config /etc/apt/apt.conf.d/02periodic if [ "$file" = "02periodic" ] then - # Get the default file and overwrite the current config - cp /etc/yunohost/apps/$app/conf/02periodic "$config_file" + # Get the default file and overwrite the current config + cp /etc/yunohost/apps/$app/conf/02periodic "$config_file" - # Recreate the default config - ynh_replace_string "__VERBOSITY__" "$unattended_verbosity" "/etc/apt/apt.conf.d/02periodic" + # Recreate the default config + ynh_replace_string --match_string="__VERBOSITY__" --replace_string="$unattended_verbosity" --target_file="/etc/apt/apt.conf.d/02periodic" fi # Calculate and store the config file checksum into the app settings -ynh_store_file_checksum "$config_file" +ynh_store_file_checksum --file="$config_file" #================================================= # END OF SCRIPT diff --git a/scripts/config b/scripts/config index a39ab13..d1e0a56 100644 --- a/scripts/config +++ b/scripts/config @@ -13,9 +13,9 @@ source /usr/share/yunohost/helpers # RETRIEVE ARGUMENTS #================================================= -app=${YNH_APP_INSTANCE_NAME:-$YNH_APP_ID} +app=$YNH_APP_INSTANCE_NAME -final_path=$(ynh_app_setting_get $app final_path) +final_path=$(ynh_app_setting_get --app=$app --key=final_path) #================================================= # SPECIFIC CODE @@ -29,43 +29,47 @@ final_path=$(ynh_app_setting_get $app final_path) # Otherwise, keep the value from the app config. # upgrade_level -old_upgrade_level="$(ynh_app_setting_get $app upgrade_level)" +old_upgrade_level="$(ynh_app_setting_get --app=$app --key=upgrade_level)" upgrade_level="${YNH_CONFIG_MAIN_UNATTENDED_CONFIGURATION_UPGRADE_LEVEL:-$old_upgrade_level}" + # ynh_update -old_ynh_update="$(ynh_app_setting_get $app ynh_update)" -old_ynh_update=$(bool_to_true_false $old_ynh_update) +old_ynh_update="$(ynh_app_setting_get --app=$app --key=ynh_update)" ynh_update="${YNH_CONFIG_MAIN_UNATTENDED_CONFIGURATION_YNH_UPDATE:-$old_ynh_update}" + # unattended_mail -old_unattended_mail="$(ynh_app_setting_get $app unattended_mail)" +old_unattended_mail="$(ynh_app_setting_get --app=$app --key=unattended_mail)" unattended_mail="${YNH_CONFIG_MAIN_UNATTENDED_CONFIGURATION_UNATTENDED_MAIL:-$old_unattended_mail}" + # previous_apticron -old_previous_apticron="$(ynh_app_setting_get $app previous_apticron)" -old_previous_apticron=$(bool_to_true_false $old_previous_apticron) +old_previous_apticron="$(ynh_app_setting_get --app=$app --key=previous_apticron)" previous_apticron="${YNH_CONFIG_MAIN_APTICRON_CONFIGURATION_PREVIOUS_APTICRON:-$old_previous_apticron}" + # previous_apticron_hour old_previous_apticron_hour="$(cat /etc/cron.d/apticron | grep --max-count=1 "^#*0.*root if.*" | cut -d' ' -f2)" previous_apticron_hour="${YNH_CONFIG_MAIN_APTICRON_CONFIGURATION_PREVIOUS_APTICRON_HOUR:-$old_previous_apticron_hour}" + # after_apticron -old_after_apticron="$(ynh_app_setting_get $app after_apticron)" -old_after_apticron=$(bool_to_true_false $old_after_apticron) +old_after_apticron="$(ynh_app_setting_get --app=$app --key=after_apticron)" after_apticron="${YNH_CONFIG_MAIN_APTICRON_CONFIGURATION_AFTER_APTICRON:-$old_after_apticron}" + # after_apticron_hour old_after_apticron_hour="$(tac /etc/cron.d/apticron | grep --max-count=1 "^#*0.*root if.*" | cut -d' ' -f2)" after_apticron_hour="${YNH_CONFIG_MAIN_APTICRON_CONFIGURATION_AFTER_APTICRON_HOUR:-$old_after_apticron_hour}" + # unattended_verbosity -old_unattended_verbosity="$(ynh_app_setting_get $app unattended_verbosity)" +old_unattended_verbosity="$(ynh_app_setting_get --app=$app --key=unattended_verbosity)" unattended_verbosity="${YNH_CONFIG_MAIN_PERIODIC_CONFIGURATION_UNATTENDED_VERBOSITY:-$old_unattended_verbosity}" + # Overwrite 02periodic config file -old_overwrite_periodic="$(ynh_app_setting_get $app overwrite_periodic)" -old_overwrite_periodic=$(bool_to_true_false $old_overwrite_periodic) +old_overwrite_periodic="$(ynh_app_setting_get --app=$app --key=overwrite_periodic)" overwrite_periodic="${YNH_CONFIG_MAIN_OVERWRITE_FILES_OVERWRITE_PERIODIC:-$old_overwrite_periodic}" + # Type of admin mail configuration -old_admin_mail_html="$(ynh_app_setting_get $app admin_mail_html)" -old_admin_mail_html=$(bool_to_true_false $old_admin_mail_html) +old_admin_mail_html="$(ynh_app_setting_get --app=$app --key=admin_mail_html)" admin_mail_html="${YNH_CONFIG_MAIN_GLOBAL_CONFIG_EMAIL_TYPE:-$old_admin_mail_html}" #================================================= @@ -73,23 +77,23 @@ admin_mail_html="${YNH_CONFIG_MAIN_GLOBAL_CONFIG_EMAIL_TYPE:-$old_admin_mail_htm #================================================= show_config() { - # here you are supposed to read some config file/database/other then print the values - # echo "YNH_CONFIG_${PANEL_ID}_${SECTION_ID}_${OPTION_ID}=value" + # here you are supposed to read some config file/database/other then print the values + # ynh_return "YNH_CONFIG_${PANEL_ID}_${SECTION_ID}_${OPTION_ID}=value" - echo "YNH_CONFIG_MAIN_UNATTENDED_CONFIGURATION_UPGRADE_LEVEL=$upgrade_level" - echo "YNH_CONFIG_MAIN_UNATTENDED_CONFIGURATION_YNH_UPDATE=$ynh_update" - echo "YNH_CONFIG_MAIN_UNATTENDED_CONFIGURATION_UNATTENDED_MAIL=$unattended_mail" + ynh_return "YNH_CONFIG_MAIN_UNATTENDED_CONFIGURATION_UPGRADE_LEVEL=$upgrade_level" + ynh_return "YNH_CONFIG_MAIN_UNATTENDED_CONFIGURATION_YNH_UPDATE=$ynh_update" + ynh_return "YNH_CONFIG_MAIN_UNATTENDED_CONFIGURATION_UNATTENDED_MAIL=$unattended_mail" - echo "YNH_CONFIG_MAIN_APTICRON_CONFIGURATION_PREVIOUS_APTICRON=$previous_apticron" - echo "YNH_CONFIG_MAIN_APTICRON_CONFIGURATION_PREVIOUS_APTICRON_HOUR=$previous_apticron_hour" - echo "YNH_CONFIG_MAIN_APTICRON_CONFIGURATION_AFTER_APTICRON=$after_apticron" - echo "YNH_CONFIG_MAIN_APTICRON_CONFIGURATION_AFTER_APTICRON_HOUR=$after_apticron_hour" + ynh_return "YNH_CONFIG_MAIN_APTICRON_CONFIGURATION_PREVIOUS_APTICRON=$previous_apticron" + ynh_return "YNH_CONFIG_MAIN_APTICRON_CONFIGURATION_PREVIOUS_APTICRON_HOUR=$previous_apticron_hour" + ynh_return "YNH_CONFIG_MAIN_APTICRON_CONFIGURATION_AFTER_APTICRON=$after_apticron" + ynh_return "YNH_CONFIG_MAIN_APTICRON_CONFIGURATION_AFTER_APTICRON_HOUR=$after_apticron_hour" - echo "YNH_CONFIG_MAIN_PERIODIC_CONFIGURATION_UNATTENDED_VERBOSITY=$unattended_verbosity" + ynh_return "YNH_CONFIG_MAIN_PERIODIC_CONFIGURATION_UNATTENDED_VERBOSITY=$unattended_verbosity" - echo "YNH_CONFIG_MAIN_OVERWRITE_FILES_OVERWRITE_PERIODIC=$overwrite_periodic" + ynh_return "YNH_CONFIG_MAIN_OVERWRITE_FILES_OVERWRITE_PERIODIC=$overwrite_periodic" - echo "YNH_CONFIG_MAIN_GLOBAL_CONFIG_EMAIL_TYPE=$admin_mail_html" + ynh_return "YNH_CONFIG_MAIN_GLOBAL_CONFIG_EMAIL_TYPE=$admin_mail_html" } #================================================= @@ -97,127 +101,126 @@ show_config() { #================================================= apply_config() { - # Change configuration if needed - unattended_upgrades_config="/etc/apt/apt.conf.d/50unattended-upgrades" - # upgrade_level - if [ "$upgrade_level" != "$old_upgrade_level" ] - then - if [ "$upgrade_level" = "Security and updates" ] - then - # Uncomment lines in the config - ynh_replace_string "//\( *\"o=Debian,a=stable\)" " \1" "$unattended_upgrades_config" - ynh_replace_string "//\( *\"o=Debian,a=stable-updates\)" " \1" "$unattended_upgrades_config" - else - # Comment lines in the config - ynh_replace_string "^ \( *\"o=Debian,a=stable\)" "//\1" "$unattended_upgrades_config" - ynh_replace_string "^ \( *\"o=Debian,a=stable-updates\)" "//\1" "$unattended_upgrades_config" - fi - ynh_app_setting_set $app upgrade_level "$upgrade_level" - fi - # ynh_update - if [ "$ynh_update" != "$old_ynh_update" ] - then - ynh_update=$(bool_to_01 $ynh_update) - if [ "$ynh_update" -eq 1 ] - then - # Add YunoHost upgrade source - ynh_replace_string "origin=Debian,codename=\${distro_codename},label=Debian-Security\";" \ - "&\n\n //YunoHost upgrade\n \"o=YunoHost,a=stable\";" "$unattended_upgrades_config" - else - # Remove lines about YunoHost - sed --in-place '/YunoHost upgrade/d' "$unattended_upgrades_config" - sed --in-place '/o=YunoHost/d' "$unattended_upgrades_config" - fi - ynh_app_setting_set $app ynh_update "$ynh_update" - fi - # unattended_mail - if [ "$unattended_mail" != "$old_unattended_mail" ] - then - if [ "$unattended_mail" = "If an upgrade has been done" ] - then - # Allow mail to root - ynh_replace_string "/*\(Unattended-Upgrade::Mail \)" "\1" "$unattended_upgrades_config" - # Send mail even if there's no errors - ynh_replace_string "/*\(Unattended-Upgrade::MailOnlyOnError \).*" "\1\"false\";" "$unattended_upgrades_config" + #================================================= + # MODIFY 50UNATTENDED-UPGRADES CONFIG + #================================================= - elif [ "$unattended_mail" = "Only if there was an error" ] - then - # Allow mail to root - ynh_replace_string "/*\(Unattended-Upgrade::Mail \)" "\1" "$unattended_upgrades_config" + unattended_upgrades_config="/etc/apt/apt.conf.d/50unattended-upgrades" + # upgrade_level + if [ "$upgrade_level" != "$old_upgrade_level" ] + then + if [ "$upgrade_level" = "Security and updates" ] + then + # Uncomment lines in the config + ynh_replace_string --match_string="//\( *\"o=Debian,a=stable\)" --replace_string=" \1" --target_file="$unattended_upgrades_config" + ynh_replace_string --match_string="//\( *\"o=Debian,a=stable-updates\)" --replace_string=" \1" --target_file="$unattended_upgrades_config" + else + # Comment lines in the config + ynh_replace_string --match_string="^ \( *\"o=Debian,a=stable\)" --replace_string="//\1" --target_file="$unattended_upgrades_config" + ynh_replace_string --match_string="^ \( *\"o=Debian,a=stable-updates\)" --replace_string="//\1" --target_file="$unattended_upgrades_config" + fi + ynh_app_setting_set --app=$app --key=upgrade_level --value="$upgrade_level" + fi + # ynh_update + if [ "$ynh_update" != "$old_ynh_update" ] + then + if [ "$ynh_update" -eq 1 ] + then + # Add YunoHost upgrade source + ynh_replace_string --match_string="origin=Debian,codename=\${distro_codename},label=Debian-Security\";" \ + --replace_string="&\n\n //YunoHost upgrade\n \"o=YunoHost,a=stable\";" --target_file="$unattended_upgrades_config" + else + # Remove lines about YunoHost + sed --in-place '/YunoHost upgrade/d' "$unattended_upgrades_config" + sed --in-place '/o=YunoHost/d' "$unattended_upgrades_config" + fi + ynh_app_setting_set --app=$app --key=ynh_update --value="$ynh_update" + fi + # unattended_mail + if [ "$unattended_mail" != "$old_unattended_mail" ] + then + if [ "$unattended_mail" = "If an upgrade has been done" ] + then + # Allow mail to root + ynh_replace_string --match_string="/*\(Unattended-Upgrade::Mail \)" --replace_string="\1" --target_file="$unattended_upgrades_config" - # Send mail only if there's an error - ynh_replace_string "/*\(Unattended-Upgrade::MailOnlyOnError \).*" "\1\"true\";" "$unattended_upgrades_config" + # Send mail even if there's no errors + ynh_replace_string --match_string="/*\(Unattended-Upgrade::MailOnlyOnError \).*" --replace_string="\1\"false\";" --target_file="$unattended_upgrades_config" - else # "Never" - # Comment "Unattended-Upgrade::Mail" - ynh_replace_string "^\(Unattended-Upgrade::Mail \)" "//\1" "$unattended_upgrades_config" - fi - ynh_app_setting_set $app unattended_mail "$unattended_mail" - fi + elif [ "$unattended_mail" = "Only if there was an error" ] + then + # Allow mail to root + ynh_replace_string --match_string="/*\(Unattended-Upgrade::Mail \)" --replace_string="\1" --target_file="$unattended_upgrades_config" - # previous_apticron - apticron_cron="/etc/cron.d/apticron" - if [ "$previous_apticron" != "$old_previous_apticron" ] - then - previous_apticron=$(bool_to_01 $previous_apticron) - if [ "$previous_apticron" = "1" ] - then - # Uncomment the first cron line - ynh_replace_string "^#\(0 $old_previous_apticron_hour .*\)" "\1" "$apticron_cron" - else - # Comment the first cron line - ynh_replace_string "^0 $old_previous_apticron_hour .*" "#&" "$apticron_cron" - fi - ynh_app_setting_set $app previous_apticron "$previous_apticron" - fi - # previous_apticron_hour - if [ "$previous_apticron_hour" != "$old_previous_apticron_hour" ] - then - # Use sed instead of ynh_replace_string to avoid the 'global' argument - sed --in-place "s/0 $old_previous_apticron_hour\( \* \* \* root if\)/0 $previous_apticron_hour\1/" "$apticron_cron" - ynh_app_setting_set $app previous_apticron_hour "$previous_apticron_hour" - fi - # after_apticron - if [ "$after_apticron" != "$old_after_apticron" ] - then - after_apticron=$(bool_to_01 $after_apticron) - if [ "$after_apticron" = "1" ] - then - # Uncomment the second cron line - ynh_replace_string "^#\(0 $old_after_apticron_hour .*\)" "\1" "$apticron_cron" - else - # Comment the second cron line - ynh_replace_string "^0 $old_after_apticron_hour .*" "#&" "$apticron_cron" - fi - ynh_app_setting_set $app after_apticron "$after_apticron" - fi - # after_apticron_hour - if [ "$after_apticron_hour" != "$old_after_apticron_hour" ] - then - # Use sed instead of ynh_replace_string to avoid the 'global' argument - sed --in-place "s/0 $old_after_apticron_hour\( \* \* \* root if\)/0 $after_apticron_hour\1/" "$apticron_cron" - ynh_app_setting_set $app after_apticron_hour "$after_apticron_hour" - fi + # Send mail only if there's an error + ynh_replace_string --match_string="/*\(Unattended-Upgrade::MailOnlyOnError \).*" --replace_string="\1\"true\";" --target_file="$unattended_upgrades_config" - # unattended_verbosity - if [ "$unattended_verbosity" != "$old_unattended_verbosity" ] - then - ynh_backup_if_checksum_is_different "/etc/apt/apt.conf.d/02periodic" + else # "Never" + # Comment "Unattended-Upgrade::Mail" + ynh_replace_string --match_string="^\(Unattended-Upgrade::Mail \)" --replace_string="//\1" --target_file="$unattended_upgrades_config" + fi + ynh_app_setting_set --app=$app --key=unattended_mail --value="$unattended_mail" + fi - ynh_replace_string "^APT::Periodic::Verbose \".*" "APT::Periodic::Verbose \"$unattended_verbosity\";" "/etc/apt/apt.conf.d/02periodic" - ynh_app_setting_set $app unattended_verbosity "$unattended_verbosity" + # previous_apticron + apticron_cron="/etc/cron.d/apticron" + if [ "$previous_apticron" != "$old_previous_apticron" ] + then + if [ "$previous_apticron" = "1" ] + then + # Uncomment the first cron line + ynh_replace_string --match_string="^#\(0 $old_previous_apticron_hour .*\)" --replace_string="\1" --target_file="$apticron_cron" + else + # Comment the first cron line + ynh_replace_string --match_string="^0 $old_previous_apticron_hour .*" --replace_string="#&" --target_file="$apticron_cron" + fi + ynh_app_setting_set --app=$app --key=previous_apticron --value="$previous_apticron" + fi + # previous_apticron_hour + if [ "$previous_apticron_hour" != "$old_previous_apticron_hour" ] + then + # Use sed instead of ynh_replace_string to avoid the 'global' argument + sed --in-place "s/0 $old_previous_apticron_hour\( \* \* \* root if\)/0 $previous_apticron_hour\1/" "$apticron_cron" + ynh_app_setting_set --app=$app --key=previous_apticron_hour --value="$previous_apticron_hour" + fi + # after_apticron + if [ "$after_apticron" != "$old_after_apticron" ] + then + if [ "$after_apticron" = "1" ] + then + # Uncomment the second cron line + ynh_replace_string --match_string="^#\(0 $old_after_apticron_hour .*\)" --replace_string="\1" --target_file="$apticron_cron" + else + # Comment the second cron line + ynh_replace_string --match_string="^0 $old_after_apticron_hour .*" --replace_string="#&" --target_file="$apticron_cron" + fi + ynh_app_setting_set --app=$app --key=after_apticron --value="$after_apticron" + fi + # after_apticron_hour + if [ "$after_apticron_hour" != "$old_after_apticron_hour" ] + then + # Use sed instead of ynh_replace_string to avoid the 'global' argument + sed --in-place "s/0 $old_after_apticron_hour\( \* \* \* root if\)/0 $after_apticron_hour\1/" "$apticron_cron" + ynh_app_setting_set --app=$app --key=after_apticron_hour --value="$after_apticron_hour" + fi - ynh_store_file_checksum "/etc/apt/apt.conf.d/02periodic" - fi + # unattended_verbosity + if [ "$unattended_verbosity" != "$old_unattended_verbosity" ] + then + ynh_backup_if_checksum_is_different --file="/etc/apt/apt.conf.d/02periodic" - # Set overwrite_periodic - overwrite_periodic=$(bool_to_01 $overwrite_periodic) - ynh_app_setting_set $app overwrite_periodic "$overwrite_periodic" + ynh_replace_string --match_string="^APT::Periodic::Verbose \".*" --replace_string="APT::Periodic::Verbose \"$unattended_verbosity\";" --target_file="/etc/apt/apt.conf.d/02periodic" + ynh_app_setting_set --app=$app --key=unattended_verbosity --value="$unattended_verbosity" - # Set admin_mail_html - admin_mail_html=$(bool_to_01 $admin_mail_html) - ynh_app_setting_set $app admin_mail_html "$admin_mail_html" + ynh_store_file_checksum --file="/etc/apt/apt.conf.d/02periodic" + fi + + # Set overwrite_periodic + ynh_app_setting_set --app=$app --key=overwrite_periodic --value="$overwrite_periodic" + + # Set admin_mail_html + ynh_app_setting_set --app=$app --key=admin_mail_html --value="$admin_mail_html" } #================================================= @@ -227,6 +230,6 @@ apply_config() { #================================================= case $1 in - show) show_config;; - apply) apply_config;; +show) show_config;; +apply) apply_config;; esac