Migrate cleanup-stale-stacks cron script
[releng/global-jjb.git] / shell / openstack-cleanup-orphaned-stacks.sh
1 #!/bin/bash -l
2 # SPDX-License-Identifier: EPL-1.0
3 ##############################################################################
4 # Copyright (c) 2017, 2018 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 # Scans OpenStack for orphaned stacks
12 echo "---> Orphaned stacks"
13
14 os_cloud="${OS_CLOUD:-vex}"
15 jenkins_urls="${JENKINS_URLS:-}"
16
17 stack_in_jenkins() {
18     # Usage: check_stack_in_jenkins STACK_NAME JENKINS_URL [JENKINS_URL...]
19     # Returns: 0 If stack is in Jenkins and 1 if stack is not in Jenkins.
20
21     STACK_NAME="${1}"
22
23     builds=()
24     for jenkins in "${@:2}"; do
25         JENKINS_URL="$jenkins/computer/api/json?tree=computer[executors[currentExecutable[url]],oneOffExecutors[currentExecutable[url]]]&xpath=//url&wrapper=builds"
26         resp=$(curl -s -w "\\n\\n%{http_code}" --globoff -H "Content-Type:application/json" "$JENKINS_URL")
27         json_data=$(echo "$resp" | head -n1)
28         #status=$(echo "$resp" | awk 'END {print $NF}')
29
30         if [[ "${jenkins}" == *"jenkins."*".org" ]] || [[ "${jenkins}" == *"jenkins."*".io" ]]; then
31             silo="production"
32         else
33             silo=$(echo "$jenkins" | sed 's/\/*$//' | awk -F'/' '{print $NF}')
34         fi
35         export silo
36         # We purposely want to wordsplit here to combine the arrays
37         # shellcheck disable=SC2206,SC2207
38         builds=(${builds[@]} $(echo "$json_data" | \
39             jq -r '.computer[].executors[].currentExecutable.url' \
40             | grep -v null | awk -F'/' '{print ENVIRON["silo"] "-" $6 "-" $7}')
41         )
42     done
43
44     if [[ "${builds[*]}" =~ $STACK_NAME ]]; then
45         return 0
46     fi
47
48     return 1
49 }
50
51 #########################
52 ## FETCH ACTIVE BUILDS ##
53 #########################
54 # Fetch stack list before fetching active builds to minimize race condition
55 # where we might be try to delete stacks while jobs are trying to start
56
57 mapfile -t OS_STACKS < <(openstack --os-cloud "$os_cloud" stack list \
58             -f value -c "Stack Name" -c "Stack Status" \
59             --property "stack_status=CREATE_COMPLETE" \
60             --property "stack_status=DELETE_FAILED" \
61             --property "stack_status=CREATE_FAILED" \
62             | awk '{print $1}')
63
64 echo "-----> Active stacks"
65 for stack in "${OS_STACKS[@]}"; do
66     echo "$stack"
67 done
68
69
70 ##########################
71 ## DELETE UNUSED STACKS ##
72 ##########################
73 echo "-----> Delete orphaned stacks"
74
75 # Search for stacks not in use by any active Jenkins systems and remove them.
76 for STACK_NAME in "${OS_STACKS[@]}"; do
77     # jenkins_urls intentially needs globbing to be passed a separate params.
78     # shellcheck disable=SC2153,SC2086
79     if stack_in_jenkins "$STACK_NAME" $jenkins_urls; then
80         # No need to delete stacks if there exists an active build for them
81         continue
82     else
83         echo "Deleting orphaned stack: $STACK_NAME"
84         lftools openstack --os-cloud "$os_cloud" stack delete "$STACK_NAME"
85     fi
86 done