Consolidate a single openstack-cron job
[releng/builder.git] / jjb / opendaylight-infra-cleanup-stale-stacks.sh
1 #!/bin/bash
2 # SPDX-License-Identifier: EPL-1.0
3 ##############################################################################
4 # Copyright (c) 2017 The Linux Foundation and others.
5 #
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Eclipse Public License v1.0
8 # which accompanies this distribution, and is available at
9 # http://www.eclipse.org/legal/epl-v10.html
10 ##############################################################################
11 echo "---> Cleanup unused stacks"
12
13 virtualenv "/tmp/v/openstack"
14 # shellcheck source=/tmp/v/openstack/bin/activate disable=SC1091
15 source "/tmp/v/openstack/bin/activate"
16 pip install --upgrade pip
17 pip install --upgrade python-openstackclient python-heatclient
18 pip install --upgrade pipdeptree
19 pipdeptree
20
21 #########################
22 ## FETCH ACTIVE BUILDS ##
23 #########################
24 # Fetch stack list before fetching active builds to minimize race condition
25 # where we might be try to delete stacks while jobs are trying to start
26 OS_STACKS=($(openstack stack list \
27             -f json -c "Stack Name" -c "Stack Status" \
28             --property "stack_status=CREATE_COMPLETE" \
29             --property "stack_status=DELETE_FAILED" \
30             --property "stack_status=CREATE_FAILED" \
31             | jq -r '.[] | ."Stack Name"'))
32
33 # Make sure we fetch active builds on both the releng and sandbox silos
34 ACTIVE_BUILDS=()
35 for silo in releng sandbox; do
36     JENKINS_URL="https://jenkins.opendaylight.org/$silo//computer/api/json?tree=computer[executors[currentExecutable[url]],oneOffExecutors[currentExecutable[url]]]&xpath=//url&wrapper=builds"
37     wget -nv -O "${silo}_builds.json" "$JENKINS_URL"
38     sleep 1  # Need to sleep for 1 second otherwise next line causes script to stall
39     ACTIVE_BUILDS=(${ACTIVE_BUILDS[@]} $( \
40         jq -r '.computer[].executors[].currentExecutable.url' "${silo}_builds.json" \
41         | grep -v null | awk -F'/' '{print $4 "-" $6 "-" $7}'))
42 done
43
44 ##########################
45 ## DELETE UNUSED STACKS ##
46 ##########################
47 # Search for stacks that are not in use by either releng or sandbox silos and
48 # delete them.
49 for STACK_NAME in "${OS_STACKS[@]}"; do
50     STACK_STATUS=$(openstack stack show -f json -c "stack_status" "$STACK_NAME" | jq -r '."stack_status"')
51     if [[ "${ACTIVE_BUILDS[*]}" =~ $STACK_NAME ]]; then
52         # No need to delete stacks if there exists an active build for them
53         continue
54     else
55         case "$STACK_STATUS" in
56             DELETE_IN_PROGRESS)
57                 echo "skipping delete, $STACK_NAME is already DELETE in progress."
58                 continue
59             ;;
60             DELETE_FAILED)
61                 echo "Stack delete failed, trying to stack abandon now."
62                 openstack stack abandon "$STACK_NAME"
63                 STACK_SHOW=$(openstack stack show "$STACK_NAME")
64                 echo "$STACK_SHOW"
65                 continue
66             ;;
67             CREATE_COMPLETE|CREATE_FAILED)
68                 echo "Deleting orphaned stack: $STACK_NAME"
69                 openstack stack delete --yes "$STACK_NAME"
70                 continue
71             ;;
72             *)
73                 continue
74             ;;
75         esac
76     fi
77 done