fix btrbk service
[distro-setup] / conflink
1 #!/bin/bash
2
3 source /a/bin/errhandle/err
4
5 m() {
6 echo "$*"
7 "$@"
8 }
9 s() { sudo "$@"; }
10 lnf() { /a/exe/lnf "$@"; }
11
12
13 shopt -s nullglob
14 shopt -s extglob # note, already set with bash -l
15
16 # If we make a link back to the root, we stop going deeper into subdir_files.
17 # This makes it so we can do subdir directories.
18 #
19 # Also note, under filesystem/, symlinks are expanded.
20
21 subdir-link-r() {
22 local root="$1"
23 local targets=()
24 if [[ $2 ]]; then
25 targets=( "$2"/!(.git|..|.) )
26 else
27 for f in "$1"/!(.git|..|.); do
28 [[ -d $f ]] && targets+=("$f") ||:
29 done
30 fi
31 local below="$( readlink -f "$root/..")"
32 for path in "${targets[@]}"; do
33 local fullpath="$(readlink -f "$path")"
34 #e $fullpath $below # debug
35 if [[ -f $path || $(dirname $(readlink -f "$fullpath")) == "$below" ]]; then
36 m lnf -T "$path" "$HOME/${path#$root/}"
37 elif [[ -d "$path" ]]; then
38 subdir-link-r "$root" "$path"
39 fi
40 done
41 }
42
43
44
45 common-file-setup() {
46 local dir fs x bdir f dst
47 for dir in "$@"; do
48 fs=$dir/filesystem
49 if [[ -e $fs && $USER =~ ^iank?$ ]]; then
50 # note, symlinks get resolved, not copied.
51 s tar --mode=g-s --owner=0 --group=0 -cz -C $fs . | s tar -xz -C /
52 fi
53
54 if [[ -e $dir/subdir_files ]]; then
55 m subdir-link-r $dir/subdir_files
56 fi
57 local x=( $dir/!(binds|subdir_files|filesystem|machine_specific|..|.) )
58 (( ${#x[@]} >= 1 )) || continue
59 m lnf ${x[@]} ~
60 done
61 }
62
63 all_dirs=({/a/c,/p/c}{,/machine_specific/$HOSTNAME})
64 # note, we assume a group of hosts does not have the
65 # same name as a single host, which is no problem on our scale.
66 for x in /p/c/machine_specific/*.hosts /a/bin/ds/machine_specific/*.hosts; do
67 if grep -qxF $HOSTNAME $x; then all_dirs+=( ${x%.hosts} ); fi
68 done
69
70 c_dirs=(/a/c{,/machine_specific/$HOSTNAME})
71 case $USER in
72 iank)
73 files=(/p/c/machine_specific/*/filesystem/etc/ssh/*_key)
74 if [[ -e $files ]]; then
75 chmod 600 ${files[@]}
76 fi
77 # p needs to go first so .ssh link is created, then config link inside it
78 m common-file-setup ${all_dirs[@]}
79
80 #### begin special extra stuff ####
81 install -d -m700 ~/gpg-agent-socket
82
83 f=/var/lib/bind
84 if [[ -e $f ]]; then
85 # reset to the original permissions.
86 m s chgrp -R bind $f
87 m s chmod g+w $f
88 fi
89 sudo bash -c 'shopt -s nullglob; for f in /etc/bind/*.key /etc/bind/*.private /etc/bind/key.*; do chgrp bind $f; done'
90 if [[ -e /etc/davpass ]] && getent group www-data &>/dev/null; then
91 s chgrp www-data /etc/davpass
92 fi
93 if [[ -e /var/lib/znc ]] && getent group znc; then
94 s chown -R znc:znc /var/lib/znc
95 fi
96 /a/exe/lnf -T /p/arbtt-capture.log ~/.arbtt/capture.log
97 f=/etc/prometheus-htpasswd
98 if [[ -e $f ]]; then
99 s chmod 640 $f /etc/prometheus-pass
100 s chown root:www-data $f
101 if getent passwd prometheus; then
102 s chown root:prometheus /etc/prometheus-pass
103 fi
104 fi
105
106 ##### end special extra stuff #####
107
108 sudo bash -c 'shopt -s nullglob; cd /etc/openvpn; for f in client/* server/*; do ln -sf $f .; done'
109 sudo bash -c 'shopt -s nullglob; cd /etc/openvpn; for f in server/*.key client/*.key; do chmod 600 $f; done'
110
111 m sudo -H -u traci "$BASH_SOURCE"
112 ;;
113 traci)
114 m common-file-setup ${c_dirs[@]}
115 ;;
116 *)
117 echo "$0: error: unexpected user"; exit 1
118 ;;
119 esac