Merge "Remove unnecessary jq call in stale-stacks"
[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     STACK_STATUS=$(openstack stack show -f value -c "stack_status" "$STACK_NAME")
50     if [[ "${ACTIVE_BUILDS[*]}" =~ $STACK_NAME ]]; then
51         # No need to delete stacks if there exists an active build for them
52         continue
53     else
54         case "$STACK_STATUS" in
55             DELETE_IN_PROGRESS)
56                 echo "skipping delete, $STACK_NAME is already DELETE in progress."
57                 continue
58             ;;
59             DELETE_FAILED)
60                 echo "Stack delete failed, trying to stack abandon now."
61                 openstack stack abandon "$STACK_NAME"
62                 STACK_SHOW=$(openstack stack show "$STACK_NAME")
63                 echo "$STACK_SHOW"
64                 continue
65             ;;
66             CREATE_COMPLETE|CREATE_FAILED)
67                 echo "Deleting orphaned stack: $STACK_NAME"
68                 openstack stack delete --yes "$STACK_NAME"
69                 continue
70             ;;
71             *)
72                 continue
73             ;;
74         esac
75     fi
76 done