- case "$OS_STATUS" in
- CREATE_COMPLETE)
- echo "Stack initialized on infrastructure successful."
- break
- ;;
- CREATE_FAILED)
- echo "ERROR: Failed to initialize infrastructure. Quitting..."
- exit 1
- ;;
- CREATE_IN_PROGRESS)
- echo "Waiting to initialize infrastructure."
- continue
- ;;
- *)
- echo "Unexpected status: $OS_STATUS"
- exit 1
- ;;
- esac
+ case "$OS_STATUS" in
+ CREATE_COMPLETE)
+ echo "Stack initialized on infrastructure successful."
+ STACK_SUCCESSFUL=true
+ break
+ ;;
+ CREATE_FAILED)
+ echo "ERROR: Failed to initialize infrastructure. Deleting stack and possibly retrying to create..."
+ openstack stack delete --yes "$STACK_NAME"
+ openstack stack show "$STACK_NAME"
+ # after stack delete, poll for 10m to know when stack is fully removed
+ # the logic here is that when "stack show $STACK_NAME" does not contain $STACK_NAME
+ # we assume it's successfully deleted and we can break to retry
+ for j in $(seq 20); do
+ sleep 30;
+ STACK_SHOW=$(openstack stack show "$STACK_NAME")
+ echo "$j: $STACK_SHOW"
+ if [[ $STACK_SHOW == *"DELETE_FAILED"* ]]; then
+ echo "stack delete failed. trying to stack abandon now"
+ openstack stack abandon "$STACK_NAME"
+ STACK_SHOW=$(openstack stack show "$STACK_NAME")
+ echo "$STACK_SHOW"
+ fi
+ if [[ $STACK_SHOW != *"$STACK_NAME"* ]]; then
+ echo "stack show on $STACK_NAME came back empty. Assuming successful delete"
+ break
+ fi
+ done
+ # if we still see $STACK_NAME in $STACK_SHOW it means the delete hasn't fully
+ # worked and we can exit forcefully
+ if [[ $STACK_SHOW == *"$STACK_NAME"* ]]; then
+ echo "stack $STACK_NAME still in stack show output after polling. Quitting!"
+ exit 1
+ fi
+ break
+ ;;
+ CREATE_IN_PROGRESS)
+ echo "Waiting to initialize infrastructure."
+ continue
+ ;;
+ *)
+ echo "Unexpected status: $OS_STATUS"
+ exit 1
+ ;;
+ esac
+ done
+ if $STACK_SUCCESSFUL; then
+ break
+ fi