blob: 58b548b4e8f5415fde41884854cfec18e82e2cd0 [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)
Alanny Lopez634ce362017-06-23 12:57:05 -050066 podname=${podname:-qemubuild${BUILD_ID}}
67 hclaim=${hclaim:-jenkins}
68 qclaim=${qclaim:-qemu-repo}
69 imgname="${imgrepo}${imgname}"
Alanny Lopez0e8ad992017-06-19 15:45:23 -050070 ;;
71 QEMU-launch)
72 ;;
73 XCAT-launch)
74 ;;
75 generic)
76 ;;
77 *)
78 exit 1
79 ;;
80esac
81
Alanny Lopez09e18652017-04-24 15:50:33 -050082
83# Build the Docker image, using the Dockerfile carried from build-setup.sh
84docker build -t ${imgname} - <<< "${Dockerfile}"
85
86# Push the image that was built to the image repository
87docker push ${imgname}
88
Alanny Lopez634ce362017-06-23 12:57:05 -050089yamlfile=$(eval "echo \"$(<./kubernetes/Templates/${invoker}-${launch}.yaml)\"" )
Alanny Lopez09e18652017-04-24 15:50:33 -050090kubectl create -f - <<< "${yamlfile}"
91
Alanny Lopez09e18652017-04-24 15:50:33 -050092# Once pod is running track logs
Alanny Lopez0e8ad992017-06-19 15:45:23 -050093if [[ "${log}" == true ]]; then
94 # Wait for Pod to be running
95 while [ -z "$(kubectl describe pod ${podname} -n ${namespace} | grep Status: | grep Running)" ]; do
96 if [ ${timeout} -lt 0 ];then
97 kubectl delete -f - <<< "${yamlfile}"
98 echo "Timeout Occured: Job failed to start running in time"
99 exit 1
100 else
101 sleep 1
102 let timeout-=1
103 fi
104 done
105 kubectl logs -f ${podname} -n ${namespace}
106fi
Alanny Lopez09e18652017-04-24 15:50:33 -0500107
Alanny Lopez0e8ad992017-06-19 15:45:23 -0500108# Delete the object if purge is true
109if [[ "${purge}" == true ]]; then
110 kubectl delete -f - <<< "${yamlfile}"
111fi