Skip to content

Commit

Permalink
Panel-config + actions fully tested
Browse files Browse the repository at this point in the history
  • Loading branch information
maniackcrudelis committed Sep 30, 2018
1 parent d7d56c2 commit eb48bf3
Show file tree
Hide file tree
Showing 7 changed files with 480 additions and 16 deletions.
22 changes: 22 additions & 0 deletions actions.json
@@ -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 changes: 89 additions & 0 deletions config_panel.json
@@ -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
}]
}]
}
]
}
22 changes: 20 additions & 2 deletions scripts/_common.sh
Expand Up @@ -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
#=================================================
Expand Down Expand Up @@ -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
Expand All @@ -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
}

#=================================================
Expand Down
102 changes: 102 additions & 0 deletions scripts/actions/reset_default_config
@@ -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"

0 comments on commit eb48bf3

Please sign in to comment.