Create jobs to handle and delete nolabs bookings

Change-Id: Ic0420e916cdf5d09793cb644162ff1d19f5d2b92
diff --git a/jjb/nolabs/booking-job-macros.yaml b/jjb/nolabs/booking-job-macros.yaml
new file mode 100644
index 0000000..4fc20d3
--- /dev/null
+++ b/jjb/nolabs/booking-job-macros.yaml
@@ -0,0 +1,41 @@
+---
+#
+# ============LICENSE_START=======================================================
+#  Copyright (C) 2019 Nordix Foundation.
+# ================================================================================
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+# SPDX-License-Identifier: Apache-2.0
+# ============LICENSE_END=========================================================
+
+- builder:
+    name: 'handlebooking-macro'
+    builders:
+      - shell:
+          !include-raw: ../cloud-infra/wait-for-pkg-manager.sh
+      - shell: |
+          #!/bin/bash
+          cp $OPENRC_FILE /tmp/openrc
+      - shell:
+          !include-raw: ./handle-booking.sh
+
+- builder:
+    name: 'deletebooking-macro'
+    builders:
+      - shell:
+          !include-raw: ../cloud-infra/wait-for-pkg-manager.sh
+      - shell: |
+          #!/bin/bash
+          cp $OPENRC_FILE /tmp/openrc
+      - shell:
+          !include-raw: ./delete-booking.sh
diff --git a/jjb/nolabs/delete-booking.sh b/jjb/nolabs/delete-booking.sh
new file mode 100644
index 0000000..5dcea5b
--- /dev/null
+++ b/jjb/nolabs/delete-booking.sh
@@ -0,0 +1,30 @@
+#!/bin/bash
+
+cd $WORKSPACE
+
+# install dependencies
+sudo apt install -y virtualenv jq > /dev/null
+/bin/rm -rf $NOLABS_VENV && mkdir -p $NOLABS_VENV
+virtualenv -p python3 $NOLABS_VENV > /dev/null
+source $NOLABS_VENV/bin/activate > /dev/null
+pip install httpie python-openstackclient python-heatclient python-novaclient > /dev/null
+source /tmp/openrc
+
+export STACK_NAME="nordix-nolabs-booking${BOOKING_ID}-${DEPLOY_SCENARIO}"
+echo "Info: STACK_NAME is set to $STACK_NAME"
+echo "Info: Deleting stack $STACK_NAME"
+echo "-------------------------------------------------"
+echo openstack stack delete -y --wait $STACK_NAME
+openstack stack delete -y --wait $STACK_NAME
+echo "-------------------------------------------------"
+
+# get authentication token
+echo "API Request: http --ignore-stdin POST http://188.212.108.245/api/tokens --auth dummy@dummy.com:dummy | jq -r '.token'"
+NOLABS_TOKEN=$(http --ignore-stdin POST http://188.212.108.245/api/tokens --auth $NOLABS_API_USERNAME:$NOLABS_API_PASSWORD | jq -r '.token')
+
+# update booking status
+echo "API Request: http --ignore-stdin PUT $NOLABS_API_BOOKINGS_ENDPOINT/$BOOKING_ID \"status_text=deploying\" \"Authorization:Bearer DUMMY_TOKEN\""
+http --ignore-stdin PUT $NOLABS_API_BOOKINGS_ENDPOINT/$BOOKING_ID \
+	"Authorization:Bearer $NOLABS_TOKEN" \
+	status_text=expired \
+	floating_ip="N/A"
diff --git a/jjb/nolabs/handle-booking.sh b/jjb/nolabs/handle-booking.sh
new file mode 100644
index 0000000..1563010
--- /dev/null
+++ b/jjb/nolabs/handle-booking.sh
@@ -0,0 +1,76 @@
+#!/bin/bash
+
+cd $WORKSPACE
+
+# install dependencies
+sudo apt install -y virtualenv jq > /dev/null
+/bin/rm -rf $NOLABS_VENV && mkdir -p $NOLABS_VENV
+virtualenv -p python3 $NOLABS_VENV > /dev/null
+source $NOLABS_VENV/bin/activate > /dev/null
+pip install httpie > /dev/null
+
+# check if the booking is cancelled or not
+echo "API Request: http --ignore-stdin GET $NOLABS_API_BOOKINGS_ENDPOINT/$BOOKING_ID | jq -r '.status_text"
+BOOKING_STATUS=$(http --ignore-stdin GET $NOLABS_API_BOOKINGS_ENDPOINT/$BOOKING_ID | jq -r '.status_text')
+if [[ $BOOKING_STATUS != 'new' ]]; then
+  echo "Info: Booking is either cancelled or not valid! Exiting!"
+  exit 0;
+fi
+
+# get authentication token
+echo "API Request: http --ignore-stdin POST http://188.212.108.245/api/tokens --auth dummy@dummy.com:dummy | jq -r '.token'"
+NOLABS_TOKEN=$(http --ignore-stdin POST http://188.212.108.245/api/tokens --auth $NOLABS_API_USERNAME:$NOLABS_API_PASSWORD | jq -r '.token')
+echo "API Request: http --ignore-stdin PUT $NOLABS_API_BOOKINGS_ENDPOINT/$BOOKING_ID \"status_text=deploying\" \"Authorization:Bearer DUMMY_TOKEN\""
+http --ignore-stdin PUT $NOLABS_API_BOOKINGS_ENDPOINT/$BOOKING_ID "status_text=deploying" \
+	"Authorization:Bearer $NOLABS_TOKEN"
+    
+# get ssh public key of the user
+echo "API Request: http GET $NOLABS_API_BOOKINGS_ENDPOINT/$BOOKING_ID | jq -r '.user_id'"
+NOLABS_USER_ID=$(http GET $NOLABS_API_BOOKINGS_ENDPOINT/$BOOKING_ID | jq -r '.user_id')
+echo "API Request: http --ignore-stdin GET $NOLABS_API_USERS_ENDPOINT/$NOLABS_USER_ID \"Authorization:Bearer DUMMY_TOKEN\" | jq -r '.ssh_public_key'"
+http --ignore-stdin GET $NOLABS_API_USERS_ENDPOINT/$NOLABS_USER_ID "Authorization:Bearer $NOLABS_TOKEN" | jq -r '.ssh_public_key' > $NOLABS_USER_SSHPUBKEY
+echo "-------------------------------------------------"
+cat $NOLABS_USER_SSHPUBKEY
+echo "-------------------------------------------------"
+
+deactivate
+# get the specific patch
+cd $WORKSPACE
+git fetch "https://gerrit.nordix.org/infra/engine" $GERRIT_REFSPEC && git checkout FETCH_HEAD
+
+export STACK_NAME="nordix-nolabs-booking${BOOKING_ID}-${DEPLOY_SCENARIO}"
+echo "Info: STACK_NAME is set to $STACK_NAME"
+cd engine
+./deploy.sh -c -r heat -u /tmp/openrc -s $DEPLOY_SCENARIO -e "$HEAT_ENVIRONMENT_FILE"
+
+cd $WORKSPACE
+
+source $NOLABS_VENV/bin/activate > /dev/null
+ENGINE_VERSION=$(git rev-parse HEAD)
+cd $WORKSPACE/.cache/repos/swconfig
+SCENARIO_VERSION=$(git rev-parse HEAD)
+cd $WORKSPACE
+#FLOATING_IP=$(cat .cache/config/inventory.ini | grep 'jumphost ansible_host' | awk '{print $2}' | cut -d'=' -f2)
+FLOATING_IP=10.10.10.10
+
+# get authentication token
+echo "API Request: http --ignore-stdin POST http://188.212.108.245/api/tokens --auth dummy@dummy.com:dummy | jq -r '.token'"
+NOLABS_TOKEN=$(http --ignore-stdin POST http://188.212.108.245/api/tokens --auth $NOLABS_API_USERNAME:$NOLABS_API_PASSWORD | jq -r '.token')
+
+# update booking
+echo "API Request: http --ignore-stdin PUT $NOLABS_API_BOOKINGS_ENDPOINT/$BOOKING_ID \
+	\"Authorization:Bearer DUMMY_TOKEN\" \
+	status_text=active \
+    floating_ip=$FLOATING_IP \
+    heat_stack_name=$STACK_NAME \
+    scenario_deploy_log_url=${BUILD_URL}consoleFull \
+    engine_version=$ENGINE_VERSION \
+    scenario_version=$SCENARIO_VERSION"
+http --ignore-stdin PUT $NOLABS_API_BOOKINGS_ENDPOINT/$BOOKING_ID \
+	"Authorization:Bearer $NOLABS_TOKEN" \
+	status_text=active \
+    floating_ip=$FLOATING_IP \
+    heat_stack_name=$STACK_NAME \
+    scenario_deploy_log_url=${BUILD_URL}consoleFull \
+    engine_version=$ENGINE_VERSION \
+    scenario_version=$SCENARIO_VERSION
diff --git a/jjb/nolabs/nolabs-booking-jobs.yaml b/jjb/nolabs/nolabs-booking-jobs.yaml
new file mode 100644
index 0000000..7a6e313
--- /dev/null
+++ b/jjb/nolabs/nolabs-booking-jobs.yaml
@@ -0,0 +1,170 @@
+---
+#
+# ============LICENSE_START=======================================================
+#  Copyright (C) 2019 Nordix Foundation.
+# ================================================================================
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+# SPDX-License-Identifier: Apache-2.0
+# ============LICENSE_END=========================================================
+
+#--------------------------------------------------------------------------------------
+# The jobs declared in this JJB configuration file are used for verifying patches
+# that are sent to the repositories below
+#   - cloud-infra/engine
+#
+# These jobs verify changes that
+#   - implement new features or fix bugs for the deployment framework, engine, itself
+#   - implement new features or fix bugs to the playbooks and roles used for utilizing
+#     open source components such as bifrost, kubespray, and kolla-ansible
+#   - bump versions of open source components or the scenarios
+#
+# These jobs are set to run on slaves with label vpod-deploy-ubuntu1604.
+# The slaves have
+#   - Ubuntu1604 as the base distro
+#   - nested virtualization
+#   - internet access
+#   - passwordless sudo
+#   - 16 CPUs/32GB RAM/200 GB Disk
+#   - docker, git vim curl wget zip unzip chrony apt-transport-https ca-certificates curl
+#     gnupg-agent software-properties-common docker-ce docker-ce-cli containerd.io openjdk-8-jre
+#
+# The slaves should not have a cloud infra scenario deployed on them.
+#--------------------------------------------------------------------------------------
+
+- project:
+    name: 'nolabs-booking'
+
+    project: 'infra/engine'
+
+    stream:
+      - 'master':
+          branch: '{stream}'
+          slave-label: 'vpod-deploy-ubuntu1804'
+          build-timeout: 60
+
+    booking-phase:
+      - 'handle'
+      - 'delete'
+
+    target-site:
+      - 'test':
+          api-users-endpoint: 'http://188.212.108.245/api/users'
+          api-bookings-endpoint: 'http://188.212.108.245/api/bookings'
+      - 'production':
+          api-users-endpoint: 'https://labs.nordix.org/api/users'
+          api-bookings-endpoint: 'https://labs.nordix.org/api/bookings'
+
+    jobs:
+      - 'nolabs-{booking-phase}-booking-{target-site}site-{stream}'
+
+- job-template:
+    name: 'nolabs-{booking-phase}-booking-{target-site}site-{stream}'
+
+    disabled: '{obj:disabled}'
+
+    concurrent: true
+
+    parameters:
+      - project-parameters:
+          project: '{project}'
+          branch: '{branch}'
+      - nordix-gerrit-parameters
+      - label:
+          name: TARGET_SITE
+          default: '{target-site}'
+          description: 'Target site to handle the bookings for.'
+      - label:
+          name: SLAVE_LABEL
+          default: '{slave-label}'
+          description: 'Slave label to schedule this job on. All slaves have Ubuntu1604 no matter what distro target nodes use.'
+      - string:
+          name: BOOKING_PHASE
+          default: '{booking-phase}'
+          description: 'Phase of the booking.'
+      - string:
+          name: BOOKING_ID
+          default: 0
+          description: 'ID of the booking to handle. Default is 0 which is a non-existent booking to prevent running the job by mistake.'
+      - string:
+          name: DEPLOY_SCENARIO
+          default: 'k8-mycoolplugin-mycoolfeature'
+          description: 'Scenario to deploy for the booking. Default is a non-existent scenario to prevent running the job by mistake.'
+      - string:
+          name: NOLABS_API_USERS_ENDPOINT
+          default: '{api-users-endpoint}'
+          description: 'API endpoint for users.'
+      - string:
+          name: NOLABS_API_BOOKINGS_ENDPOINT
+          default: '{api-bookings-endpoint}'
+          description: 'API endpoint for bookings.'
+      - string:
+          name: NOLABS_USER_SSHPUBKEY
+          default: '/tmp/nolabs/id_rsa.pub'
+          description: 'SSH public key for the user who created the booking.'
+      - string:
+          name: NOLABS_VENV
+          default: '/tmp/nolabs/.venv'
+          description: 'Python virtualenv location to use.'
+      - string:
+          name: GERRIT_REFSPEC
+          default: 'refs/changes/79/2079/8'
+          description: 'Refspec to checkout'
+      - string:
+          name: HEAT_ENVIRONMENT_FILE
+          default: 'https://gerrit.nordix.org/gitweb?p=infra/engine.git;a=blob_plain;f=engine/provisioner/heat/playbooks/roles/install-configure-heat/files/heat-environment.yaml;h=401e1c4f547e9a2631e68d800cbcadef81b85641;hb=70df134c93b7ca3232ae723c3de98ad2b0299728'
+          description: 'Using a special heat environment file until the change gets merged.'
+
+    properties:
+      - logrotate
+      - build-blocker:
+          use-build-blocker: true
+          blocking-jobs:
+            - 'cloud-infra-verify-engine-.*'
+            - 'cloud-infra-verify-scenario-.*'
+            - 'cloud-infra-periodic-engine-.*'
+            - 'k8-.*'
+            - 'nolabs-.*'
+          block-level: 'NODE'
+      - throttle:
+          max-per-node: 1
+          max-total: 3
+          option: project
+
+    wrappers:
+      - credentials-binding:
+          - file:
+              credential-id: openrc-city-nolabs-default-nordix.sh
+              variable: OPENRC_FILE
+          - username-password-separated:
+              username: NOLABS_API_USERNAME
+              password: NOLABS_API_PASSWORD
+      - mask-passwords
+      - build-timeout:
+          timeout: '{build-timeout}'
+      - fix-workspace-permissions
+      - openstack:
+          single-use: True
+
+    scm:
+      - git-scm:
+          ssh-credentials-id: nordixinfra-nordix-gerrit-ssh
+          branch: '{branch}'
+          refspec: ''
+          wipe_workspace: true
+          clean_before: false
+
+    builders:
+      - '{booking-phase}booking-macro'
+
+# vim: set ts=2 sw=2 expandtab: