blob: 600fc3584c4da718d964da2e25e921570e7e2237 [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
26# timeout = the amount of time in seconds that the build will wait for
27# the pod to start running on the cluster
Alanny Lopez51186882017-08-01 16:14:41 -050028# imgname = the name the image that will be passed to the kubernetes api
29# to build the containers. The image with the tag imgname will
30# be built in the invoker script. This script will then tag it
31# to include the registry in the name, push it, and update the
32# imgname to be what was pushed to the registry. Users should
33# not include the registry in the original imgname.
Alanny Lopez09e18652017-04-24 15:50:33 -050034# podname = the name of the pod, will be needed to trace down the logs
35#
36###############################################################################
Alanny Lopez0e8ad992017-06-19 15:45:23 -050037# Variables that act as script options:
38# invoker = name of what this script is being called by or for, used to
39# determine the template to use for YAML file
40# log = set to true to make the script tail the container logs of pod
41# purge = set to true delete the created object once script completes
42# launch = used to determine the template for YAML file, Usually brought
43# in by sourcing from another script but can be declared
44#
45###############################################################################
Alanny Lopez09e18652017-04-24 15:50:33 -050046
47# Kubernetes Variables
48namespace=${namespace:-openbmc}
Alanny Lopez09e18652017-04-24 15:50:33 -050049imgrepo=${imgrepo:-master.cfc:8500/openbmc/}
50imgplsec=${imgplsec:-regkey}
51timeout=${timeout:-60}
52
Alanny Lopez0e8ad992017-06-19 15:45:23 -050053# Options which decide script behavior
54invoker=${invoker:-${1}}
55log=${log:-${2}}
56purge=${purge:-${3}}
57launch=${launch:-${4}}
58
59# Set the variables for the specific invoker to fill in the YAML template
60# Other variables in the template not declared here are expected to be declared by invoker.
61case ${invoker} in
62 OpenBMC-build)
63 hclaim=${hclaim:-jenkins}
64 sclaim=${sclaim:-shared-state-cache}
65 oclaim=${oclaim:-openbmc-reference-repo}
Alanny Lopez51186882017-08-01 16:14:41 -050066 newimgname=${newimgname:-${imgrepo}${distro}:${imgtag}-${ARCH}}
Alanny Lopez0e8ad992017-06-19 15:45:23 -050067 podname=${podname:-openbmc${BUILD_ID}-${target}-builder}
68 ;;
69 QEMU-build)
Alanny Lopez634ce362017-06-23 12:57:05 -050070 podname=${podname:-qemubuild${BUILD_ID}}
71 hclaim=${hclaim:-jenkins}
72 qclaim=${qclaim:-qemu-repo}
Alanny Lopez51186882017-08-01 16:14:41 -050073 newimgname="${imgrepo}${imgname}"
Alanny Lopez0e8ad992017-06-19 15:45:23 -050074 ;;
75 QEMU-launch)
76 ;;
77 XCAT-launch)
78 ;;
79 generic)
80 ;;
81 *)
82 exit 1
83 ;;
84esac
85
Alanny Lopez09e18652017-04-24 15:50:33 -050086
Alanny Lopez51186882017-08-01 16:14:41 -050087# Tag the image created by the invoker with the new image name that includes the imgrepo
88docker tag ${imgname} ${newimgname}
89imgname=${newimgname}
Alanny Lopez09e18652017-04-24 15:50:33 -050090
91# Push the image that was built to the image repository
92docker push ${imgname}
93
Alanny Lopez634ce362017-06-23 12:57:05 -050094yamlfile=$(eval "echo \"$(<./kubernetes/Templates/${invoker}-${launch}.yaml)\"" )
Alanny Lopez09e18652017-04-24 15:50:33 -050095kubectl create -f - <<< "${yamlfile}"
96
Alanny Lopez09e18652017-04-24 15:50:33 -050097# Once pod is running track logs
Alanny Lopez0e8ad992017-06-19 15:45:23 -050098if [[ "${log}" == true ]]; then
99 # Wait for Pod to be running
Alanny Lopez51186882017-08-01 16:14:41 -0500100 while [ -z "$(kubectl describe pod ${podname} -n ${namespace} | grep Status: | grep Running)" ]
101 do
Alanny Lopez0e8ad992017-06-19 15:45:23 -0500102 if [ ${timeout} -lt 0 ];then
103 kubectl delete -f - <<< "${yamlfile}"
104 echo "Timeout Occured: Job failed to start running in time"
105 exit 1
106 else
107 sleep 1
108 let timeout-=1
109 fi
110 done
111 kubectl logs -f ${podname} -n ${namespace}
112fi
Alanny Lopez09e18652017-04-24 15:50:33 -0500113
Alanny Lopez0e8ad992017-06-19 15:45:23 -0500114# Delete the object if purge is true
115if [[ "${purge}" == true ]]; then
116 kubectl delete -f - <<< "${yamlfile}"
117fi