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"
17 # Usage: check_stack_in_jenkins STACK_NAME JENKINS_URL [JENKINS_URL...]
18 # Returns: 0 If stack is in Jenkins and 1 if stack is not in Jenkins.
23 for jenkins in "${@:2}"; do
24 JENKINS_URL="$jenkins/computer/api/json?tree=computer[executors[currentExecutable[url]],oneOffExecutors[currentExecutable[url]]]&xpath=//url&wrapper=builds"
25 resp=$(curl -s -w "\\n\\n%{http_code}" --globoff -H "Content-Type:application/json" "$JENKINS_URL")
26 json_data=$(echo "$resp" | head -n1)
27 #status=$(echo "$resp" | awk 'END {print $NF}')
29 if [[ "${jenkins}" == *"jenkins."*".org" ]]; then
32 silo=$(echo "$jenkins" | sed 's/\/*$//' | awk -F'/' '{print $NF}')
35 # We purposely want to wordsplit here to combine the arrays
36 # shellcheck disable=SC2206,SC2207
37 builds=(${builds[@]} $(echo "$json_data" | \
38 jq -r '.computer[].executors[].currentExecutable.url' \
39 | grep -v null | awk -F'/' '{print ENVIRON["silo"] "-" $6 "-" $7}')
43 if [[ "${builds[*]}" =~ $STACK_NAME ]]; then
50 #########################
51 ## FETCH ACTIVE BUILDS ##
52 #########################
53 # Fetch stack list before fetching active builds to minimize race condition
54 # where we might be try to delete stacks while jobs are trying to start
56 # We purposely need word splitting here to create the OS_STACKS array.
57 # shellcheck disable=SC2207
58 OS_STACKS=($(openstack stack list \
59 -f value -c "Stack Name" -c "Stack Status" \
60 --property "stack_status=CREATE_COMPLETE" \
61 --property "stack_status=DELETE_FAILED" \
62 --property "stack_status=CREATE_FAILED" \
65 echo "---> Active stacks"
66 for stack in "${OS_STACKS[@]}"; do
70 ##########################
71 ## DELETE UNUSED STACKS ##
72 ##########################
73 echo "---> Delete orphaned stacks"
75 # Search for stacks that are not in use by either releng or sandbox silos and
77 for STACK_NAME in "${OS_STACKS[@]}"; do
78 echo "Checking if orphaned $STACK_NAME"
80 # JENKINS_URLS is provided by the Jenkins Job declaration and intentially
81 # needs to be globbed.
82 # shellcheck disable=SC2153,SC2086
83 if stack_in_jenkins "$STACK_NAME" $JENKINS_URLS; then
84 # No need to delete stacks if there exists an active build for them
87 status=$(openstack stack show -f value -c "stack_status" "$STACK_NAME")
90 echo "skipping delete, $STACK_NAME is already DELETE in progress."
94 # Abandon is not supported in Vexxhost so let's keep trying to
96 # echo "Stack delete failed, trying to stack abandon now."
97 # openstack stack abandon "$STACK_NAME"
98 echo "Deleting orphaned stack: $STACK_NAME"
99 openstack stack delete --yes "$STACK_NAME"
101 echo "------------------------------------"
103 echo "------------------------------------"
104 openstack stack show "$STACK_NAME" -f yaml
105 echo "------------------------------------"
108 CREATE_COMPLETE|CREATE_FAILED)
109 echo "Deleting orphaned stack: $STACK_NAME"
110 openstack stack delete --yes "$STACK_NAME"