Skip to content

Commit

Permalink
Improve cleanup and check scripts (#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
ruzickap authored Aug 18, 2020
1 parent 0d812ff commit 110673f
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 21 deletions.
21 changes: 13 additions & 8 deletions tools/cleanup_all_vms.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

PACKER_CACHE_DIR=${PACKER_CACHE_DIR:-/var/tmp/packer_cache}
PACKER_IMAGES_OUTPUT_DIR=${PACKER_IMAGES_OUTPUT_DIR:-/var/tmp/packer-templates-images}
LOGDIR=${LOGDIR:-${PACKER_IMAGES_OUTPUT_DIR}}
TMPDIR=${TMPDIR:-/tmp}
LOGDIR=${LOGDIR:-${TMPDIR}}

set -euo pipefail

Expand All @@ -24,9 +24,6 @@ if [[ -d "${TMPDIR}" ]]; then
done < <(find "${TMPDIR}" -maxdepth 2 ! -readable -prune -o -type f -name Vagrantfile -printf "%h\0")
fi

echo "*** Remove logs created by vagrant_init_destroy_boxes.sh form \"${LOGDIR}\""
test -d "${LOGDIR}" && find "${LOGDIR}" -maxdepth 1 -mindepth 1 -name "*-init.log" -delete

echo "*** Remove all PACKER_CACHE_DIR mess (not the iso files)"
test -d "${PACKER_CACHE_DIR}" && find "${PACKER_CACHE_DIR}" -mindepth 1 ! \( -type f -name "*.iso" \) -user "${USER}" -delete -print

Expand Down Expand Up @@ -71,11 +68,19 @@ if command -v virsh &>/dev/null; then
fi

if [[ -d "${LOGDIR}" ]]; then
echo "*** Remove directory: ${LOGDIR}"
rm -rvf "${LOGDIR}"
echo "*** Remove logs created by vagrant_init_destroy_boxes.sh form \"${LOGDIR}\""
find "${LOGDIR}" -maxdepth 1 -mindepth 1 -name "*-init.log" -delete -print
if [[ ! "$(ls -A "${LOGDIR}")" ]]; then
echo "*** Remove directory: ${LOGDIR}"
rmdir -v "${LOGDIR}"
fi
fi

if [[ -d "${PACKER_IMAGES_OUTPUT_DIR}" ]]; then
echo "*** Remove directory: ${PACKER_IMAGES_OUTPUT_DIR}"
rm -rvf "${PACKER_IMAGES_OUTPUT_DIR}"
echo "*** Remove boxes and logs created by build.sh form \"${PACKER_IMAGES_OUTPUT_DIR}\""
find "${PACKER_IMAGES_OUTPUT_DIR}" "${LOGDIR}" -maxdepth 1 -mindepth 1 -name "*-packer.log" -delete -print
if [[ ! "$(ls -A "${PACKER_IMAGES_OUTPUT_DIR}")" ]]; then
echo "*** Remove directory: ${PACKER_IMAGES_OUTPUT_DIR}"
rmdir -v "${PACKER_IMAGES_OUTPUT_DIR}"
fi
fi
61 changes: 48 additions & 13 deletions vagrant_init_destroy_boxes.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,69 @@ set -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:-${TMPDIR}}
STDOUT="/dev/null"
export VAGRANT_IGNORE_WINRM_PLUGIN=true

vagrant_box_add() {
vagrant box add "${VAGRANT_BOX_FILE}" --name="${VAGRANT_BOX_NAME}" --force
vagrant box add "${VAGRANT_BOX_FILE}" --name="${VAGRANT_BOX_NAME}" --force > ${STDOUT}
}

vagrant_init_up() {
vagrant init "${VAGRANT_BOX_NAME}"
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}" | grep -v 'Progress:'
vagrant up --provider "${VAGRANT_BOX_PROVIDER}" > ${STDOUT}
}

check_vagrant_vm() {
VAGRANT_VM_IP=$(vagrant ssh-config | awk '/HostName/ { print $2 }')

case ${VAGRANT_BOX_FILE} in
*windows* )
echo "*** Getting version: systeminfo | findstr /B /C:\"OS Name\" /C:\"OS Version\""
vagrant winrm --shell cmd --command 'systeminfo | findstr /B /C:"OS Name" /C:"OS Version"'
echo "*** Running: vagrant winrm --shell powershell --command 'Get-Service ...'"
vagrant winrm --shell powershell --command "Get-ChildItem -Path Cert:\LocalMachine\TrustedPublisher; Get-Service | where {\$_.Name -match \".*QEMU.*|.*Spice.*|.*vdservice.*|.*VBoxService.*\"}; Get-WmiObject -Class Win32_Product; Get-WmiObject Win32_PnPSignedDriver | where {\$_.devicename -match \".*Red Hat.*|.*VirtIO.*\"} | select devicename, driverversion" | uniq
echo "*** Running: vagrant winrm --shell powershell --command 'cscript slmgr.vbs...'"
vagrant winrm --shell cmd --command 'cscript C:\Windows\System32\slmgr.vbs /dli'
echo "*** vagrant winrm --shell powershell --command .... finished"
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

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

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

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 or 180 days !"
vagrant_cleanup
exit 4
fi

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}\" !"
exit 5
fi
;;
*centos* | *ubuntu* )
echo "*** Checking if there are some packages to upgrade (there should be none)"
Expand All @@ -55,14 +89,15 @@ check_vagrant_vm() {
}

vagrant_cleanup() {
vagrant destroy -f
vagrant box remove -f "${VAGRANT_BOX_NAME}"
vagrant destroy -f > ${STDOUT}
vagrant box remove -f "${VAGRANT_BOX_NAME}" > ${STDOUT}

if [[ "${VAGRANT_BOX_NAME}" =~ "libvirt" ]]; then
virsh --connect=qemu:///system vol-delete --pool default --vol "${VAGRANT_BOX_NAME}_vagrant_box_image_0.img"
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}
rm "${LOG_FILE}"
rmdir "${VAGRANT_CWD}"
}

Expand Down

0 comments on commit 110673f

Please sign in to comment.