rename err to bash-bear
[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 # usage: set DEBUG env var to anything to output to stdout
6
7 # Note: running this inside a cronjob, it wont mail any output if we end
8 # up rebooting from this script.
9
10 if [ -z "$BASH_VERSION" ]; then echo "error: shell is not bash" >&2; exit 1; fi
11
12 source /usr/local/lib/bash-bear
13 pre="${0##*/}:"
14 PATH="/sbin:$PATH"
15 m() { printf "$pre %s\n" "$*"; "$@"; }
16 e() { printf "$pre %s\n" "$*"; }
17 err() { echo "[$(date +'%Y-%m-%d %H:%M:%S%z')]: $pre: $*" >&2; }
18
19 [[ $EUID == 0 ]] || exec sudo -E "${BASH_SOURCE[0]}" "$@"
20
21 hn=$(hostname -f)
22 source /a/bin/bash_unpublished/source-state
23
24 d() {
25 if [[ $DEBUG ]]; then
26 pee cat "wall -n"
27 else
28 # 2>/dev/null hopefully gets rid of errors like: wall: /dev/pts/0: No such file or directory
29 sed 's/^/myupgrade /' | pee logger "wall -n" 2>/dev/null
30 fi
31 }
32 myreboot() {
33 for x in {30..1}; do
34 echo "pid $$. unattended upgrade, rebooting in $((x*10)) seconds" | wall -n 2>/dev/null
35 sleep 10
36 done
37 for x in {30..1}; do
38 if ! fuser /var/lib/dpkg/lock &> /dev/null; then
39 echo "pid $$. unattended upgrade, rebooting now" | d
40 /a/bin/ds/keyscript-on
41 /sbin/reboot
42 exit 0
43 fi
44 echo "pid $$. unattended upgrade reboot waiting 10 seconds for dpkg lock" | wall -n 2>/dev/null
45 sleep 10
46 done
47 echo "pid $$. dpkg locked for 5 minutes, automatic reboot failed" | d
48 }
49
50
51 case $hn in
52 kd)
53 /c/roles/prom/files/simple/usr/local/bin/fsf-install-prometheus
54 /c/roles/prom_export/files/simple/usr/local/bin/fsf-install-node-exporter -l 127.0.0.1
55 ;;
56 li|bk|je)
57 /c/roles/prom_export/files/simple/usr/local/bin/fsf-install-node-exporter -l 127.0.0.1
58 ;;
59 *)
60 /c/roles/prom_export/files/simple/usr/local/bin/fsf-install-node-exporter
61 ;;
62 esac
63
64
65 # TODO: executed from cron, this doesnt sent an email when we reboot,
66 # because rebooting from the script stops the ability to send email.
67 # We should figure some workaround.
68
69 # I originally had the idea that for MAIL_HOST, I'd just send an email
70 # if something needs restarting. But atm, I have a bad habit of ignoring
71 # those emails.
72
73 if ! needrestart -p -k &>/dev/null; then
74 myreboot
75 fi
76
77 needrestart -b -l -r a |& logger -t myupgrade
78 sleep 1
79
80 # Restart based on library doesnt always work. If we still
81 # detect outdated libraries after trying to automatically restart,
82 # then do a reboot unless we are on systems we care a bit more about
83 # uptime, then send us an email.
84 #
85 # Note: I've noticed restart based on library has tended to screw up
86 # desktop related daemons because they have bugs when restarted in
87 # isolation instead of as part of bring up and down the whole desktop.
88 # But, I'd rather something gets messed up than things not get
89 # restarted.
90 if ! /sbin/needrestart -p -l &>/dev/null; then
91 if [[ $hn == "$MAIL_HOST" || $hn == kd ]]; then
92 # send us an email so we can decide what to do
93 needrestart -r l
94 else
95 myreboot
96 fi
97 fi