blob: 1273af13f6d88895e3b12590668e122f576cf48f [file] [log] [blame]
Alanny Lopez09e18652017-04-24 15:50:33 -05001#!/bin/bash
2###############################################################################
3#
4# Script used to assist in launching Kubernetes jobs/pods. Expects to be used
Alanny Lopez0e8ad992017-06-19 15:45:23 -05005# as an supplemental script to the scripts that want to launch their containers
6# on a Kubernetes cluster.
Alanny Lopez09e18652017-04-24 15:50:33 -05007#
8###############################################################################
9#
10# Requirements:
11# - Docker login credentials defined inside ~/.docker/config.json
12# - Kubectl installed and configured on machine running the script
13# - Access to a Kubernetes Cluster using v1.5.2 or newer
14# - NFS directories for OpenBMC repo cache, BitBake shared state cache, and
15# shared Jenkins home directory that holds workspaces.
16# - All NFS directories should have RWX permissions for user being used to run
17# the build-setup.sh script
18# - Persistent Volume and Claims created and mounted to NFS directories
19# - Image pull secret exists for image pulls in Kubernetes cluster namespace
20#
21###############################################################################
22# Variables used to create Kubernetes Job:
23# namespace = the namespace to be used within the Kubernetes cluster
Alanny Lopez09e18652017-04-24 15:50:33 -050024# registry = the registry to use to pull and push images
25# imgplsec = the image pull secret used to access registry if needed
Alanny Lopez26b2bc52017-09-29 16:46:00 -050026# jobtimeout = the amount of time in seconds that the build will wait for
27# the job to be created in the api of the cluster.
28# podtimeout = the amount of time in seconds that the build will wait for
29# the pod to start running on the cluster.
Alanny Lopez51186882017-08-01 16:14:41 -050030# imgname = the name the image that will be passed to the kubernetes api
31# to build the containers. The image with the tag imgname will
32# be built in the invoker script. This script will then tag it
33# to include the registry in the name, push it, and update the
34# imgname to be what was pushed to the registry. Users should
35# not include the registry in the original imgname.
Alanny Lopez09e18652017-04-24 15:50:33 -050036# podname = the name of the pod, will be needed to trace down the logs
37#
38###############################################################################
Alanny Lopez0e8ad992017-06-19 15:45:23 -050039# Variables that act as script options:
40# invoker = name of what this script is being called by or for, used to
41# determine the template to use for YAML file
42# log = set to true to make the script tail the container logs of pod
43# purge = set to true delete the created object once script completes
44# launch = used to determine the template for YAML file, Usually brought
45# in by sourcing from another script but can be declared
Alanny Lopezd1bb5b32017-09-20 11:32:40 -050046# workaround = Used to enable the logging workaround, when set will launch a
47# modified template that waits for a command. In most cases it
48# will be waiting to have a script run via kubectl exec. Needed
49# when using a version of Kubernetes that has known issues that
50# impact the retrieval of container logs when using kubectl.
51# Defaulting to be true whenever logging is enabled until ICP
52# upgrades their Kubernetes version.
Alanny Lopez0e8ad992017-06-19 15:45:23 -050053###############################################################################
Alanny Lopez09e18652017-04-24 15:50:33 -050054
55# Kubernetes Variables
56namespace=${namespace:-openbmc}
Alanny Lopez09e18652017-04-24 15:50:33 -050057imgrepo=${imgrepo:-master.cfc:8500/openbmc/}
58imgplsec=${imgplsec:-regkey}
Alanny Lopez26b2bc52017-09-29 16:46:00 -050059jobtimeout=${jobtimeout:-60}
60podtimeout=${podtimeout:-600}
Alanny Lopez09e18652017-04-24 15:50:33 -050061
Alanny Lopez0e8ad992017-06-19 15:45:23 -050062# Options which decide script behavior
63invoker=${invoker:-${1}}
64log=${log:-${2}}
65purge=${purge:-${3}}
66launch=${launch:-${4}}
Alanny Lopezd1bb5b32017-09-20 11:32:40 -050067workaround=${workaround:-${log}}
Alanny Lopez0e8ad992017-06-19 15:45:23 -050068
69# Set the variables for the specific invoker to fill in the YAML template
Alanny Lopez26b2bc52017-09-29 16:46:00 -050070# Other variables in the template not declared here are declared by invoker
Alanny Lopez0e8ad992017-06-19 15:45:23 -050071case ${invoker} in
72 OpenBMC-build)
Alanny Lopeza6b7d4b2017-10-19 09:58:25 -050073 wclaim=${wclaim:-jenkins-slave-space}
Alanny Lopez0e8ad992017-06-19 15:45:23 -050074 sclaim=${sclaim:-shared-state-cache}
75 oclaim=${oclaim:-openbmc-reference-repo}
Alanny Lopez51186882017-08-01 16:14:41 -050076 newimgname=${newimgname:-${imgrepo}${distro}:${imgtag}-${ARCH}}
Alanny Lopez0e8ad992017-06-19 15:45:23 -050077 podname=${podname:-openbmc${BUILD_ID}-${target}-builder}
78 ;;
79 QEMU-build)
Alanny Lopez634ce362017-06-23 12:57:05 -050080 podname=${podname:-qemubuild${BUILD_ID}}
Alanny Lopeza6b7d4b2017-10-19 09:58:25 -050081 wclaim=${wclaim:-jenkins-slave-space}
Alanny Lopez634ce362017-06-23 12:57:05 -050082 qclaim=${qclaim:-qemu-repo}
Alanny Lopez51186882017-08-01 16:14:41 -050083 newimgname="${imgrepo}${imgname}"
Alanny Lopez0e8ad992017-06-19 15:45:23 -050084 ;;
85 QEMU-launch)
Alanny Lopez07b4d5b2017-08-01 16:24:07 -050086 deployname=${deployname:-qemu-launch-deployment}
87 podname=${podname:-qemu-instance}
88 replicas=${replicas:-5}
Alanny Lopeza6b7d4b2017-10-19 09:58:25 -050089 wclaim=${wclaim:-jenkins-slave-space}
90 jenkins_subpath=${jenkins_subpath:-Openbmc-Build/openbmc/build}
Alanny Lopezeba5ad42017-08-18 14:48:37 -050091 newimgname="${imgrepo}qemu-instance"
Alanny Lopez0e8ad992017-06-19 15:45:23 -050092 ;;
93 XCAT-launch)
94 ;;
95 generic)
96 ;;
97 *)
98 exit 1
99 ;;
100esac
101
Alanny Lopez26b2bc52017-09-29 16:46:00 -0500102# Tag the image created by the invoker with a name that includes the imgrepo
Alanny Lopez51186882017-08-01 16:14:41 -0500103docker tag ${imgname} ${newimgname}
104imgname=${newimgname}
Alanny Lopez09e18652017-04-24 15:50:33 -0500105
106# Push the image that was built to the image repository
107docker push ${imgname}
108
Alanny Lopezeba5ad42017-08-18 14:48:37 -0500109if [[ "$ARCH" == x86_64 ]]; then
110 ARCH=amd64
111fi
Alanny Lopez26b2bc52017-09-29 16:46:00 -0500112
Alanny Lopezd1bb5b32017-09-20 11:32:40 -0500113extras=""
114if [[ "${workaround}" == "true" ]]; then
115 extras+="-v2"
116fi
117
118yamlfile=$(eval "echo \"$(<./kubernetes/Templates/${invoker}-${launch}${extras}.yaml)\"")
Alanny Lopez09e18652017-04-24 15:50:33 -0500119kubectl create -f - <<< "${yamlfile}"
120
Alanny Lopez26b2bc52017-09-29 16:46:00 -0500121# If launch is a job we have to find the podname with identifiers
122if [[ "${launch}" == "job" ]]; then
123 while [ -z ${replace} ]
124 do
125 if [ ${jobtimeout} -lt 0 ]; then
126 kubectl delete -f - <<< "${yamlfile}"
Gunnar Mills5f811802017-10-25 16:10:27 -0500127 echo "Timeout occurred before job was present in the API"
Alanny Lopez26b2bc52017-09-29 16:46:00 -0500128 exit 1
129 else
130 sleep 1
131 let jobtimeout-=1
132 fi
133 replace=$(kubectl get pods -n ${namespaces} | grep ${podname} | awk 'print $1')
134 done
135 podname=${replace}
136fi
137
138
Alanny Lopez09e18652017-04-24 15:50:33 -0500139# Once pod is running track logs
Alanny Lopez0e8ad992017-06-19 15:45:23 -0500140if [[ "${log}" == true ]]; then
141 # Wait for Pod to be running
Alanny Lopez259185c2017-10-12 15:39:03 -0500142 checkstatus="kubectl describe pod ${podname} -n ${namespace}"
143 status=$( ${checkstatus} | grep Status: )
Alanny Lopez26b2bc52017-09-29 16:46:00 -0500144 while [ -z "$( echo ${status} | grep Running)" ]
Alanny Lopez51186882017-08-01 16:14:41 -0500145 do
Alanny Lopez26b2bc52017-09-29 16:46:00 -0500146 if [ ${podtimeout} -lt 0 ]; then
Alanny Lopez0e8ad992017-06-19 15:45:23 -0500147 kubectl delete -f - <<< "${yamlfile}"
Gunnar Mills5f811802017-10-25 16:10:27 -0500148 echo "Timeout occurred before pod was Running"
Alanny Lopez0e8ad992017-06-19 15:45:23 -0500149 exit 1
150 else
151 sleep 1
Alanny Lopez26b2bc52017-09-29 16:46:00 -0500152 let podtimeout-=1
Alanny Lopez0e8ad992017-06-19 15:45:23 -0500153 fi
Alanny Lopez259185c2017-10-12 15:39:03 -0500154 status=$( ${checkstatus} | grep Status: )
Alanny Lopez0e8ad992017-06-19 15:45:23 -0500155 done
Alanny Lopezd1bb5b32017-09-20 11:32:40 -0500156 # Tail the logs of the pod, if workaround enabled start executing build script instead.
157 if [[ "${workaround}" == "true" ]]; then
158 kubectl exec -it ${podname} -n ${namespace} ${WORKSPACE}/build.sh
159 else
160 kubectl logs -f ${podname} -n ${namespace}
161 fi
Alanny Lopez0e8ad992017-06-19 15:45:23 -0500162fi
Alanny Lopez09e18652017-04-24 15:50:33 -0500163
Alanny Lopez0e8ad992017-06-19 15:45:23 -0500164# Delete the object if purge is true
165if [[ "${purge}" == true ]]; then
166 kubectl delete -f - <<< "${yamlfile}"
167fi