This repository has been archived by the owner on Mar 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
/
provsn
executable file
·249 lines (214 loc) · 5.85 KB
/
provsn
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#!/usr/bin/env bash
set -eo pipefail; [[ $PROVSN_TRACE ]] && set -x
# deps: parallel bash ssh jq readlink dirname find xargs sed awk uniq sort
provsn_template() {
local src="$1"
local dest="$2"
# escape double-quotes so they won't interfere with rendering
local tpl="$(cat $src | sed -e 's/"/\\"/g')"
# render the template by printing it out, let shell take care of variables
local out="$(eval "printf %s\\\n \"$tpl\"")"
[ -z "$out" ] \
&& echo "error: unknown template: $src" 1>&2 \
&& exit 1
# detect and raise unknown var errors
for line in "$out"; do
local regexp='ERR_UNKNOWN_VAR_([0-9a-z_]+)'
[[ "$line" =~ $regexp ]] \
&& echo "error: unknown var: ${BASH_REMATCH[1]} (in $src)" 1>&2 \
&& exit 1
done
printf %s\\n "$out" > "$dest"
}
var() {
local key="$1"
[ "$2" ] && key="$1_$2"
local prefixes="all_ $host""_"
# default to an error message, in case none of the prefixes yields a value
for p in $prefixes; do
# try printing the variable with that prefix
local try=$(eval 'printf %s\\n "${'$p$key'[@]}"')
# if it yielded something, save that as the result
[ -z "$try" ] || local value=${try[@]}
done
declare -p value 2>/dev/null >/dev/null || local value="ERR_UNKNOWN_VAR_$key"
printf %s\\n "${value[@]}"
}
lookup() {
local key=$1
local value=$(var "$key")
[ "$value" == "ERR_UNKNOWN_VAR_$key" ] \
&& echo "error: unknown var: $key (in unit=$unit)" 1>&2 \
&& exit 1
printf %s\\n "${value[@]}"
}
find_units() {
for u in `find ./*/ -name env.sh | xargs readlink -f | xargs dirname`; do
u="$u/"
u="${u#$(pwd)/}"
u="${u%/}"
[ -z "$u" ] && u="."
echo "$u"
done | LC_ALL=C sort
}
resolve_pattern() {
local pattern="$1"
test -n "$pattern" || pattern="all"
if test "$pattern" = "all"; then
printf %s\\n "${provsn_hosts[@]}"
else
printf %s\\n "${provsn_hosts[@]}" | grep -x "$pattern"
fi
}
provsn_usage() {
echo "specify a command: provsn build|upload|install|exec|run|units|hosts"
}
provsn_upload() {
local host="$1"
echo "$host: uploading"
tar -c -C "$provsn_root/.build/$host" . |
ssh $(lookup ssh_options) $(lookup ssh) \
"set -e && rm -rf /opt/provsn && mkdir -p /opt/provsn && tar -x -C /opt/provsn"
}
provsn_exec() {
local host="$1"
local cmd="$2"
ssh $(lookup ssh_options) $(lookup ssh) \
"set -e && cd /opt/provsn && PROVSN_TRACE=$PROVSN_TRACE $cmd |& awk '{print \"$host: \" \$0}' /dev/stdin"
}
provsn_build() {
local host="$1"
rm -rf "$provsn_root/.build/$host"
mkdir -p "$provsn_root/.build/$host"
cp -a "$0" env.sh "$provsn_root/.build/$host/"
echo "$host" > "$provsn_root/.build/$host/host"
local omit_units=($(lookup omit_build))
local unit
for unit in $(lookup units); do
local omit=$(printf %s\\n "${omit_units[@]}" | grep -xF $unit || true)
if [ ! -z "$omit" ]; then
echo "$host: omit $unit"
else
local parent_dir="$provsn_root/.build/$host/$(dirname $unit)"
[ ! -d "$parent_dir" ] && mkdir -p $parent_dir
[ -d "$provsn_root/$unit" ] && cp -a "$provsn_root/$unit" "$provsn_root/.build/$host/$unit"
fi
done
provsn_root="$provsn_root/.build/$host" provsn_run build.sh | awk "{print \"$host: \" \$0}"
}
provsn_run() {
local host=$(cat "$provsn_root/host")
local script="$1"
local unit
echo "run $script"
for unit in $(lookup units); do
if [ -x "$provsn_root/$unit/$script" ]; then
cd "$provsn_root/$unit" && . "$script"
cd "$provsn_root"
fi
done
echo "finished run $script"
}
provsn_root=`pwd`
. "$provsn_root/env.sh"
find_units() {
local host=
local the_units=()
for host in ${provsn_hosts[@]}; do
for u in $(lookup units); do
the_units+=($u)
done
done
printf %s\\n ${the_units[@]} | sort | uniq
}
provsn_units=`find_units`
for unit in ${provsn_units[@]}; do
if [ -f "$unit/env.sh" ]; then
. "$unit/env.sh"
else
echo "skipping $unit"
fi
done
unit=
test -z "$PROVSN_JOBS" && PROVSN_JOBS=4
# exits if no host matched
# returns 1 if parallel did happen
# returns 0 if it didn't
provsn_in_parallel() {
hosts=`resolve_pattern "$2"`
test -z "$hosts" && echo "error: unknown host: $2" 1>&2 && exit 1
n=`printf %s\\\n "${hosts[@]}" | wc -l`
if test 1 -lt "$n"; then
printf %s\\n "${hosts[@]}" \
| parallel -j$PROVSN_JOBS --halt 1 --line-buffer --no-notice "$0" "$1" {} "'$3'"
return 1
fi
return 0
}
case $1 in
deploy)
provsn_in_parallel "$@" || exit 0
provsn_build "$2"
provsn_upload "$2"
provsn_exec "$2" "./provsn run install.sh"
;;
build)
provsn_in_parallel "$@" || exit 0
# creates host's build dir
# copies provsn and env.sh files to build dirs
# put the host name somewhere in the build dirs
# builddir/provsn run build.sh
provsn_build "$2"
;;
upload)
provsn_in_parallel "$@" || exit 0
# uploads each host's build dir
provsn_upload "$2"
;;
install)
provsn_in_parallel "$@" || exit 0
# execs provsn run install.sh
provsn_exec "$2" "./provsn run install.sh"
;;
run)
# runs the script
# requires host name in builddir
provsn_run "$2"
;;
exec)
provsn_in_parallel "$@" || exit 0
provsn_exec "$2" "$3"
;;
get)
hosts=`resolve_pattern "$2"`
test -z "$hosts" && echo "error: unknown host: $2" 1>&2 && exit 1
if test "$(printf %s\\n ${hosts[@]} | wc -l)" -gt 1; then
for host in ${hosts[@]}; do
printf %s\\n "$(lookup "$3")" | awk "{print \"$host: \" \$0}" /dev/stdin
done
else
host="${hosts[0]}"
printf %s\\n "$(lookup $3)"
fi
;;
units)
test -z "$2" && printf %s\\n "${provsn_units[@]}" && exit 0
hosts=`resolve_pattern "$2"`
test -z "$hosts" && echo "error: unknown host: $2" 1>&2 && exit 1
for host in ${hosts[@]}; do
printf %s\\n "$(lookup units)"
done | sort | uniq
;;
hosts)
resolve_pattern "$2"
;;
"")
provsn_usage
;;
*)
{ echo "error: unknown command: $1";
echo;
provsn_usage; } 1>&2
exit 1
;;
esac