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