Skip to content

Commit

Permalink
add notify_admin functionality
Browse files Browse the repository at this point in the history
Signed-off-by: nachoparker <nacho@ownyourbits.com>
  • Loading branch information
nachoparker committed Feb 5, 2020
1 parent 986046f commit 0c538ae
Show file tree
Hide file tree
Showing 22 changed files with 45 additions and 63 deletions.
14 changes: 8 additions & 6 deletions bin/ncp/NETWORKING/letsencrypt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,15 @@ configure()
# Set up auto-renewal
cat > /etc/cron.weekly/letsencrypt-ncp <<EOF
#!/bin/bash
source /usr/local/etc/library.sh
# renew and notify
$letsencrypt renew --quiet
# notify if fails
[[ \$? -ne 0 ]] && ncc notification:generate \
$NOTIFYUSER "SSL renewal error" \
-l "SSL certificate renewal failed. See /var/log/letsencrypt/letsencrypt.log"
[[ \$? -ne 0 ]] && notify_admin \
"SSL renewal error" \
"SSL certificate renewal failed. See /var/log/letsencrypt/letsencrypt.log"
# cleanup
rm -rf $ncdir/.well-known
Expand All @@ -84,9 +85,10 @@ EOF
mkdir -p /etc/letsencrypt/renewal-hooks/deploy
cat > /etc/letsencrypt/renewal-hooks/deploy/ncp <<EOF
#!/bin/bash
/usr/local/bin/ncc notification:generate \
$NOTIFYUSER "SSL renewal" \
-l "Your SSL certificate(s) \$RENEWED_DOMAINS has been renewed for another 90 days"
source /usr/local/etc/library.sh
notify_admin \
"SSL renewal" \
"Your SSL certificate(s) \$RENEWED_DOMAINS has been renewed for another 90 days"
exit 0
EOF
chmod +x /etc/letsencrypt/renewal-hooks/deploy/ncp
Expand Down
7 changes: 4 additions & 3 deletions bin/ncp/SYSTEM/nc-hdd-monitor.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,11 @@ EOF
}

cat >> /usr/local/etc/ncp-hdd-notif.sh <<EOF
source /usr/local/etc/library.sh
wall "\$SMARTD_MESSAGE"
sudo -u www-data php /var/www/nextcloud/occ notification:generate \
$NOTIFYUSER "NextCloudPi HDD health \$SMARTD_FAILTYPE" \
-l "\$SMARTD_MESSAGE"
notify_admin \
"NextCloudPi HDD health \$SMARTD_FAILTYPE" \
"\$SMARTD_MESSAGE"
EOF
chmod +x /usr/local/etc/ncp-hdd-notif.sh

Expand Down
4 changes: 2 additions & 2 deletions bin/ncp/UPDATES/nc-autoupdate-nc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ configure()

cat > /etc/cron.daily/ncp-autoupdate-nc <<EOF
#!/bin/bash
source /usr/local/etc/library.sh
echo -e "[ncp-update-nc]" >> /var/log/ncp.log
/usr/local/bin/ncp-update-nc "$NCVER" 2>&1 | tee -a /var/log/ncp.log
Expand All @@ -29,8 +30,7 @@ if [[ \${PIPESTATUS[0]} -eq 0 ]]; then
VER="\$( /usr/local/bin/ncc status | grep "version:" | awk '{ print \$3 }' )"
sudo -u www-data php /var/www/nextcloud/occ notification:generate \
"$NOTIFYUSER" "NextCloudPi" -l "Nextcloud was updated to \$VER"
notify_admin "NextCloudPi" "Nextcloud was updated to \$VER"
fi
echo "" >> /var/log/ncp.log
EOF
Expand Down
5 changes: 2 additions & 3 deletions bin/ncp/UPDATES/nc-autoupdate-ncp.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@ configure()

cat > /etc/cron.daily/ncp-autoupdate <<EOF
#!/bin/bash
source /usr/local/etc/library.sh
if /usr/local/bin/ncp-test-updates; then
/usr/local/bin/ncp-update || exit 1
/usr/local/bin/ncc notification:generate \
"$NOTIFYUSER" "NextCloudPi" \
-l "NextCloudPi was updated to \$( cat /usr/local/etc/ncp-version )"
notify_admin "NextCloudPi" "NextCloudPi was updated to \$(cat /usr/local/etc/ncp-version)"
fi
EOF
chmod 755 /etc/cron.daily/ncp-autoupdate
Expand Down
14 changes: 8 additions & 6 deletions bin/ncp/UPDATES/nc-notify-updates.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ configure()
# code
cat > /usr/local/bin/ncp-notify-update <<EOF
#!/bin/bash
source /usr/local/etc/library.sh
VERFILE=/usr/local/etc/ncp-version
LATEST=/var/run/.ncp-latest-version
NOTIFIED=/var/run/.ncp-version-notified
Expand All @@ -43,16 +44,17 @@ echo "Found update from \$( cat \$VERFILE ) to \$( cat \$LATEST ). Sending notif
IFACE=\$( ip r | grep "default via" | awk '{ print \$5 }' | head -1 )
IP=\$( ip a show dev "\$IFACE" | grep global | grep -oP '\d{1,3}(\.\d{1,3}){3}' | head -1 )
/usr/local/bin/ncc notification:generate \
$USER "NextCloudPi update" \
-l "Update from \$( cat \$VERFILE ) to \$( cat \$LATEST ) is available. Update from https://\$IP:4443"
notify_admin \
"NextCloudPi update" \
"Update from \$( cat \$VERFILE ) to \$( cat \$LATEST ) is available. Update from https://\$IP:4443"
cat \$LATEST > \$NOTIFIED
EOF
chmod +x /usr/local/bin/ncp-notify-update

