diff --git a/tools/cleanup_all_vms.sh b/tools/cleanup_all_vms.sh index 27f3c60c..374bcab6 100755 --- a/tools/cleanup_all_vms.sh +++ b/tools/cleanup_all_vms.sh @@ -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 @@ -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 @@ -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 diff --git a/vagrant_init_destroy_boxes.sh b/vagrant_init_destroy_boxes.sh index dc836349..2e3723bc 100755 --- a/vagrant_init_destroy_boxes.sh +++ b/vagrant_init_destroy_boxes.sh @@ -5,21 +5,22 @@ 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() { @@ -27,13 +28,46 @@ check_vagrant_vm() { 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)" @@ -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}" }