Fix: Use lf-activate-venv to install openstack dep
[releng/global-jjb.git] / shell / openstack-cleanup-orphaned-servers.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 servers
12 echo "---> Orphaned servers"
13
14 os_cloud="${OS_CLOUD:-vex}"
15 jenkins_urls="${JENKINS_URLS:-}"
16
17 minion_in_jenkins() {
18     # Usage: minion_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     MINION="${1}"
22
23     minions=()
24     for jenkins in "${@:2}"; do
25         JENKINS_URL="$jenkins/computer/api/json?tree=computer[displayName]"
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 [ "$status" != 200 ]; then
31             >&2 echo "ERROR: Failed to fetch data from $JENKINS_URL with status code $status"
32             >&2 echo "$resp"
33             exit 1
34         fi
35
36         # We purposely want to wordsplit here to combine the arrays
37         # shellcheck disable=SC2206,SC2207
38         minions=(${minions[@]} $(echo "$json_data" | \
39             jq -r '.computer[].displayName' | grep -v master)
40         )
41     done
42
43     if [[ "${minions[*]}" =~ $MINION ]]; then
44         return 0
45     fi
46
47     return 1
48 }
49
50 # shellcheck disable=SC1090
51 source ~/lf-env.sh
52
53 # Check if openstack venv was previously created
54 if [ -f "/tmp/.os_lf_venv" ]; then
55     os_lf_venv=$(cat "/tmp/.os_lf_venv")
56 fi
57
58 if [ -d "${os_lf_venv}" ] && [ -f "${os_lf_venv}/bin/openstack" ]; then
59     echo "Re-use existing venv: ${os_lf_venv}"
60     PATH=$os_lf_venv/bin:$PATH
61 else
62     lf-activate-venv --python python3 "cryptography<3.4" \
63         "lftools[openstack]" \
64         kubernetes \
65         "niet~=1.4.2" \
66         python-heatclient \
67         python-openstackclient \
68         python-magnumclient \
69         setuptools \
70         "openstacksdk<0.99" \
71         yq
72 fi
73 ##########################
74 ## FETCH ACTIVE MINIONS ##
75 ##########################
76 # Fetch server list before fetching active minions to minimize race condition
77 # where we might be trying to delete servers while jobs are trying to start
78
79 mapfile -t OS_SERVERS < <(openstack --os-cloud "$os_cloud" server list -f value -c "Name" | grep -E 'prd|snd')
80
81 echo "-----> Active servers"
82 for server in "${OS_SERVERS[@]}"; do
83     echo "$server"
84 done
85
86
87 #############################
88 ## DELETE ORPHANED SERVERS ##
89 #############################
90 echo "-----> Delete orphaned servers"
91
92 # Search for servers not in use by any active Jenkins systems and remove them.
93 for server in "${OS_SERVERS[@]}"; do
94     # jenkins_urls intentially needs globbing to be passed a separate params.
95     # needs to be globbed.
96     # shellcheck disable=SC2153,SC2086
97     if minion_in_jenkins "$server" $jenkins_urls; then
98         # No need to delete server if it is still attached to Jenkins
99         continue
100     else
101         echo "Deleting orphaned server: $server"
102         lftools openstack --os-cloud "$os_cloud" \
103             server remove --minutes 15 "$server"
104     fi
105 done