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
21 ##########################
22 ## FETCH ACTIVE MINIONS ##
23 ##########################
24 # Fetch server list before fetching active minions to minimize race condition
25 # where we might be trying to delete servers while jobs are trying to start
26 OS_SERVERS=($(openstack server list -f value -c "Name" | grep -E 'prd|snd'))
28 # Make sure we fetch active minions on both the releng and sandbox silos
30 for silo in releng sandbox; do
31 query="https://jenkins.opendaylight.org/$silo/computer/api/json?tree=computer[displayName]"
32 resp=$(curl -s -w "\n\n%{http_code}" --header "Accept: application/json" "$query")
33 json_data=$(echo "$resp" | head -n1)
34 ACTIVE_MINIONS=(${ACTIVE_MINIONS[@]} $( echo "$json_data" \
35 | jq -r '.computer[].displayName' | grep -v master))
38 #############################
39 ## DELETE ORPHANED SERVERS ##
40 #############################
41 # Search for servers that are not in use by either releng or sandbox silos and
43 for server in "${OS_SERVERS[@]}"; do
44 if [[ "${ACTIVE_MINIONS[*]}" =~ $server ]]; then
45 # No need to delete server if it is still attached to Jenkins
48 echo "Deleting $server"
49 openstack server delete "$server"