Add script for removing old artifacts from VMware

This commit is contained in:
Jan Stodola 2019-02-21 15:28:48 +01:00 committed by Alexander Todorov
parent 28bd68793b
commit ce110afd22
3 changed files with 141 additions and 0 deletions

View File

@ -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'

View File

@ -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

View File

@ -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())