various fixes
[distro-setup] / conflink
1 #!/bin/bash
2
3 source /a/bin/errhandle/err
4 err-cleanup() {
5 echo 1 >~/.local/conflink
6 }
7
8
9 usage() {
10 cat <<EOF
11 Usage: ${0##*/} [OPTIONS]
12 Link or otherwise install configuration files.
13
14 -f For fast. Dont use lnf, use ln -sf. Good for updating existing files.
15 -v Verbose show all the files getting linked and whatnot.
16 EOF
17 exit $1
18 }
19
20
21 m() {
22 "$@"
23 }
24 s() { sudo "$@"; }
25 v() {
26 echo "$*"
27 "$@"
28 }
29
30 lnf() { /a/exe/lnf "$@"; }
31
32
33
34 ##### begin command line parsing ########
35
36 # ensure we can handle args with spaces or empty.
37 ret=0; getopt -T || ret=$?
38 [[ $ret == 4 ]] || { echo "Install util-linux for enhanced getopt" >&2; exit 1; }
39
40 fast=false
41 verbose=false
42 temp=$(getopt -l help hvf "$@") || usage 1
43 eval set -- "$temp"
44 while true; do
45 case $1 in
46 -v) verbose=true ;;
47 -f) fast=true ;;
48 -h|--help) usage ;;
49 --) shift; break ;;
50 *) echo "$0: unexpected args: $*" >&2 ; usage 1 ;;
51 esac
52 shift
53 done
54 readonly fast verbose
55
56 ##### end command line parsing ########
57
58
59 if $fast; then
60 lnf() { ln -sf "$@"; }
61 fi
62
63 if $verbose; then
64 m() {
65 echo "$*"
66 "$@"
67 }
68 fi
69
70 shopt -s nullglob
71 shopt -s extglob
72 shopt -s dotglob
73
74 # If we make a link back to the root, we stop going deeper into subdir_files.
75 # This makes it so we can do subdir directories. eg
76 # /p/c/subdir_files/.config/gajim -> ../../gagim
77 #
78 # Also note, under filesystem/, symlinks are expanded.
79
80 subdir-link-r() {
81 local root="$1"
82 local targets=()
83 if [[ $2 ]]; then
84 targets=( "$2"/!(.git|..|.|.#*) )
85 else
86 for f in "$1"/!(.git|..|.|.#*); do
87 if [[ -d $f ]]; then targets+=("$f"); fi
88 done
89 fi
90 local below
91 below="$( readlink -f "$root/..")"
92 for path in "${targets[@]}"; do
93 local fullpath
94 fullpath="$(readlink -f "$path")"
95 if [[ -f $path || $(dirname "$fullpath") == "$below" ]]; then
96 m lnf -T "$path" "$HOME/${path#$root/}"
97 elif [[ -d "$path" ]]; then
98 subdir-link-r "$root" "$path"
99 fi
100 done
101 }
102
103
104 common-file-setup() {
105 local dir fs x f reload_systemd
106 local -a reload_services
107 local -a restart_services
108 reload_systemd=false
109 for dir in "$@"; do
110 fs=$dir/filesystem
111 if [[ -e $fs && $user =~ ^iank?$ ]]; then
112 # we dont want t, instead c for checksum.
113 # That way we dont set times on directories.
114 # -a = -rlptgoD
115 cmd=( s rsync -rclpgoDiSAX --chown=root:root --chmod=g-s
116 --exclude=/etc/dovecot/users
117 --exclude='/etc/exim4/passwd*'
118 --exclude='/etc/exim4/*.pem'
119 $fs/ / )
120 echo "${cmd[@]@Q}"
121 while read -r line; do
122 file="${line:12}"
123 case $file in
124 etc/prometheus/rules/iank.yml|etc/prometheus/prometheus.yml)
125 case $HOSTNAME in
126 kd)
127 if systemctl is-active prometheus &>/dev/null; then
128 v s systemctl reload prometheus
129 fi
130 ;;
131 esac
132 ;;
133 etc/systemd/system/*)
134 reload_systemd=true
135 ;;
136 etc/dnsmasq.d/*)
137 restart_services+=(dnsmasq)
138 ;;
139 etc/systemd/resolved.conf.d/*)
140 restart_services+=(systemd-resolved)
141 ;;
142 esac
143 # Previously did this with tar, but it doesn't
144 # update directory permissions.
145 #
146 # S = do spare files efficiently
147 # A = preserve acls
148 # X = preserve extended attributes
149 # i = itemize
150 done < <("${cmd[@]}")
151 fi
152
153 if ! $fast && [[ -e $dir/subdir_files ]]; then
154 m subdir-link-r $dir/subdir_files
155 fi
156 local x=( $dir/!(binds|subdir_files|filesystem|machine_specific|..|.|.#*) )
157 (( ${#x[@]} >= 1 )) || continue
158 m lnf ${x[@]} ~
159 done
160 if $reload_systemd; then
161 v s systemctl daemon-reload
162 fi
163 for service in ${restart_services[@]}; do
164 if systemctl is-active $service >/dev/null; then
165 v s systemctl restart $service
166 fi
167 done
168 }
169
170 user=$(id -un)
171 all_dirs=({/a/bin/ds,/p/c}{,/machine_specific/$HOSTNAME})
172 # note, we assume a group of hosts does not have the
173 # same name as a single host, which is no problem on our scale.
174 for x in /p/c/machine_specific/*.hosts /a/bin/ds/machine_specific/*.hosts; do
175 if grep -qxF $HOSTNAME $x; then all_dirs+=( ${x%.hosts} ); fi
176 done
177
178
179 c_dirs=(/a/c{,/machine_specific/$HOSTNAME})
180 case $user in
181 iank)
182 # old files 2022-03
183 for t in systemstatus epanicclean btrfsmaintstop dynamicipupdate; do
184 f=/etc/systemd/system/$t.timer
185 if [[ -e $f ]]; then
186 v systemctl stop $t.timer
187 v systemctl disable $t.timer
188 s rm -fv $f
189 reload_systemd=true
190 fi
191 done
192 # old 2022-04
193 if [[ -e /etc/cron.daily/check-lets-encrypt-ssl-settings ]]; then
194 m s rm -f /etc/cron.daily/check-lets-encrypt-ssl-settings
195 fi
196 # conversion from whole folder subdir to individual files.
197 if [[ -L /home/iank/.config/copyq ]]; then
198 rm -fv /home/iank/.config/copyq
199 fi
200
201 /a/bin/ds/install-my-scripts
202 files=(/p/c/machine_specific/*/filesystem/etc/ssh/*_key
203 /p/c/machine_specific/*/filesystem/etc/openvpn/client/*.key
204 /p/c/filesystem/etc/openvpn/client/*.key
205 /p/c/filesystem/etc/openvpn/easy-rsa/keys/*.key
206 )
207 if [[ -e ${files[0]} ]]; then
208 chmod 600 ${files[@]}
209 fi
210 # p needs to go first so .ssh link is created, then config link inside it
211 m common-file-setup ${all_dirs[@]}
212
213 #### begin special extra stuff ####
214 install -d -m700 ~/gpg-agent-socket
215
216 f=/var/lib/bind
217 if [[ -e $f ]]; then
218 # reset to the original permissions.
219 m s chgrp -R bind $f
220 m s chmod g+w $f
221 fi
222 sudo bash -c 'shopt -s nullglob; for f in /etc/bind/*.key /etc/bind/*.private /etc/bind/key.*; do chgrp bind $f; done'
223 if [[ -e /etc/caldav-htpasswd ]] && getent group www-data &>/dev/null; then
224 s chgrp www-data /etc/caldav-htpasswd
225 fi
226 if [[ -e /var/lib/znc ]] && getent group znc; then
227 s chown -R znc:znc /var/lib/znc
228 fi
229 for f in /etc/prometheus-{,export-}htpasswd; do
230 if [[ -e $f ]]; then
231 s chmod 640 $f
232 if getent passwd www-data &>/dev/null; then
233 s chown root:www-data $f
234 fi
235 fi
236 done
237 f=/etc/prometheus-pass
238 if [[ -e $f ]]; then
239 # note: this is duplicative of the file's own permissions
240 s chmod 640 $f /etc/prometheus-pass
241 if getent passwd prometheus &>/dev/null; then
242 s chown root:prometheus $f
243 fi
244 fi
245 ##### end special extra stuff #####
246
247 if ! $fast; then
248 m sudo -H -u user2 "${BASH_SOURCE[0]}"
249 fi
250
251 f=/a/bin/distro-setup/system-status
252 if [[ -x $f ]]; then
253 $f _
254 fi
255 mkdir -p ~/.local
256 echo 0 >~/.local/conflink
257
258 ;;
259 user2)
260 m common-file-setup ${c_dirs[@]}
261 ;;
262 *)
263 echo "$0: error: unexpected user"; exit 1
264 ;;
265 esac