Merge "Add 6Wind quagga to ubuntu16.04 and centos7 images"
[releng/builder.git] / jjb / opendaylight-infra-stack.sh
1 #!/bin/bash
2 virtualenv "$WORKSPACE/.venv-openstack"
3 # shellcheck disable=SC1090
4 source "$WORKSPACE/.venv-openstack/bin/activate"
5 pip install --upgrade pip
6 pip install --upgrade python-openstackclient python-heatclient
7 pip freeze
8
9 cd /builder/openstack-hot || exit 1
10
11 JOB_SUM=$(echo "$JOB_NAME" | sum | awk '{{ print $1 }}')
12 VM_NAME="$JOB_SUM-$BUILD_NUMBER"
13
14 OS_TIMEOUT=10  # Minutes to wait for OpenStack VM to come online
15 STACK_RETRIES=3  # Number of times to retry creating a stack before fully giving up
16 STACK_SUCCESSFUL=false
17 # seq X refers to waiting for X minutes for OpenStack to return
18 # a status that is not CREATE_IN_PROGRESS before giving up.
19 openstack limits show --absolute
20 openstack limits show --rate
21 echo "Trying up to $STACK_RETRIES times to create $STACK_NAME."
22 for try in $(seq $STACK_RETRIES); do
23     # shellcheck disable=SC1083
24     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"
25     echo "$try: Waiting for $OS_TIMEOUT minutes to create $STACK_NAME."
26     for i in $(seq $OS_TIMEOUT); do
27         sleep 60
28         OS_STATUS=$(openstack stack show -f json -c stack_status "$STACK_NAME" | jq -r '.stack_status')
29         echo "$i: $OS_STATUS"
30
31         case "$OS_STATUS" in
32             CREATE_COMPLETE)
33                 echo "Stack initialized on infrastructure successful."
34                 STACK_SUCCESSFUL=true
35                 break
36             ;;
37             CREATE_FAILED)
38                 echo "ERROR: Failed to initialize infrastructure. Deleting stack and possibly retrying to create..."
39                 openstack stack delete --yes "$STACK_NAME"
40                 openstack stack show "$STACK_NAME"
41                 # after stack delete, poll for 10m to know when stack is fully removed
42                 # the logic here is that when "stack show $STACK_NAME" does not contain $STACK_NAME
43                 # we assume it's successfully deleted and we can break to retry
44                 for j in $(seq 20); do
45                     sleep 30;
46                     STACK_SHOW=$(openstack stack show "$STACK_NAME")
47                     echo "$j: $STACK_SHOW"
48                     if [[ $STACK_SHOW == *"DELETE_FAILED"* ]]; then
49                         echo "stack delete failed. trying to stack abandon now"
50                         openstack stack abandon "$STACK_NAME"
51                         STACK_SHOW=$(openstack stack show "$STACK_NAME")
52                         echo "$STACK_SHOW"
53                     fi
54                     if [[ $STACK_SHOW != *"$STACK_NAME"* ]]; then
55                         echo "stack show on $STACK_NAME came back empty. Assuming successful delete"
56                         break
57                     fi
58                 done
59                 # if we still see $STACK_NAME in $STACK_SHOW it means the delete hasn't fully
60                 # worked and we can exit forcefully
61                 if [[ $STACK_SHOW == *"$STACK_NAME"* ]]; then
62                     echo "stack $STACK_NAME still in stack show output after polling. Quitting!"
63                     exit 1
64                 fi
65                 break
66             ;;
67             CREATE_IN_PROGRESS)
68                 echo "Waiting to initialize infrastructure."
69                 continue
70             ;;
71             *)
72                 echo "Unexpected status: $OS_STATUS"
73                 exit 1
74             ;;
75         esac
76     done
77     if $STACK_SUCCESSFUL; then
78         break
79     fi
80 done
81
82 # capture stack info in console logs
83 openstack stack show "$STACK_NAME"
84
85 if ! $STACK_SUCCESSFUL; then
86     exit 1
87 fi