Chore: Upgrade Jenkins-job-builder to 6.3.0
[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         PARAMS="tree=computer[executors[currentExecutable[url]],"
26         PARAMS=$PARAMS"oneOffExecutors[currentExecutable[url]]]"
27         PARAMS=$PARAMS"&xpath=//url&wrapper=builds"
28         JENKINS_URL="$jenkins/computer/api/json?$PARAMS"
29         resp=$(curl -s -w "\\n\\n%{http_code}" --globoff -H "Content-Type:application/json" "$JENKINS_URL")
30         json_data=$(echo "$resp" | head -n1)
31         status=$(echo "$resp" | awk 'END {print $NF}')
32
33         if [ "$status" != 200 ]; then
34             >&2 echo "ERROR: Failed to fetch data from $JENKINS_URL with status code $status"
35             >&2 echo "$resp"
36             exit 1
37         fi
38
39         if [[ "${jenkins}" == *"jenkins."*".org" ]] || [[ "${jenkins}" == *"jenkins."*".io" ]]; then
40             silo="production"
41         else
42             silo=$(echo "$jenkins" | sed 's/\/*$//' | awk -F'/' '{print $NF}')
43         fi
44         export silo
45         # We purposely want to wordsplit here to combine the arrays
46         # shellcheck disable=SC2206,SC2207
47         builds=(${builds[@]} $(echo "$json_data" | \
48             jq -r '.computer[].executors[].currentExecutable.url' \
49             | grep -v null | awk -F'/' '{print ENVIRON["silo"] "-" $6 "-" $7}')
50         )
51     done
52
53     if [[ "${builds[*]}" =~ $STACK_NAME ]]; then
54         return 0
55     fi
56
57     return 1
58 }
59
60 # shellcheck disable=SC1090
61 source ~/lf-env.sh
62
63 lf-activate-venv --python python3 "lftools[openstack]" \
64     kubernetes \
65     niet \
66     python-heatclient \
67     python-openstackclient \
68     python-magnumclient \
69     yq
70
71 set -x
72 #########################
73 ## FETCH ACTIVE BUILDS ##
74 #########################
75 # Fetch COE cluster list before fetching active stacks. K8s cluster creates
76 # stack that does not match JOB_NAME, therefore ignore them while processing
77 # orphaned stacks and handle them separatly.
78 # The stack naming scheme is limited in the source code to take only first 20
79 # chars from the JOB_NAME, and the rest is randomly generated value for
80 # uniqueness:
81 # https://github.com/openstack/magnum/blob/master/magnum/drivers/heat/driver.py#L202-L212
82 mapfile -t OS_COE_CLUSTERS_ID < <(openstack --os-cloud "${os_cloud}" coe cluster list \
83             -f value -c "uuid" -c "name" \
84             | grep -E '(DELETE_FAILED|UNKNOWN|UNHEALTHY)' | awk '{print $1}')
85
86 echo "-----> Active clusters -> stacks"
87 # mapfile -t OS_COE_STACKS_ID
88 OS_COE_STACKS=()
89 for cluster_id in "${OS_COE_CLUSTERS_ID[@]}"; do
90     # find active stacks id associated with the COE cluster
91     stack_id=$(openstack --os-cloud "${os_cloud}" coe cluster show "${cluster_id}" \
92                 -f value -c "stack_id")
93     # get the stack name associated with the COE cluster
94     stack_name=$(openstack --os-cloud "${os_cloud}" stack show "${stack_id}" \
95                 -f value -c "stack_name")
96     OS_COE_STACKS+=("${stack_id}")
97     echo "clusterid:${cluster_id} -> stackid:${stack_id} stack_name: ${stack_name}"
98 done
99
100 if [[ ${#OS_COE_STACKS[@]} -gt "0" ]]; then
101     echo "${OS_COE_STACKS[*]}"
102     echo "-----> Active COE cluster stacks"
103     for cstack in "${OS_COE_STACKS[@]}"; do
104         echo "$cstack"
105     done
106 fi
107
108 # Fetch stack list before fetching active builds to minimize race condition
109 # where we might be try to delete stacks while jobs are trying to start
110 mapfile -t OS_STACKS < <(openstack --os-cloud "$os_cloud" stack list \
111             -f value -c "Stack Name" -c "Stack Status" \
112             --property "stack_status=CREATE_COMPLETE" \
113             --property "stack_status=DELETE_FAILED" \
114             --property "stack_status=CREATE_FAILED" \
115             | awk '{print $1}')
116
117 echo "-----> Active stacks"
118 for stack in "${OS_STACKS[@]}"; do
119     echo "$stack"
120 done
121
122
123 ##########################
124 ## DELETE UNUSED STACKS ##
125 ##########################
126 echo "-----> Delete orphaned stacks"
127
128 # Search for stacks not in use by any active Jenkins systems and remove them.
129 for STACK_NAME in "${OS_STACKS[@]}"; do
130     # Check for COE cluster stack is present
131     # shellcheck disable=SC2153,SC2086
132     if [[ ${#OS_COE_STACKS[@]} -gt "0" ]] && [[ ${OS_COE_STACKS[*]} =~ ${STACK_NAME} ]]; then
133         # Do not delete a stack linked to COE cluster, handle them separatly.
134         continue
135     # jenkins_urls intentially needs globbing to be passed a separate params.
136     elif stack_in_jenkins "$STACK_NAME" $jenkins_urls; then
137         # No need to delete stacks if there exists an active build for them
138         continue
139     else
140         echo "Deleting orphaned stack: ${STACK_NAME}"
141         lftools openstack --os-cloud "${os_cloud}" stack delete --force "${STACK_NAME}"
142     fi
143 done