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