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