-
Notifications
You must be signed in to change notification settings - Fork 64
/
vagrant.bashrc
550 lines (494 loc) · 15.2 KB
/
vagrant.bashrc
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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
### VAGRANT ###################################################################
# avoid needing to always add --provider=libvirt
export VAGRANT_DEFAULT_PROVIDER=libvirt
#alias vagrant='/opt/vagrant/bin/vagrant'
# if oh-my-vagrant has the mainstream entrypoint installed, then use it instead
OMV=`which omv 2> /dev/null`
if [ "$OMV" != '' ] && [ "$VAGRANT" = '' ]; then
VAGRANT='omv'
else
VAGRANT='vagrant'
fi
alias vs="$VAGRANT status"
alias vp="$VAGRANT provision"
alias vup="$VAGRANT up"
alias vrsync="$VAGRANT rsync"
alias vdestroy="$VAGRANT destroy"
alias vrm-rf="$VAGRANT --omv-reallyrmonce=true status"
function vlog {
VAGRANT_LOG=info $VAGRANT "$@" 2> vagrant.log
}
# vagrant ssh (better than vagrant ssh)
function vssh {
[ "$1" = '' ] || [ "$2" != '' -a "$2" != '-c' ] || [ "$2" = '-c' -a "$3" = '' ] && echo "Usage: vssh <vm-name> [-c COMMAND] - vagrant screen" 1>&2 && return 1
vfile='Vagrantfile'
if [[ "$VAGRANT" == omv* ]]; then
vfile='omv.yaml'
fi
wd=`pwd` # save wd, then find the Vagrant project
while [ "`pwd`" != '/' ] && [ ! -e "`pwd`/$vfile" ] && [ ! -d "`pwd`/.vagrant/" ]; do
#echo "pwd is `pwd`"
cd ..
done
pwd=`pwd`
cd $wd
if [ ! -e "$pwd/$vfile" ] || [ ! -d "$pwd/.vagrant/" ]; then
echo 'Vagrant project not found!' 1>&2 && return 2
fi
# if we find the omv.yaml file, it takes precendence for mtime lookups
if [ -e "$pwd/omv.yaml" ]; then
pfile="$pwd/omv.yaml"
elif [ -e "$pwd/Vagrantfile" ]; then
pfile="$pwd/Vagrantfile"
else
echo 'No vagrant definition found!' 1>&2 && return 2
fi
host="$1" # save variables
cmd=''
if [ "$2" = '-c' -a "$3" != '' ]; then
shift 2
cmd="$@" # join $3...last
fi
d="$pwd/.ssh"
f="$d/$host.config"
h="$host"
# hostname extraction from user@host pattern
p=`expr index "$host" '@'`
if [ $p -gt 0 ]; then
let "l = ${#h} - $p"
h=${h:$p:$l}
fi
cdfile="$pwd/.vagrant/$h.cd"
# if cd file is missing, or is older than the definition file, re-generate...
# or if mtime of $f is > than 5 minutes (5 * 60 seconds), re-generate...
if [ ! -e "$cdfile" ] || \
[ $(stat -c '%Y' "$pfile" 2> /dev/null) -gt $(stat -c '%Y' "$cdfile" 2> /dev/null) ] || \
[ `date -d "now - $(stat -c '%Y' "$f" 2> /dev/null) seconds" +%s` -gt 300 ]; then
mkdir -p "$d"
# we cache the lookup because this command is slow...
$VAGRANT ssh-config "$h" > "$f" || rm "$f"
fi
cddir="`cat $cdfile 2>/dev/null`"
if [ -e "$cdfile" ] && [ -n "$cddir" ]; then
# switch into the cd dir, and then run that users shell
_cmd="cd '$cddir' && exec $(getent passwd `whoami` | awk -F ':' '{print $7}')"
if [ "$cmd" != '' ]; then
_cmd="$_cmd -c '$cmd'" # add on the -c to bash
fi
cmd="$_cmd"
unset _cmd
fi
[ -e "$f" ] && ssh -t -F "$f" "$host" "$cmd"
e=$?
if [ $e -eq 255 ]; then
# you probably want a shorter timeout if you see this often
echo 'Maybe cached connection was stale? Cleaning...'
rm -f "$f" # clean stale ssh connection
# TODO: recurse up to one time?
else
return $e
fi
}
# vagrant sftp
function vsftp {
[ "$1" = '' ] || [ "$2" != '' ] && echo "Usage: vsftp <vm-name> - vagrant sftp" 1>&2 && return 1
vfile='Vagrantfile'
if [[ "$VAGRANT" == omv* ]]; then
vfile='omv.yaml'
fi
wd=`pwd` # save wd, then find the Vagrant project
while [ "`pwd`" != '/' ] && [ ! -e "`pwd`/$vfile" ] && [ ! -d "`pwd`/.vagrant/" ]; do
#echo "pwd is `pwd`"
cd ..
done
pwd=`pwd`
cd $wd
if [ ! -e "$pwd/$vfile" ] || [ ! -d "$pwd/.vagrant/" ]; then
echo 'Vagrant project not found!' 1>&2 && return 2
fi
# if we find the omv.yaml file, it takes precendence for mtime lookups
if [ -e "$pwd/omv.yaml" ]; then
pfile="$pwd/omv.yaml"
elif [ -e "$pwd/Vagrantfile" ]; then
pfile="$pwd/Vagrantfile"
else
echo 'No vagrant definition found!' 1>&2 && return 2
fi
d="$pwd/.ssh"
f="$d/$1.config"
h="$1"
# hostname extraction from user@host pattern
p=`expr index "$1" '@'`
if [ $p -gt 0 ]; then
let "l = ${#h} - $p"
h=${h:$p:$l}
fi
cdfile="$pwd/.vagrant/$h.cd"
# if cd file is missing, or is older than the definition file, re-generate...
# or if mtime of $f is > than 5 minutes (5 * 60 seconds), re-generate...
if [ ! -e "$cdfile" ] || \
[ $(stat -c '%Y' "$pfile" 2> /dev/null) -gt $(stat -c '%Y' "$cdfile" 2> /dev/null) ] || \
[ `date -d "now - $(stat -c '%Y' "$f" 2> /dev/null) seconds" +%s` -gt 300 ]; then
mkdir -p "$d"
# we cache the lookup because this command is slow...
$VAGRANT ssh-config "$h" > "$f" || rm "$f"
fi
cdstr='' # path to append to end of sftp
cddir="`cat $cdfile 2>/dev/null`"
if [ -e "$cdfile" ] && [ -n "$cddir" ]; then
cdstr=":$cddir"
fi
[ -e "$f" ] && sftp -F "$f" "$1""$cdstr"
}
# vagrant screen
function vscreen {
[ "$1" = '' ] || [ "$2" != '' ] && echo "Usage: vscreen <vm-name> - vagrant screen" 1>&2 && return 1
vfile='Vagrantfile'
if [[ "$VAGRANT" == omv* ]]; then
vfile='omv.yaml'
fi
wd=`pwd` # save wd, then find the Vagrant project
while [ "`pwd`" != '/' ] && [ ! -e "`pwd`/$vfile" ] && [ ! -d "`pwd`/.vagrant/" ]; do
#echo "pwd is `pwd`"
cd ..
done
pwd=`pwd`
cd $wd
if [ ! -e "$pwd/$vfile" ] || [ ! -d "$pwd/.vagrant/" ]; then
echo 'Vagrant project not found!' 1>&2 && return 2
fi
# if we find the omv.yaml file, it takes precendence for mtime lookups
if [ -e "$pwd/omv.yaml" ]; then
pfile="$pwd/omv.yaml"
elif [ -e "$pwd/Vagrantfile" ]; then
pfile="$pwd/Vagrantfile"
else
echo 'No vagrant definition found!' 1>&2 && return 2
fi
cmd='screen -xRR'
d="$pwd/.ssh"
f="$d/$1.config"
h="$1"
# hostname extraction from user@host pattern
p=`expr index "$1" '@'`
if [ $p -gt 0 ]; then
let "l = ${#h} - $p"
h=${h:$p:$l}
fi
cdfile="$pwd/.vagrant/$h.cd"
# if cd file is missing, or is older than the definition file, re-generate...
# or if mtime of $f is > than 5 minutes (5 * 60 seconds), re-generate...
if [ ! -e "$cdfile" ] || \
[ $(stat -c '%Y' "$pfile" 2> /dev/null) -gt $(stat -c '%Y' "$cdfile" 2> /dev/null) ] || \
[ `date -d "now - $(stat -c '%Y' "$f" 2> /dev/null) seconds" +%s` -gt 300 ]; then
mkdir -p "$d"
# we cache the lookup because this command is slow...
$VAGRANT ssh-config "$h" > "$f" || rm "$f"
fi
cddir="`cat $cdfile 2>/dev/null`"
if [ -e "$cdfile" ] && [ -n "$cddir" ]; then
cmd="cd '$cddir' && $cmd"
fi
[ -e "$f" ] && ssh -t -F "$f" "$1" $cmd
if [ $? -eq 255 ]; then
# you probably want a shorter timeout if you see this often
echo 'Maybe cached connection was stale? Cleaning...'
rm -f "$f" # clean stale ssh connection
# TODO: recurse up to one time?
fi
}
# vagrant cssh
function vcssh {
[ "$1" = '' ] && echo "Usage: vcssh [options] [user@]<vm1>[ [user@]vm2[ [user@]vmN...]] - vagrant cssh" 1>&2 && return 1
vfile='Vagrantfile'
if [[ "$VAGRANT" == omv* ]]; then
vfile='omv.yaml'
fi
wd=`pwd` # save wd, then find the Vagrant project
while [ "`pwd`" != '/' ] && [ ! -e "`pwd`/$vfile" ] && [ ! -d "`pwd`/.vagrant/" ]; do
#echo "pwd is `pwd`"
cd ..
done
pwd=`pwd`
cd $wd
if [ ! -e "$pwd/$vfile" ] || [ ! -d "$pwd/.vagrant/" ]; then
echo 'Vagrant project not found!' 1>&2 && return 2
fi
d="$pwd/.ssh"
cssh="$d/cssh"
cmd=''
cat='cat '
screen=''
options=''
multi='f'
special=''
for i in "$@"; do # loop through the list of hosts and arguments!
#echo $i
if [ "$special" = 'debug' ]; then # optional arg value...
special=''
if [ "$1" -ge 0 -o "$1" -le 4 ]; then
cmd="$cmd $i"
continue
fi
fi
if [ "$multi" = 'y' ]; then # get the value of the argument
multi='n'
cmd="$cmd '$i'"
continue
fi
if [ "${i:0:1}" = '-' ]; then # does argument start with: - ?
# build a --screen option
if [ "$i" = '--screen' ]; then
screen=' -o RequestTTY=yes'
cmd="$cmd --action 'screen -xRR'"
continue
fi
if [ "$i" = '--debug' ]; then
special='debug'
cmd="$cmd $i"
continue
fi
if [ "$i" = '--options' ]; then
options=" $i"
continue
fi
# NOTE: commented-out options are probably not useful...
# match for key => value argument pairs
if [ "$i" = '--action' -o "$i" = '-a' ] || \
[ "$i" = '--autoclose' -o "$i" = '-A' ] || \
#[ "$i" = '--cluster-file' -o "$i" = '-c' ] || \
#[ "$i" = '--config-file' -o "$i" = '-C' ] || \
#[ "$i" = '--evaluate' -o "$i" = '-e' ] || \
[ "$i" = '--font' -o "$i" = '-f' ] || \
#[ "$i" = '--master' -o "$i" = '-M' ] || \
#[ "$i" = '--port' -o "$i" = '-p' ] || \
#[ "$i" = '--tag-file' -o "$i" = '-c' ] || \
[ "$i" = '--term-args' -o "$i" = '-t' ] || \
[ "$i" = '--title' -o "$i" = '-T' ] || \
[ "$i" = '--username' -o "$i" = '-l' ] ; then
multi='y' # loop around to get second part
cmd="$cmd $i"
continue
else # match single argument flags...
cmd="$cmd $i"
continue
fi
fi
f="$d/$i.config"
h="$i"
# hostname extraction from user@host pattern
p=`expr index "$i" '@'`
if [ $p -gt 0 ]; then
let "l = ${#h} - $p"
h=${h:$p:$l}
fi
# if mtime of $f is > than 5 minutes (5 * 60 seconds), re-generate...
if [ `date -d "now - $(stat -c '%Y' "$f" 2> /dev/null) seconds" +%s` -gt 300 ]; then
mkdir -p "$d"
# we cache the lookup because this command is slow...
$VAGRANT ssh-config "$h" > "$f" || rm "$f"
fi
if [ -e "$f" ]; then
cmd="$cmd $i"
cat="$cat $f" # append config file to list
fi
done
cat="$cat > $cssh"
#echo $cat
eval "$cat" # generate combined config file
#echo $cmd && return 1
#[ -e "$cssh" ] && cssh --options "-F ${cssh}$options" $cmd
# running: bash -c glues together --action 'foo --bar' type commands...
[ -e "$cssh" ] && bash -c "cssh --options '-F ${cssh}${screen}$options' $cmd"
}
# vagrant forward (ssh -L)
function vfwd {
[ "$1" = '' ] || [ "$2" = '' ] && echo "Usage: vfwd <vm-name> hostport:guestport [hostport:guestport] - vagrant ssh forward" 1>&2 && return 1
vfile='Vagrantfile'
if [[ "$VAGRANT" == omv* ]]; then
vfile='omv.yaml'
fi
wd=`pwd` # save wd, then find the Vagrant project
while [ "`pwd`" != '/' ] && [ ! -e "`pwd`/$vfile" ] && [ ! -d "`pwd`/.vagrant/" ]; do
#echo "pwd is `pwd`"
cd ..
done
pwd=`pwd`
cd $wd
if [ ! -e "$pwd/$vfile" ] || [ ! -d "$pwd/.vagrant/" ]; then
echo 'Vagrant project not found!' 1>&2 && return 2
fi
d="$pwd/.ssh"
f="$d/$1.config"
h="$1"
# hostname extraction from user@host pattern
p=`expr index "$1" '@'`
if [ $p -gt 0 ]; then
let "l = ${#h} - $p"
h=${h:$p:$l}
fi
# if mtime of $f is > than 5 minutes (5 * 60 seconds), re-generate...
if [ `date -d "now - $(stat -c '%Y' "$f" 2> /dev/null) seconds" +%s` -gt 300 ]; then
mkdir -p "$d"
# we cache the lookup because this command is slow...
$VAGRANT ssh-config "$h" > "$f" || rm "$f"
fi
name="$1"
shift # pop off the vmname
fwd=() # array
cmd='ssh'
for x in "${@}"
do
#echo "pair: $x"
port=`echo "$x" | awk -F ':' '{print $1}'`
if [ "$port" -le 1024 ]; then
cmd='sudo ssh' # sudo needed for < 1024
fi
b=`echo "$x" | awk -F ':' '{print "-L "$1":localhost:"$2}'`
fwd+=("$b") # append
done
echo ${fwd[@]} # show the -L commands
[ -e "$f" ] && $cmd -N -F "$f" root@"$name" "${fwd[@]}"
}
# vagrant ansible
function vansible {
[ "$1" = '' ] && echo "Usage: vansible [ansible args] - vagrant ansible" 1>&2 && return 1
vfile='Vagrantfile'
if [[ "$VAGRANT" == omv* ]]; then
vfile='omv.yaml'
fi
wd=`pwd` # save wd, then find the Vagrant project
while [ "`pwd`" != '/' ] && [ ! -e "`pwd`/$vfile" ] && [ ! -d "`pwd`/.vagrant/" ]; do
#echo "pwd is `pwd`"
cd ..
done
pwd=`pwd`
cd $wd
if [ ! -e "$pwd/$vfile" ] || [ ! -d "$pwd/.vagrant/" ]; then
echo 'Vagrant project not found!' 1>&2 && return 2
fi
k="`echo ~/.vagrant.d/insecure_private_key`"
if [ ! -e "$k" ]; then
echo 'Vagrant private key not found!' 1>&2 && return 3
fi
i="$pwd/.vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory"
if [ ! -e "$i" ]; then
echo 'Vagrant generated inventory not found!' 1>&2 && return 4
fi
ansible --private-key="$k" --inventory="$i" "$@"
}
# vagrant test (run tests from the `tests` omv.yaml variable)
function vtest {
[ "$1" != '' ] && echo "Usage: vtest - vagrant test" 1>&2 && return 1
vfile='Vagrantfile'
if [[ "$VAGRANT" == omv* ]]; then
vfile='omv.yaml'
fi
wd=`pwd` # save wd, then find the Vagrant project
while [ "`pwd`" != '/' ] && [ ! -e "`pwd`/$vfile" ] && [ ! -d "`pwd`/.vagrant/" ]; do
#echo "pwd is `pwd`"
cd ..
done
pwd=`pwd`
cd $wd
if [ ! -e "$pwd/$vfile" ] || [ ! -d "$pwd/.vagrant/" ]; then
echo 'Vagrant project not found!' 1>&2 && return 2
fi
# if we find the omv.yaml file, it takes precendence for mtime lookups
if [ -e "$pwd/omv.yaml" ]; then
pfile="$pwd/omv.yaml"
elif [ -e "$pwd/Vagrantfile" ]; then
pfile="$pwd/Vagrantfile"
else
echo 'No vagrant definition found!' 1>&2 && return 2
fi
$VAGRANT status &>/dev/null # cause the tests file to be generated
if [ ! -e "$pwd/.vagrant/tests.sh" ]; then
echo 'No vagrant tests.sh file found!' 1>&2 && return 3
fi
"$pwd/.vagrant/tests.sh" # run the tests
r=$?
#$VAGRANT destroy # clean up!
return $r
}
# vagrant test+ (run fancy tests from the `tests` omv.yaml variable)
function vtest+ {
[ "$2" != '' ] && echo "Usage: vtest+ [omv.yaml] - vagrant test plus" 1>&2 && return 1
owd=`pwd` # original wd
if [ "$1" != '' ]; then
[[ ! "$1" == *'.yaml' ]] && echo "File: '$1' must be a .yaml file!" 1>&2 && return 1
[ ! -e "$1" ] && echo "File: '$1' does not exist!" 1>&2 && return 1
d=`mktemp --tmpdir -d vtest.XXX`
cp "$1" "$d"/omv.yaml
cd "$d"
mkdir '.vagrant'
fi
vfile='Vagrantfile'
if [[ "$VAGRANT" == omv* ]]; then
vfile='omv.yaml'
fi
wd=`pwd` # save wd, then find the Vagrant project
while [ "`pwd`" != '/' ] && [ ! -e "`pwd`/$vfile" ] && [ ! -d "`pwd`/.vagrant/" ]; do
#echo "pwd is `pwd`"
cd ..
done
pwd=`pwd`
cd $wd
if [ ! -e "$pwd/$vfile" ] || [ ! -d "$pwd/.vagrant/" ]; then
cd $owd
echo 'Vagrant project not found!' 1>&2 && return 2
fi
# if we find the omv.yaml file, it takes precendence for mtime lookups
if [ -e "$pwd/omv.yaml" ]; then
pfile="$pwd/omv.yaml"
elif [ -e "$pwd/Vagrantfile" ]; then
pfile="$pwd/Vagrantfile"
else
cd $owd
echo 'No vagrant definition found!' 1>&2 && return 2
fi
$VAGRANT status &>/dev/null # cause the tests file to be generated
if [ ! -e "$pwd/.vagrant/tests.sh" ]; then
cd $owd
echo 'No vagrant tests.sh file found!' 1>&2 && return 3
fi
LINE=$(printf '=%.0s' `seq -s ' ' $(tput cols)`) # a terminal width string
count=0
failures=""
# loop through tests
while read -r -u "$fd" i; do # http://mywiki.wooledge.org/BashFAQ/089
count=`expr $count + 1`
echo "$i" | grep -q '^#' && continue # ignore comments
#export _TMPDIR='/tmp/oh-my-vagrant/' # we can add to env like this
out="$($i 2>&1)" # run and capture stdout & stderr
e=$? # save exit code
if [ $e -ne 0 ]; then
# store failures...
failures=$(
# prepend previous failures if any
[ -n "${failures}" ] && echo "$failures" && echo "$LINE"
echo "Lineno: $count"
echo "Script: $i"
# if we see 124, it might be the exit value of timeout!
[ $e -eq 124 ] && echo "Exited: $e (timeout?)" || echo "Exited: $e"
if [ "$out" = "" ]; then
echo "Output: (empty!)"
else
echo "Output:"
echo "$out"
fi
)
else
echo -e "ok\t$i\t(line: $count)" # pass
fi
done {fd}< "$pwd/.vagrant/tests.sh" # send in the test file line by line
cd $owd
# display errors
if [[ -n "${failures}" ]]; then
echo 'FAIL'
echo 'The following tests failed:'
echo "${failures}"
return 1
fi
return 0
}