6d5e553c1a0fb6baa3a325607110dd1d145d71da
[releng/builder.git] / jjb / odl-openstack-cleanup-stale-stacks.sh
1 #!/bin/bash -l
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 # 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"
15
16 stack_in_jenkins() {
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.
19
20     STACK_NAME="${1}"
21
22     builds=()
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}')
28
29         if [[ "${jenkins}" == *"jenkins."*".org" ]]; then
30             silo="production"
31         else
32             silo=$(echo "$jenkins" | sed 's/\/*$//' | awk -F'/' '{print $NF}')
33         fi
34         export silo
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}')
40         )
41     done
42
43     if [[ "${builds[*]}" =~ $STACK_NAME ]]; then
44         return 0
45     fi
46
47     return 1
48 }
49
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
55
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" \
63             | awk '{print $1}'))
64
65 echo "---> Active stacks"
66 for stack in "${OS_STACKS[@]}"; do
67     echo "$stack"
68 done
69
70 ##########################
71 ## DELETE UNUSED STACKS ##
72 ##########################
73 echo "---> Delete orphaned stacks"
74
75 # Search for stacks that are not in use by either releng or sandbox silos and
76 # delete them.
77 for STACK_NAME in "${OS_STACKS[@]}"; do
78     echo "Checking if orphaned $STACK_NAME"
79
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
85         continue
86     else
87         status=$(openstack stack show -f value -c "stack_status" "$STACK_NAME")
88         case "$status" in
89             DELETE_IN_PROGRESS)
90                 echo "skipping delete, $STACK_NAME is already DELETE in progress."
91                 continue
92             ;;
93             DELETE_FAILED)
94                 # Abandon is not supported in Vexxhost so let's keep trying to
95                 # delete for now...
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"
100
101                 echo "------------------------------------"
102                 echo "Stack details"
103                 echo "------------------------------------"
104                 openstack stack show "$STACK_NAME" -f yaml
105                 echo "------------------------------------"
106                 continue
107             ;;
108             CREATE_COMPLETE|CREATE_FAILED)
109                 echo "Deleting orphaned stack: $STACK_NAME"
110                 openstack stack delete --yes "$STACK_NAME"
111                 continue
112             ;;
113             *)
114                 continue
115             ;;
116         esac
117     fi
118 done