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