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 ##############################################################################
12 virtualenv "/tmp/v/openstack"
13 # shellcheck source=/tmp/v/openstack/bin/activate disable=SC1091
14 source "/tmp/v/openstack/bin/activate"
15 pip install --upgrade pip
16 pip install --upgrade python-openstackclient python-heatclient
17 pip install --upgrade pipdeptree
20 #########################
21 ## FETCH ACTIVE BUILDS ##
22 #########################
23 # Fetch stack list before fetching active builds to minimize race condition
24 # where we might be try to delete stacks while jobs are trying to start
25 OS_STACKS=($(openstack stack list \
26 -f json -c "Stack Name" -c "Stack Status" \
27 --property "stack_status=CREATE_COMPLETE" \
28 --property "stack_status=DELETE_FAILED" \
29 --property "stack_status=CREATE_FAILED" \
30 | jq -r '.[] | ."Stack Name"'))
32 # Make sure we fetch active builds on both the releng and sandbox silos
34 for silo in releng sandbox; do
35 JENKINS_URL="https://jenkins.opendaylight.org/$silo//computer/api/json?tree=computer[executors[currentExecutable[url]],oneOffExecutors[currentExecutable[url]]]&xpath=//url&wrapper=builds"
36 wget -nv -O "${silo}_builds.json" "$JENKINS_URL"
37 sleep 1 # Need to sleep for 1 second otherwise next line causes script to stall
38 ACTIVE_BUILDS=(${ACTIVE_BUILDS[@]} $( \
39 jq -r '.computer[].executors[].currentExecutable.url' "${silo}_builds.json" \
40 | grep -v null | awk -F'/' '{print $4 "-" $6 "-" $7}'))
43 ##########################
44 ## DELETE UNUSED STACKS ##
45 ##########################
46 # Search for stacks that are not in use by either releng or sandbox silos and
48 for STACK_NAME in "${OS_STACKS[@]}"; do
49 STACK_STATUS=$(openstack stack show -f json -c "stack_status" "$STACK_NAME" | jq -r '."stack_status"')
50 if [[ "${ACTIVE_BUILDS[*]}" =~ $STACK_NAME ]]; then
51 # No need to delete stacks if there exists an active build for them
54 case "$STACK_STATUS" in
56 echo "skipping delete, $STACK_NAME is already DELETE in progress."
60 echo "Stack delete failed, trying to stack abandon now."
61 # stack abandon does not work on RS, therefore requires acquiring a token
62 # and using http delete method to abondon DELETE_FAILED stacks
63 # Todo: remove the change once RS fixes the issue upstream
64 # openstack stack abandon "$STACK_NAME"
65 STACK_ID=$(openstack stack show -f json -c "id" "$STACK_NAME" | jq -r '."id"')
66 TOKEN=$(openstack token issue -f json -c id | jq -r '.id')
67 curl -si -X DELETE -H "Content-Type: application/json" -H "Accept: application/json"\
68 -H "x-auth-token: $TOKEN"\
69 "https://dfw.orchestration.api.rackspacecloud.com/v1/904885/stacks/$STACK_NAME/$STACK_ID/abandon"
70 STACK_SHOW=$(openstack stack show "$STACK_NAME")
74 CREATE_COMPLETE|CREATE_FAILED)
75 echo "Deleting orphaned stack: $STACK_NAME"
76 openstack stack delete --yes "$STACK_NAME"