Use -m pip from python instead of pip directly
[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 PYTHON="$WORKSPACE/.venv-openstack/bin/python"
6 OPENSTACK="$WORKSPACE/.venv-openstack/bin/openstack"
7 $PYTHON -m pip install --upgrade pip
8 $PYTHON -m pip install --upgrade python-openstackclient python-heatclient
9 $PYTHON -m pip freeze
10
11 cd /builder/openstack-hot || exit 1
12
13 JOB_SUM=$(echo "$JOB_NAME" | sum | awk '{{ print $1 }}')
14 VM_NAME="$JOB_SUM-$BUILD_NUMBER"
15
16 OS_TIMEOUT=10  # Minutes to wait for OpenStack VM to come online
17 STACK_RETRIES=3  # Number of times to retry creating a stack before fully giving up
18 STACK_SUCCESSFUL=false
19 # seq X refers to waiting for X minutes for OpenStack to return
20 # a status that is not CREATE_IN_PROGRESS before giving up.
21 $PYTHON $OPENSTACK limits show --absolute
22 $PYTHON $OPENSTACK limits show --rate
23 echo "Trying up to $STACK_RETRIES times to create $STACK_NAME."
24 for try in $(seq $STACK_RETRIES); do
25     # shellcheck disable=SC1083
26     $PYTHON $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"
27     echo "$try: Waiting for $OS_TIMEOUT minutes to create $STACK_NAME."
28     for i in $(seq $OS_TIMEOUT); do
29         sleep 60
30         OS_STATUS=$($PYTHON $OPENSTACK stack show -f json -c stack_status "$STACK_NAME" | jq -r '.stack_status')
31         echo "$i: $OS_STATUS"
32
33         case "$OS_STATUS" in
34             CREATE_COMPLETE)
35                 echo "Stack initialized on infrastructure successful."
36                 STACK_SUCCESSFUL=true
37                 break
38             ;;
39             CREATE_FAILED)
40                 echo "ERROR: Failed to initialize infrastructure. Deleting stack and possibly retrying to create..."
41                 $PYTHON $OPENSTACK stack delete --yes "$STACK_NAME"
42                 $PYTHON $OPENSTACK stack show "$STACK_NAME"
43                 # after stack delete, poll for 10m to know when stack is fully removed
44                 # the logic here is that when "stack show $STACK_NAME" does not contain $STACK_NAME
45                 # we assume it's successfully deleted and we can break to retry
46                 for j in $(seq 20); do
47                     sleep 30;
48                     STACK_SHOW=$($PYTHON $OPENSTACK stack show "$STACK_NAME")
49                     echo "$j: $STACK_SHOW"
50                     if [[ $STACK_SHOW == *"DELETE_FAILED"* ]]; then
51                         echo "stack delete failed. trying to stack abandon now"
52                         # stack abandon does not work on RS, therefore requires acquiring a token
53                         # and using http delete method to abondon DELETE_FAILED stacks
54                         # Todo: remove the change once RS fixes the issue upstream
55                         # openstack stack abandon "$STACK_NAME"
56                         STACK_ID=$($PYTHON $OPENSTACK stack show -f json -c "id" "$STACK_NAME" | jq -r '."id"')
57                         TOKEN=$($PYTHON $OPENSTACK token issue -f json -c id | jq -r '.id')
58                         curl -si -X DELETE -H "Content-Type: application/json" -H "Accept: application/json"\
59                             -H "x-auth-token: $TOKEN"\
60                             "https://dfw.orchestration.api.rackspacecloud.com/v1/904885/stacks/$STACK_NAME/$STACK_ID/abandon"
61                         STACK_SHOW=$($PYTHON $OPENSTACK stack show "$STACK_NAME")
62                         echo "$STACK_SHOW"
63                     fi
64                     if [[ $STACK_SHOW != *"$STACK_NAME"* ]]; then
65                         echo "stack show on $STACK_NAME came back empty. Assuming successful delete"
66                         break
67                     fi
68                 done
69                 # if we still see $STACK_NAME in $STACK_SHOW it means the delete hasn't fully
70                 # worked and we can exit forcefully
71                 if [[ $STACK_SHOW == *"$STACK_NAME"* ]]; then
72                     echo "stack $STACK_NAME still in stack show output after polling. Quitting!"
73                     exit 1
74                 fi
75                 break
76             ;;
77             CREATE_IN_PROGRESS)
78                 echo "Waiting to initialize infrastructure."
79                 continue
80             ;;
81             *)
82                 echo "Unexpected status: $OS_STATUS"
83                 # DO NOT exit on unexpected status. Rackspace sometimes returns unexpected status
84                 # before returning an expected status. Just print the message and loop until we have
85                 # a confirmed state or timeout.
86                 # exit 1
87             ;;
88         esac
89     done
90     if $STACK_SUCCESSFUL; then
91         break
92     fi
93 done
94
95 # capture stack info in console logs
96 $PYTHON $OPENSTACK stack show "$STACK_NAME"
97
98 if ! $STACK_SUCCESSFUL; then
99     exit 1
100 fi