cat > /usr/local/bin/ncp-notify-unattended-upgrade <<EOF
#!/bin/bash
source /usr/local/etc/library.sh
LOGFILE=/var/log/unattended-upgrades/unattended-upgrades.log
STAMPFILE=/var/run/.ncp-notify-unattended-upgrades
Expand All @@ -74,9 +76,9 @@ sed -i 's|INFO Packages that will be upgraded:|INFO Packages that will be upgrad
echo -e "Packages automatically upgraded: \$PKGS\\n"
# notify
/usr/local/bin/ncc notification:generate \
$USER "NextCloudPi Unattended Upgrades" \
-l "Packages automatically upgraded \$PKGS"
notify_admin \
"NextCloudPi Unattended Upgrades" \
"Packages automatically upgraded \$PKGS"
EOF
chmod +x /usr/local/bin/ncp-notify-unattended-upgrade

Expand Down
3 changes: 2 additions & 1 deletion bin/ncp/UPDATES/nc-update-nc-apps-auto.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ configure()

cat > "$cronfile" <<EOF
#!/bin/bash
source /usr/local/etc/library.sh
OUT="\$(
echo "[ nc-update-nc-apps-auto ]"
echo "checking for updates..."
Expand All @@ -28,7 +29,7 @@ echo "checking for updates..."
echo "\$OUT" >> /var/log/ncp.log
APPS=\$( echo "\$OUT" | grep 'updated\$' | awk '{ print \$1 }')
[[ "\$APPS" != "" ]] && /usr/local/bin/ncc notification:generate "$USER" "Apps updated" -l "\$APPS"
[[ "\$APPS" != "" ]] && notify_admin "Apps updated" "\$APPS"
EOF
chmod 755 "$cronfile"
echo "automatic app updates enabled"
Expand Down
8 changes: 6 additions & 2 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@

