-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvmware.sh
executable file
·42 lines (32 loc) · 1.37 KB
/
vmware.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
#!/bin/bash
set -euo pipefail
# include the common library
source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)/lib/common.sh"
#---------------------------------------------------------------
# vmware cleanup
#---------------------------------------------------------------
greenprint 'Starting vmware cleanup'
GOVC_CMD=/tmp/govc
# We need govc to talk to vSphere
if ! hash govc; then
greenprint 'Installing govc'
pushd /tmp || exit
curl -Ls --retry 5 --output govc.tar.gz \
https://github.com/vmware/govmomi/releases/download/v0.30.4/govc_Linux_x86_64.tar.gz
tar -xzvf govc.tar.gz
chmod +x ./govc
$GOVC_CMD version
popd || exit
fi
GOVC_AUTH="${GOVMOMI_USERNAME}:${GOVMOMI_PASSWORD}@${GOVMOMI_URL}"
TAGGED=$($GOVC_CMD tags.attached.ls -u "${GOVC_AUTH}" -k "gitlab-ci-test" | xargs -r ${GOVC_CMD} ls -u "${GOVC_AUTH}" -k -L)
for vm in $TAGGED; do
# Could use JSON output, but it takes much longer, as it includes more properties
CREATION_TIME=$($GOVC_CMD vm.info -u "${GOVC_AUTH}" -k "${vm}" | awk '$1 ~ /^ *Boot/ { print $3 " " $4 $5 }')
if [[ $(date -d "${CREATION_TIME}" +%s) -lt ${DELETE_TIME} ]]; then
$GOVC_CMD vm.destroy -u "${GOVC_AUTH}" -k "${vm}"
echo "Destroyed vm: ${vm}"
else
echo "The instance ${vm} was launched less than ${HOURS_BACK} hours ago"
fi
done