-
Notifications
You must be signed in to change notification settings - Fork 0
/
install_vagrant.sh
executable file
·1002 lines (888 loc) · 22.4 KB
/
install_vagrant.sh
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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
VAGRANT_PLUGINS="disksize vbguest sshfs openstack-provider"
VERBOSE=0
SKIP_VIRTUALBOX=0
SKIP_PLUGINS=0
REINSTALL_PLUGINS=0
WRAPPER=0
VAGRANT_LATEST_RELEASE=""
VBOX_LATEST_RELEASE=""
ARCH=""
KERNEL_NAME=""
OS_NAME=""
OS_TYPE=""
mirrorsupdate=0
ctrl_c(){
exit 1
}
trap ctrl_c INT
_compare_versions(){
if [[ $1 == $2 ]]; then # Versions 1 and 2 are the same
return 0
fi
local IFS=.
local i
local ver1=($1)
local ver2=($2)
# fill empty fields in ver1 with zeros
for ((i=${#ver1[@]}; i<${#ver2[@]}; i++)); do
ver1[i]=0
done
for ((i=0; i<${#ver1[@]}; i++)); do
if [[ -z ${ver2[i]} ]]; then
# fill empty fields in ver2 with zeros
ver2[i]=0
fi
if ((10#${ver1[i]} > 10#${ver2[i]})); then # Version 1 is greater than 2
return 1
fi
if ((10#${ver1[i]} < 10#${ver2[i]})); then # Version 2 is greater than 1
return 2
fi
done
return 0
}
_has_minimum_version(){
local result=1
local current=$1
local required=$2
_compare_versions "$current" "$required"
local ret=$?
if [[ $ret -lt 2 ]]; then
result=0
fi
return $result
}
_get_system_details(){
local result=1
ARCH="$(uname -m)"
KERNEL_NAME="$(uname -s)"
case $KERNEL_NAME in
Linux)
OS_NAME=$(lsb_release -is)
if [[ "$OS_NAME" = "Debian" || "$OS_NAME" = "Ubuntu" ]]; then
OS_TYPE="debian"
result=0
else
echo "OS not supported!"
fi
;;
Darwin)
OS_NAME=$(awk '/SOFTWARE LICENSE AGREEMENT FOR macOS/' '/System/Library/CoreServices/Setup Assistant.app/Contents/Resources/en.lproj/OSXSoftwareLicense.rtf' | awk '{print $(NF-1)" "$NF}' | rev | cut -c2- | rev)
if [[ "$ARCH" = "x86_64" ]]; then
OS_TYPE="mac_amd64"
result=0
else
echo "Architecture <$ARCH> not supported!"
fi
;;
*)
echo "OS not supported!"
esac
return $result
}
_apt(){
local result=1
if [[ "$1" = "showlatest" ]]; then
local version=$(apt-cache policy $2 | awk '/^[ \t]+Candidate: / {print $NF}' | awk -F '-' '{print $1}')
if [[ "$version" != "" ]]; then
result=0
echo "$version"
fi
else
apt-get "$@" >/dev/null 2>&1
result=$?
fi
return $result
}
_dmg(){
local result=1
case $1 in
install)
if [[ "echo $2 | grep '\.dmg$'" != "" && -f $2 ]]; then
local mountpoint=$(hdiutil mount $2 2>/dev/null | tail -1 | grep '\/Volumes')
result=$?
local mountdevice=$(echo $mountpoint | awk '{print $1}')
mountpoint=$(echo $mountpoint | awk -F '/Volumes' '{print $NF}')
if [[ $result -eq 0 ]]; then
mountpoint="/Volumes${mountpoint}"
cp -R "$mountpoint" /Applications 2>/dev/null
result=$?
fi
if [[ $result -eq 0 ]]; then
hdiutil unmount "$mountdevice" >/dev/null 2>&1
result=$?
fi
fi
;;
esac
return $result
}
_install_brew(){
curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh >/dev/null 2>&1
}
_brew(){
local result=0
if [[ "$(command -v brew)" = "" ]]; then
_install_brew >/dev/null 2>&1
result=$?
fi
if [[ $result -eq 0 ]]; then
if [[ "$1" = "showlatest" ]]; then
local url="https://formulae.brew.sh"
local path=""
local pkgname=$(brew search $2 | grep "${2}$")
local basename=$(echo $pkgname | awk -F '/' '{print $(NF-1)}')
pkgname=$(echo $pkgname | awk -F '/' '{print $NF}')
if [[ "$pkgname" != "" ]]; then
if [[ "$basename" != "$pkgname" ]]; then
path+="/$basename"
fi
path+="/$pkgname"
local version=$(curl --silent $url/$path | grep "version:" | awk -F '">' '{print $2}' | awk -F '</a>' '{print $1}' | cut -d, -f1)
if [[ "$version" != "" ]]; then
if [[ "$basename" = "cask" ]]; then
version="cask/$version"
fi
echo "$version"
else
result=1
fi
fi
else
brew "$@"
result=$?
fi
fi
return $result
}
_mirrors_update(){
local result=1
if [[ $mirrorsupdate -eq 0 ]]; then
case $OS_TYPE in
debian)
_apt update >/dev/null 2>&1
result=$?
;;
mac_amd64)
_brew config >/dev/null 2>&1
result=$?
;;
esac
fi
return $result
}
_install_virtualbox(){
local result=0
local install=0
if [[ "$(which virtualbox)" = "" ]]; then
if [[ $VERBOSE -eq 1 ]]; then
echo -n "VirtualBox not found! Installing... "
fi
install=1
fi
VBOX_LATEST_RELEASE=""
local maccask=" "
case $OS_TYPE in
debian)
VBOX_LATEST_RELEASE=$(_apt showlatest virtualbox)
;;
mac_amd64)
VBOX_LATEST_RELEASE=$(_brew showlatest virtualbox)
if [[ "$(echo $VBOX_LATEST_RELEASE | grep '^cask/')" != "" ]]; then
maccask=" --cask "
VBOX_LATEST_RELEASE=$(echo $VBOX_LATEST_RELEASE | cut -d/ -f2)
fi
;;
*)
echo "OS not supported!"
result=1
;;
esac
if [[ "$VBOX_LATEST_RELEASE" = "" ]]; then
echo "Unable to find the latest release version for VirtualBox!"
result=1
fi
if [[ $VERBOSE -eq 1 ]]; then
echo "VirtualBox latest release: $VBOX_LATEST_RELEASE"
fi
if [[ $result -eq 0 && $install -eq 0 ]]; then
local version=$(vboxmanage --version 2>/dev/null | awk -F '_' '{print $1}')
if [[ $VERBOSE -eq 1 ]]; then
echo "VirtualBox current version: $version"
fi
_has_minimum_version $version $VBOX_LATEST_RELEASE
local ret=$?
if [[ $ret -eq 0 ]]; then
echo "VirtualBox is up to date."
else
if [[ $VERBOSE -eq 1 ]]; then
echo -n "VirtualBox is not up to date! Installed: <$version>, Available: <$VBOX_LATEST_RELEASE>. Updating... "
fi
install=1
fi
fi
if [[ $result -eq 0 && $install -eq 1 ]]; then
_mirrors_update
fi
if [[ $result -eq 0 && $install -eq 1 ]]; then
case $OS_TYPE in
debian)
if [[ $VERBOSE -eq 1 ]]; then
echo "version $VBOX_LATEST_RELEASE for $OS_NAME"
fi
_apt install --yes virtualbox
result=$?
;;
mac_amd64)
if [[ $VERBOSE -eq 1 ]]; then
echo "version $VBOX_LATEST_RELEASE for $OS_NAME"
fi
_brew install${maccask}virtualbox
result=$?
;;
esac
fi
if [[ $result -eq 0 && "$(vboxmanage --version 2>/dev/null | awk -F '_' '{print $1}')" != "$VBOX_LATEST_RELEASE" ]]; then
echo "Unable to install the required VirtualBox version!"
result=1
fi
return $result
}
_install_vagrant(){
local result=0
local install=0
local ret
if [[ "$(which vagrant)" = "" ]]; then
if [[ $VERBOSE -eq 1 ]]; then
echo -n "Vagrant not found! Installing... "
fi
install=1
fi
local baseurl="https://releases.hashicorp.com/vagrant"
local url=""
local filename=""
if [[ "$VAGRANT_LATEST_RELEASE" = "" ]]; then
VAGRANT_LATEST_RELEASE="$(curl --silent $baseurl/ | awk -F '"' '/vagrant_/ {print $2}' | head -1 | cut -d/ -f3)"
fi
if [[ "$VAGRANT_LATEST_RELEASE" = "" ]]; then
echo "Unable to find the latest release version for Vagrant!"
result=1
fi
if [[ $result -eq 0 && $install -eq 0 ]]; then
version=$(vagrant --version | awk '{print $NF}')
_has_minimum_version $version $VAGRANT_LATEST_RELEASE
ret=$?
if [[ $ret -eq 0 ]]; then
if [[ $VERBOSE -eq 1 ]]; then
echo "Vagrant is up to date."
fi
else
if [[ $VERBOSE -eq 1 ]]; then
echo -n "Vagrant is not up to date! Installed: <$version>, Available: <$VAGRANT_LATEST_RELEASE>. Updating... "
fi
install=1
fi
fi
if [[ $result -eq 0 && $install -eq 1 ]]; then
url="$baseurl/$VAGRANT_LATEST_RELEASE"
filename="vagrant_${VAGRANT_LATEST_RELEASE}_${ARCH}"
case $OS_TYPE in
debian)
if [[ $VERBOSE -eq 1 ]]; then
echo "version $VAGRANT_LATEST_RELEASE for $OS_NAME"
fi
curl --silent -O $url/$filename.deb
ret=0
if [[ -f ./$filename.deb ]]; then
_apt install --yes ./$filename.deb
ret=$?
rm ./$filename.deb
else
ret=1
fi
if [[ $ret -ne 0 ]]; then
echo "Failed to install Vagrant $VAGRANT_LATEST_RELEASE!"
result=$ret
fi
;;
mac_amd64)
if [[ $VERBOSE -eq 1 ]]; then
echo "version $VAGRANT_LATEST_RELEASE for $OS_NAME"
fi
curl --silent -O $url/$filename.dmg
ret=0
if [[ -f ./$filename.dmg ]]; then
_dmg install ./$filename.dmg
ret=$?
rm ./$filename.dmg
else
ret=1
fi
if [[ $ret -ne 0 ]]; then
echo "Failed to install Vagrant $VAGRANT_LATEST_RELEASE!"
result=$ret
fi
;;
*)
echo "OS not supported!"
result=1
;;
esac
fi
if [[ $result -eq 0 ]]; then
_replace_vagrant_wrapper
result=$?
fi
return $result
}
_replace_vagrant_wrapper(){
local result=0
local vagrantscript=$(which vagrant)
if [[ "$vagrantscript" = "" ]]; then
result=1
fi
if [[ $WRAPPER -eq 1 ]]; then
if [[ $result -eq 0 ]]; then
if [[ -f ${vagrantscript}.bak && "$(head -1 ${vagrantscript}.bak | grep 'bash$')" != "" && "$(wc -l ${vagrantscript}.bak | cut -d' ' -f1)" -lt 10 ]]; then
mv $vagrantscript.bak $vagrantscript
else
cat << EOF > $vagrantscript
#!/usr/bin/env bash
#
# This script just forwards all arguments to the real vagrant binary.
/opt/vagrant/bin/vagrant "$@"
EOF
fi
fi
fi
if [[ $result -eq 0 && "$(head -1 $vagrantscript | grep 'bash$')" != "" && "$(wc -l $vagrantscript | cut -d' ' -f1)" -lt 10 ]]; then
if [[ $VERBOSE -eq 1 ]]; then
echo -n "Replacing Vagrant wrapper..."
fi
mv $vagrantscript $vagrantscript.bak
result=$?
if [[ $result -eq 0 ]]; then
cat << EOF | tee $vagrantscript >/dev/null 2>&1
#!/bin/bash
BYPASS=0
function ctrl_c(){
exit 1
}
trap ctrl_c INT
ENVVARS="VAGRANT_OPENSTACK_VERSION_CHECK=disabled"
VAGRANT=/opt/vagrant/bin/vagrant
if [[ ! -f \$VAGRANT ]]; then
echo "File not found: \$VAGRANT" >&2
exit 1
fi
if [[ "\$ENVVARS" != "" ]]; then
for envvar in \$(echo "\$ENVVARS"); do
export \$envvar
done
fi
MAX_TRIES=3
SSH_OPTIONS="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
CURRENT_PATH=\$(pwd)
ORIG_ARGS=()
ORIG_ARGS_COUNT=\$#
while [[ \$# -gt 0 ]]; do
ORIG_ARGS+=("\$1")
shift
done
set -- "\${ORIG_ARGS[@]}"
ACTION=""
VMS_DETAILS=""
VMS=""
VMS_NUMBER=0
VM=""
VM_REGEX=""
SSH_COMMAND=""
USER=""
PROVISION_AFTER_UP=0
PARALLEL=0
vagrantfile=\$CURRENT_PATH/Vagrantfile
go_ahead=0
if [[ \$BYPASS -ne 1 && -f \$vagrantfile ]]; then
echo "Analysing..." >&2
POSITIONAL=()
SSH_POSITIONAL=()
case \$1 in
ssh)
ACTION="ssh"
for ssh_arg in \$SSH_OPTIONS; do
SSH_POSITIONAL+=("\$ssh_arg")
done
go_ahead=1
;;
up)
ACTION="up"
VMS_DETAILS=\$(\$VAGRANT status 2>/dev/null | awk '/^[a-zA-Z].*\(.*)\$/')
VMS=\$(echo "\$VMS_DETAILS" | awk '{print \$1}')
VMS_NUMBER=\$(echo "\$VMS" | wc -l)
if [[ \$VMS_NUMBER -gt 1 ]]; then
go_ahead=1
fi
;;
esac
if [[ \$go_ahead -eq 1 ]]; then
ssh_args=0
previous_arg=""
arg=""
while [[ \$# -gt 0 ]]; do
arg="\$1"
save_arg=1
case \$arg in
up)
PROVISION_AFTER_UP=1
save_arg=0
;;
ssh)
save_arg=0
;;
-c)
SSH_COMMAND="\$2"
shift
save_arg=0
;;
-p)
save_arg=0
;;
--provision)
save_arg=0
;;
--no-provision)
save_arg=0
PROVISION_AFTER_UP=0
;;
--parallel)
save_arg=0
PARALLEL=1
;;
--destroy-on-error|--no-destroy-on-error|--no-parallel|--install-provider|--no-install-provider)
:
;;
--color|--no-color|--machine-readable|-v|--version|--debug|--timestamp|--debug-timestamp|--no-tty|-h|--help)
:
;;
--) # Last normal argument was the previous one. Starting from here, there are SSH arguments
if [[ "\$ACTION" = "ssh" ]]; then
ssh_args=1
if [[ "\$(echo \$previous_arg | grep '@')" != "" ]]; then
USER=\$(echo \$previous_arg | awk -F '@' '{print \$1}')
VM=\$(echo \$previous_arg | awk -F '@' '{print \$2}')
else
go_ahead=0 # Bypass to vagrant
break
fi
fi
save_arg=0
;;
*)
if [[ \$ssh_args -eq 0 ]]; then
case \$ACTION in
up)
vm_regex=0
if [[ "\$(echo \$arg | awk -F '/' '{print NF}')" = "3" ]]; then
vm_regex=1
VM_REGEX=\$(echo \$arg | awk -F '/' '{print \$2}')
fi
for vm in \$VMS; do
if [[ \$vm_regex -eq 1 ]]; then
if [[ "\$(echo \$vm | grep -E "\$VM_REGEX")" != "" ]]; then
if [[ "\$VM" != "" ]]; then
VM+=" "
fi
VM+="\$vm"
save_arg=0
fi
elif [[ "\$arg" = "\$vm" ]]; then
VM=\$arg
save_arg=0
fi
done
;;
esac
if [[ \$# -eq 1 ]]; then # Last normal argument
case \$ACTION in
ssh)
if [[ "\$(echo \$arg | grep '@')" != "" ]]; then
USER=\$(echo \$arg | awk -F '@' '{print \$1}')
tmpVM=\$(echo \$arg | awk -F '@' '{print \$2}')
if [[ -f ./vagrant-ssh.config && "\$(grep "^Host \$tmpVM" vagrant-ssh.config)" != "" ]]; then
VMS=\$tmpVM
VM=\$tmpVM
VMS_NUMBER=1
else
VMS_DETAILS=\$(\$VAGRANT status 2>/dev/null | awk '/^[a-zA-Z].*\(.*)\$/')
VMS=\$(echo "\$VMS_DETAILS" | awk '{print \$1}')
VMS_NUMBER=\$(echo "\$VMS" | wc -l)
for vm in \$VMS; do
if [[ "\$tmpVM" = "\$vm" ]]; then
echo "Checking <\$vm> VM status..." >&2
VM_STATUS=\$(echo "\$VMS_DETAILS" | grep -E "^\$vm[ \t]+" | awk '{print \$2}')
if [[ "\$VM_STATUS" = "running" || "\$VM_STATUS" = "active" ]]; then
VM=\$vm
break
fi
fi
done
fi
if [[ "\$VM" != "" ]]; then
save_arg=0
else
go_ahead=0 # Bypass to vagrant for error message (we have a user and vm specified, but vm is not defined or not running)
break
fi
else
go_ahead=0 # Bypass to vagrant
break
fi
;;
esac
fi
fi
;;
esac
if [[ \$save_arg -eq 1 ]]; then
if [[ \$ssh_args -eq 0 ]]; then
if [[ "\$arg" != "" ]]; then
POSITIONAL+=("\$arg")
fi
else
if [[ "\$arg" != "" ]]; then
SSH_POSITIONAL+=("\$arg")
fi
fi
fi
previous_arg="\$arg"
shift
done
if [[ \$go_ahead -eq 1 ]]; then
case \$ACTION in
ssh)
set -- "\${SSH_POSITIONAL[@]}"
;;
up)
if [[ "\$VM" != "" ]]; then
VMS="\$VM"
fi
set -- "\${POSITIONAL[@]}"
;;
esac
fi
fi
fi
if [[ \$go_ahead -eq 1 ]]; then
case \$ACTION in
ssh)
if [[ "\$VM" != "" && "\$USER" != "" ]]; then
echo "Searching for <\$VM> VM ssh config..." >&2
if [[ -f ./vagrant-ssh.config && "\$(grep "^Host \$VM" vagrant-ssh.config)" != "" ]]; then
vm_ssh_config=\$(grep -A3 "^Host \$VM" vagrant-ssh.config)
else
vm_ssh_config=\$(\$VAGRANT ssh-config \$VM)
fi
vm_ssh_host=\$(echo "\$vm_ssh_config" | awk '/HostName / {print \$2}')
vm_ssh_port=\$(echo "\$vm_ssh_config" | awk '/Port / {print \$2}')
SSH_ARGS=("\$@")
vm_ssh_user=\$(echo "\$vm_ssh_config" | awk '/User / {print \$2}')
if [[ "\$vm_ssh_user" = "\$USER" ]]; then
SSH_ARGS=()
while IFS=\$'\n' read -r item; do
ssh_var_key=\$(echo "\$item" | awk '{print \$1}')
ssh_var_val=\$(echo "\$item" | awk '{print \$2}')
if [[ "\$ssh_var_key" != "HostName" && "\$ssh_var_key" != "User" && "\$ssh_var_key" != "Port" ]]; then
SSH_ARGS+=("-o")
SSH_ARGS+=("\$ssh_var_key=\$ssh_var_val")
fi
done <<< "\$(echo "\$vm_ssh_config" | awk '/^ /')"
fi
ssh -p \$vm_ssh_port \${SSH_ARGS[@]} \$USER@\$vm_ssh_host "\$SSH_COMMAND" 2>/dev/null
ret=\$?
if [[ \$ret -ne 0 ]]; then
echo "Unable to establish a ssh connection with host <\$vm_ssh_host> on port <\$vm_ssh_port> with user <\$USER>!" >&2
exit \$ret
fi
else
BYPASS=1
fi
;;
up)
if [[ \$PARALLEL -eq 0 ]]; then
for vm in \$VMS; do
i=1
while [[ \$i -le \$MAX_TRIES ]]; do
if [[ \$i -gt 1 ]]; then
\$VAGRANT halt \$vm
fi
\$VAGRANT up --no-provision \$@ \$vm
if [[ \$? -ne 0 ]]; then
i=\$((i + 1))
err_msg="VAGRANT FAILED TO LAUNCH THEN CONNECT WITH SSH TO VM <\$vm>."
if [[ \$i -le \$MAX_TRIES ]]; then
echo "\$err_msg Retry: \$i/\$MAX_TRIES" >&2
else
echo "\$err_msg No more retries for this VM!" >&2
read "Continue with other VMs by pressing any key, or cancel with CTRL-C"
fi
else
if [[ \$PROVISION_AFTER_UP -eq 1 ]]; then
VM_STATUS=\$(echo "\$VMS_DETAILS" | grep -E "^\$vm[ \t]+" | awk '{print \$2}')
if [[ "\$VM_STATUS" != "running" && "\$VM_STATUS" != "active" ]]; then
\$VAGRANT provision \$@ \$vm
fi
fi
break
fi
done
done
else
if [[ "\$VM_REGEX" != "" ]]; then
VM_REGEX=" /\$VM_REGEX/"
fi
\$VAGRANT up --parallel --no-provision\${VM_REGEX} \$@
#\$VAGRANT provision \$@ # This will UNFORTUNATELY (AND COSTLY) be done in sequence!!
# THIS WAS AN INTERESTING ATTEMPT TO PROVIDE PARALLEL FEATURE FOR PROVISION (it's not supported by Vagrant) BUT IT FAILS WITH STRANGE ERRORS (apt fails in some vms with never seen before errors, and this is random)!
# Actually, those failures happened in the pre-provision trigger, but I still don't know if it will happen in provision scripts as well!
# Meanwhile, I've implemented retries in my apt parts of my pre-provision trigger and... it did the trick!!
# Hopefully I can let it like that! It's SO MUCH faster than sequential provisioning...
if [[ \$PROVISION_AFTER_UP -eq 1 ]]; then
for vm in \$VMS; do
\$VAGRANT provision \$vm \$@ &
done
wait
fi
fi
;;
esac
else
BYPASS=1
fi
if [[ \$BYPASS -eq 1 ]]; then
set -- "\${ORIG_ARGS[@]}"
\$VAGRANT \$@
fi
EOF
result=$?
fi
if [[ $result -eq 0 ]]; then
chmod +x $vagrantscript
result=$?
fi
if [[ $result -eq 0 ]]; then
echo "done"
fi
fi
return $result
}
_install_vagrant_plugins_for_user(){
local result=0
local user=$1
local usercheck
local plugin
local plugincheck
local install=0
local upgrade=0
usercheck=`grep "^\$user:" /etc/passwd`
if [[ "$usercheck" != "" && "$user" != "ubuntu" && "$user" != "vagrant" ]]; then
if [[ $REINSTALL_PLUGINS -eq 1 ]]; then
echo
echo "Removing vagrant plugins for user <$user>..."
su - $user -c "vagrant plugin expunge --force"
fi
echo
echo "Checking/installing vagrant plugins for user <$user>..."
for plugin in $VAGRANT_PLUGINS; do
result=1
pluginfile=$(ls "$(dirname "$(realpath "$0")")"/vagrant-$plugin-*.gem 2>/dev/null)
plugincheck=$(su - $user -c "vagrant plugin list" | grep "^vagrant-$plugin ")
pluginavailvers=""
plugincurrentvers=""
if [[ "$pluginfile" != "" ]]; then
pluginavailvers=$(echo $pluginfile | awk -F '-' '{print $NF}' | rev | cut -c5- | rev)
else
pluginavailvers=$(gem list --remote vagrant-$plugin | grep "^vagrant-$plugin " | awk '{print $2}' | cut -c2- | rev | cut -c2- | rev)
fi
if [[ "$plugincheck" != "" ]]; then
plugincurrentvers=$(echo $plugincheck | awk '{print $2}' | cut -d, -f1 | cut -c2-)
fi
install=0
upgrade=0
if [[ "$pluginavailvers" != "" ]]; then
if [[ "$plugincurrentvers" != "" ]]; then
_has_minimum_version $plugincurrentvers $pluginavailvers
ret=$?
if [[ $ret -ne 0 ]]; then
echo "vagrant-$plugin: current <$plugincurrentvers>, available <$pluginavailvers>. Upgrade required..."
install=1
upgrade=1
fi
else
echo "vagrant-$plugin: New installation required."
install=1
fi
else
echo "Error: Unable to find the plugin vagrant-$plugin"
fi
if [[ $install -eq 1 ]]; then
if [[ "$pluginfile" != "" ]]; then
pluginfile=$(realpath $pluginfile)
su - $user -c "vagrant plugin install $pluginfile"
result=$?
if [[ $result -ne 0 ]]; then
echo "Error while trying to update $pluginfile!"
break
fi
else
if [[ $upgrade -eq 1 ]]; then
su - $user -c "vagrant plugin update vagrant-$plugin"
result=$?
if [[ $result -ne 0 ]]; then
echo "Error while trying to update vagrant-$plugin!"
break
fi
else
su - $user -c "vagrant plugin install vagrant-$plugin"
result=$?
if [[ $result -ne 0 ]]; then
echo "Error while trying to install vagrant-$plugin!"
break
fi
fi
fi
else
result=0
if [[ $VERBOSE -eq 1 ]]; then
echo "vagrant-$plugin has already the newest version: $plugincurrentvers"
fi
fi
done
if [[ $result -eq 0 ]]; then
echo "done with user <$user>"
fi
fi
return $result
}
_install_vagrant_plugins(){
local result=0
if [[ $SKIP_PLUGINS -ne 1 ]]; then
local user
local ret
for user in $(ls /home); do
result=0
ret=1
_install_vagrant_plugins_for_user $user
ret=$?
if [[ $ret -ne 0 ]]; then
result=$ret
break
fi
done
if [[ $result -eq 0 ]]; then
result=0
ret=1
_install_vagrant_plugins_for_user root
ret=$?
if [[ $ret -ne 0 ]]; then
result=$ret
break
fi
fi
fi
return $result
}
POSITIONAL=()
while [[ $# -gt 0 ]]; do
case $1 in
-v|--verbose)
VERBOSE=1
shift
;;
-n|--no-virtualbox)
SKIP_VIRTUALBOX=1
shift
;;
-s|--skip-plugins)
SKIP_PLUGINS=1
shift
;;
-r|--reinstall-plugins)
REINSTALL_PLUGINS=1
shift
;;
-w|--wrapper)
WRAPPER=1
shift
;;
-h|--help)
cat << EOF
Usage: $0 [OPTION]
Automatically install/update VirtualBox, Vagrant and some Vagrant plugins with latest available versions on Debian/Ubuntu or MacOS systems.
OPTION
======
-n|--no-virtualbox Do not install VirtualBox.
-s|--skip-plugins Do not check/install/update Vagrant plugins (without skipping, this operation takes a while).
-s|--reinstall-plugins Do a complete reinstallation of plugins.
-w|--wrapper Force the reinstallation of the vagrant wrapper.
-v|--verbose Be verbose.
-h|--help Print this help page.
EOF
shift
exit 0
;;
*)
POSITIONAL+=("$1")
shift
;;
esac
done
set -- "${POSITIONAL[@]}"
ret=0
if [[ $(id -u) -ne 0 ]]; then
echo "Call me with sudo!"
exit 1
fi
_get_system_details
ret=$?
if [[ $ret -ne 0 ]]; then
exit $ret
fi
echo "Running $OS_NAME ($KERNEL_NAME) on $ARCH"
echo
if [[ $VERBOSE -eq 0 ]]; then
echo "Installing/Updating everything required. This may take a while..."
fi
if [[ $SKIP_VIRTUALBOX -ne 1 ]]; then
_install_virtualbox
ret=$?
if [[ $ret -ne 0 ]]; then
exit $ret
fi
if [[ $VERBOSE -eq 1 ]]; then
echo
fi
fi
_install_vagrant
ret=$?
if [[ $ret -ne 0 ]]; then
exit $ret
fi
if [[ $VERBOSE -eq 1 ]]; then
echo
fi
_install_vagrant_plugins
ret=$?
if [[ $ret -ne 0 ]]; then
exit $ret
fi
echo
echo "Running $OS_NAME ($KERNEL_NAME) on $ARCH"
echo
if [[ $SKIP_VIRTUALBOX -ne 1 ]]; then
echo "VirtualBox $(vboxmanage --version)"
fi
vagrant --version
echo