Alanny Lopez | 09e1865 | 2017-04-24 15:50:33 -0500 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | ############################################################################### |
| 3 | # |
| 4 | # Script used to assist in launching Kubernetes jobs/pods. Expects to be used |
Alanny Lopez | 0e8ad99 | 2017-06-19 15:45:23 -0500 | [diff] [blame] | 5 | # as an supplemental script to the scripts that want to launch their containers |
| 6 | # on a Kubernetes cluster. |
Alanny Lopez | 09e1865 | 2017-04-24 15:50:33 -0500 | [diff] [blame] | 7 | # |
| 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 Lopez | 09e1865 | 2017-04-24 15:50:33 -0500 | [diff] [blame] | 24 | # registry = the registry to use to pull and push images |
| 25 | # imgplsec = the image pull secret used to access registry if needed |
Alanny Lopez | 26b2bc5 | 2017-09-29 16:46:00 -0500 | [diff] [blame] | 26 | # 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 Lopez | 5118688 | 2017-08-01 16:14:41 -0500 | [diff] [blame] | 30 | # 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 Lopez | 09e1865 | 2017-04-24 15:50:33 -0500 | [diff] [blame] | 36 | # podname = the name of the pod, will be needed to trace down the logs |
| 37 | # |
| 38 | ############################################################################### |
Alanny Lopez | 0e8ad99 | 2017-06-19 15:45:23 -0500 | [diff] [blame] | 39 | # 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 |
Alanny Lopez | d1bb5b3 | 2017-09-20 11:32:40 -0500 | [diff] [blame^] | 46 | # workaround = Used to enable the logging workaround, when set will launch a |
| 47 | # modified template that waits for a command. In most cases it |
| 48 | # will be waiting to have a script run via kubectl exec. Needed |
| 49 | # when using a version of Kubernetes that has known issues that |
| 50 | # impact the retrieval of container logs when using kubectl. |
| 51 | # Defaulting to be true whenever logging is enabled until ICP |
| 52 | # upgrades their Kubernetes version. |
Alanny Lopez | 0e8ad99 | 2017-06-19 15:45:23 -0500 | [diff] [blame] | 53 | ############################################################################### |
Alanny Lopez | 09e1865 | 2017-04-24 15:50:33 -0500 | [diff] [blame] | 54 | |
| 55 | # Kubernetes Variables |
| 56 | namespace=${namespace:-openbmc} |
Alanny Lopez | 09e1865 | 2017-04-24 15:50:33 -0500 | [diff] [blame] | 57 | imgrepo=${imgrepo:-master.cfc:8500/openbmc/} |
| 58 | imgplsec=${imgplsec:-regkey} |
Alanny Lopez | 26b2bc5 | 2017-09-29 16:46:00 -0500 | [diff] [blame] | 59 | jobtimeout=${jobtimeout:-60} |
| 60 | podtimeout=${podtimeout:-600} |
Alanny Lopez | 09e1865 | 2017-04-24 15:50:33 -0500 | [diff] [blame] | 61 | |
Alanny Lopez | 0e8ad99 | 2017-06-19 15:45:23 -0500 | [diff] [blame] | 62 | # Options which decide script behavior |
| 63 | invoker=${invoker:-${1}} |
| 64 | log=${log:-${2}} |
| 65 | purge=${purge:-${3}} |
| 66 | launch=${launch:-${4}} |
Alanny Lopez | d1bb5b3 | 2017-09-20 11:32:40 -0500 | [diff] [blame^] | 67 | workaround=${workaround:-${log}} |
Alanny Lopez | 0e8ad99 | 2017-06-19 15:45:23 -0500 | [diff] [blame] | 68 | |
| 69 | # Set the variables for the specific invoker to fill in the YAML template |
Alanny Lopez | 26b2bc5 | 2017-09-29 16:46:00 -0500 | [diff] [blame] | 70 | # Other variables in the template not declared here are declared by invoker |
Alanny Lopez | 0e8ad99 | 2017-06-19 15:45:23 -0500 | [diff] [blame] | 71 | case ${invoker} in |
| 72 | OpenBMC-build) |
Alanny Lopez | eba5ad4 | 2017-08-18 14:48:37 -0500 | [diff] [blame] | 73 | hclaim=${hclaim:-jenkins-slave-space} |
Alanny Lopez | 0e8ad99 | 2017-06-19 15:45:23 -0500 | [diff] [blame] | 74 | sclaim=${sclaim:-shared-state-cache} |
| 75 | oclaim=${oclaim:-openbmc-reference-repo} |
Alanny Lopez | 5118688 | 2017-08-01 16:14:41 -0500 | [diff] [blame] | 76 | newimgname=${newimgname:-${imgrepo}${distro}:${imgtag}-${ARCH}} |
Alanny Lopez | 0e8ad99 | 2017-06-19 15:45:23 -0500 | [diff] [blame] | 77 | podname=${podname:-openbmc${BUILD_ID}-${target}-builder} |
| 78 | ;; |
| 79 | QEMU-build) |
Alanny Lopez | 634ce36 | 2017-06-23 12:57:05 -0500 | [diff] [blame] | 80 | podname=${podname:-qemubuild${BUILD_ID}} |
Alanny Lopez | eba5ad4 | 2017-08-18 14:48:37 -0500 | [diff] [blame] | 81 | hclaim=${hclaim:-jenkins-slave-space} |
Alanny Lopez | 634ce36 | 2017-06-23 12:57:05 -0500 | [diff] [blame] | 82 | qclaim=${qclaim:-qemu-repo} |
Alanny Lopez | 5118688 | 2017-08-01 16:14:41 -0500 | [diff] [blame] | 83 | newimgname="${imgrepo}${imgname}" |
Alanny Lopez | 0e8ad99 | 2017-06-19 15:45:23 -0500 | [diff] [blame] | 84 | ;; |
| 85 | QEMU-launch) |
Alanny Lopez | 07b4d5b | 2017-08-01 16:24:07 -0500 | [diff] [blame] | 86 | deployname=${deployname:-qemu-launch-deployment} |
| 87 | podname=${podname:-qemu-instance} |
| 88 | replicas=${replicas:-5} |
Alanny Lopez | eba5ad4 | 2017-08-18 14:48:37 -0500 | [diff] [blame] | 89 | hclaim=${hclaim:-jenkins-slave-space} |
Alanny Lopez | b6c42d8 | 2017-10-13 09:07:53 -0500 | [diff] [blame] | 90 | jenkins_subpath=${jenkins_subpath:-workspace/Openbmc-Build/openbmc/build} |
Alanny Lopez | eba5ad4 | 2017-08-18 14:48:37 -0500 | [diff] [blame] | 91 | newimgname="${imgrepo}qemu-instance" |
Alanny Lopez | 0e8ad99 | 2017-06-19 15:45:23 -0500 | [diff] [blame] | 92 | ;; |
| 93 | XCAT-launch) |
| 94 | ;; |
| 95 | generic) |
| 96 | ;; |
| 97 | *) |
| 98 | exit 1 |
| 99 | ;; |
| 100 | esac |
| 101 | |
Alanny Lopez | 26b2bc5 | 2017-09-29 16:46:00 -0500 | [diff] [blame] | 102 | # Tag the image created by the invoker with a name that includes the imgrepo |
Alanny Lopez | 5118688 | 2017-08-01 16:14:41 -0500 | [diff] [blame] | 103 | docker tag ${imgname} ${newimgname} |
| 104 | imgname=${newimgname} |
Alanny Lopez | 09e1865 | 2017-04-24 15:50:33 -0500 | [diff] [blame] | 105 | |
| 106 | # Push the image that was built to the image repository |
| 107 | docker push ${imgname} |
| 108 | |
Alanny Lopez | eba5ad4 | 2017-08-18 14:48:37 -0500 | [diff] [blame] | 109 | if [[ "$ARCH" == x86_64 ]]; then |
| 110 | ARCH=amd64 |
| 111 | fi |
Alanny Lopez | 26b2bc5 | 2017-09-29 16:46:00 -0500 | [diff] [blame] | 112 | |
Alanny Lopez | d1bb5b3 | 2017-09-20 11:32:40 -0500 | [diff] [blame^] | 113 | extras="" |
| 114 | if [[ "${workaround}" == "true" ]]; then |
| 115 | extras+="-v2" |
| 116 | fi |
| 117 | |
| 118 | yamlfile=$(eval "echo \"$(<./kubernetes/Templates/${invoker}-${launch}${extras}.yaml)\"") |
Alanny Lopez | 09e1865 | 2017-04-24 15:50:33 -0500 | [diff] [blame] | 119 | kubectl create -f - <<< "${yamlfile}" |
| 120 | |
Alanny Lopez | 26b2bc5 | 2017-09-29 16:46:00 -0500 | [diff] [blame] | 121 | # If launch is a job we have to find the podname with identifiers |
| 122 | if [[ "${launch}" == "job" ]]; then |
| 123 | while [ -z ${replace} ] |
| 124 | do |
| 125 | if [ ${jobtimeout} -lt 0 ]; then |
| 126 | kubectl delete -f - <<< "${yamlfile}" |
Gunnar Mills | 5f81180 | 2017-10-25 16:10:27 -0500 | [diff] [blame] | 127 | echo "Timeout occurred before job was present in the API" |
Alanny Lopez | 26b2bc5 | 2017-09-29 16:46:00 -0500 | [diff] [blame] | 128 | exit 1 |
| 129 | else |
| 130 | sleep 1 |
| 131 | let jobtimeout-=1 |
| 132 | fi |
| 133 | replace=$(kubectl get pods -n ${namespaces} | grep ${podname} | awk 'print $1') |
| 134 | done |
| 135 | podname=${replace} |
| 136 | fi |
| 137 | |
| 138 | |
Alanny Lopez | 09e1865 | 2017-04-24 15:50:33 -0500 | [diff] [blame] | 139 | # Once pod is running track logs |
Alanny Lopez | 0e8ad99 | 2017-06-19 15:45:23 -0500 | [diff] [blame] | 140 | if [[ "${log}" == true ]]; then |
| 141 | # Wait for Pod to be running |
Alanny Lopez | 259185c | 2017-10-12 15:39:03 -0500 | [diff] [blame] | 142 | checkstatus="kubectl describe pod ${podname} -n ${namespace}" |
| 143 | status=$( ${checkstatus} | grep Status: ) |
Alanny Lopez | 26b2bc5 | 2017-09-29 16:46:00 -0500 | [diff] [blame] | 144 | while [ -z "$( echo ${status} | grep Running)" ] |
Alanny Lopez | 5118688 | 2017-08-01 16:14:41 -0500 | [diff] [blame] | 145 | do |
Alanny Lopez | 26b2bc5 | 2017-09-29 16:46:00 -0500 | [diff] [blame] | 146 | if [ ${podtimeout} -lt 0 ]; then |
Alanny Lopez | 0e8ad99 | 2017-06-19 15:45:23 -0500 | [diff] [blame] | 147 | kubectl delete -f - <<< "${yamlfile}" |
Gunnar Mills | 5f81180 | 2017-10-25 16:10:27 -0500 | [diff] [blame] | 148 | echo "Timeout occurred before pod was Running" |
Alanny Lopez | 0e8ad99 | 2017-06-19 15:45:23 -0500 | [diff] [blame] | 149 | exit 1 |
| 150 | else |
| 151 | sleep 1 |
Alanny Lopez | 26b2bc5 | 2017-09-29 16:46:00 -0500 | [diff] [blame] | 152 | let podtimeout-=1 |
Alanny Lopez | 0e8ad99 | 2017-06-19 15:45:23 -0500 | [diff] [blame] | 153 | fi |
Alanny Lopez | 259185c | 2017-10-12 15:39:03 -0500 | [diff] [blame] | 154 | status=$( ${checkstatus} | grep Status: ) |
Alanny Lopez | 0e8ad99 | 2017-06-19 15:45:23 -0500 | [diff] [blame] | 155 | done |
Alanny Lopez | d1bb5b3 | 2017-09-20 11:32:40 -0500 | [diff] [blame^] | 156 | # Tail the logs of the pod, if workaround enabled start executing build script instead. |
| 157 | if [[ "${workaround}" == "true" ]]; then |
| 158 | kubectl exec -it ${podname} -n ${namespace} ${WORKSPACE}/build.sh |
| 159 | else |
| 160 | kubectl logs -f ${podname} -n ${namespace} |
| 161 | fi |
Alanny Lopez | 0e8ad99 | 2017-06-19 15:45:23 -0500 | [diff] [blame] | 162 | fi |
Alanny Lopez | 09e1865 | 2017-04-24 15:50:33 -0500 | [diff] [blame] | 163 | |
Alanny Lopez | 0e8ad99 | 2017-06-19 15:45:23 -0500 | [diff] [blame] | 164 | # Delete the object if purge is true |
| 165 | if [[ "${purge}" == true ]]; then |
| 166 | kubectl delete -f - <<< "${yamlfile}" |
| 167 | fi |