bunch of fixes, change sy host, deploy some new stuff
[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 myreboot() {
23 for x in {30..1}; do
24 echo "pid $$. unattended upgrade, rebooting in $((x*10)) seconds" | wall -n
25 sleep 10
26 done
27 for x in {30..1}; do
28 if ! fuser /var/lib/dpkg/lock &> /dev/null; then
29 echo "pid $$. unattended upgrade, rebooting now" | pee cat "wall -n"
30 /a/bin/ds/keyscript-on
31 /sbin/reboot
32 exit 0
33 fi
34 echo "pid $$. unattended upgrade reboot waiting 10 seconds for dpkg lock" | wall -n
35 sleep 10
36 done
37 echo "pid $$. dpkg locked for 5 minutes, automatic reboot failed" | pee cat "wall -n"
38 }
39
40
41 if [[ $HOSTNAME != "$MAIL_HOST" ]]; then
42 ksta=$(needrestart -b | sed -n 's/NEEDRESTART-KSTA: *//p');
43 case $ksta in
44 1) : ;;
45 [023]) myreboot ;;
46 *) err error: unknown kernel status ;;
47 esac
48 fi
49
50 # needsrestarts automatic restarts failed.
51 if ! out=$(/sbin/needrestart -p 2>&1); then
52 e "$out"
53 fi