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".
|
|
|
|
if [[ "$1" != '-w' ]]
|
|
|
|
then
|
|
|
|
if which systemd-cat >/dev/null 2>&1
|
|
|
|
then
|
|
|
|
if [[ "$1" != "is-logging" ]]
|
|
|
|
then
|
|
|
|
exec systemd-cat -t upgrade-and-reboot "$0" is-logging "$@"
|
|
|
|
else
|
|
|
|
shift
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
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
|
|
|
|
2020-11-10 08:50:20 +00:00
|
|
|
logins=$(ps h -C sshd -o euser | awk '$1 != "root" && $1 != "sshd" && $1 != "sshmon"')
|
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
|
|
|
|
|
|
|
|
softlockdir=/var/lib/bundlewrap/soft-${node.name}
|
|
|
|
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
|
|
|
|
|
2020-11-10 08:50:20 +00:00
|
|
|
(
|
|
|
|
apt-get update
|
|
|
|
DEBIAN_FRONTEND=noninteractive apt-get -y -q -o Dpkg::Options::=--force-confold -o Dpkg::Options::=--force-confdef dist-upgrade
|
|
|
|
)
|
2020-10-18 13:33:41 +00:00
|
|
|
ret=$?
|
2020-11-21 17:07:01 +00:00
|
|
|
|
|
|
|
echo "apt-get dist-upgrade exited $ret"
|
|
|
|
echo "$ret" > "$statusfile"
|
|
|
|
|
2020-10-18 13:33:41 +00:00
|
|
|
if (( $ret != 0 ))
|
|
|
|
then
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2020-10-23 02:33:01 +00:00
|
|
|
DEBIAN_FRONTEND=noninteractive apt-get -y -q autoclean
|
|
|
|
DEBIAN_FRONTEND=noninteractive apt-get -y -q autoremove
|
2020-10-18 13:33:41 +00:00
|
|
|
|
|
|
|
if [[ -f /var/run/reboot-required ]]
|
|
|
|
then
|
2020-10-23 02:33:01 +00:00
|
|
|
date | mail -s "SYSREBOOTNOW ${node.name}" ${data['mail']}
|
2020-10-18 13:33:41 +00:00
|
|
|
systemctl reboot
|
|
|
|
fi
|