-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvagrant-lib.sh
110 lines (93 loc) · 2.77 KB
/
vagrant-lib.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
#!/usr/bin/env bash
# Copyleft EuroLinux
# Author: Alex Baranowski (alex@euro-linux.com)
# License: MIT
preflight_check(){
# This function checks if most important vars inplace
[ -n "$PROVIDER" ] || determine_provider
[ -n "$BOX_NAME" ] || {
echo "Sorry BOX_NAME must be set"
return 1
}
}
determine_provider(){
# FIXME in the future more providers
if hash VirtualBox 2> /dev/null; then
export PROVIDER=virtualbox
elif hash vmware 2> /dev/null; then
export PROVIDER=vmware_workstation
else
PROVIDER=libvirt
fi
export PROVIDER
}
vagrant_update_box(){
vagrant box update --box "$BOX_NAME" --provider "$PROVIDER"
}
vagrant_init(){
set -euo pipefail
vagrant init "$BOX_NAME"
}
vagrant_init_from_template(){
[ -n "$VAGRANT_TEMPLATE" ] || {
echo "TEMPLATE not defined!"
return 1
}
if [ -z ${BOX_VERSION+x} ]; then
vagrant init "$BOX_NAME" --template "$VAGRANT_TEMPLATE"
else
vagrant init "$BOX_NAME" --template "$VAGRANT_TEMPLATE" --box_version "$BOX_VERSION"
fi
}
vagrant_up(){
vagrant up --provider="$PROVIDER"
}
vagrant_halt(){
# forcefully shutdown if there is no success in 120 sec
timeout 120 vagrant halt || vagrant halt -f
}
vagrant_destroy(){
set -euo pipefail
vagrant destroy -f
rm -f Vagrantfile
}
vagrant_remove_box(){
vagrant box remove -f "$BOX_NAME"
}
vagrant_remove_box_all(){
vagrant box remove -f --all "$BOX_NAME"
}
vagrant_run_command(){
vagrant ssh -c "$*"
}
vagrant_run_command_as_root(){
vagrant ssh -c "sudo $*"
}
vagrant_copy_file_from_machine(){
vagrant scp ":$*" .
}
vagrant_purge_libvirt_domain(){
# WARNING! If there is another (started later, for example) vagrant domain with the same name it will be destroyed!
dir_name=$(pwd)
domain_name="$(basename "$dir_name")_default"
sudo virsh destroy --domain "$domain_name" || echo "domain already destroyed"
sudo virsh undefine --domain "$domain_name" || echo "domain already undefined"
}
vagrant_purge_libvirt_boxes(){
# vagrant-libvirt won't remove boxes images from libvirt default pool, so we have to do this manualy
# VAGRANTSLASH is string that is used to recognize from app.vagrantup.com
if [ $PROVIDER == 'libvirt' ]; then
echo "Libvirt provider require additonal cleaning"
for i in $(sudo virsh vol-list --pool default | grep "VAGRANTSLASH" | awk '{print $1}'); do
echo "Removing $i libvirt volume"
sudo virsh vol-delete --pool default "$i"
done
fi
}
vagrant_remove_all_boxes(){
for i in $( vagrant box list | awk '{print $1}' | sort | uniq ); do
echo "Purging box $i"
vagrant box remove -f --all "$i"
done
vagrant_purge_libvirt_boxes
}