Merge "Fix: Use lf-activate-venv to install openstack dep"
[releng/builder.git] / jjb / opendaylight-infra-stack.sh
1 #!/bin/sh -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 # shellcheck disable=SC1090
20 . ~/lf-env.sh
21
22 # Check if openstack venv was previously created
23 if [ -f "/tmp/.os_lf_venv" ]; then
24     os_lf_venv=$(cat "/tmp/.os_lf_venv")
25 fi
26
27 if [ -d "${os_lf_venv}" ] && [ -f "${os_lf_venv}/bin/openstack" ]; then
28     echo "Re-use existing venv: ${os_lf_venv}"
29     PATH=$os_lf_venv/bin:$PATH
30 else
31     lf-activate-venv --python python3 \
32         decorator \
33         python-heatclient \
34         python-openstackclient \
35         python-magnumclient \
36         yq
37 fi
38
39 JOB_SUM=$(echo "$JOB_NAME" | sum | awk '{{ print $1 }}')
40 VM_NAME="$JOB_SUM-$BUILD_NUMBER"
41
42 OS_TIMEOUT=15  # Minutes to wait for OpenStack VM to come online
43 STACK_RETRIES=2  # Number of times to retry creating a stack before fully giving up
44 STACK_SUCCESSFUL=false
45 # seq X refers to waiting for X minutes for OpenStack to return
46 # a status that is not CREATE_IN_PROGRESS before giving up.
47 openstack limits show --absolute
48 openstack limits show --rate
49 echo "Trying up to $STACK_RETRIES times to create $STACK_NAME."
50 for try in $(seq $STACK_RETRIES); do
51     # shellcheck disable=SC1083
52     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"
53     echo "$try: Waiting for $OS_TIMEOUT minutes to create $STACK_NAME."
54     for i in $(seq $OS_TIMEOUT); do
55         sleep 60
56         OS_STATUS=$(openstack stack show -f value -c stack_status "$STACK_NAME")
57         echo "$i: $OS_STATUS"
58
59         case "$OS_STATUS" in
60             CREATE_COMPLETE)
61                 echo "Stack initialized on infrastructure successful."
62                 STACK_SUCCESSFUL=true
63                 break
64             ;;
65             CREATE_FAILED)
66                 reason=$(openstack stack show "$STACK_NAME" -f value -c stack_status_reason)
67                 echo "ERROR: Failed to initialize infrastructure. Reason: $reason"
68                 openstack stack resource list -n 25 "$STACK_NAME"
69
70                 echo "Deleting stack and possibly retrying to create..."
71                 openstack stack delete --yes "$STACK_NAME"
72
73                 # after stack delete, poll for 10m to know when stack is fully removed
74                 # the logic here is that when "stack show $STACK_NAME" does not contain $STACK_NAME
75                 # we assume it's successfully deleted and we can break to retry
76                 for j in $(seq 20); do
77                     sleep 30
78                     delete_status=$(openstack stack show "$STACK_NAME" -f value -c stack_status)
79                     echo "$j: $delete_status"
80                     if [ "$delete_status" = "DELETE_FAILED" ]; then
81                         reason=$(openstack stack show "$STACK_NAME" -f value -c stack_status_reason)
82                         echo "ERROR: Failed to delete $STACK_NAME. Reason: $reason"
83
84                         # Abandon is not supported in Vexxhost so let's keep trying to
85                         # delete for now...
86                         # echo "Stack delete failed, trying to stack abandon now."
87                         # openstack stack abandon "$STACK_NAME"
88                         echo "Deleting failed stack: $STACK_NAME"
89                         openstack stack delete --yes "$STACK_NAME"
90                     fi
91
92                     if ! openstack stack show "$STACK_NAME" -f value -c stack_status; then
93                         echo "Stack show on $STACK_NAME came back empty. Assuming successful delete"
94                         break
95                     fi
96                 done
97
98                 # If we still see $STACK_NAME in `openstack stack show` it means the delete hasn't fully
99                 # worked and we can exit forcefully
100                 if openstack stack show "$STACK_NAME" -f value -c stack_status; then
101                     echo "Stack $STACK_NAME still in cloud output after polling. Quitting!"
102                     exit 1
103                 fi
104                 break
105             ;;
106             CREATE_IN_PROGRESS)
107                 echo "Waiting to initialize infrastructure."
108                 continue
109             ;;
110             *)
111                 echo "Unexpected status: $OS_STATUS"
112                 # DO NOT exit on unexpected status. Rackspace sometimes returns unexpected status
113                 # before returning an expected status. Just print the message and loop until we have
114                 # a confirmed state or timeout.
115                 # exit 1
116             ;;
117         esac
118     done
119     if $STACK_SUCCESSFUL; then
120         break
121     fi
122 done
123
124 # capture stack info in console logs
125 echo "------------------------------------"
126 echo "Stack details"
127 echo "------------------------------------"
128 openstack stack show "$STACK_NAME" -f yaml
129 echo "------------------------------------"
130
131 if ! $STACK_SUCCESSFUL; then
132     exit 1
133 fi