Add Kubernetes Functionality to launch QEMU containers

Made modifications to the qemu launching script to allow for kubernetes
integration. Added the default variables for a launch to the kubernetes
launch helper script. Generated the yaml file used to create the
deployment for QEMU. Commented out the testing portion of the
run-qemu-robot-test.sh script for now since the tests do not exist
anymore. This way script can still be used for launching QEMU instances.

Change-Id: I6ec5a33a0e6c84cd169894d931c5535a4e9329d7
Signed-off-by: Alanny Lopez <alanny.lopez@ibm.com>
diff --git a/kubernetes/Templates/QEMU-launch-deployment.yaml b/kubernetes/Templates/QEMU-launch-deployment.yaml
new file mode 100644
index 0000000..6016ddf
--- /dev/null
+++ b/kubernetes/Templates/QEMU-launch-deployment.yaml
@@ -0,0 +1,62 @@
+apiVersion: extensions/v1beta1
+kind: Deployment
+metadata:
+  name: ${deployname}
+  namespace: ${namespace}
+spec:
+  replicas: ${replicas}
+  selector:
+    matchLabels:
+      app: ${deployname}
+  template:
+    metadata:
+      labels:
+        app: ${deployname}
+    spec:
+      volumes:
+      - name: home
+        persistentVolumeClaim:
+          claimName: ${hclaim}
+      nodeSelector:
+        arch: ${ARCH}
+      securityContext:
+        runAsUser: 0
+      containers:
+      - image: ${imgname}
+        name: ${podname}
+        command: [\"${OBMC_BUILD_DIR}/boot-qemu.sh\"]
+        tty: true
+        workingDir: ${OBMC_BUILD_DIR}
+        env:
+        - name: QEMU_RUN_TIME
+          value: \"${QEMU_RUN_TIMER}\"
+        - name: QEMU_ARCH
+          value: \"${QEMU_ARCH}\"
+        - name: HOME
+          value: \"${OBMC_BUILD_DIR}\"
+        ports:
+        - containerPort: 443
+          name: https
+          protocol: TCP
+        - containerPort: 80
+          name: http
+          protocol: TCP
+        - containerPort: 4000
+          name: rest-api
+          protocol: TCP
+        - containerPort: 22
+          name: ssh
+          protocol: TCP
+        resources:
+          requests:
+            memory: "600Mi"
+            cpu: "400m"
+          limits:
+            memory: "1Gi"
+            cpu: "1000m"
+        volumeMounts:
+        - name: home
+          subPath: ${jenkins_subpath}
+          mountPath: ${OBMC_BUILD_DIR}
+      imagePullSecrets:
+      - name: ${imgplsec}
diff --git a/kubernetes/kubernetes-launch.sh b/kubernetes/kubernetes-launch.sh
index 600fc35..233f83f 100755
--- a/kubernetes/kubernetes-launch.sh
+++ b/kubernetes/kubernetes-launch.sh
@@ -73,6 +73,12 @@
     newimgname="${imgrepo}${imgname}"
     ;;
   QEMU-launch)
+    deployname=${deployname:-qemu-launch-deployment}
+    podname=${podname:-qemu-instance}
+    replicas=${replicas:-5}
+    hclaim=${hclaim:-jenkins}
+    jenkins_subpath=${jenkins_subpath:-workspace/Openbmc-Build/build}
+    newimgname="${imgrepo}${imgname}"
     ;;
   XCAT-launch)
     ;;
diff --git a/run-qemu-robot-test.sh b/run-qemu-robot-test.sh
index 32786b9..be87575 100755
--- a/run-qemu-robot-test.sh
+++ b/run-qemu-robot-test.sh
@@ -30,6 +30,12 @@
 #                       directory was changed in the build-setup.sh run, this
 #                       variable should also be changed. Otherwise, the default
 #                       should be used.
+#  LAUNCH             = Used to determine how to launch the qemu robot test
+#                       containers. The options are "local", and "k8s". It will
+#                       default to local which will launch a single container
+#                       to do the runs. If specified k8s will launch a group of
+#                       containers into a kubernetes cluster using the helper
+#                       script.
 #
 ###############################################################################
 
@@ -40,6 +46,7 @@
 DOCKER_IMG_NAME=${DOCKER_IMG_NAME:-openbmc/ubuntu-robot-qemu}
 OBMC_BUILD_DIR=${OBMC_BUILD_DIR:-/tmp/openbmc/build}
 UPSTREAM_WORKSPACE=${UPSTREAM_WORKSPACE:-${1}}
+LAUNCH=${LAUNCH:-local}
 
 # Determine the architecture
 ARCH=$(uname -m)
@@ -68,72 +75,88 @@
 # Copy the scripts to start and verify QEMU in the workspace
 cp $DIR/scripts/boot-qemu* ${UPSTREAM_WORKSPACE}
 
-# Move into the upstream workspace directory
-cd ${UPSTREAM_WORKSPACE}
+################################################################################
 
-# Start QEMU docker instance
-# root in docker required to open up the https/ssh ports
-obmc_qemu_docker=$(docker run --detach \
-                              --user root \
-                              --env HOME=${OBMC_BUILD_DIR} \
-                              --env QEMU_RUN_TIMER=${QEMU_RUN_TIMER} \
-                              --env QEMU_ARCH=${QEMU_ARCH} \
-                              --workdir "${OBMC_BUILD_DIR}"           \
-                              --volume "${UPSTREAM_WORKSPACE}":"${OBMC_BUILD_DIR}" \
-                              --tty \
-                              ${DOCKER_IMG_NAME} ${OBMC_BUILD_DIR}/boot-qemu-test.exp)
+if [[ ${LAUNCH} == "local" ]]; then
 
