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 echo "---> Cleanup orphaned servers"
13 virtualenv "/tmp/v/openstack"
14 # shellcheck source=/tmp/v/openstack/bin/activate disable=SC1091
15 source "/tmp/v/openstack/bin/activate"
16 pip install --upgrade pip
17 pip install --upgrade python-openstackclient python-heatclient
18 pip install --upgrade pipdeptree
22 # Usage: check_stack_in_jenkins STACK_NAME JENKINS_URL [JENKINS_URL...]
23 # Returns: 0 If stack is in Jenkins and 1 if stack is not in Jenkins.
28 for jenkins in "${@:2}"; do
29 JENKINS_URL="$jenkins/computer/api/json?tree=computer[displayName]"
30 resp=$(curl -s -w "\\n\\n%{http_code}" --globoff -H "Content-Type:application/json" "$JENKINS_URL")
31 json_data=$(echo "$resp" | head -n1)
32 #status=$(echo "$resp" | awk 'END {print $NF}')
34 # We purposely want to wordsplit here to combine the arrays
35 # shellcheck disable=SC2206,SC2207
36 minions=(${minions[@]} $(echo "$json_data" | \
37 jq -r '.computer[].displayName' | grep -v master)
41 if [[ "${minions[*]}" =~ $MINION ]]; then
48 ##########################
49 ## FETCH ACTIVE MINIONS ##
50 ##########################
51 # Fetch server list before fetching active minions to minimize race condition
52 # where we might be trying to delete servers while jobs are trying to start
54 # We purposely need word splitting here to create the OS_SERVERS array.
55 # shellcheck disable=SC2207
56 OS_SERVERS=($(openstack server list -f value -c "Name" | grep -E 'prd|snd'))
58 #############################
59 ## DELETE ORPHANED SERVERS ##
60 #############################
61 # Search for servers that are not in use by either releng or sandbox silos and
63 for server in "${OS_SERVERS[@]}"; do
64 # JENKINS_URLS is provided by the Jenkins Job declaration and intentially
65 # needs to be globbed.
66 # shellcheck disable=SC2153,SC2086
67 if minion_in_jenkins "$server" $JENKINS_URLS; then
68 # No need to delete server if it is still attached to Jenkins
71 echo "Deleting $server"
72 openstack server delete "$server"