c358fcb5c59a8894e79df07837d9cda8417a0ee8
[distro-setup] / filesystem / usr / local / bin / myupgrade
1 #!/bin/bash
2 # Copyright (C) 2019 Ian Kelling
3 # SPDX-License-Identifier: AGPL-3.0-or-later
4
5 # Note: running this inside a cronjob, it wont mail any output if we end
6 # up rebooting from this script.
7
8 if [ -z "$BASH_VERSION" ]; then echo "error: shell is not bash" >&2; exit 1; fi
9
10 source /usr/local/lib/err
11 pre="${0##*/}:"
12 PATH="/sbin:$PATH"
13 m() { printf "$pre %s\n" "$*"; "$@"; }
14 e() { printf "$pre %s\n" "$*"; }
15 err() { echo "[$(date +'%Y-%m-%d %H:%M:%S%z')]: $pre: $*" >&2; }
16
17 [[ $EUID == 0 ]] || exec sudo -E "${BASH_SOURCE[0]}" "$@"
18
19 hn=$(hostname -f)
20 source /a/bin/bash_unpublished/source-state
21
22 l() {
23 "$@" |& systemd-cat -t myupgrade
24 }
25
26 ## temporary
27 case $HOSTNAME in
28 kd)
29 exit 0
30 ;;
31 esac
32
33 if checkrestart -b /a/bin/ds/checkrestart-blacklist -p -t &>/dev/null; then
34 exit 0
35 fi
36
37 if [[ $HOSTNAME == "$MAIL_HOST" ]]; then
38 # no automatic reboot for this host, just make an alert
39 checkrestart -b /a/bin/ds/checkrestart-blacklist -p 2>/dev/null ||:
40 exit 0
41 else
42 l checkrestart -b /a/bin/ds/checkrestart-blacklist -p 2>/dev/null ||:
43 fi
44
45 for x in {30..1}; do
46 echo "pid $$. unattended upgrade, rebooting in $((x*10)) seconds" | wall -n
47 sleep 10
48 done
49 for x in {30..1}; do
50 if ! fuser /var/lib/dpkg/lock &> /dev/null; then
51 echo "pid $$. unattended upgrade, rebooting now" | pee cat "wall -n"
52 /a/bin/ds/keyscript-on
53 /sbin/reboot
54 exit 0
55 fi
56 echo "pid $$. unattended upgrade reboot waiting 10 seconds for dpkg lock" | wall -n
57 sleep 10
58 done
59 echo "pid $$. dpkg locked for 5 minutes, automatic reboot failed" | pee cat "wall -n"