bash-bear rename and a few improvements
[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 set -e; . /usr/local/lib/bash-bear; set +e
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 new_host=$HOSTNAME
30 fi
31 m sed -ri "s/MAIL_HOST=.*/MAIL_HOST=$new_host/" /a/bin/bash_unpublished/source-state
32 fi
33 source /a/bin/bash_unpublished/source-state
34
35 # fixing up a bad state that servers got in.
36 if [[ -e /dev/shm/iank-status ]]; then
37 chown iank.iank /dev/shm/iank-status
38 fi
39
40
41 if [[ $HOSTNAME == "$MAIL_HOST" ]]; then
42 mailhost_p=1
43 else
44 mailhost_p=0
45 fi
46 # https://www.robustperception.io/how-to-have-labels-for-machine-roles
47 dir=/var/lib/prometheus/node-exporter
48 if [[ -e $dir ]]; then
49 cat > $dir/mailhost.prom <<EOF
50 host_role{role="mailhost"} $mailhost_p
51 EOF
52 fi
53
54
55
56 if [[ $HOSTNAME == "$MAIL_HOST" || $HOSTNAME == kd ]]; then
57 m systemctl --now enable btrbk.timer
58 else
59 serm systemctl --now disable btrbk.timer
60 fi
61
62
63 if dpkg -s rss2email &>/dev/null; then
64 if [[ $HOSTNAME == "$MAIL_HOST" ]]; then
65 # arbtt disabled for now
66 #DISPLAY=:0 arbtt-capture --sample-rate=10 &
67 m systemctl --now enable rss2email.timer
68 else
69 files=(/sysd-mail-once/btrbk*)
70 if (( ${#files[@]} )); then
71 rm -f ${files[@]}
72 fi
73
74 serm systemctl stop rss2email.service
75 serm systemctl --now disable rss2email.timer
76 # arbtt disabled for now
77 # for ((i=0; i<10; i++)); do
78 # killall arbtt-capture || break
79 # sleep 1
80 # if [[ $i == 9 ]]; then
81 # exit 1
82 # fi
83 # done
84 fi
85 fi
86
87 # todo: whats going on here? figure out if this should be removed.
88
89 # if dpkg -s radicale &>/dev/null; then
90 # if [[ $HOSTNAME == "$MAIL_HOST" ]]; then
91 # m systemctl restart radicale
92 # m systemctl enable radicale
93 # if [[ -e /etc/logrotate.d/radicale.disabled ]]; then
94 # m mv /etc/logrotate.d/radicale{.disabled,}
95 # fi
96 # else
97 # m systemctl stop radicale
98 # m systemctl disable radicale
99 # # weekly logrotate tries to restart radicale even if it's a disabled service in flidas.
100 # if [[ -e /etc/logrotate.d/radicale ]]; then
101 # m mv /etc/logrotate.d/radicale{,.disabled}
102 # fi
103 # fi
104 # fi
105
106
107 # todo: this needs to be disabled earlier than primary-setup in switch-mail-host
108
109 # if dpkg -s profile-sync-daemon &>/dev/null; then
110 # if [[ $HOSTNAME == "$MAIL_HOST" ]]; then
111 # m systemctl --user start psd
112 # m systemctl --user enable psd
113 # else
114 # m systemctl --user stop psd
115 # m systemctl --user disable psd
116 # fi
117 # fi
118
119
120 m /a/exe/mail-setup
121 exit 0