Add script to initialized openbmc env in Kubernetes

Added a new script that can be used to deploy all neccessary objects
required to do builds into the Kubernetes API. It also has the added
function of being able to deploy a Jenkins master into Kubernetes using
the "build-jenkins.sh" script.

Updated the storage-setup.sh to use lowercase variable names.

Change-Id: I98ab47ffc03ee91c9ffb4cd5e570e374278fb94a
Signed-off-by: Alanny Lopez <Alanny.Lopez@ibm.com>
diff --git a/kubernetes/init-openbmc-k8s-env.sh b/kubernetes/init-openbmc-k8s-env.sh
new file mode 100755
index 0000000..951f718
--- /dev/null
+++ b/kubernetes/init-openbmc-k8s-env.sh
@@ -0,0 +1,144 @@
+#!/bin/bash
+###############################################################################
+#
+# This script is for initializing the Kubernetes environement needed to run all
+# the kubernetes integrated scripts in Kubernetes.
+# - Provisions the PV's and PVC's for:
+#   * The Kubernetes JNLP Jenkins slave's shared workspace
+#   * Shared state cache
+#   * Openbmc/openbmc git reference repository
+#   * Openbmc/qemu git reference repository
+# - Create docker-registry secret for pulling from the internal repo
+# - Create the config.json used to mount docker configuration to Kubernetes
+#   Jenkins slaves that build and push docker images via shell scripts.
+# Optionally:
+# - Launch a Jenkins Master deployment into Kubernetes.
+# - Provision the PV and PVC for the Jenkin Master home directory
+#
+# Instructions:
+#  Suggested way to run is to create a seperate script that will export all the
+#  necessary variables and then source in this script. But editing this one
+#  works as well.
+#
+###############################################################################
+#
+# Requirements:
+#  - NFS server with directory to use as path for mount
+#  - Access to an existing Kubernetes Cluster
+#  - Kubectl installed and configured on machine running script
+#
+###############################################################################
+#
+# Variables used to initialize environment:
+#  ns           = Name of namespace we will be deploying the components into,
+#                 defaults to "openbmc".
+#  nfsip        = IP address of the NFS server we will be using for mounting a
+#                 Persistent Volume (PV) to, defaults to "10.0.0.0", should be
+#                 replaced with an actual IP address of an NFS server.
+#  reclaim      = The reclaim policy that will be used when creating the PV
+#                 look at k8s docs for more info on this. Defaults to "Retain".
+#  path_prefix  = The prefix we will add to the nfspath of the directories we
+#                 intend to mount. This is used to place all the different
+#                 directories into the same parent folder on the NFS server.
+#                 defaults to "/san_mount/openbmc_k8s", should be changed to
+#                 a valid path on your NFS server.
+#  regserver    = The docker registry which will be used when pushing and
+#                 pulling images. For internal use, it will be the internal
+#                 registry created by ICP, defaults to "master.icp:8500" must
+#                 be changed to an actual registry.
+#  username     = The username that will be used to login to the regserver,
+#                 defaults to "admin", should be changed.
+#  pass         = The password that will be used to login to the regserver,
+#                 defaults to "password", should be changed.
+#  email        = The email that will be used to login to the regserver,
+#                 defaults to "email@place.holder", should be changed.
+#  k8s_master   = Set to True if you want to deploy a Jenkins Master into k8s,
+#                 defaults to True.
+###############################################################################
+
+build_scripts_dir=${build_scripts_dir:-"$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/.."}
+
+ns=${ns:-openbmc}
+nfsip=${nfsip:-10.0.0.0}
+regserver=${regserver:-master.icp:8500}
+reclaim=${reclaim:-Retain}
+path_prefix=${path_prefix:-/san_mount/openbmc_k8s}
+username=${username:-admin}
+pass=${pass:-password}
+email=${email:-email\@place.holder}
+k8s_master=${k8s_master:-True}
+
+echo "Create the Jenkins Slave Workspace PVC"
+name="jenkins-slave-space"
+size="100Gi"
+mode="ReadWriteMany"
+nfspath="${path_prefix}/jenkins-slave-space"
+source ${build_scripts_dir}/kubernetes/storage-setup.sh
+
+echo "Create the Shared State Cache PVC"
+name="shared-state-cache"
+size="100Gi"
+mode="ReadWriteMany"
+nfspath="${path_prefix}/sstate-cache"
+source ${build_scripts_dir}/kubernetes/storage-setup.sh
+
+echo "Create the Openbmc Reference PVC"
+name="openbmc-reference-repo"
+size="1Gi"
+mode="ReadWriteMany"
+nfspath="${path_prefix}/openbmc"
+source ${build_scripts_dir}/kubernetes/storage-setup.sh
+
+echo "Create the QEMU Reference PVC"
+name="qemu-repo"
+size="1Gi"
+mode="ReadWriteMany"
+nfspath="${path_prefix}/qemu"
+source ${build_scripts_dir}/kubernetes/storage-setup.sh
+
+# Create the regkey secret for the internal docker registry
+kubectl create secret docker-registry regkey -n $ns \
+--docker-username=${username} \
+--docker-password=${pass} \
+--docker-email=${email} \
+--docker-server=${regserver}
+
+# Create the docker config.json secret using the base64 encode of
+# '${username}:${pass}'
+
+base64up=$( echo -n "${username}:${pass}" | base64 )
+cat >> config.json << EOF
+{
+  "auths": {
+    "${regserver}": {
+      "auth": "${base64up}"
+    }
+  }
+}
+EOF
+
+chmod ugo+rw config.json
+kubectl create secret generic docker-config -n $ns --from-file=./config.json
+rm -f ./config.json
+
+if [[ "${k8s_master}" ==  "True" ]]; then
+  # Create the Jenkins Master Home PVC
+  echo "Create the Jenkins Master Home PVC"
+  name="jenkins-home"
+  size="2Gi"
+  mode="ReadWriteOnce"
+  nfspath="${path_prefix}/jenkins-master-home"
+  source ${build_scripts_dir}/kubernetes/storage-setup.sh
+
+  # Launch the Jenkins Master
+  launch="k8s"
+  # Clean up variables before sourcing the build-jenkins.sh
+  unset ns \
+  nfsip \
+  regserver \
+  reclaim \
+  path_prefix \
+  username \
+  pass email
+  source ${build_scripts_dir}/build-jenkins.sh
+fi
diff --git a/kubernetes/storage-setup.sh b/kubernetes/storage-setup.sh
index 0adffe6..10ac76a 100755
--- a/kubernetes/storage-setup.sh
+++ b/kubernetes/storage-setup.sh
@@ -15,15 +15,15 @@
 ###############################################################################
 #
 # The script expects a few variables which are needed to define PV's and PVC's
