Merge "Add jenkins option to set custom debug levels"
[releng/builder.git] / jjb / opendaylight-infra-stack.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 freeze
18
19 cd /builder/openstack-hot || exit 1
20
21 JOB_SUM=$(echo "$JOB_NAME" | sum | awk '{{ print $1 }}')
22 VM_NAME="$JOB_SUM-$BUILD_NUMBER"
23
24 OS_TIMEOUT=10  # Minutes to wait for OpenStack VM to come online
25 STACK_RETRIES=3  # Number of times to retry creating a stack before fully giving up
26 STACK_SUCCESSFUL=false
27 # seq X refers to waiting for X minutes for OpenStack to return
28 # a status that is not CREATE_IN_PROGRESS before giving up.
29 openstack limits show --absolute
30 openstack limits show --rate
31 echo "Trying up to $STACK_RETRIES times to create $STACK_NAME."
32 for try in $(seq $STACK_RETRIES); do
33     # shellcheck disable=SC1083
34     openstack stack create --timeout "$OS_TIMEOUT" -t {stack-template} -e "$WORKSPACE/opendaylight-infra-environment.yaml" --parameter "job_name=$VM_NAME" --parameter "silo=$SILO" "$STACK_NAME"
35     echo "$try: Waiting for $OS_TIMEOUT minutes to create $STACK_NAME."
36     for i in $(seq $OS_TIMEOUT); do
37         sleep 60
38         OS_STATUS=$(openstack stack show -f json -c stack_status "$STACK_NAME" | jq -r '.stack_status')
39         echo "$i: $OS_STATUS"
40
41         case "$OS_STATUS" in
42             CREATE_COMPLETE)
43                 echo "Stack initialized on infrastructure successful."
44                 STACK_SUCCESSFUL=true
45                 break
46             ;;
47             CREATE_FAILED)
48                 echo "ERROR: Failed to initialize infrastructure. Deleting stack and possibly retrying to create..."
49                 openstack stack delete --yes "$STACK_NAME"
50                 openstack stack show "$STACK_NAME"
51                 # after stack delete, poll for 10m to know when stack is fully removed
52                 # the logic here is that when "stack show $STACK_NAME" does not contain $STACK_NAME
53                 # we assume it's successfully deleted and we can break to retry
54                 for j in $(seq 20); do
55                     sleep 30;
56                     STACK_SHOW=$(openstack stack show "$STACK_NAME")
57                     echo "$j: $STACK_SHOW"
58                     if [[ $STACK_SHOW == *"DELETE_FAILED"* ]]; then
59                         echo "stack delete failed. trying to stack abandon now"
60                         # stack abandon does not work on RS, therefore requires acquiring a token
61                         # and using http delete method to abondon DELETE_FAILED stacks
62                         # Todo: remove the change once RS fixes the issue upstream
63                         # openstack stack abandon "$STACK_NAME"
64                         STACK_ID=$(openstack stack show -f json -c "id" "$STACK_NAME" | jq -r '."id"')
65                         TOKEN=$(openstack token issue -f json -c id | jq -r '.id')
66                         curl -si -X DELETE -H "Content-Type: application/json" -H "Accept: application/json"\
67                             -H "x-auth-token: $TOKEN"\
68                             "https://dfw.orchestration.api.rackspacecloud.com/v1/904885/stacks/$STACK_NAME/$STACK_ID/abandon"
69                         STACK_SHOW=$(openstack stack show "$STACK_NAME")
70                         echo "$STACK_SHOW"
71                     fi
72                     if [[ $STACK_SHOW != *"$STACK_NAME"* ]]; then
73                         echo "stack show on $STACK_NAME came back empty. Assuming successful delete"
74                         break
75                     fi
76                 done
77                 # if we still see $STACK_NAME in $STACK_SHOW it means the delete hasn't fully
78                 # worked and we can exit forcefully
79                 if [[ $STACK_SHOW == *"$STACK_NAME"* ]]; then
80                     echo "stack $STACK_NAME still in stack show output after polling. Quitting!"
81                     exit 1
82                 fi
83                 break
84             ;;
85             CREATE_IN_PROGRESS)
86                 echo "Waiting to initialize infrastructure."
87                 continue
88             ;;
89             *)
90                 echo "Unexpected status: $OS_STATUS"
91                 # DO NOT exit on unexpected status. Rackspace sometimes returns unexpected status
92                 # before returning an expected status. Just print the message and loop until we have
93                 # a confirmed state or timeout.
94                 # exit 1
95             ;;
96         esac
97     done
98     if $STACK_SUCCESSFUL; then
99         break
100     fi
101 done
102
103 # capture stack info in console logs
104 openstack stack show "$STACK_NAME"
105
106 if ! $STACK_SUCCESSFUL; then
107     exit 1
108 fi