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 # Cleanup stale stacks in the cloud
12 # Requires the variable JENKINS_URLS declared in the job as a space separated
13 # list of Jenkins instances to check for active builds.
14 echo "---> Cleanup stale stacks"
16 # shellcheck source=/tmp/v/openstack/bin/activate disable=SC1091
17 source "/tmp/v/openstack/bin/activate"
20 # Usage: check_stack_in_jenkins STACK_NAME JENKINS_URL [JENKINS_URL...]
21 # Returns: 0 If stack is in Jenkins and 1 if stack is not in Jenkins.
26 for jenkins in "${@:2}"; do
27 JENKINS_URL="$jenkins/computer/api/json?tree=computer[executors[currentExecutable[url]],oneOffExecutors[currentExecutable[url]]]&xpath=//url&wrapper=builds"
28 resp=$(curl -s -w "\\n\\n%{http_code}" --globoff -H "Content-Type:application/json" "$JENKINS_URL")
29 json_data=$(echo "$resp" | head -n1)
30 #status=$(echo "$resp" | awk 'END {print $NF}')
32 if [[ "${jenkins}" == *"jenkins."*".org" ]]; then
35 silo=$(echo "$jenkins" | sed 's/\/*$//' | awk -F'/' '{print $NF}')
38 # We purposely want to wordsplit here to combine the arrays
39 # shellcheck disable=SC2206,SC2207
40 builds=(${builds[@]} $(echo "$json_data" | \
41 jq -r '.computer[].executors[].currentExecutable.url' \
42 | grep -v null | awk -F'/' '{print ENVIRON["silo"] "-" $6 "-" $7}')
46 if [[ "${builds[*]}" =~ $STACK_NAME ]]; then
53 #########################
54 ## FETCH ACTIVE BUILDS ##
55 #########################
56 # Fetch stack list before fetching active builds to minimize race condition
57 # where we might be try to delete stacks while jobs are trying to start
59 # We purposely need word splitting here to create the OS_STACKS array.
60 # shellcheck disable=SC2207
61 OS_STACKS=($(openstack stack list \
62 -f value -c "Stack Name" -c "Stack Status" \
63 --property "stack_status=CREATE_COMPLETE" \
64 --property "stack_status=DELETE_FAILED" \
65 --property "stack_status=CREATE_FAILED" \
68 echo "---> Active stacks"
69 for stack in "${OS_STACKS[@]}"; do
73 ##########################
74 ## DELETE UNUSED STACKS ##
75 ##########################
76 echo "---> Delete orphaned stacks"
78 # Search for stacks that are not in use by either releng or sandbox silos and
80 for STACK_NAME in "${OS_STACKS[@]}"; do
81 echo "Checking if orphaned $STACK_NAME"
83 # JENKINS_URLS is provided by the Jenkins Job declaration and intentially
84 # needs to be globbed.
85 # shellcheck disable=SC2153,SC2086
86 if stack_in_jenkins "$STACK_NAME" $JENKINS_URLS; then
87 # No need to delete stacks if there exists an active build for them
90 status=$(openstack stack show -f value -c "stack_status" "$STACK_NAME")
93 echo "skipping delete, $STACK_NAME is already DELETE in progress."
97 # Abandon is not supported in Vexxhost so let's keep trying to
99 # echo "Stack delete failed, trying to stack abandon now."
100 # openstack stack abandon "$STACK_NAME"
101 echo "Deleting orphaned stack: $STACK_NAME"
102 openstack stack delete --yes "$STACK_NAME"
104 echo "------------------------------------"
106 echo "------------------------------------"
107 openstack stack show "$STACK_NAME" -f yaml
108 echo "------------------------------------"
111 CREATE_COMPLETE|CREATE_FAILED)
112 echo "Deleting orphaned stack: $STACK_NAME"
113 openstack stack delete --yes "$STACK_NAME"