Mainly add external monitoring of mail server
[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 if [ -z "$BASH_VERSION" ]; then echo "error: shell is not bash" >&2; exit 1; fi
6
7 source /usr/local/lib/err
8 pre="${0##*/}:"
9 m() { printf "$pre %s\n" "$*"; "$@"; }
10 e() { printf "$pre %s\n" "$*"; }
11 err() { echo "[$(date +'%Y-%m-%d %H:%M:%S%z')]: $pre: $*" >&2; }
12
13 if [[ $EUID == 0 ]]; then
14 err "run as nonroot"
15 exit 1
16 fi
17
18 hn=$(hostname -f)
19 source /a/bin/bash_unpublished/source-state
20
21 l() {
22 "$@" |& systemd-cat -t myupgrade
23 }
24 l /a/bin/buildscripts/rust
25
26 has_x=false
27 for pkg in xorg wayland; do
28 if dpkg -s -- $x |& grep -Fx "Status: install ok installed" &> /dev/null; then
29 has_x=true
30 break
31 fi
32 done
33
34 if $has_x; then
35 l /a/bin/buildscripts/tor-browser
36 l /a/bin/buildscripts/misc
37 fi
38
39
40 source /a/bin/distro-setup/path-add-function
41 export GOPATH=$HOME/go
42 path-add $GOPATH/bin
43 path-add /usr/local/go/bin
44 l /a/bin/buildscripts/go
45 l go get -u mvdan.cc/fdroidcl
46
47 cmd="sudo /usr/sbin/checkrestart -p"
48 if [[ $($cmd | sed '/^Found 0 processes using old versions of upgraded files$/d' | wc -l) != 0 ]]; then
49 $cmd -v | pee cat "wall -n"
50 fi
51
52 # no automatic reboot for these hosts
53 if [[ $HOSTNAME == "$MAIL_HOST" || $hn == li.b8.nz ]]; then
54 exit 0
55 fi
56
57 if [[ -s /var/log/checkrestart.log ]]; then
58 for x in {30..1}; do
59 echo "pid $PID. unattended upgrade, rebooting in $((x*10)) seconds" | sudo wall -n
60 sleep 10
61 done
62 for x in {30..1}; do
63 if ! fuser /var/lib/dpkg/lock &> /dev/null; then
64 echo "pid $PID. unattended upgrade, rebooting now" | pee cat "sudo wall -n"
65 sudo /sbin/reboot
66 exit 0
67 fi
68 echo "pid $PID. unattended upgrade reboot waiting 10 seconds for dpkg lock" | sudo wall -n
69 sleep 10
70 done
71 echo "pid $PID. dpkg locked for 5 minutes, automatic reboot failed" | pee cat "sudo wall -n"
72 fi