fixes, logging, and better shellcheck conformance
[distro-setup] / primary-setup
1 #!/bin/bash
2
3 # usage $0 [MAIL_HOST]
4 # setup things which involve being the primary host or not
5
6 source /usr/local/lib/err
7
8 script_name="${BASH_SOURCE[0]}"
9 script_name="${script_name##*/}"
10 pre="$script_name:${0##*/}:${SSH_CLIENT:+ $HOSTNAME:}"
11 m() { printf "$pre %s\n" "$*"; "$@"; }
12 e() { printf "$pre %s\n" "$*"; }
13 err() { echo "[$(date +'%Y-%m-%d %H:%M:%S%z')]: $0: $*" >&2; }
14 # service maybe
15 serm() {
16 local service
17 service="${*: -1}"
18 if [[ ! -s /etc/systemd/system/$service ]]; then
19 return 0
20 fi
21 m "$@"
22 }
23
24 [[ $EUID == 0 ]] || exec sudo -E "${BASH_SOURCE[0]}" "$@"
25
26 if [[ $1 ]]; then
27 new_host=$1
28 if [[ $new_host == localhost ]]; then
29 mailhost_p=1
30 new_host=$HOSTNAME
31 else
32 mailhost_p=0
33 fi
34
35 # https://www.robustperception.io/how-to-have-labels-for-machine-roles
36 dir=/var/lib/prometheus/node-exporter
37 if [[ -e $dir ]]; then
38 cat > $dir/mailhost.prom <<EOF
39 host_role{role="mailhost"} $mailhost_p
40 EOF
41 fi
42
43 m sed -ri "s/MAIL_HOST=.*/MAIL_HOST=$new_host/" /a/bin/bash_unpublished/source-state
44 fi
45 source /a/bin/bash_unpublished/source-state
46
47 # fixing up a bad state that servers got in.
48 if [[ -e /dev/shm/iank-status ]]; then
49 chown iank.iank /dev/shm/iank-status
50 fi
51
52
53 if [[ $HOSTNAME == "$MAIL_HOST" || $HOSTNAME == kd ]]; then
54 m systemctl --now enable btrbk.timer
55 else
56 serm systemctl --now disable btrbk.timer
57 fi
58
59
60 if dpkg -s rss2email &>/dev/null; then
61 if [[ $HOSTNAME == "$MAIL_HOST" ]]; then
62 # arbtt disabled for now
63 #DISPLAY=:0 arbtt-capture --sample-rate=10 &
64 m systemctl --now enable rss2email.timer
65 else
66 files=(/sysd-mail-once/btrbk*)
67 if (( ${#files[@]} )); then
68 rm -f ${files[@]}
69 fi
70
71 serm systemctl stop rss2email.service
72 serm systemctl --now disable rss2email.timer
73 # arbtt disabled for now
74 # for ((i=0; i<10; i++)); do
75 # killall arbtt-capture || break
76 # sleep 1
77 # if [[ $i == 9 ]]; then
78 # exit 1
79 # fi
80 # done
81 fi
82 fi
83
84 # todo: whats going on here? figure out if this should be removed.
85
86 # if dpkg -s radicale &>/dev/null; then
87 # if [[ $HOSTNAME == "$MAIL_HOST" ]]; then
88 # m systemctl restart radicale
89 # m systemctl enable radicale
90 # if [[ -e /etc/logrotate.d/radicale.disabled ]]; then
91 # m mv /etc/logrotate.d/radicale{.disabled,}
92 # fi
93 # else
94 # m systemctl stop radicale
95 # m systemctl disable radicale
96 # # weekly logrotate tries to restart radicale even if it's a disabled service in flidas.
97 # if [[ -e /etc/logrotate.d/radicale ]]; then
98 # m mv /etc/logrotate.d/radicale{,.disabled}
99 # fi
100 # fi
101 # fi
102
103
104 # todo: this needs to be disabled earlier than primary-setup in switch-mail-host
105
106 # if dpkg -s profile-sync-daemon &>/dev/null; then
107 # if [[ $HOSTNAME == "$MAIL_HOST" ]]; then
108 # m systemctl --user start psd
109 # m systemctl --user enable psd
110 # else
111 # m systemctl --user stop psd
112 # m systemctl --user disable psd
113 # fi
114 # fi
115
116
117 m /a/exe/mail-setup
118 exit 0