+  # Start QEMU docker instance
+  # root in docker required to open up the https/ssh ports
+  obmc_qemu_docker=$(docker run --detach \
+                                --user root \
+                                --env HOME=${OBMC_BUILD_DIR} \
+                                --env QEMU_RUN_TIMER=${QEMU_RUN_TIMER} \
+                                --env QEMU_ARCH=${QEMU_ARCH} \
+                                --workdir "${OBMC_BUILD_DIR}"           \
+                                --volume "${UPSTREAM_WORKSPACE}":"${OBMC_BUILD_DIR}" \
+                                --tty \
+                                ${DOCKER_IMG_NAME} ${OBMC_BUILD_DIR}/boot-qemu-test.exp)
 
-# We can use default ports because we're going to have the 2
-# docker instances talk over their private network
-DOCKER_SSH_PORT=22
-DOCKER_HTTPS_PORT=443
-DOCKER_QEMU_IP_ADDR="$(docker inspect $obmc_qemu_docker |  \
-                      grep -m 1 "IPAddress\":" | cut -d '"' -f 4)"
+  # We can use default ports because we're going to have the 2
+  # docker instances talk over their private network
+  DOCKER_SSH_PORT=22
+  DOCKER_HTTPS_PORT=443
+  DOCKER_QEMU_IP_ADDR="$(docker inspect $obmc_qemu_docker |  \
+                       grep -m 1 "IPAddress\":" | cut -d '"' -f 4)"
 
-# Now wait for the OpenBMC QEMU Docker instance to get to standby
-attempt=60
-while [ $attempt -gt 0 ]; do
+  #Now wait for the OpenBMC QEMU Docker instance to get to standby
+  attempt=60
+  while [ $attempt -gt 0 ]; do
     attempt=$(( $attempt - 1 ))
     echo "Waiting for qemu to get to standby (attempt: $attempt)..."
     result=$(docker logs $obmc_qemu_docker)
     if grep -q 'OPENBMC-READY' <<< $result ; then
-        echo "QEMU is ready!"
-        # Give QEMU a few secs to stablize
-        sleep 5
-        break
+      echo "QEMU is ready!"
+      # Give QEMU a few secs to stablize
+      sleep 5
+      break
     fi
-    sleep 2
-done
+      sleep 2
+  done
 
-if [ "$attempt" -eq 0 ]; then
+  if [ "$attempt" -eq 0 ]; then
     echo "Timed out waiting for QEMU, exiting"
     exit 1
+  fi
+
+  # Now run the Robot test (Tests commented out until they are working again)
+
+  # Timestamp for job
+  #echo "Robot Test started, $(date)"
+
+  #mkdir -p ${WORKSPACE}
+  #cd ${WORKSPACE}
+
+  # Copy in the script which will execute the Robot tests
+  #cp $DIR/scripts/run-robot.sh ${WORKSPACE}
+
+  # Run the Docker container to execute the Robot test cases
+  # The test results will be put in ${WORKSPACE}
+  #docker run --rm \
+  #           --user root \
+  #           --env HOME=${HOME} \
+  #           --env IP_ADDR=${DOCKER_QEMU_IP_ADDR} \
+  #           --env SSH_PORT=${DOCKER_SSH_PORT} \
+  #           --env HTTPS_PORT=${DOCKER_HTTPS_PORT} \
+  #           --workdir ${HOME} \
+  #           --volume ${WORKSPACE}:${HOME} \
+  #           --tty \
+  #           ${DOCKER_IMG_NAME} ${HOME}/run-robot.sh
+
+  # Now stop the QEMU Docker image
+  docker stop $obmc_qemu_docker
+
+elif [[ ${LAUNCH} == "k8s" ]]; then
+  # Package the Upstream into an image based off the one created by the build-qemu-robot.sh
+  # Dockerfile = $( cat << EOF
+  imgname=$DOCKER_IMG_NAME
+  cd $DIR
+  source ./kubernetes/kubernetes-launch.sh QEMU-launch false false deployment
+
+  # Xcat Launch (NYI)
+
+  # source ./kubernetes/kubernetes-launch.sh XCAT-launch true true
+
+else
+  echo "LAUNCH variable invalid, Exiting"
+  exit 1
 fi
-
-# Now run the Robot test
-
-# Timestamp for job
-echo "Robot Test started, $(date)"
-
-mkdir -p ${WORKSPACE}
-cd ${WORKSPACE}
-
-# Copy in the script which will execute the Robot tests
-cp $DIR/scripts/run-robot.sh ${WORKSPACE}
-
-# Run the Docker container to execute the Robot test cases
-# The test results will be put in ${WORKSPACE}
-docker run --rm \
-           --user root \
-           --env HOME=${HOME} \
-           --env IP_ADDR=${DOCKER_QEMU_IP_ADDR} \
-           --env SSH_PORT=${DOCKER_SSH_PORT} \
-           --env HTTPS_PORT=${DOCKER_HTTPS_PORT} \
-           --workdir ${HOME} \
-           --volume ${WORKSPACE}:${HOME} \
-           --tty \
-           ${DOCKER_IMG_NAME} ${HOME}/run-robot.sh
-
-# Now stop the QEMU Docker image
-docker stop $obmc_qemu_docker