-
Notifications
You must be signed in to change notification settings - Fork 113
/
vagrant_init_destroy_boxes.sh
executable file
·160 lines (129 loc) · 5.54 KB
/
vagrant_init_destroy_boxes.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
#!/usr/bin/env bash
set -eu -o pipefail
BOXES_LIST=${*:-$(find . -maxdepth 1 \( -name "*ubuntu*.box" -o -name "*centos*.box" -o -name "*windows*.box" \) -printf "%f\n" | sort | tr "\n" " ")}
TMPDIR=${TMPDIR:-/tmp}
LOGDIR=${LOGDIR:-/var/tmp/packer-templates-logs}
STDOUT="/dev/null"
export VAGRANT_IGNORE_WINRM_PLUGIN=true
vagrant_box_add() {
vagrant box add "${VAGRANT_BOX_FILE}" --name="${VAGRANT_BOX_NAME}" --force > ${STDOUT}
}
vagrant_init_up() {
vagrant init "${VAGRANT_BOX_NAME}" > ${STDOUT}
# Disable VirtualBox GUI
if [[ "${VAGRANT_BOX_PROVIDER}" = "virtualbox" ]]; then
sed -i '/config.vm.box =/a \ \ config.vm.provider "virtualbox" do |v|\n \ \ \ v.gui = false\n\ \ end' "${VAGRANT_CWD}/Vagrantfile"
fi
vagrant up --provider "${VAGRANT_BOX_PROVIDER}" > ${STDOUT}
}
check_vagrant_vm() {
vagrant ssh-config | head -5 > "${VAGRANT_CWD}/ssh-config"
case ${VAGRANT_BOX_FILE} in
*windows*)
sleep 100
echo '*** Running: Certificate "Red Hat" or "Oracle" driver check'
TRUSTED_CERTIFICATES=$(vagrant winrm --shell powershell --command "Get-ChildItem -Path Cert:\LocalMachine\TrustedPublisher" | uniq)
if [[ ! ${TRUSTED_CERTIFICATES} =~ (Red Hat|Oracle) ]]; then
echo "${TRUSTED_CERTIFICATES}"
echo "*** There are no certificates from 'Red Hat' or 'Oracle' installed !"
vagrant_cleanup
exit 1
fi
echo '*** Running: "QEMU" or "Virtualbox" driver check'
VIRT_SERVICES=$(vagrant winrm --shell powershell --command "Get-Service | where {\$_.Name -match \".*QEMU.*|.*Spice.*|.*vdservice.*|.*VBoxService.*\"}" | uniq)
if [[ ! ${VIRT_SERVICES} =~ (QEMU|Spice|vdservice|VBoxService) ]]; then
echo "${VIRT_SERVICES}"
echo "*** There are no 'Virtualization services/addons' running !"
vagrant_cleanup
exit 2
fi
echo '*** Running: "Red Hat" and "VirtIO" driver check'
if [[ ${VAGRANT_BOX_FILE} =~ "libvirt" ]]; then
VIRT_DEVICES=$(vagrant winrm --shell powershell --command "Get-WmiObject Win32_PnPSignedDriver | where {\$_.devicename -match \".*Red Hat.*|.*VirtIO.*\"} | select devicename, driverversion" | uniq)
if [[ ! ${VIRT_DEVICES} =~ (Red Hat|VirtIO) ]]; then
echo "${VIRT_DEVICES}"
echo "*** There are no 'Virtualization services/addons' running !"
vagrant_cleanup
exit 3
fi
fi
echo '*** Running: Windows version check'
WIN_VERSION=$(vagrant winrm --shell cmd --command 'systeminfo | findstr /B /C:"OS Name" /C:"OS Version"')
if [[ ! ${VAGRANT_BOX_FILE} =~ $(echo "${WIN_VERSION}" | awk '/^OS Name/ { print tolower($4 "-" $5 "-" $6) }') ]]; then
echo "${WIN_VERSION}"
echo "*** Windows version mismatch \"$(echo "${WIN_VERSION}" | awk '{ print tolower($4 "-" $5 "-" $6) }')\" vs \"${VAGRANT_BOX_FILE}\" !"
vagrant_cleanup
exit 4
fi
echo '*** Running: Windows license check'
LICENSE_STATUS=$(vagrant winrm --shell cmd --command "cscript C:\Windows\System32\slmgr.vbs /dli" | uniq)
if [[ ! ${LICENSE_STATUS} =~ (10|90|180)\ day ]]; then
echo "${LICENSE_STATUS}"
echo "*** Licensing issue - expiration should be 10, 90 or 180 days !"
vagrant_cleanup
exit 5
fi
;;
*centos* | *ubuntu*)
echo '*** Checking if there are some packages to upgrade (there should be none)'
vagrant ssh --command '
grep PRETTY_NAME /etc/os-release;
sudo sh -c "test -x /usr/bin/apt && apt-get update 2>&1 > /dev/null && echo \"apt list -qq --upgradable\" && apt list -qq --upgradable";
sudo sh -c "test -x /usr/bin/yum && yum list -q updates";
id; sudo id
'
;;
esac
echo '*** Running: sshpass'
sshpass -pvagrant ssh -q -F "${VAGRANT_CWD}/ssh-config" -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o ControlMaster=no -o PreferredAuthentications=password -o PubkeyAuthentication=no default 'cd'
}
vagrant_cleanup() {
vagrant destroy -f > ${STDOUT}
vagrant box remove -f "${VAGRANT_BOX_NAME}" > ${STDOUT}
if [[ "${VAGRANT_BOX_NAME}" =~ "libvirt" ]]; then
virsh --quiet --connect=qemu:///system vol-delete --pool default --vol "${VAGRANT_BOX_NAME}_vagrant_box_image_0.img"
fi
rm -rf "${VAGRANT_CWD}"/{Vagrantfile,.vagrant,ssh-config}
rmdir "${VAGRANT_CWD}"
}
# trap ctrl-c and call ctrl_c()
trap ctrl_c INT
ctrl_c() {
echo "** Trapped CTRL-C"
vagrant_cleanup
}
#######
# Main
#######
main() {
if [[ -n "${BOXES_LIST}" ]]; then
if [[ ! -d "${TMPDIR}" ]]; then
echo "*** Directory \"${TMPDIR}\" doesn't exist !"
exit 1
fi
test -d "${LOGDIR}" || mkdir -p "${LOGDIR}"
for VAGRANT_BOX_FILE in ${BOXES_LIST}; do
VAGRANT_BOX_NAME=$(basename "${VAGRANT_BOX_FILE%.*}")
export VAGRANT_BOX_NAME
export VAGRANT_BOX_PROVIDER=${VAGRANT_BOX_NAME##*-}
export VAGRANT_CWD="${TMPDIR}/${VAGRANT_BOX_NAME}"
export LOG_FILE="${LOGDIR}/${VAGRANT_BOX_NAME}-init.log"
if [[ -s "${LOG_FILE}" ]]; then
echo "*** The logfile \"${LOG_FILE}\" already exists - skipping..."
continue
fi
echo -e "*** ${VAGRANT_BOX_FILE} [${VAGRANT_BOX_NAME}] (${VAGRANT_BOX_PROVIDER}) (${VAGRANT_CWD})" | tee "${LOG_FILE}"
if [[ -d "${VAGRANT_CWD}" ]]; then
echo "*** Directory \"${VAGRANT_CWD}\" already exist !"
exit 1
fi
mkdir "${VAGRANT_CWD}"
vagrant_box_add
vagrant_init_up 2>&1 | tee -a "${LOG_FILE}"
check_vagrant_vm 2>&1 | tee -a "${LOG_FILE}"
vagrant_cleanup
echo "*** Completed"
done
fi
}
main