blob: c403e12d31a05226e91f6c0afbb83c064164c58d [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
28# imgname = the name the image will be given when built, must include
29# the repo in name for the push command to work.
30# podname = the name of the pod, will be needed to trace down the logs
31#
32###############################################################################
Alanny Lopez0e8ad992017-06-19 15:45:23 -050033# Variables that act as script options:
34# invoker = name of what this script is being called by or for, used to
35# determine the template to use for YAML file
36# log = set to true to make the script tail the container logs of pod
37# purge = set to true delete the created object once script completes
38# launch = used to determine the template for YAML file, Usually brought
39# in by sourcing from another script but can be declared
40#
41###############################################################################
Alanny Lopez09e18652017-04-24 15:50:33 -050042
43# Kubernetes Variables
44namespace=${namespace:-openbmc}
Alanny Lopez09e18652017-04-24 15:50:33 -050045imgrepo=${imgrepo:-master.cfc:8500/openbmc/}
46imgplsec=${imgplsec:-regkey}
47timeout=${timeout:-60}
48
Alanny Lopez0e8ad992017-06-19 15:45:23 -050049# Options which decide script behavior
50invoker=${invoker:-${1}}
51log=${log:-${2}}
52purge=${purge:-${3}}
53launch=${launch:-${4}}
54
55# Set the variables for the specific invoker to fill in the YAML template
56# Other variables in the template not declared here are expected to be declared by invoker.
57case ${invoker} in
58 OpenBMC-build)
59 hclaim=${hclaim:-jenkins}
60 sclaim=${sclaim:-shared-state-cache}
61 oclaim=${oclaim:-openbmc-reference-repo}
62 imgname=${imgname:-${imgrepo}${distro}:${imgtag}-${ARCH}}
63 podname=${podname:-openbmc${BUILD_ID}-${target}-builder}
64 ;;
65 QEMU-build)
66 ;;
67 QEMU-launch)
68 ;;
69 XCAT-launch)
70 ;;
71 generic)
72 ;;
73 *)
74 exit 1
75 ;;
76esac
77
Alanny Lopez09e18652017-04-24 15:50:33 -050078
79# Build the Docker image, using the Dockerfile carried from build-setup.sh
80docker build -t ${imgname} - <<< "${Dockerfile}"
81
82# Push the image that was built to the image repository
83docker push ${imgname}
84
Alanny Lopez0e8ad992017-06-19 15:45:23 -050085yamlfile=$(eval "echo \"$(<./Templates/${invoker}-${launch}.yaml)\"" )
Alanny Lopez09e18652017-04-24 15:50:33 -050086kubectl create -f - <<< "${yamlfile}"
87
Alanny Lopez09e18652017-04-24 15:50:33 -050088# Once pod is running track logs
Alanny Lopez0e8ad992017-06-19 15:45:23 -050089if [[ "${log}" == true ]]; then
90 # Wait for Pod to be running
91 while [ -z "$(kubectl describe pod ${podname} -n ${namespace} | grep Status: | grep Running)" ]; do
92 if [ ${timeout} -lt 0 ];then
93 kubectl delete -f - <<< "${yamlfile}"
94 echo "Timeout Occured: Job failed to start running in time"
95 exit 1
96 else
97 sleep 1
98 let timeout-=1
99 fi
100 done
101 kubectl logs -f ${podname} -n ${namespace}
102fi
Alanny Lopez09e18652017-04-24 15:50:33 -0500103
Alanny Lopez0e8ad992017-06-19 15:45:23 -0500104# Delete the object if purge is true
105if [[ "${purge}" == true ]]; then
106 kubectl delete -f - <<< "${yamlfile}"
107fi