bundlewrap/bundles/apt/files/upgrade-and-reboot
2020-10-23 04:33:01 +02:00

52 lines
1.4 KiB
Bash

#!/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
logins=$(ps h -C sshd -o euser | awk '$1 != "root" && $1 != "sshd"')
if [[ -n "$logins" ]]
then
echo "Will abort now, there are active SSH logins: $logins"
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
apt-get update
DEBIAN_FRONTEND=noninteractive apt-get -y -q -o Dpkg::Options::=--force-confold -o Dpkg::Options::=--force-confdef dist-upgrade
ret=$?
if (( $ret != 0 ))
then
exit 1
fi
DEBIAN_FRONTEND=noninteractive apt-get -y -q autoclean
DEBIAN_FRONTEND=noninteractive apt-get -y -q autoremove
if [[ -f /var/run/reboot-required ]]
then
date | mail -s "SYSREBOOTNOW ${node.name}" ${data['mail']}
systemctl reboot
fi