Chore: Update gerrit-review-action to latest v0.8
[releng/builder.git] / jjb / opendaylight-infra-stack.sh
1 #!/bin/bash -l
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 # TODO: Remove the if-statement once we have fully migrated to /opt/ciman
13 if [ -d "/opt/ciman/openstack-hot" ]; then
14     cd /opt/ciman/openstack-hot || exit 1
15 else
16     cd /builder/openstack-hot || exit 1
17 fi
18
19 # openstack cli is failing with the decorator package
20 sudo pip install --upgrade --quiet decorator
21
22 JOB_SUM=$(echo "$JOB_NAME" | sum | awk '{{ print $1 }}')
23 VM_NAME="$JOB_SUM-$BUILD_NUMBER"
24
25 OS_TIMEOUT=15  # Minutes to wait for OpenStack VM to come online
26 STACK_RETRIES=2  # Number of times to retry creating a stack before fully giving up
27 STACK_SUCCESSFUL=false
28 # seq X refers to waiting for X minutes for OpenStack to return
29 # a status that is not CREATE_IN_PROGRESS before giving up.
30 openstack limits show --absolute
31 openstack limits show --rate
32 echo "Trying up to $STACK_RETRIES times to create $STACK_NAME."
33 for try in $(seq $STACK_RETRIES); do
34     # shellcheck disable=SC1083
35     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"
36     echo "$try: Waiting for $OS_TIMEOUT minutes to create $STACK_NAME."
37     for i in $(seq $OS_TIMEOUT); do
38         sleep 60
39         OS_STATUS=$(openstack stack show -f value -c stack_status "$STACK_NAME")
40         echo "$i: $OS_STATUS"
41
42         case "$OS_STATUS" in
43             CREATE_COMPLETE)
44                 echo "Stack initialized on infrastructure successful."
45                 STACK_SUCCESSFUL=true
46                 break
47             ;;
48             CREATE_FAILED)
49                 reason=$(openstack stack show "$STACK_NAME" -f value -c stack_status_reason)
50                 echo "ERROR: Failed to initialize infrastructure. Reason: $reason"
51                 openstack stack resource list -n 25 "$STACK_NAME"
52
53                 echo "Deleting stack and possibly retrying to create..."
54                 openstack stack delete --yes "$STACK_NAME"
55
56                 # after stack delete, poll for 10m to know when stack is fully removed
57                 # the logic here is that when "stack show $STACK_NAME" does not contain $STACK_NAME
58                 # we assume it's successfully deleted and we can break to retry
59                 for j in $(seq 20); do
60                     sleep 30
61                     delete_status=$(openstack stack show "$STACK_NAME" -f value -c stack_status)
62                     echo "$j: $delete_status"
63                     if [[ $delete_status == "DELETE_FAILED" ]]; then
64                         reason=$(openstack stack show "$STACK_NAME" -f value -c stack_status_reason)
65                         echo "ERROR: Failed to delete $STACK_NAME. Reason: $reason"
66
67                         # Abandon is not supported in Vexxhost so let's keep trying to
68                         # delete for now...
69                         # echo "Stack delete failed, trying to stack abandon now."
70                         # openstack stack abandon "$STACK_NAME"
71                         echo "Deleting failed stack: $STACK_NAME"
72                         openstack stack delete --yes "$STACK_NAME"
73                     fi
74
75                     if ! openstack stack show "$STACK_NAME" -f value -c stack_status; then
76                         echo "Stack show on $STACK_NAME came back empty. Assuming successful delete"
77                         break
78                     fi
79                 done
80
81                 # If we still see $STACK_NAME in `openstack stack show` it means the delete hasn't fully
82                 # worked and we can exit forcefully
83                 if openstack stack show "$STACK_NAME" -f value -c stack_status; then
84                     echo "Stack $STACK_NAME still in cloud output after polling. Quitting!"
85                     exit 1
86                 fi
87                 break
88             ;;
89             CREATE_IN_PROGRESS)
90                 echo "Waiting to initialize infrastructure."
91                 continue
92             ;;
93             *)
94                 echo "Unexpected status: $OS_STATUS"
95                 # DO NOT exit on unexpected status. Rackspace sometimes returns unexpected status
96                 # before returning an expected status. Just print the message and loop until we have
97                 # a confirmed state or timeout.
98                 # exit 1
99             ;;
100         esac
101     done
102     if $STACK_SUCCESSFUL; then
103         break
104     fi
105 done
106
107 # capture stack info in console logs
108 echo "------------------------------------"
109 echo "Stack details"
110 echo "------------------------------------"
111 openstack stack show "$STACK_NAME" -f yaml
112 echo "------------------------------------"
113
114 if ! $STACK_SUCCESSFUL; then
115     exit 1
116 fi