2 # SPDX-License-Identifier: EPL-1.0
3 ##############################################################################
4 # Copyright (c) 2017 The Linux Foundation and others.
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"
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
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"'))
33 # Make sure we fetch active builds on both the releng and sandbox silos
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}'))
44 ##########################
45 ## DELETE UNUSED STACKS ##
46 ##########################
47 # Search for stacks that are not in use by either releng or sandbox silos and
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
55 case "$STACK_STATUS" in
57 echo "skipping delete, $STACK_NAME is already DELETE in progress."
61 echo "Stack delete failed, trying to stack abandon now."
62 openstack stack abandon "$STACK_NAME"
63 STACK_SHOW=$(openstack stack show "$STACK_NAME")
67 CREATE_COMPLETE|CREATE_FAILED)
68 echo "Deleting orphaned stack: $STACK_NAME"
69 openstack stack delete --yes "$STACK_NAME"