2020-10-18 13:33:41 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
# With systemd, we can force logging to the journal. This is better than
|
|
|
|
# spamming the world with cron mails. You can then view these logs using
|
|
|
|
# "journalctl -rat upgrade-and-reboot".
|
2021-01-23 10:32:35 +00:00
|
|
|
if which logger >/dev/null 2>&1
|
2020-10-18 13:33:41 +00:00
|
|
|
then
|
2021-03-26 08:02:48 +00:00
|
|
|
# Dump stdout and stderr to logger, which will then put everything
|
|
|
|
# into the journal.
|
2021-01-23 10:32:35 +00:00
|
|
|
exec 1> >(logger -t upgrade-and-reboot -p user.info)
|
2021-03-26 08:02:48 +00:00
|
|
|
exec 2> >(logger -t upgrade-and-reboot -p user.error)
|
2020-10-18 13:33:41 +00:00
|
|
|
fi
|
|
|
|
|
2021-05-08 06:31:04 +00:00
|
|
|
. /etc/upgrade-and-reboot.conf
|
|
|
|
|
|
|
|
echo "Starting upgrade-and-reboot for node $nodename ..."
|
2021-05-02 08:45:21 +00:00
|
|
|
|
2020-11-21 09:30:05 +00:00
|
|
|
statusfile="/var/tmp/unattended_upgrades.status"
|
2020-11-10 08:50:20 +00:00
|
|
|
# Workaround, because /var/tmp is usually 1777
|
|
|
|
[[ "$UID" == 0 ]] && chown root:root "$statusfile"
|
2020-10-18 13:33:41 +00:00
|
|
|
|
2023-04-01 04:50:37 +00:00
|
|
|
logins=$(ps h -C sshd -o euser | awk '$1 != "root" && $1 != "sshd" && $1 != "sshmon" && $1 != "nobody"')
|
2020-10-18 13:33:41 +00:00
|
|
|
if [[ -n "$logins" ]]
|
|
|
|
then
|
|
|
|
echo "Will abort now, there are active SSH logins: $logins"
|
2020-11-10 08:50:20 +00:00
|
|
|
echo "abort_ssh" > "$statusfile"
|
2020-10-18 13:33:41 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2021-05-08 06:31:04 +00:00
|
|
|
softlockdir=/var/lib/bundlewrap/soft-$nodename
|
2020-10-18 13:33:41 +00:00
|
|
|
mkdir -p "$softlockdir"
|
|
|
|
printf '{"comment": "UPDATE", "date": %s, "expiry": %s, "id": "UNATTENDED", "items": ["*"], "user": "root@localhost"}\n' \
|
|
|
|
$(date +%s) \
|
|
|
|
$(date -d 'now + 30 mins' +%s) \
|
|
|
|
>"$softlockdir"/UNATTENDED
|
|
|
|
trap 'rm -f "$softlockdir"/UNATTENDED' EXIT
|
|
|
|
|
2021-05-08 06:31:04 +00:00
|
|
|
do-unattended-upgrades
|
2020-10-18 13:33:41 +00:00
|
|
|
ret=$?
|
2020-11-21 17:07:01 +00:00
|
|
|
|
|
|
|
echo "$ret" > "$statusfile"
|
2020-10-18 13:33:41 +00:00
|
|
|
if (( $ret != 0 ))
|
|
|
|
then
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2022-01-30 10:43:14 +00:00
|
|
|
if [[ -f /var/run/reboot-required ]] && [[ "$auto_reboot_enabled" == "True" ]]
|
2020-10-18 13:33:41 +00:00
|
|
|
then
|
2022-01-30 10:43:14 +00:00
|
|
|
if [[ -n "$reboot_mail_to" ]]
|
2021-04-02 06:12:51 +00:00
|
|
|
then
|
2022-01-30 10:43:14 +00:00
|
|
|
date | mail -s "SYSREBOOTNOW $nodename" "$reboot_mail_to"
|
2021-04-02 06:12:51 +00:00
|
|
|
fi
|
2022-01-30 10:43:14 +00:00
|
|
|
systemctl reboot
|
2020-10-18 13:33:41 +00:00
|
|
|
fi
|
2022-01-04 14:14:39 +00:00
|
|
|
|
|
|
|
echo "upgrade-and-reboot for node $nodename is DONE"
|