-#  NS      = Namespace under which to create the mounts on the cluster
-#  NFSIP   = Server IP for NFS server that will be used
-#  NFSPATH = Path of the directory that will be mounted from NFS server
-#  SIZE    = The size of the volume, numeric value followed by Gi or Mi
-#  NAME    = The name of the PV and PVC that will be used by the Kubernetes
+#  ns      = Namespace under which to create the mounts on the cluster
+#  nfsip   = Server IP for NFS server that will be used
+#  nfspath = Path of the directory that will be mounted from NFS server
+#  size    = The size of the volume, numeric value followed by Gi or Mi
+#  name    = The name of the PV and PVC that will be used by the Kubernetes
 #            system to refer to PV/PVC
-#  MODE    = ReadWriteOnce|ReadOnlyMany|ReadWriteMany
+#  mode    = ReadWriteOnce|ReadOnlyMany|ReadWriteMany
 #            Access Mode used by NFS normally uses ReadWriteMany
-#  RECLAIM = recycle|delete|retain
+#  reclaim = recycle|delete|retain
 #            The policy, defines what occurs when claim on PV is released, can
 #            be either: recycle, delete, or retain.
 #
@@ -32,13 +32,13 @@
 #
 ###############################################################################
 
-NS=${NS:-openbmc}
-NFSIP=${NFSIP:-NFS-Server}
-NFSPATH=${NFSPATH:-/san/dir}
-SIZE=${SIZE:-10Gi}
-NAME=${NAME:-placeholder}
-MODE=${MODE:-ReadWriteMany}
-RECLAIM=${RECLAIM:-Retain}
+ns=${ns:-openbmc}
+nfsip=${nfsip:-NFS-Server}
+nfspath=${nfspath:-/san/dir}
+size=${size:-10Gi}
+name=${name:-placeholder}
+mode=${mode:-ReadWriteMany}
+reclaim=${reclaim:-Retain}
 
 # Generate the PV
 pv=$(cat << EOF
@@ -46,24 +46,24 @@
 kind: PersistentVolume
 metadata:
   labels:
-    app: ${NS}
-  name: ${NAME}
-  namespace: ${NS}
+    app: ${name}
+  name: ${name}
+  namespace: ${ns}
 spec:
   accessModes:
-  - ${MODE}
+  - ${mode}
   capacity:
-    storage: ${SIZE}
+    storage: ${size}
   nfs:
-    path: ${NFSPATH}
-    server: ${NFSIP}
-  persistentVolumeReclaimPolicy: ${RECLAIM}
+    path: ${nfspath}
+    server: ${nfsip}
+  persistentVolumeReclaimPolicy: ${reclaim}
 EOF
 )
 
 # create the volume
-if [ -z $(kubectl get pv --namespace=${NS} | grep '^'${NAME}' ' | cut -d " " -f1) ];then
-  echo "Creating Persistent Volume ${NAME}"
+if [ -z $(kubectl get pv --namespace=${ns} | grep '^'${name}' ' | cut -d " " -f1) ];then
+  echo "Creating Persistent Volume ${name}"
   kubectl create -f - <<< "${pv}"
 else
   echo "Persistent Volume already Exists"
@@ -75,23 +75,23 @@
 apiVersion: v1
 kind: PersistentVolumeClaim
 metadata:
-  name: ${NAME}
-  namespace: ${NS}
+  name: ${name}
+  namespace: ${ns}
 spec:
   accessModes:
-  - ${MODE}
+  - ${mode}
   resources:
     requests:
-      storage: ${SIZE}
+      storage: ${size}
   selector:
     matchLabels:
-      app: ${NS}
+      app: ${name}
 EOF
 )
 
 # create PVC's to bind the PV's
-if [ -z $(kubectl get pvc --namespace=${NS} | grep '^'${NAME}' ' | cut -d " " -f1) ];then
-  echo "Creating Persistent Volume Claim ${NAME}"
+if [ -z $(kubectl get pvc --namespace=${ns} | grep '^'${name}' ' | cut -d " " -f1) ];then
+  echo "Creating Persistent Volume Claim ${name}"
   kubectl create -f - <<< "${pvc}"
 else
   echo "Persistent volume claim already exists."