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