blob: 65dc3130b29dfa6ca1080e9eabc4f8900ad6196b [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
46#
47###############################################################################
Alanny Lopez09e18652017-04-24 15:50:33 -050048
49# Kubernetes Variables
50namespace=${namespace:-openbmc}
Alanny Lopez09e18652017-04-24 15:50:33 -050051imgrepo=${imgrepo:-master.cfc:8500/openbmc/}
52imgplsec=${imgplsec:-regkey}
Alanny Lopez26b2bc52017-09-29 16:46:00 -050053jobtimeout=${jobtimeout:-60}
54podtimeout=${podtimeout:-600}
Alanny Lopez09e18652017-04-24 15:50:33 -050055
Alanny Lopez0e8ad992017-06-19 15:45:23 -050056# Options which decide script behavior
57invoker=${invoker:-${1}}
58log=${log:-${2}}
59purge=${purge:-${3}}
60launch=${launch:-${4}}
61
62# Set the variables for the specific invoker to fill in the YAML template
Alanny Lopez26b2bc52017-09-29 16:46:00 -050063# Other variables in the template not declared here are declared by invoker
Alanny Lopez0e8ad992017-06-19 15:45:23 -050064case ${invoker} in
65 OpenBMC-build)
Alanny Lopezeba5ad42017-08-18 14:48:37 -050066 hclaim=${hclaim:-jenkins-slave-space}
Alanny Lopez0e8ad992017-06-19 15:45:23 -050067 sclaim=${sclaim:-shared-state-cache}
68 oclaim=${oclaim:-openbmc-reference-repo}
Alanny Lopez51186882017-08-01 16:14:41 -050069 newimgname=${newimgname:-${imgrepo}${distro}:${imgtag}-${ARCH}}
Alanny Lopez0e8ad992017-06-19 15:45:23 -050070 podname=${podname:-openbmc${BUILD_ID}-${target}-builder}
71 ;;
72 QEMU-build)
Alanny Lopez634ce362017-06-23 12:57:05 -050073 podname=${podname:-qemubuild${BUILD_ID}}
Alanny Lopezeba5ad42017-08-18 14:48:37 -050074 hclaim=${hclaim:-jenkins-slave-space}
Alanny Lopez634ce362017-06-23 12:57:05 -050075 qclaim=${qclaim:-qemu-repo}
Alanny Lopez51186882017-08-01 16:14:41 -050076 newimgname="${imgrepo}${imgname}"
Alanny Lopez0e8ad992017-06-19 15:45:23 -050077 ;;
78 QEMU-launch)
Alanny Lopez07b4d5b2017-08-01 16:24:07 -050079 deployname=${deployname:-qemu-launch-deployment}
80 podname=${podname:-qemu-instance}
81 replicas=${replicas:-5}
Alanny Lopezeba5ad42017-08-18 14:48:37 -050082 hclaim=${hclaim:-jenkins-slave-space}
Alanny Lopezb6c42d82017-10-13 09:07:53 -050083 jenkins_subpath=${jenkins_subpath:-workspace/Openbmc-Build/openbmc/build}
Alanny Lopezeba5ad42017-08-18 14:48:37 -050084 newimgname="${imgrepo}qemu-instance"
Alanny Lopez0e8ad992017-06-19 15:45:23 -050085 ;;
86 XCAT-launch)
87 ;;
88 generic)
89 ;;
90 *)
91 exit 1
92 ;;
93esac
94
Alanny Lopez26b2bc52017-09-29 16:46:00 -050095# Tag the image created by the invoker with a name that includes the imgrepo
Alanny Lopez51186882017-08-01 16:14:41 -050096docker tag ${imgname} ${newimgname}
97imgname=${newimgname}
Alanny Lopez09e18652017-04-24 15:50:33 -050098
99# Push the image that was built to the image repository
100docker push ${imgname}
101
Alanny Lopezeba5ad42017-08-18 14:48:37 -0500102if [[ "$ARCH" == x86_64 ]]; then
103 ARCH=amd64
104fi
Alanny Lopez26b2bc52017-09-29 16:46:00 -0500105
106yamlfile=$(eval "echo \"$(<./kubernetes/Templates/${invoker}-${launch}.yaml)\"")
Alanny Lopez09e18652017-04-24 15:50:33 -0500107kubectl create -f - <<< "${yamlfile}"
108
Alanny Lopez26b2bc52017-09-29 16:46:00 -0500109# If launch is a job we have to find the podname with identifiers
110if [[ "${launch}" == "job" ]]; then
111 while [ -z ${replace} ]
112 do
113 if [ ${jobtimeout} -lt 0 ]; then
114 kubectl delete -f - <<< "${yamlfile}"
Gunnar Mills5f811802017-10-25 16:10:27 -0500115 echo "Timeout occurred before job was present in the API"
Alanny Lopez26b2bc52017-09-29 16:46:00 -0500116 exit 1
117 else
118 sleep 1
119 let jobtimeout-=1
120 fi
121 replace=$(kubectl get pods -n ${namespaces} | grep ${podname} | awk 'print $1')
122 done
123 podname=${replace}
124fi
125
126
Alanny Lopez09e18652017-04-24 15:50:33 -0500127# Once pod is running track logs
Alanny Lopez0e8ad992017-06-19 15:45:23 -0500128if [[ "${log}" == true ]]; then
129 # Wait for Pod to be running
Alanny Lopez259185c2017-10-12 15:39:03 -0500130 checkstatus="kubectl describe pod ${podname} -n ${namespace}"
131 status=$( ${checkstatus} | grep Status: )
Alanny Lopez26b2bc52017-09-29 16:46:00 -0500132 while [ -z "$( echo ${status} | grep Running)" ]
Alanny Lopez51186882017-08-01 16:14:41 -0500133 do
Alanny Lopez26b2bc52017-09-29 16:46:00 -0500134 if [ ${podtimeout} -lt 0 ]; then
Alanny Lopez0e8ad992017-06-19 15:45:23 -0500135 kubectl delete -f - <<< "${yamlfile}"
Gunnar Mills5f811802017-10-25 16:10:27 -0500136 echo "Timeout occurred before pod was Running"
Alanny Lopez0e8ad992017-06-19 15:45:23 -0500137 exit 1
138 else
139 sleep 1
Alanny Lopez26b2bc52017-09-29 16:46:00 -0500140 let podtimeout-=1
Alanny Lopez0e8ad992017-06-19 15:45:23 -0500141 fi
Alanny Lopez259185c2017-10-12 15:39:03 -0500142 status=$( ${checkstatus} | grep Status: )
Alanny Lopez0e8ad992017-06-19 15:45:23 -0500143 done
Alanny Lopez26b2bc52017-09-29 16:46:00 -0500144 # Tail the logs of the pod
Alanny Lopez0e8ad992017-06-19 15:45:23 -0500145 kubectl logs -f ${podname} -n ${namespace}
146fi
Alanny Lopez09e18652017-04-24 15:50:33 -0500147
Alanny Lopez0e8ad992017-06-19 15:45:23 -0500148# Delete the object if purge is true
149if [[ "${purge}" == true ]]; then
150 kubectl delete -f - <<< "${yamlfile}"
151fi