blob: a9b0c5f80a541bf7a421e6dd725627285d32f114 [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###############################################################################
Alanny Lopeze08e8702018-02-24 18:07:13 -060022# Script Variables:
23# build_scripts_dir The path for the openbmc-build-scripts directory.
24# Default: The parent directory containing this script
25#
26# Kubernetes Variables:
Alanny Lopez1246b032018-02-24 23:34:55 -060027# img_pl_sec The image pull secret used to access registry if needed
Alanny Lopeze08e8702018-02-24 18:07:13 -060028# Default: "regkey"
Alanny Lopez1246b032018-02-24 23:34:55 -060029# registry The registry to use to pull and push images
Alanny Lopeze08e8702018-02-24 18:07:13 -060030# Default: "master.cfc:8500/openbmc/""
Alanny Lopez1246b032018-02-24 23:34:55 -060031# job_timeout The amount of time in seconds that the build will wait for
Alanny Lopeze08e8702018-02-24 18:07:13 -060032# the job to be created in the api of the cluster.
33# Default: "60"
34# namespace The namespace to be used within the Kubernetes cluster
35# Default: "openbmc"
Alanny Lopez1246b032018-02-24 23:34:55 -060036# pod_timeout The amount of time in seconds that the build will wait for
Alanny Lopeze08e8702018-02-24 18:07:13 -060037# the pod to start running on the cluster.
38# Default: "600"
39#
40# YAML File Variables (No Defaults):
Alanny Lopez1246b032018-02-24 23:34:55 -060041# img_name The name the image that will be passed to the kubernetes
Alanny Lopeze08e8702018-02-24 18:07:13 -060042# api to build the containers. The image with the tag
Alanny Lopez1246b032018-02-24 23:34:55 -060043# img_name will be built in the invoker script. This script
Alanny Lopeze08e8702018-02-24 18:07:13 -060044# will then tag it to include the registry in the name, push
Alanny Lopez1246b032018-02-24 23:34:55 -060045# it, and update the img_name to be what was pushed to the
Alanny Lopeze08e8702018-02-24 18:07:13 -060046# registry. Users should not include the registry in the
Alanny Lopez1246b032018-02-24 23:34:55 -060047# original img_name.
48# pod_name The name of the pod, needed to trace down the logs.
Alanny Lopeze08e8702018-02-24 18:07:13 -060049#
50# Deployment Option Variables (No Defaults):
51# invoker Name of what this script is being called by or for, used
52# to determine the template to use for YAML file.
53# launch Used to determine the template used for the YAML file,
54# normally carried in by sourcing this script in another
55# script that has declared it.
56# log If set to true the script will tail the container logs
57# as part of the bash script.
58# purge If set to true it will delete the created object once this
59# script ends.
60# workaround Used to enable the logging workaround, when set will
61# launch a modified template that waits for a command. In
62# most cases it will be waiting to have a script run via
63# kubectl exec. Required when using a version of Kubernetes
64# that has known issues that impact the retrieval of
65# container logs when using kubectl. Defaulting to be true
66# whenever logging is enabled until ICP upgrades their
67# Kubernetes version to a version that doesn't need this.
Alanny Lopez09e18652017-04-24 15:50:33 -050068#
69###############################################################################
Alanny Lopeze08e8702018-02-24 18:07:13 -060070build_scripts_dir=${build_scripts_dir:-"$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/.."}
Alanny Lopez09e18652017-04-24 15:50:33 -050071
72# Kubernetes Variables
73namespace=${namespace:-openbmc}
Alanny Lopez1246b032018-02-24 23:34:55 -060074registry=${registry:-master.cfc:8500/openbmc/}
75img_pl_sec=${img_pl_sec:-regkey}
76job_timeout=${job_timeout:-60}
77pod_timeout=${pod_timeout:-600}
Alanny Lopez09e18652017-04-24 15:50:33 -050078
Alanny Lopez0e8ad992017-06-19 15:45:23 -050079# Options which decide script behavior
80invoker=${invoker:-${1}}
81log=${log:-${2}}
82purge=${purge:-${3}}
83launch=${launch:-${4}}
Alanny Lopezd1bb5b32017-09-20 11:32:40 -050084workaround=${workaround:-${log}}
Alanny Lopez0e8ad992017-06-19 15:45:23 -050085
86# Set the variables for the specific invoker to fill in the YAML template
Alanny Lopez26b2bc52017-09-29 16:46:00 -050087# Other variables in the template not declared here are declared by invoker
Alanny Lopez0e8ad992017-06-19 15:45:23 -050088case ${invoker} in
Alanny Lopezf7799d02018-02-24 23:29:35 -060089 Build-Jenkins)
90 deploy_name=${deploy_name:-jenkins-master}
91 pod_name=${pod_name:-jenkins-master-pod}
92 new_img_name="${img_repo}jenkins-master-${ARCH}:${j_vrsn}"
93 h_claim=${h_claim:-jenkins-home}
94 cluster_ip=${cluster_ip:-10.0.0.175}
95 http_nodeport=${http_nodeport:-32222}
96 agent_nodeport=${agent_nodeport:-32223}
97 ;;
Alanny Lopez0e8ad992017-06-19 15:45:23 -050098 OpenBMC-build)
Alanny Lopez1246b032018-02-24 23:34:55 -060099 w_claim=${w_claim:-jenkins-slave-space}
100 s_claim=${s_claim:-shared-state-cache}
101 o_claim=${o_claim:-openbmc-reference-repo}
102 new_img_name=${new_img_name:-${registry}${distro}:${img_tag}-${ARCH}}
103 pod_name=${pod_name:-openbmc${BUILD_ID}-${target}-builder}
Alanny Lopez0e8ad992017-06-19 15:45:23 -0500104 ;;
105 QEMU-build)
Alanny Lopez1246b032018-02-24 23:34:55 -0600106 pod_name=${pod_name:-qemubuild${BUILD_ID}}
107 w_claim=${w_claim:-jenkins-slave-space}
108 q_claim=${q_claim:-qemu-repo}
109 new_img_name="${registry}${img_name}"
Alanny Lopez0e8ad992017-06-19 15:45:23 -0500110 ;;
111 QEMU-launch)
Alanny Lopez1246b032018-02-24 23:34:55 -0600112 deploy_name=${deploy_name:-qemu-launch-deployment}
113 pod_name=${pod_name:-qemu-instance}
Alanny Lopez07b4d5b2017-08-01 16:24:07 -0500114 replicas=${replicas:-5}
Alanny Lopez1246b032018-02-24 23:34:55 -0600115 w_claim=${w_claim:-jenkins-slave-space}
Alanny Lopeza6b7d4b2017-10-19 09:58:25 -0500116 jenkins_subpath=${jenkins_subpath:-Openbmc-Build/openbmc/build}
Alanny Lopez1246b032018-02-24 23:34:55 -0600117 new_img_name="${registry}qemu-instance"
Alanny Lopez0e8ad992017-06-19 15:45:23 -0500118 ;;
119 XCAT-launch)
120 ;;
121 generic)
122 ;;
123 *)
124 exit 1
125 ;;
126esac
127
Alanny Lopez1246b032018-02-24 23:34:55 -0600128# Tag the image created by the invoker with a name that includes the registry
129docker tag ${img_name} ${new_img_name}
130img_name=${new_img_name}
Alanny Lopez09e18652017-04-24 15:50:33 -0500131
132# Push the image that was built to the image repository
Alanny Lopez1246b032018-02-24 23:34:55 -0600133docker push ${img_name}
Alanny Lopez09e18652017-04-24 15:50:33 -0500134
Alanny Lopezeba5ad42017-08-18 14:48:37 -0500135if [[ "$ARCH" == x86_64 ]]; then
136 ARCH=amd64
137fi
Alanny Lopez26b2bc52017-09-29 16:46:00 -0500138
Alanny Lopezd1bb5b32017-09-20 11:32:40 -0500139extras=""
140if [[ "${workaround}" == "true" ]]; then
141 extras+="-v2"
142fi
143
Alanny Lopez1246b032018-02-24 23:34:55 -0600144yaml_file=$(eval "echo \"$(<${build_scripts_dir}/kubernetes/Templates/${invoker}-${launch}${extras}.yaml)\"")
145kubectl create -f - <<< "${yaml_file}"
Alanny Lopez09e18652017-04-24 15:50:33 -0500146
Alanny Lopez1246b032018-02-24 23:34:55 -0600147# If launch is a job we have to find the pod_name with identifiers
Alanny Lopez26b2bc52017-09-29 16:46:00 -0500148if [[ "${launch}" == "job" ]]; then
149 while [ -z ${replace} ]
150 do
Alanny Lopez1246b032018-02-24 23:34:55 -0600151 if [ ${job_timeout} -lt 0 ]; then
152 kubectl delete -f - <<< "${yaml_file}"
Gunnar Mills5f811802017-10-25 16:10:27 -0500153 echo "Timeout occurred before job was present in the API"
Alanny Lopez26b2bc52017-09-29 16:46:00 -0500154 exit 1
155 else
156 sleep 1
Alanny Lopez1246b032018-02-24 23:34:55 -0600157 let job_timeout-=1
Alanny Lopez26b2bc52017-09-29 16:46:00 -0500158 fi
Alanny Lopezf7799d02018-02-24 23:29:35 -0600159 replace=$(kubectl get pods -n ${namespace} | grep ${pod_name} | awk 'print $1')
Alanny Lopez26b2bc52017-09-29 16:46:00 -0500160 done
Alanny Lopez1246b032018-02-24 23:34:55 -0600161 pod_name=${replace}
Alanny Lopez26b2bc52017-09-29 16:46:00 -0500162fi
163
164
Alanny Lopez09e18652017-04-24 15:50:33 -0500165# Once pod is running track logs
Alanny Lopez0e8ad992017-06-19 15:45:23 -0500166if [[ "${log}" == true ]]; then
167 # Wait for Pod to be running
Alanny Lopez1246b032018-02-24 23:34:55 -0600168 check_status="kubectl describe pod ${pod_name} -n ${namespace}"
169 status=$( ${check_status} | grep Status: )
Alanny Lopez26b2bc52017-09-29 16:46:00 -0500170 while [ -z "$( echo ${status} | grep Running)" ]
Alanny Lopez51186882017-08-01 16:14:41 -0500171 do
Alanny Lopez1246b032018-02-24 23:34:55 -0600172 if [ ${pod_timeout} -lt 0 ]; then
173 kubectl delete -f - <<< "${yaml_file}"
Gunnar Mills5f811802017-10-25 16:10:27 -0500174 echo "Timeout occurred before pod was Running"
Alanny Lopez0e8ad992017-06-19 15:45:23 -0500175 exit 1
176 else
177 sleep 1
Alanny Lopez1246b032018-02-24 23:34:55 -0600178 let pod_timeout-=1
Alanny Lopez0e8ad992017-06-19 15:45:23 -0500179 fi
Alanny Lopez1246b032018-02-24 23:34:55 -0600180 status=$( ${check_status} | grep Status: )
Alanny Lopez0e8ad992017-06-19 15:45:23 -0500181 done
Alanny Lopezd1bb5b32017-09-20 11:32:40 -0500182 # Tail the logs of the pod, if workaround enabled start executing build script instead.
183 if [[ "${workaround}" == "true" ]]; then
Alanny Lopez1246b032018-02-24 23:34:55 -0600184 kubectl exec -it ${pod_name} -n ${namespace} ${WORKSPACE}/build.sh
Alanny Lopezd1bb5b32017-09-20 11:32:40 -0500185 else
Alanny Lopez1246b032018-02-24 23:34:55 -0600186 kubectl logs -f ${pod_name} -n ${namespace}
Alanny Lopezd1bb5b32017-09-20 11:32:40 -0500187 fi
Alanny Lopez0e8ad992017-06-19 15:45:23 -0500188fi
Alanny Lopez09e18652017-04-24 15:50:33 -0500189
Alanny Lopez0e8ad992017-06-19 15:45:23 -0500190# Delete the object if purge is true
191if [[ "${purge}" == true ]]; then
Alanny Lopez1246b032018-02-24 23:34:55 -0600192 kubectl delete -f - <<< "${yaml_file}"
Alanny Lopez0e8ad992017-06-19 15:45:23 -0500193fi