a9fa8924475cb2a4873f811c8bbb6fad9a523655
[infra/cicd.git] / jjb / nolabs / handle-booking.sh
1 #!/bin/bash
2 set -o errexit
3 set -o nounset
4 set -o pipefail
5
6 cd $WORKSPACE
7
8 # install dependencies
9 sudo add-apt-repository "deb http://archive.ubuntu.com/ubuntu $(lsb_release -sc) main universe restricted multiverse" > /dev/null
10 sudo apt install -y virtualenv jq > /dev/null
11 /bin/rm -rf $NOLABS_VENV && mkdir -p $NOLABS_VENV
12 virtualenv -p python3 $NOLABS_VENV > /dev/null
13 set +u
14 source $NOLABS_VENV/bin/activate > /dev/null
15 set -u
16 pip install httpie > /dev/null
17
18 # check if the booking is cancelled or not
19 echo "API Request: http --ignore-stdin GET $NOLABS_API_BOOKINGS_ENDPOINT/$BOOKING_ID | jq -r '.status_text'"
20 BOOKING_STATUS=$(http --ignore-stdin GET $NOLABS_API_BOOKINGS_ENDPOINT/$BOOKING_ID | jq -r '.status_text')
21 if [[ $BOOKING_STATUS != 'new' ]]; then
22   echo "Info: Booking is either cancelled or not valid! Exiting!"
23   exit 0;
24 fi
25
26 # get authentication token
27 echo "API Request: http --ignore-stdin POST $NOLABS_API_TOKENS_ENDPOINT --auth dummy@dummy.com:dummy | jq -r '.token'"
28 NOLABS_TOKEN=$(http --ignore-stdin POST $NOLABS_API_TOKENS_ENDPOINT --auth $NOLABS_API_USERNAME:$NOLABS_API_PASSWORD | jq -r '.token')
29 echo "API Request: http --ignore-stdin PUT $NOLABS_API_BOOKINGS_ENDPOINT/$BOOKING_ID \"status_text=deploying\" \"Authorization:Bearer DUMMY_TOKEN\""
30 http --ignore-stdin PUT $NOLABS_API_BOOKINGS_ENDPOINT/$BOOKING_ID "status_text=deploying" \
31     "Authorization:Bearer $NOLABS_TOKEN"
32
33 # get ssh public key of the user
34 echo "API Request: http GET $NOLABS_API_BOOKINGS_ENDPOINT/$BOOKING_ID | jq -r '.user_id'"
35 NOLABS_USER_ID=$(http GET $NOLABS_API_BOOKINGS_ENDPOINT/$BOOKING_ID | jq -r '.user_id')
36 echo "API Request: http --ignore-stdin GET $NOLABS_API_USERS_ENDPOINT/$NOLABS_USER_ID \"Authorization:Bearer DUMMY_TOKEN\" | jq -r '.ssh_public_key'"
37 http --ignore-stdin GET $NOLABS_API_USERS_ENDPOINT/$NOLABS_USER_ID "Authorization:Bearer $NOLABS_TOKEN" | jq -r '.ssh_public_key' > $NOLABS_USER_SSHPUBKEY
38 echo "-------------------------------------------------"
39 cat $NOLABS_USER_SSHPUBKEY
40 echo "-------------------------------------------------"
41
42 set +u
43 deactivate
44 set -u
45 # get the specific patch
46 cd $WORKSPACE
47 git fetch "https://gerrit.nordix.org/infra/engine" $GERRIT_REFSPEC && git checkout FETCH_HEAD
48
49 export STACK_NAME="nordix-nolabs-booking${BOOKING_ID}-${DEPLOY_SCENARIO}"
50 echo "Info: STACK_NAME is set to $STACK_NAME"
51 cd engine
52 ./deploy.sh -c -r heat -u /tmp/openrc -s $DEPLOY_SCENARIO -e "$HEAT_ENVIRONMENT_FILE"
53
54 cd $WORKSPACE
55
56 set +u
57 source $NOLABS_VENV/bin/activate > /dev/null
58 set -u
59 ENGINE_VERSION=$(git rev-parse HEAD)
60 cd $WORKSPACE/.cache/repos/swconfig
61 SCENARIO_VERSION=$(git rev-parse HEAD)
62 cd $WORKSPACE
63 FLOATING_IP=$(cat .cache/config/inventory.ini | grep 'jumphost ansible_host' | awk '{print $2}' | cut -d'=' -f2)
64
65 # get authentication token
66 echo "API Request: http --ignore-stdin POST $NOLABS_API_TOKENS_ENDPOINT --auth dummy@dummy.com:dummy | jq -r '.token'"
67 NOLABS_TOKEN=$(http --ignore-stdin POST $NOLABS_API_TOKENS_ENDPOINT --auth $NOLABS_API_USERNAME:$NOLABS_API_PASSWORD | jq -r '.token')
68
69 # update booking
70 echo "API Request: http --ignore-stdin PUT $NOLABS_API_BOOKINGS_ENDPOINT/$BOOKING_ID \
71     \"Authorization:Bearer DUMMY_TOKEN\" \
72     status_text=active \
73     floating_ip=$FLOATING_IP \
74     heat_stack_name=$STACK_NAME \
75     scenario_deploy_log_url=${BUILD_URL}consoleFull \
76     engine_version=$ENGINE_VERSION \
77     scenario_version=$SCENARIO_VERSION"
78 http --ignore-stdin PUT $NOLABS_API_BOOKINGS_ENDPOINT/$BOOKING_ID \
79     "Authorization:Bearer $NOLABS_TOKEN" \
80     status_text=active \
81     floating_ip=$FLOATING_IP \
82     heat_stack_name=$STACK_NAME \
83     scenario_deploy_log_url=${BUILD_URL}consoleFull \
84     engine_version=$ENGINE_VERSION \
85     scenario_version=$SCENARIO_VERSION