Continue to delete instead of abandon
[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
12 virtualenv "/tmp/v/openstack"
13 # shellcheck source=/tmp/v/openstack/bin/activate disable=SC1091
14 source "/tmp/v/openstack/bin/activate"
15 pip install --upgrade pip
16 pip install --upgrade python-openstackclient python-heatclient
17 pip install --upgrade pipdeptree
18 pipdeptree
19
20 #########################
21 ## FETCH ACTIVE BUILDS ##
22 #########################
23 # Fetch stack list before fetching active builds to minimize race condition
24 # where we might be try to delete stacks while jobs are trying to start
25 OS_STACKS=($(openstack stack list \
26             -f value -c "Stack Name" -c "Stack Status" \
27             --property "stack_status=CREATE_COMPLETE" \
28             --property "stack_status=DELETE_FAILED" \
29             --property "stack_status=CREATE_FAILED" \
30             | awk '{print $1}'))
31
32 # Make sure we fetch active builds on both the releng and sandbox silos
33 ACTIVE_BUILDS=()
34 for silo in releng sandbox; do
35     JENKINS_URL="https://jenkins.opendaylight.org/$silo//computer/api/json?tree=computer[executors[currentExecutable[url]],oneOffExecutors[currentExecutable[url]]]&xpath=//url&wrapper=builds"
36     wget -nv -O "${silo}_builds.json" "$JENKINS_URL"
37     sleep 1  # Need to sleep for 1 second otherwise next line causes script to stall
38     ACTIVE_BUILDS=(${ACTIVE_BUILDS[@]} $( \
39         jq -r '.computer[].executors[].currentExecutable.url' "${silo}_builds.json" \
40         | grep -v null | awk -F'/' '{print $4 "-" $6 "-" $7}'))
41 done
42
43 ##########################
44 ## DELETE UNUSED STACKS ##
45 ##########################
46 # Search for stacks that are not in use by either releng or sandbox silos and
47 # delete them.
48 for STACK_NAME in "${OS_STACKS[@]}"; do
49     echo "Deleting stack $STACK_NAME"
50     STACK_STATUS=$(openstack stack show -f value -c "stack_status" "$STACK_NAME")
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                 # Abandon is not supported in Vexxhost so let's keep trying to
62                 # delete for now...
63                 # echo "Stack delete failed, trying to stack abandon now."
64                 # openstack stack abandon "$STACK_NAME"
65                 echo "Deleting orphaned stack: $STACK_NAME"
66                 openstack stack delete --yes "$STACK_NAME"
67                 STACK_SHOW=$(openstack stack show "$STACK_NAME")
68                 echo "$STACK_SHOW"
69                 continue
70             ;;
71             CREATE_COMPLETE|CREATE_FAILED)
72                 echo "Deleting orphaned stack: $STACK_NAME"
73                 openstack stack delete --yes "$STACK_NAME"
74                 continue
75             ;;
76             *)
77                 continue
78             ;;
79         esac
80     fi
81 done