-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathav
executable file
·505 lines (423 loc) · 12.8 KB
/
av
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
#!/usr/bin/env bash
set -euo pipefail
set -E
export LOG_LEVEL=${LOG_LEVEL:-info}
export GIT_TERMINAL_PROMPT=0
lib=$(dirname $(readlink -f $0))
source $lib/config
source $lib/log
log_level info
version=1.4.0
lock_file=$lib/.lock
pause_file=$lib/.pause
data_dir=$lib/data
status_file=$data_dir/status
force=""
command=""
command_arg=""
no_fetch=""
local_inventory=""
inventory_git_url=${inventory_git_url:-}
valid_name='^[a-z0-9_\-]+$'
function usage {
cat <<EOF
av - manage configuration for your hosts
Usage:
av [options] [command] [command-args]
Options:
--help Show this help message
--version Show version
--log-level <level> Set the log level (trace,debug,info,warn,critical) [default: info]
--inventory <path> Use a local inventory at the specified path
--no-fetch Don't fetch the inventory before running the command
--force Run even if a pause or run lock is set
--quiet|-q Suppress log messages
Commands:
status Report the status of the last run of \`apply\` [default]
host <host> <action> Perform actions specific to a given host; more below
apply Apply roles and their associated modules on this host
fetch Update local database by fetching from upstream
directive Run any outstanding directives from the inventory
recover Reset run lock file after a failure
pause Set the pause lock to avoid periodic runs while debugging
resume Resume periodic runs after a pause
list-hosts [filter] List hosts with an optional filter (e.g. role=web)
list-modules List all available modules
list-roles List all available roles
Host actions:
host <host> [host...] Show attributes for the host(s)
host <host> add Add the host to the inventory
host <host> remove Remove the host from the inventory
host <host> add-module <module> Add the module to the host in the inventory
host <host> remove-module <module> Remove the module from the host in the inventory
host <host> add-role <role> Add the role to the host in the inventory
host <host> remove-role <role> Remove the role from the host in the inventory
EOF
}
function version {
echo $version
}
function handle_err {
local _ec="$?"
local _cmd="${BASH_COMMAND:-unknown}"
log_error "The command \`${_cmd}\` exited with exit code ${_ec}"
}
function handle_exit {
if (($? != 0)); then
log_error Failed
status FAIL
fi
}
function status {
local date=$(date +'%Y-%m-%d %H:%M:%S')
mkdir -p $(dirname $status_file)
echo $date STATUS $1 > $status_file
}
function apply_module {
local module=$1
log_info Applying module $module
local opts=$([[ $LOG_LEVEL == 'trace' ]] && echo '-x')
# eval to not include quote characters in values
eval export $(host_variables $(hostname))
bash $opts $inventory_dir/modules/$module/apply
mkdir -p $data_dir/applied_modules
echo OK > $data_dir/applied_modules/$module
}
function test_module {
local module=$1
[[ -e $inventory_dir/modules/$module/test ]] || return 0
log_info Testing module $module
local opts=$([[ $LOG_LEVEL == 'trace' ]] && echo '-x')
if ! bash $opts $inventory_dir/modules/$module/test > /dev/null; then
log_error Test failed for module $module
exit 1
else
log_info Tests passed for module $module
fi
}
function drop_module {
local module=$1
log_info Dropping module $module
local opts=$([[ $LOG_LEVEL == 'trace' ]] && echo '-x')
bash $opts $inventory_dir/modules/$module/drop
unlink $data_dir/applied_modules/$module
}
# parse command line arguments
while [[ "$#" -gt 0 ]]; do
case $1 in
--force) force=1;;
--no-fetch) no_fetch=1;;
--quiet|-q) log_quiet;;
--help) usage && exit;;
--version) version && exit;;
--log-level) shift && log_level $1;;
--inventory) shift && no_fetch=1 && local_inventory=$(readlink -f $1);;
*)
if [[ -z "$command" ]]; then
command=$1
elif [[ -z "$command_arg" ]]; then
command_arg=$@
fi
;;
esac
shift
done
cd $aviary_root
[[ ! -z "$local_inventory" ]] && inventory_dir=$local_inventory
[[ -z "$command" ]] && command=status
function host_roles {
local host=${1:-$(hostname)}
roles=$(grep -hs ^ $inventory_dir/hosts/$host/roles || true)
echo "$roles"
}
function host_modules {
local host=${1:-$(hostname)}
# identify our roles
roles=$(grep -hs ^ $inventory_dir/hosts/$host/roles || true)
roles_pattern={${roles//$'\n'/,},}
modules=$(eval "{ grep -hs ^ $inventory_dir/hosts/$host/modules $inventory_dir/roles/$roles_pattern/modules || true; }" | awk '!x[$0]++')
echo "$modules"
}
function host_variables {
local host=${1:-$(hostname)}
roles=$(grep -hs ^ $inventory_dir/hosts/$host/roles || true)
roles_pattern={${roles//$'\n'/,},}
variables=$(eval "{ grep -hs ^ $inventory_dir/roles/$roles_pattern/variables $inventory_dir/hosts/$host/variables || true; }" | awk '!x[$0]++')
echo "$variables"
}
function identify {
if [[ ! -d $inventory_dir/hosts/$(hostname) ]]; then
log_error "Couldn't find host $(hostname) in inventory"
exit 1
fi
# identify our roles and modules
roles=$(host_roles)
modules=$(host_modules)
# find applied modules no longer in the inventory
mkdir -p $data_dir/applied_modules
applied_modules=$(ls $data_dir/applied_modules)
droppable_modules=$(comm -13 <(echo "$modules" | sort) <(echo "$applied_modules" | sort))
}
function check_inventory {
if [[ -z "$inventory_git_url" ]]; then
log_error "No inventory_git_url configured in $lib/config"
exit 1
fi
if [[ ! -e $inventory_dir ]]; then
log_error "Couldn't find inventory at $inventory_dir"
exit 1
fi
}
function execute_directives {
if [[ -d $inventory_dir/directives ]]; then
# find directives from within the last day which have not been executed
inventory_directives=$(find $inventory_dir/directives -type f -mtime 0 -exec ls -1rt {} + | xargs -n1 basename)
mkdir -p $data_dir/executed_directives
for directive in $inventory_directives; do
mtime=$(stat -c "%Y" $inventory_dir/directives/$directive)
if ! grep -qhs $mtime $data_dir/executed_directives/$directive; then
log_info Executing directive $directive
echo $mtime > $data_dir/executed_directives/$directive
bash $inventory_dir/directives/$directive
fi
done
fi
}
function fetch_inventory {
if [[ ! -z "$local_inventory" ]]; then
log_info "Using local inventory $(readlink -f $local_inventory)"
elif [[ ! -z "$no_fetch" ]]; then
log_info "Not fetching inventory"
else
check_inventory
log_info "Fetching inventory"
if [[ -e $inventory_dir/.git ]]; then
if [[ -n $(git -C $inventory_dir status --porcelain) ]]; then
log_error "Local inventory checkout is dirty; try --no-fetch or reset"
exit 1
fi
git -C $inventory_dir reset --hard > /dev/null
git -C $inventory_dir clean -fd > /dev/null
fi
(timeout 60 git -C $inventory_dir pull > /dev/null) || (timeout 60 git clone $inventory_git_url $inventory_dir > /dev/null)
fi
}
function host_entry {
variables=$(host_variables $host)
roles=$((grep -hs ^ $inventory_dir/hosts/$host/roles || true) | sed 's/^/role=/g')
modules=$(host_modules $host | sed 's/^/module=/g')
entries="host=${host} "$(echo $roles $modules $variables | paste -s)$'\n'
echo "$entries"
}
function host_remove_module {
local host=$1
local module=$2
local modules_file=$inventory_dir/hosts/$host/modules
echo "$((grep -hs ^ $modules_file || true) | grep -v $module)" > $modules_file
#cp $modules_file /var/tmp/m1
#grep -v '^$' $modules_file || true > $modules_file
}
function host_add_module {
local host=$1
local module=$2
local modules_file=$inventory_dir/hosts/$host/modules
if [[ -z "$(ls $inventory_dir/modules | grep $module)" ]]; then
log_error "couldn't find module $module"
exit 1
fi
mkdir -p $inventory_dir/hosts/$host
host_remove_module $host $module
echo $module >> $modules_file
}
function host_remove_role {
local host=$1
local role=$2
local roles_file=$inventory_dir/hosts/$host/roles
echo "$(cat $roles_file | grep -v $subcommand_arg)" > $roles_file
}
function host_add_role {
local host=$1
local role=$2
local roles_file=$inventory_dir/hosts/$host/roles
if [[ -z "$(ls $inventory_dir/roles | grep $role)" ]]; then
log_error "Couldn't find module $role"
exit 1
fi
mkdir -p $inventory_dir/hosts/$host
host_remove_module $host $role
echo $role >> $roles_file
}
function check_roles {
for role in $(ls -1 $inventory_dir/roles); do
if [[ ! -e $inventory_dir/roles/$role/modules ]]; then
log_warn "No modules file for role \`$role\`"
fi
malformed_modules=$(grep -sPv $valid_name $inventory_dir/roles/$role/modules || true);
if [[ ! -z $malformed_modules ]]; then
log_error Malformed role modules: $malformed_modules
fi
for module in $(grep -hs ^ $inventory_dir/roles/$role/modules || true); do
if [[ ! -e $inventory_dir/modules/$module ]]; then
log_warn "Role \`$role\` module \`$module\` does not exist"
fi
done
done
}
function check_modules {
for module in $(ls -1 $inventory_dir/modules); do
if [[ ! -e $inventory_dir/modules/$module/apply ]]; then
log_warn "No apply script for module \`$module\`"
fi
done
}
function check_hosts {
for host in $(ls -1 $inventory_dir/hosts); do
if [[ -z $(ls -1 $inventory_dir/hosts/$host/{modules,roles}) ]]; then
log_warn "No roles for host $host"
fi
done
}
case $command in
version)
echo $version
;;
apply)
trap handle_err ERR
trap handle_exit EXIT
fetch_inventory
identify
# consult pause file
if [[ -e $pause_file && ! $force ]]; then
log_warn 'Bailing for pause lock file; try `av resume`'
exit 1
fi
# set up our lock file
if [[ -e $lock_file && ! $force ]]; then
log_error 'Bailing for run lock file; try `av recover`'
exit 1
else
touch $lock_file
fi
# apply modules
log_info Running apply
for module in $modules; do
apply_module $module
test_module $module
done
# drop obsolete modules
for droppable_module in $droppable_modules; do
drop_module $droppable_module
done
rm $lock_file
status OK
log_info 'Done'
;;
status)
# list out what we would do
fetch_inventory
identify
echo Using inventory $inventory_dir
echo
echo hostname: $(hostname)
echo roles: ${roles:-'<none>'}
echo modules: ${modules:-'<none>'}
echo
[[ ! -e $status_file ]] && echo 'STATUS UNKNOWN' > $status_file
cat $status_file
echo
[[ -e $lock_file ]] && echo '-- Run lock is set; clear with `av recover` --'
[[ -e $pause_file ]] && echo '-- Pause lock is set; clear with `av resume` --'
;;
directive)
fetch_inventory
execute_directives
;;
list-hosts)
filters=$command_arg
fetch_inventory
entries=""
hosts=$(ls -1 $inventory_dir/hosts)
for host in $hosts; do
host_entry=$(host_entry $host)
entries=${entries}$host$'\t'${host_entry}$'\n'
done
log_debug $'\n'"$entries"
for filter in $filters; do
entries=$(echo "$entries" | grep -P $filter)
done
echo "$entries" | grep -v '^$' | cut -f1
;;
list-modules)
fetch_inventory
ls -1 $inventory_dir/modules
;;
list-roles)
fetch_inventory
ls -1 $inventory_dir/roles
;;
check)
check_modules
check_roles
check_hosts
;;
inventory)
read subcommand subcommand_arg <<< $command_arg
case $subcommand in
diff)
git -C $inventory_dir diff
;;
push)
git -C $inventory_dir push
;;
esac
;;
host)
read host subcommand subcommand_arg <<< $command_arg
case $subcommand in
add)
mkdir $inventory_dir/hosts/$host
touch $inventory_dir/hosts/$host/.gitkeep
;;
remove)
rm -rf $inventory_dir/hosts/$host
;;
add-module)
host_add_module $host $subcommand_arg
;;
remove-module)
host_remove_module $host $subcommand_arg
;;
add-role)
host_add_role $host $subcommand_arg
;;
remove-role)
host_remove_role $host $subcommand_arg
;;
*)
for host in $command_arg; do
entry=$(host_entry $host)
echo -e "$entry" | sed 's/ /\n/g'
done
;;
esac
;;
fetch)
# get our repo updated
fetch_inventory
;;
recover)
# get our repo updated
rm $lock_file
;;
resume)
# get our repo updated
rm $pause_file
;;
pause)
# get our repo updated
touch $pause_file
;;
*)
log_error unknown command $command
;;
esac