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