[v1.20.7](https://github.com/nextcloud/nextcloudpi/commit/8ce053f) (2020-01-26) fail2ban: update regex for NC17
[v1.20.9](https://github.com/nextcloud/nextcloudpi/commit/135f4d4) (2020-02-04) add notify_admin functionality

[v1.20.8](https://github.com/nextcloud/nextcloudpi/commit/986046f) (2020-02-05) nc-backup: add more info to description (#1073)

[v1.20.7](https://github.com/nextcloud/nextcloudpi/commit/b404765) (2020-01-26) fail2ban: update regex for NC17

[v1.20.6 ](https://github.com/nextcloud/nextcloudpi/commit/4a99207) (2020-01-21) ncp-config: dont save passwords

Expand All @@ -11,7 +15,7 @@

[v1.20.2 ](https://github.com/nextcloud/nextcloudpi/commit/953c47a) (2019-11-13) Extend the ssh configuration check by calling the echo command if the first check fails.

[v1.20.1](https://github.com/nextcloud/nextcloudpi/commit/6d0bc6b) (2019-12-19) Revert "build: dont use empty values by default"
[v1.20.1 ](https://github.com/nextcloud/nextcloudpi/commit/6d0bc6b) (2019-12-19) Revert "build: dont use empty values by default"

[v1.20.0 ](https://github.com/nextcloud/nextcloudpi/commit/f75c415) (2019-12-19) upgrade to NC17.0.2

Expand Down
9 changes: 9 additions & 0 deletions etc/library.sh
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,15 @@ function apt_install()
apt-get install -y --no-install-recommends -o Dpkg::Options::=--force-confdef
}

function notify_admin()
{
local header="$1"
local msg="$2"
local admin=$(mysql -u root nextcloud -Nse "select uid from oc_group_user where gid='admin' limit 1;")
[[ "${admin}" == "" ]] && { echo "admin user not found" >&2; return 0; }
ncc notification:generate "${admin}" "${header}" -l "${msg}" || true
}

# License
#
# This script is free software; you can redistribute it and/or modify it
Expand Down
3 changes: 1 addition & 2 deletions etc/ncp-config.d/l10n/letsencrypt/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"EMAIL": "電子郵件",
"Internet access is required for this configuration to complete\nBoth ports 80 and 443 need to be accessible from the internet\n \nYour certificate will be automatically renewed every month": "使用letsencrypt提供的SSL證書\n在執行這個動作之前,請開啟網路且開啟Port 80和443\n完成此動作之後,SSL證書將每個月自動更新續期。",
"Warning": "警告",
"letsencrypt": "SSL證書",
"NOTIFYUSER":"提醒用戶"
"letsencrypt": "SSL證書"
}
}
1 change: 0 additions & 1 deletion etc/ncp-config.d/l10n/nc-autoupdate-nc/pt.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"translations":{
"ACTIVE":"Ativo",
"NOTIFYUSER":"Noticar Usuario",
"Automatically apply Nextcloud updates":"Aplicar atualizações automaticas ao Nextcloud",
"nc-autoupdate-nc":"nc-autoupdate-nc"
}
Expand Down
1 change: 0 additions & 1 deletion etc/ncp-config.d/l10n/nc-autoupdate-nc/zh.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"translations":{
"ACTIVE":"開啟",
"NOTIFYUSER":"提醒用戶",
"Automatically apply Nextcloud updates":"自動更新Nextcloud",
"nc-autoupdate-nc":"自動更新NC"
}
Expand Down
2 changes: 1 addition & 1 deletion etc/ncp-config.d/l10n/nc-autoupdate-ncp/es.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"translations":{"ACTIVE":"Activado","NOTIFYUSER":"Notificar usuario"}}
{"translations":{"ACTIVE":"Activado"}}
2 changes: 1 addition & 1 deletion etc/ncp-config.d/l10n/nc-autoupdate-ncp/pt.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"translations":{"ACTIVE":"Ativo","NOTIFYUSER":"Notificar usuário"}}
{"translations":{"ACTIVE":"Ativo"}}
1 change: 0 additions & 1 deletion etc/ncp-config.d/l10n/nc-autoupdate-ncp/zh.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"translations":{
"ACTIVE":"開啟",
"NOTIFYUSER":"提示使用者",
"Automatically apply NextCloudPi updates":"自動更新NextcloudPi",
"nc-autoupdate-ncp":"自動更新NCP"
}
Expand Down
1 change: 0 additions & 1 deletion etc/ncp-config.d/l10n/nc-hdd-monitor/pt.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"translations": {
"ACTIVE": "Ativo",
"Monitor HDD health automatically":"Monitorar saúde do HDD automaticamente",
"NOTIFYUSER":"Notificar usuário",
"EMAIL":"E-mail"
}
}
1 change: 0 additions & 1 deletion etc/ncp-config.d/l10n/nc-hdd-monitor/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"translations": {
"ACTIVE": "開啟",
"Monitor HDD health automatically":"定期檢查HDD硬碟狀態",
"NOTIFYUSER":"提示使用者",
"EMAIL":"電子郵件"
}
}
6 changes: 0 additions & 6 deletions etc/ncp-config.d/letsencrypt.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@
"name": "Email",
"value": "",
"suggest": "mycloud@ownyourbits.com"
},
{
"id": "NOTIFYUSER",
"name": "Notify user",
"value": "ncp",
"suggest": "ncp"
}
]
}
6 changes: 0 additions & 6 deletions etc/ncp-config.d/nc-autoupdate-nc.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@
"name": "Active",
"value": "no",
"type": "bool"
},
{
"id": "NOTIFYUSER",
"name": "Notify user",
"value": "ncp",
"suggest": "ncp"
}
]
}
6 changes: 0 additions & 6 deletions etc/ncp-config.d/nc-autoupdate-ncp.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@
"name": "Active",
"value": "no",
"type": "bool"
},
{
"id": "NOTIFYUSER",
"name": "Notify user",
"value": "ncp",
"suggest": "ncp"
}
]
}
6 changes: 0 additions & 6 deletions etc/ncp-config.d/nc-backup-auto.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,6 @@
"name": "Number of backups to keep",
"suggest": "4",
"value": "4"
},
{
"id": "NOTIFYUSER",
"name": "Notify user",
"suggest": "ncp",
"value": "ncp"
}
]
}
6 changes: 0 additions & 6 deletions etc/ncp-config.d/nc-hdd-monitor.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@
"value": "no",
"type": "bool"
},
{
"id": "NOTIFYUSER",
"name": "Notify user",
"value": "ncp",
"suggest": "ncp"
},
{
"id": "EMAIL",
"name": "Email",
Expand Down
2 changes: 1 addition & 1 deletion update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ check_distro "$NCPCFG" && check_distro etc/ncp.cfg || {
msg="Update to $release_new available. Get the latest container to upgrade" || \
msg="Update to $release_new available. Type 'sudo ncp-dist-upgrade' to upgrade"
echo "${msg}"
ncc notification:generate "ncp" "New distribution available" -l "${msg}"
notify_admin "New distribution available" "${msg}"
wall "${msg}"
cat > /etc/update-motd.d/30ncp-dist-upgrade <<EOF
#!/bin/bash
Expand Down

0 comments on commit 0c538ae

Please sign in to comment.