Panel-config + actions fully tested

This commit is contained in:
Maniack Crudelis 2018-09-30 12:02:50 +02:00
parent d7d56c2d88
commit eb48bf36a6
7 changed files with 480 additions and 16 deletions

22
actions.json Normal file
View file

@ -0,0 +1,22 @@
[{
"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."
}
}]

89
config_panel.json Normal file
View file

@ -0,0 +1,89 @@
{
"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:<br>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:<br>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.<br>",
"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:<br>1, 2, 3.",
"help": "1: Progress report only.<br>2: Progress report and command outputs.<br>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
}]
}]
}
]
}

View file

@ -30,6 +30,24 @@ IS_PACKAGE_CHECK () {
return $(env | grep -c container=lxc)
}
#=================================================
# BOOLEAN CONVERTER
#=================================================
bool_to_01 () {
local var="$1"
[ "$var" = "true" ] && var=1
[ "$var" = "false" ] && var=0
echo "$var"
}
bool_to_true_false () {
local var="$1"
[ "$var" = "1" ] && var=true
[ "$var" = "0" ] && var=false
echo "$var"
}
#=================================================
# EXPERIMENTAL HELPERS
#=================================================
@ -517,7 +535,7 @@ EOF
ynh_store_file_checksum "$finalfail2banjailconf"
ynh_store_file_checksum "$finalfail2banfilterconf"
systemctl reload fail2ban
systemctl restart fail2ban
local fail2ban_error="$(journalctl -u fail2ban | tail -n50 | grep "WARNING.*$app.*")"
if [ -n "$fail2ban_error" ]
then
@ -532,7 +550,7 @@ EOF
ynh_remove_fail2ban_config () {
ynh_secure_remove "/etc/fail2ban/jail.d/$app.conf"
ynh_secure_remove "/etc/fail2ban/filter.d/$app.conf"
systemctl reload fail2ban
systemctl restart fail2ban
}
#=================================================

View file

@ -0,0 +1,102 @@
#!/bin/bash
#=================================================
# GENERIC STARTING
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source scripts/_common.sh
source /usr/share/yunohost/helpers
#=================================================
# RETRIEVE ARGUMENTS
#=================================================
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)"
#=================================================
# SORT OUT THE CONFIG FILE TO HANDLE
#=================================================
file="$1"
if [ "$file" = "50unattended-upgrades" ]; then
config_file="/etc/apt/apt.conf.d/50unattended-upgrades"
elif [ "$file" = "02periodic" ]; then
config_file="/etc/apt/apt.conf.d/02periodic"
fi
#=================================================
# SPECIFIC ACTION
#=================================================
# RESET THE CONFIG FILE
#=================================================
# Verify the checksum and backup the file if it's different
ynh_backup_if_checksum_is_different "$config_file"
if [ "$file" = "50unattended-upgrades" ]
then
# 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
# 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
# Allow MinimalSteps upgrading to reduce risk in case of reboot
ynh_replace_string "//\(Unattended-Upgrade::MinimalSteps\).*" "\1 \"true\";" "$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"
# Send mail even if there's no errors
ynh_replace_string "//\(Unattended-Upgrade::MailOnlyOnError \).*" "\1\"false\";" "$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"
# Send mail only if there's an error
ynh_replace_string "//\(Unattended-Upgrade::MailOnlyOnError \).*" "\1\"true\";" "$config_file"
else # "Never"
# Comment "Unattended-Upgrade::Mail" if isn't already commented
ynh_replace_string "^\(Unattended-Upgrade::Mail \)" "//\1" "$config_file"
fi
fi
if [ "$file" = "02periodic" ]
then
# 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"
fi
# Calculate and store the config file checksum into the app settings
ynh_store_file_checksum "$config_file"

221
scripts/config Normal file
View file

@ -0,0 +1,221 @@
#!/bin/bash
#=================================================
# GENERIC STARTING
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source _common.sh
source /usr/share/yunohost/helpers
#=================================================
# RETRIEVE ARGUMENTS
#=================================================
app=${YNH_APP_INSTANCE_NAME:-$YNH_APP_ID}
final_path=$(ynh_app_setting_get $app 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 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)
ynh_update="${YNH_CONFIG_MAIN_UNATTENDED_CONFIGURATION_YNH_UPDATE:-$old_ynh_update}"
# unattended_mail
old_unattended_mail="$(ynh_app_setting_get $app 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)
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)
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)"
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)
overwrite_periodic="${YNH_CONFIG_MAIN_OVERWRITE_FILES_OVERWRITE_PERIODIC:-$old_overwrite_periodic}"
#=================================================
# SHOW_CONFIG FUNCTION FOR 'SHOW' COMMAND
#=================================================
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"
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"
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"
echo "YNH_CONFIG_MAIN_PERIODIC_CONFIGURATION_UNATTENDED_VERBOSITY=$unattended_verbosity"
echo "YNH_CONFIG_MAIN_OVERWRITE_FILES_OVERWRITE_PERIODIC=$overwrite_periodic"
}
#=================================================
# MODIFY THE CONFIGURATION
#=================================================
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"
elif [ "$unattended_mail" = "Only if there was an error" ]
then
# Allow mail to root
ynh_replace_string "/*\(Unattended-Upgrade::Mail \)" "\1" "$unattended_upgrades_config"
# Send mail only if there's an error
ynh_replace_string "/*\(Unattended-Upgrade::MailOnlyOnError \).*" "\1\"true\";" "$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
# 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
# unattended_verbosity
if [ "$unattended_verbosity" != "$old_unattended_verbosity" ]
then
ynh_backup_if_checksum_is_different "/etc/apt/apt.conf.d/02periodic"
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"
ynh_store_file_checksum "/etc/apt/apt.conf.d/02periodic"
fi
# Set overwrite_periodic
overwrite_periodic=$(bool_to_01 $overwrite_periodic)
ynh_app_setting_set $app overwrite_periodic "$overwrite_periodic"
}
#=================================================
# GENERIC FINALIZATION
#=================================================
# SELECT THE ACTION FOLLOWING THE GIVEN ARGUMENT
#=================================================
case $1 in
show) show_config;;
apply) apply_config;;
esac

