45eaae9f4c5b13ea623af4584aed9b6f568ef597
[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                         # stack abandon does not work on RS, therefore requires acquiring a token
51                         # and using http delete method to abondon DELETE_FAILED stacks
52                         # Todo: remove the change once RS fixes the issue upstream
53                         # openstack stack abandon "$STACK_NAME"
54                         STACK_ID=$(openstack stack show -f json -c "id" "$STACK_NAME" | jq -r '."id"')
55                         TOKEN=$(openstack token issue -f json -c id | jq -r '.id')
56                         curl -si -X DELETE -H "Content-Type: application/json" -H "Accept: application/json"\
57                             -H "x-auth-token: $TOKEN"\
58                             "https://dfw.orchestration.api.rackspacecloud.com/v1/904885/stacks/$STACK_NAME/$STACK_ID/abandon"
59                         STACK_SHOW=$(openstack stack show "$STACK_NAME")
60                         echo "$STACK_SHOW"
61                     fi
62                     if [[ $STACK_SHOW != *"$STACK_NAME"* ]]; then
63                         echo "stack show on $STACK_NAME came back empty. Assuming successful delete"
64                         break
65                     fi
66                 done
67                 # if we still see $STACK_NAME in $STACK_SHOW it means the delete hasn't fully
68                 # worked and we can exit forcefully
69                 if [[ $STACK_SHOW == *"$STACK_NAME"* ]]; then
70                     echo "stack $STACK_NAME still in stack show output after polling. Quitting!"
71                     exit 1
72                 fi
73                 break
74             ;;
75             CREATE_IN_PROGRESS)
76                 echo "Waiting to initialize infrastructure."
77                 continue
78             ;;
79             *)
80                 echo "Unexpected status: $OS_STATUS"
81                 exit 1
82             ;;
83         esac
84     done
85     if $STACK_SUCCESSFUL; then
86         break
87     fi
88 done
89
90 # capture stack info in console logs
91 openstack stack show "$STACK_NAME"
92
93 if ! $STACK_SUCCESSFUL; then
94     exit 1
95 fi