Improve Heat implementation error handling
[releng/builder.git] / jjb / opendaylight-infra-cleanup-stale-stacks.sh
1 #!/bin/bash
2 virtualenv $WORKSPACE/.venv
3 source $WORKSPACE/.venv/bin/activate
4 pip install --upgrade pip
5 pip install --upgrade python-openstackclient python-heatclient
6 pip freeze
7
8 #########################
9 ## FETCH ACTIVE BUILDS ##
10 #########################
11 # Make sure we fetch active builds on both the releng and sandbox silos
12 ACTIVE_BUILDS=()
13 for silo in releng sandbox; do
14     JENKINS_URL="https://jenkins.opendaylight.org/$silo//computer/api/json?tree=computer[executors[currentExecutable[url]],oneOffExecutors[currentExecutable[url]]]&xpath=//url&wrapper=builds"
15     wget --no-verbose -O $silo_builds.json $JENKINS_URL
16     sleep 1  # Need to sleep for 1 second otherwise next line causes script to stall
17     ACTIVE_BUILDS=(${ACTIVE_BUILDS[@]} ` \
18         jq -r '.computer[].executors[].currentExecutable.url' $silo_builds.json \
19         | grep -v null | awk -F'/' '{print $6 "-" $7}'`)
20 done
21
22 ##########################
23 ## DELETE UNUSED STACKS ##
24 ##########################
25 # Search for stacks taht are not in use by either releng or sandbox silos and
26 # delete them.
27 OS_STACKS=(`openstack --os-cloud rackspace stack list \
28             -f json -c "Stack Name" -c "Stack Status" \
29             --property "stack_status=CREATE_COMPLETE" \
30             --property "stack_status=DELETE_FAILED" \
31             --property "stack_status=CREATE_FAILED" \
32             | jq -r '.[] | ."Stack Name"'`)
33 for stack in ${OS_STACKS[@]}; do
34     if [[ "${ACTIVE_BUILDS[@]}" =~ $stack ]]; then
35         # No need to delete stacks if there exists an active build for them
36         continue
37     else
38         echo "Deleting orphaned stack: $stack"
39         openstack --os-cloud rackspace stack delete --yes $stack
40     fi
41 done