View file

@ -32,12 +32,13 @@ app=$YNH_APP_INSTANCE_NAME
# STORE SETTINGS FROM MANIFEST
#=================================================
ynh_app_setting_set $app upgrade_level $upgrade_level
ynh_app_setting_set $app upgrade_level "$upgrade_level"
ynh_app_setting_set $app ynh_update $ynh_update
ynh_app_setting_set $app previous_apticron $previous_apticron
ynh_app_setting_set $app after_apticron $after_apticron
ynh_app_setting_set $app unattended_mail $unattended_mail
ynh_app_setting_set $app unattended_mail "$unattended_mail"
ynh_app_setting_set $app unattended_verbosity $unattended_verbosity
ynh_app_setting_set $app overwrite_periodic "1"
#=================================================
# STANDARD MODIFICATIONS
@ -64,26 +65,23 @@ ynh_replace_string "# CUSTOM_NO_UPDATES_SUBJECT=.*" \
#=================================================
unattended_upgrades_config="/etc/apt/apt.conf.d/50unattended-upgrades"
distro_codename=$(lsb_release -cs)
# Make a backup of 50unattended-upgrades
cp -a "$unattended_upgrades_config" "/etc/apt/50unattended-upgrades.backup"
# Configure upgrade sources
# Allow security update
ynh_replace_string "//\(.*\"o=Debian,n=${distro_codename},l=Debian-Security\";\)" "\1" "$unattended_upgrades_config"
# Allow other updates
if [ "$upgrade_level" = "Security and updates" ]
then
ynh_replace_string "//\(.*\"o=Debian,n=$distro_codename\";\)" "\1" "$unattended_upgrades_config"
ynh_replace_string "//\(.*\"o=Debian,n=$distro_codename-updates\";\)" "\1" "$unattended_upgrades_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
# 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\";" "$unattended_upgrades_config"
"&\n\n //YunoHost upgrade\n \"o=YunoHost,a=stable\";" "$unattended_upgrades_config"
fi
# Allow MinimalSteps upgrading to reduce risk in case of reboot

View file

@ -8,7 +8,6 @@
source _common.sh
source /usr/share/yunohost/helpers
source _sed
#=================================================
# LOAD SETTINGS
@ -17,6 +16,7 @@ source _sed
app=$YNH_APP_INSTANCE_NAME
unattended_verbosity=$(ynh_app_setting_get $app unattended_verbosity)
overwrite_periodic=$(ynh_app_setting_get $app overwrite_periodic)
#=================================================
# CHECK VERSION
@ -24,6 +24,16 @@ unattended_verbosity=$(ynh_app_setting_get $app unattended_verbosity)
upgrade_type=$(ynh_check_app_version_changed)
#=================================================
# ENSURE DOWNWARD COMPATIBILITY
#=================================================
# If overwrite_periodic doesn't exist, create it
if [ -z "$overwrite_periodic" ]; then
overwrite_periodic=1
ynh_app_setting_set $app overwrite_periodic $overwrite_periodic
fi
#=================================================
# BACKUP BEFORE UPGRADE THEN ACTIVE TRAP
#=================================================
@ -65,13 +75,17 @@ ynh_install_app_dependencies apticron unattended-upgrades apt-listchanges
# UPGRADE APT PERIODIC FOR UNATTENDED
#=================================================
ynh_backup_if_checksum_is_different "/etc/apt/apt.conf.d/02periodic"
# Overwrite 02periodic config file only if it's allowed
if [ $overwrite_periodic -eq 1 ]
then
ynh_backup_if_checksum_is_different "/etc/apt/apt.conf.d/02periodic"
cp "../conf/02periodic" "/etc/apt/apt.conf.d/02periodic"
ynh_replace_string "__VERBOSITY__" "$unattended_verbosity" "/etc/apt/apt.conf.d/02periodic"
# This config file is used by /etc/cron.daily/apt
cp "../conf/02periodic" "/etc/apt/apt.conf.d/02periodic"
ynh_replace_string "__VERBOSITY__" "$unattended_verbosity" "/etc/apt/apt.conf.d/02periodic"
# This config file is used by /etc/cron.daily/apt
ynh_store_file_checksum "/etc/apt/apt.conf.d/02periodic"
ynh_store_file_checksum "/etc/apt/apt.conf.d/02periodic"
fi
#=================================================
# UPGRADE APTICRON CRON FILE