unattended_upgrades_ynh/scripts/config
2024-01-09 17:54:00 +01:00

229 lines
10 KiB
Bash

#!/bin/bash
#=================================================
# GENERIC STARTING
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source _common.sh
source /usr/share/yunohost/helpers
#=================================================
# RETRIEVE ARGUMENTS
#=================================================
app=$YNH_APP_INSTANCE_NAME
final_path=$(ynh_app_setting_get --app=$app --key=final_path)
#=================================================
# SPECIFIC CODE
#=================================================
# LOAD VALUES
#=================================================
# Load the real value from the app config or elsewhere.
# Then get the value from the form.
# If the form has a value for a variable, take the value from the form,
# Otherwise, keep the value from the app config.
# 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=$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=$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=$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=$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=$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=$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=$app --key=admin_mail_html)"
admin_mail_html="${YNH_CONFIG_MAIN_GLOBAL_CONFIG_EMAIL_TYPE:-$old_admin_mail_html}"
#=================================================
# SHOW_CONFIG FUNCTION FOR 'SHOW' COMMAND
#=================================================
show_config() {
# 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"
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"
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"
ynh_return "YNH_CONFIG_MAIN_PERIODIC_CONFIGURATION_UNATTENDED_VERBOSITY=$unattended_verbosity"
ynh_return "YNH_CONFIG_MAIN_OVERWRITE_FILES_OVERWRITE_PERIODIC=$overwrite_periodic"
ynh_return "YNH_CONFIG_MAIN_GLOBAL_CONFIG_EMAIL_TYPE=$admin_mail_html"
}
#=================================================
# MODIFY THE CONFIGURATION
#=================================================
apply_config() {
#=================================================
# MODIFY 50UNATTENDED-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 even if there's no errors
ynh_replace_string --match_string="/*\(Unattended-Upgrade::MailOnlyOnError \).*" --replace_string="\1\"false\";" --target_file="$unattended_upgrades_config"
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"
# 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"
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
# 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
# unattended_verbosity
if [ "$unattended_verbosity" != "$old_unattended_verbosity" ]
then
ynh_backup_if_checksum_is_different --file="/etc/apt/apt.conf.d/02periodic"
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"
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"
}
#=================================================
# GENERIC FINALIZATION
#=================================================
ynh_app_config_run $1