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.
15 virtualenv "/tmp/v/openstack"
16 # shellcheck source=/tmp/v/openstack/bin/activate disable=SC1091
17 source "/tmp/v/openstack/bin/activate"
18 pip install --upgrade pip
19 pip install --upgrade python-openstackclient python-heatclient
20 pip install --upgrade pipdeptree
24 # Usage: check_stack_in_jenkins STACK_NAME JENKINS_URL [JENKINS_URL...]
25 # Returns: 0 If stack is in Jenkins and 1 if stack is not in Jenkins.
30 for jenkins in "${@:2}"; do
31 JENKINS_URL="$jenkins/computer/api/json?tree=computer[executors[currentExecutable[url]],oneOffExecutors[currentExecutable[url]]]&xpath=//url&wrapper=builds"
32 resp=$(curl -s -w "\\n\\n%{http_code}" --globoff -H "Content-Type:application/json" "$JENKINS_URL")
33 json_data=$(echo "$resp" | head -n1)
34 #status=$(echo "$resp" | awk 'END {print $NF}')
36 if [[ "${jenkins}" == *"jenkins."*".org" ]]; then
39 silo=$(echo "$jenkins" | sed 's/\/*$//' | awk -F'/' '{print $NF}')
42 # We purposely want to wordsplit here to combine the arrays
43 # shellcheck disable=SC2206,SC2207
44 builds=(${builds[@]} $(echo "$json_data" | \
45 jq -r '.computer[].executors[].currentExecutable.url' \
46 | grep -v null | awk -F'/' '{print ENVIRON["silo"] "-" $6 "-" $7}')
50 if [[ "${builds[*]}" =~ $STACK_NAME ]]; then
57 #########################
58 ## FETCH ACTIVE BUILDS ##
59 #########################
60 # Fetch stack list before fetching active builds to minimize race condition
61 # where we might be try to delete stacks while jobs are trying to start
63 # We purposely need word splitting here to create the OS_STACKS array.
64 # shellcheck disable=SC2207
65 OS_STACKS=($(openstack stack list \
66 -f value -c "Stack Name" -c "Stack Status" \
67 --property "stack_status=CREATE_COMPLETE" \
68 --property "stack_status=DELETE_FAILED" \
69 --property "stack_status=CREATE_FAILED" \
72 echo "---> Active stacks"
73 for stack in "${OS_STACKS[@]}"; do
77 ##########################
78 ## DELETE UNUSED STACKS ##
79 ##########################
80 echo "---> Delete orphaned stacks"
82 # Search for stacks that are not in use by either releng or sandbox silos and
84 for STACK_NAME in "${OS_STACKS[@]}"; do
85 echo "Checking if orphaned $STACK_NAME"
87 # JENKINS_URLS is provided by the Jenkins Job declaration and intentially
88 # needs to be globbed.
89 # shellcheck disable=SC2153,SC2086
90 if stack_in_jenkins "$STACK_NAME" $JENKINS_URLS; then
91 # No need to delete stacks if there exists an active build for them
94 status=$(openstack stack show -f value -c "stack_status" "$STACK_NAME")
97 echo "skipping delete, $STACK_NAME is already DELETE in progress."
101 # Abandon is not supported in Vexxhost so let's keep trying to
103 # echo "Stack delete failed, trying to stack abandon now."
104 # openstack stack abandon "$STACK_NAME"
105 echo "Deleting orphaned stack: $STACK_NAME"
106 openstack stack delete --yes "$STACK_NAME"
108 echo "------------------------------------"
110 echo "------------------------------------"
111 openstack stack show "$STACK_NAME" -f yaml
112 echo "------------------------------------"
115 CREATE_COMPLETE|CREATE_FAILED)
116 echo "Deleting orphaned stack: $STACK_NAME"
117 openstack stack delete --yes "$STACK_NAME"