Merge "Run pybot per suite and collect individual logs"
[releng/builder.git] / jjb / opendaylight-infra-cleanup-stale-stacks.sh
1 #!/bin/bash
2 # SPDX-License-Identifier: EPL-1.0
3 ##############################################################################
4 # Copyright (c) 2017 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
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
18 pipdeptree
19
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"'))
31
32 # Make sure we fetch active builds on both the releng and sandbox silos
33 ACTIVE_BUILDS=()
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}'))
41 done
42
43 ##########################
44 ## DELETE UNUSED STACKS ##
45 ##########################
46 # Search for stacks that are not in use by either releng or sandbox silos and
47 # delete them.
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
52         continue
53     else
54         case "$STACK_STATUS" in
55             DELETE_IN_PROGRESS)
56                 echo "skipping delete, $STACK_NAME is already DELETE in progress."
57                 continue
58             ;;
59             DELETE_FAILED)
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")
71                 echo "$STACK_SHOW"
72                 continue
73             ;;
74             CREATE_COMPLETE|CREATE_FAILED)
75                 echo "Deleting orphaned stack: $STACK_NAME"
76                 openstack stack delete --yes "$STACK_NAME"
77                 continue
78             ;;
79             *)
80                 continue
81             ;;
82         esac
83     fi
84 done