minor fix and add automatic updates
[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 /p/c/filesystem/etc/openvpn/client/*.key
75 /p/c/filesystem/etc/openvpn/easy-rsa/keys/*.key
76 /p/c/machine_specific/kw/filesystem/etc/openvpn/client/*.key
77 )
78 if [[ -e $files ]]; then
79 chmod 600 ${files[@]}
80 fi
81 # p needs to go first so .ssh link is created, then config link inside it
82 m common-file-setup ${all_dirs[@]}
83
84 #### begin special extra stuff ####
85 install -d -m700 ~/gpg-agent-socket
86
87 f=/var/lib/bind
88 if [[ -e $f ]]; then
89 # reset to the original permissions.
90 m s chgrp -R bind $f
91 m s chmod g+w $f
92 fi
93 sudo bash -c 'shopt -s nullglob; for f in /etc/bind/*.key /etc/bind/*.private /etc/bind/key.*; do chgrp bind $f; done'
94 if [[ -e /etc/davpass ]] && getent group www-data &>/dev/null; then
95 s chgrp www-data /etc/davpass
96 fi
97 if [[ -e /var/lib/znc ]] && getent group znc; then
98 s chown -R znc:znc /var/lib/znc
99 fi
100 /a/exe/lnf -T /p/arbtt-capture.log ~/.arbtt/capture.log
101 f=/etc/prometheus-htpasswd
102 if [[ -e $f ]]; then
103 s chmod 640 $f /etc/prometheus-pass
104 s chown root:www-data $f
105 if getent passwd prometheus; then
106 s chown root:prometheus /etc/prometheus-pass
107 fi
108 fi
109
110 ##### end special extra stuff #####
111
112 sudo bash -c 'shopt -s nullglob; cd /etc/openvpn; for f in client/* server/*; do ln -sf $f .; done'
113
114 m sudo -H -u traci "$BASH_SOURCE"
115 ;;
116 traci)
117 m common-file-setup ${c_dirs[@]}
118 ;;
119 *)
120 echo "$0: error: unexpected user"; exit 1
121 ;;
122 esac