From f02eab78a34b9abd8730294dd9a79ac8ffef4e12 Mon Sep 17 00:00:00 2001 From: Jan Stodola Date: Thu, 21 Feb 2019 15:28:48 +0100 Subject: [PATCH] Add script for removing old artifacts from VMware (cherry picked from commit ce110afd22b068b503d23a0ec718c9f1603abc23) --- Makefile | 1 + tests/cleanup/remove_old_objects_vmware.sh | 75 ++++++++++++++++++++++ tests/cleanup/vmware_list_vms.py | 65 +++++++++++++++++++ 3 files changed, 141 insertions(+) create mode 100755 tests/cleanup/remove_old_objects_vmware.sh create mode 100755 tests/cleanup/vmware_list_vms.py diff --git a/Makefile b/Makefile index 4583da3f..946e7d36 100644 --- a/Makefile +++ b/Makefile @@ -72,6 +72,7 @@ clean_cloud_envs: sudo -E ./tests/cleanup/remove_old_objects_aws.sh sudo -E ./tests/cleanup/remove_old_objects_openstack.sh sudo -E ./tests/cleanup/remove_old_objects_azure.sh + sudo -E ./tests/cleanup/remove_old_objects_vmware.sh # make sure all cleanup scripts finished successfully sudo sh -c 'grep RESULT_STRING /var/tmp/beakerlib-*/TestResults | grep -v PASS && exit 1 || exit 0' diff --git a/tests/cleanup/remove_old_objects_vmware.sh b/tests/cleanup/remove_old_objects_vmware.sh new file mode 100755 index 00000000..13df5e9f --- /dev/null +++ b/tests/cleanup/remove_old_objects_vmware.sh @@ -0,0 +1,75 @@ +#!/bin/bash +# Script removes virtual machines and other artifacts older than HOURS_LIMIT (24 hours by default) from VMware vShere + +. /usr/share/beakerlib/beakerlib.sh + + +rlJournalStart + rlPhaseStartSetup + if [ -z "$V_HOST" ]; then + rlFail "V_HOST is empty!" + else + rlLogInfo "V_HOST=$V_HOST" + fi + + if [ -z "$V_USERNAME" ]; then + rlFail "V_USERNAME is empty!" + else + rlLogInfo "V_USERNAME=$V_USERNAME" + fi + + if [ -z "$V_PASSWORD" ]; then + rlFail "V_PASSWORD is empty!" + else + rlLogInfo "V_PASSWORD is configured" + fi + + # VMs older than HOURS_LIMIT will be deleted + HOURS_LIMIT="${HOURS_LIMIT:-24}" + export TIMESTAMP=`date -u -d "$HOURS_LIMIT hours ago" '+%FT%T'` + + rlLogInfo "HOURS_LIMIT=$HOURS_LIMIT" + rlLogInfo "TIMESTAMP=$TIMESTAMP" + + for package in python3-pip git; do + if ! rlCheckRpm "$package"; then + rlRun -t -c "dnf -y install $package" + rlAssertRpm "$package" + fi + done + + rlRun -t -c "pip3 install pyvmomi" + + TMP_DIR=`mktemp -d /tmp/composer-vmware.XXXXX` + SAMPLES="$TMP_DIR/pyvmomi-community-samples" + if [ ! -d "$SAMPLES" ]; then + rlRun -t -c "git clone https://github.com/weldr/pyvmomi-community-samples $SAMPLES" + pushd $SAMPLES && git checkout composer_testing && popd + fi + SAMPLES="$SAMPLES/samples" + SCRIPT_DIR=$(dirname "$0") + rlPhaseEnd + + rlPhaseStartTest "Delete old VMs" + # list all VMs + rlRun -t -c 'python3 $SCRIPT_DIR/vmware_list_vms.py --host $V_HOST --user $V_USERNAME --password $V_PASSWORD --disable_ssl_verification > $TMP_DIR/vmware_vms' 0 'Getting a list of VMs' + + while read name uuid creation_date; do + # remove VMs with name starting "Composer-Auto-VM" and older than $TIMESTAMP + echo $name | grep ^Composer-Auto-VM > /dev/null + if [ $? -eq 0 -a "$creation_date" \< "$TIMESTAMP" ]; then + # note: vmdk disk is removed when destroying the VM + rlRun 'python3 $SAMPLES/destroy_vm.py -S -s $V_HOST -u $V_USERNAME -p $V_PASSWORD --uuid $uuid' 0 "Delete VM: $name UUID: $uuid" + rlAssert0 "VM destroyed" $? + fi + done < $TMP_DIR/vmware_vms + + rlPhaseEnd + + rlPhaseStartCleanup + rlRun -t -c "rm -rf $TMP_DIR" + rlPhaseEnd + +rlJournalEnd +rlJournalPrintText + diff --git a/tests/cleanup/vmware_list_vms.py b/tests/cleanup/vmware_list_vms.py new file mode 100755 index 00000000..7724524d --- /dev/null +++ b/tests/cleanup/vmware_list_vms.py @@ -0,0 +1,65 @@ +#!/usr/bin/env python +# list all VMs in vSphere and print their name, UUID and date/time of creation + +import atexit +import argparse +import getpass +import ssl + +from pyVim import connect +from pyVmomi import vim + + +def setup_args(): + parser = argparse.ArgumentParser() + + parser.add_argument('--host', required=True, help='vSphere service to connect to') + parser.add_argument('--port', type=int, default=443, help="Port number (default is 443)") + parser.add_argument('--username', required=True, help='User name') + parser.add_argument('--password', help='User password') + parser.add_argument('--disable_ssl_verification', action='store_true', help='Disable ssl host certificate verification') + + args = parser.parse_args() + if not args.password: + args.password = getpass.getpass() + return args + +def print_vm_datetime(vm): + create_date = vm.config.createDate + # spaces are used as field separators, remove them from VM names + name = vm.config.name.replace(' ', '') + uuid = vm.config.instanceUuid + if create_date: + print(name, uuid, create_date.isoformat()) + +def main(): + args = setup_args() + + sslContext = None + if args.disable_ssl_verification: + sslContext = ssl.SSLContext(ssl.PROTOCOL_SSLv23) + sslContext.verify_mode = ssl.CERT_NONE + + try: + service_instance = connect.SmartConnect(host=args.host, + port=args.port, + user=args.username, + pwd=args.password, + sslContext=sslContext) + except: + print("Unable to connect to %s" % args.host) + return 1 + + atexit.register(connect.Disconnect, service_instance) + + content = service_instance.RetrieveContent() + viewType = [vim.VirtualMachine] + container = content.viewManager.CreateContainerView(content.rootFolder, viewType, recursive=True) + + for child in container.view: + print_vm_datetime(child) + + return 0 + +if __name__ == "__main__": + exit(main())