host info updates
[distro-setup] / filesystem / usr / local / bin / myupgrade
1 #!/bin/bash
2 # I, Ian Kelling, follow the GNU license recommendations at
3 # https://www.gnu.org/licenses/license-recommendations.en.html. They
4 # recommend that small programs, < 300 lines, be licensed under the
5 # Apache License 2.0. This file contains or is part of one or more small
6 # programs. If a small program grows beyond 300 lines, I plan to switch
7 # its license to GPL.
8
9 # Copyright 2024 Ian Kelling
10
11 # Licensed under the Apache License, Version 2.0 (the "License");
12 # you may not use this file except in compliance with the License.
13 # You may obtain a copy of the License at
14
15 # http://www.apache.org/licenses/LICENSE-2.0
16
17 # Unless required by applicable law or agreed to in writing, software
18 # distributed under the License is distributed on an "AS IS" BASIS,
19 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 # See the License for the specific language governing permissions and
21 # limitations under the License.
22
23
24 # usage: set DEBUG env var to anything to output to stdout
25
26 # Note: running this inside a cronjob, it wont mail any output if we end
27 # up rebooting from this script.
28
29 set -e; . /usr/local/lib/bash-bear; set +e
30
31 pre="${0##*/}:"
32 PATH="/sbin:$PATH"
33 m() { printf "$pre %s\n" "$*"; "$@"; }
34 e() { printf "$pre %s\n" "$*"; }
35 err() { echo "[$(date +'%Y-%m-%d %H:%M:%S%z')]: $pre: $*" >&2; }
36
37 [[ $EUID == 0 ]] || exec sudo -E "${BASH_SOURCE[0]}" "$@"
38
39 hn=$(hostname -f)
40 source /a/bin/bash_unpublished/source-state
41
42 d() {
43 if [[ $DEBUG ]]; then
44 pee cat "wall -n"
45 else
46 # 2>/dev/null hopefully gets rid of errors like: wall: /dev/pts/0: No such file or directory
47 sed 's/^/myupgrade /' | pee logger "wall -n" 2>/dev/null
48 fi
49 }
50 myreboot() {
51 if [[ -e /tmp/myupgrade-noreboot ]]; then
52 e "would rebooted but /tmp/myupgrade-noreboot exists"
53 return
54 fi
55
56 for x in {30..1}; do
57 echo "pid $$. unattended upgrade, rebooting in $((x*10)) seconds" | wall -n 2>/dev/null
58 sleep 10
59 done
60 for x in {30..1}; do
61 if ! fuser /var/lib/dpkg/lock &> /dev/null; then
62 echo "pid $$. unattended upgrade, rebooting now" | d
63 /a/bin/ds/keyscript-on
64 /sbin/reboot
65 exit 0
66 fi
67 echo "pid $$. unattended upgrade reboot waiting 10 seconds for dpkg lock" | wall -n 2>/dev/null
68 sleep 10
69 done
70 echo "pid $$. dpkg locked for 5 minutes, automatic reboot failed" | d
71 }
72
73
74 case $hn in
75 kd)
76 /c/roles/prom/files/simple/usr/local/bin/fsf-install-prometheus
77 /c/roles/prom_export/files/simple/usr/local/bin/fsf-install-node-exporter -l 127.0.0.1
78 ;;
79 li|bk|je)
80 /c/roles/prom_export/files/simple/usr/local/bin/fsf-install-node-exporter -l 127.0.0.1
81 ;;
82 *)
83 /c/roles/prom_export/files/simple/usr/local/bin/fsf-install-node-exporter
84 ;;
85 esac
86
87 case $hn in
88 sy)
89 /b/fai/config/distro-install-common/install-stable-kernel-debs
90 ;;
91 esac
92
93
94 # TODO: executed from cron, this doesnt sent an email when we reboot,
95 # because rebooting from the script stops the ability to send email.
96 # We should figure some workaround.
97
98 # I originally had the idea that for MAIL_HOST, I'd just send an email
99 # if something needs restarting. But atm, I have a bad habit of ignoring
100 # those emails.
101
102 if ! needrestart -p -k &>/dev/null; then
103 myreboot
104 fi
105
106 needrestart -b -l -r a |& logger -t myupgrade
107 sleep 1
108
109 # Restart based on library doesnt always work. If we still
110 # detect outdated libraries after trying to automatically restart,
111 # then do a reboot unless we are on systems we care a bit more about
112 # uptime, then send us an email.
113 #
114 # Note: I've noticed restart based on library has tended to screw up
115 # desktop related daemons because they have bugs when restarted in
116 # isolation instead of as part of bring up and down the whole desktop.
117 # But, I'd rather something gets messed up than things not get
118 # restarted.
119 if ! /sbin/needrestart -p &>/dev/null; then
120 if [[ $hn == "$MAIL_HOST" || $hn == kd ]]; then
121 # send us an email so we can decide what to do
122 needrestart -r l
123 else
124 myreboot
125 fi
126 fi