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