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 |
| 26 | # timeout = the amount of time in seconds that the build will wait for |
| 27 | # the pod to start running on the cluster |
Alanny Lopez | 5118688 | 2017-08-01 16:14:41 -0500 | [diff] [blame] | 28 | # 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 Lopez | 09e1865 | 2017-04-24 15:50:33 -0500 | [diff] [blame] | 34 | # podname = the name of the pod, will be needed to trace down the logs |
| 35 | # |
| 36 | ############################################################################### |
Alanny Lopez | 0e8ad99 | 2017-06-19 15:45:23 -0500 | [diff] [blame] | 37 | # 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 Lopez | 09e1865 | 2017-04-24 15:50:33 -0500 | [diff] [blame] | 46 | |
| 47 | # Kubernetes Variables |
| 48 | namespace=${namespace:-openbmc} |
Alanny Lopez | 09e1865 | 2017-04-24 15:50:33 -0500 | [diff] [blame] | 49 | imgrepo=${imgrepo:-master.cfc:8500/openbmc/} |
| 50 | imgplsec=${imgplsec:-regkey} |
| 51 | timeout=${timeout:-60} |
| 52 | |
Alanny Lopez | 0e8ad99 | 2017-06-19 15:45:23 -0500 | [diff] [blame] | 53 | # Options which decide script behavior |
| 54 | invoker=${invoker:-${1}} |
| 55 | log=${log:-${2}} |
| 56 | purge=${purge:-${3}} |
| 57 | launch=${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. |
| 61 | case ${invoker} in |
| 62 | OpenBMC-build) |
Alanny Lopez | eba5ad4 | 2017-08-18 14:48:37 -0500 | [diff] [blame^] | 63 | hclaim=${hclaim:-jenkins-slave-space} |
Alanny Lopez | 0e8ad99 | 2017-06-19 15:45:23 -0500 | [diff] [blame] | 64 | sclaim=${sclaim:-shared-state-cache} |
| 65 | oclaim=${oclaim:-openbmc-reference-repo} |
Alanny Lopez | 5118688 | 2017-08-01 16:14:41 -0500 | [diff] [blame] | 66 | newimgname=${newimgname:-${imgrepo}${distro}:${imgtag}-${ARCH}} |
Alanny Lopez | 0e8ad99 | 2017-06-19 15:45:23 -0500 | [diff] [blame] | 67 | podname=${podname:-openbmc${BUILD_ID}-${target}-builder} |
| 68 | ;; |
| 69 | QEMU-build) |
Alanny Lopez | 634ce36 | 2017-06-23 12:57:05 -0500 | [diff] [blame] | 70 | podname=${podname:-qemubuild${BUILD_ID}} |
Alanny Lopez | eba5ad4 | 2017-08-18 14:48:37 -0500 | [diff] [blame^] | 71 | hclaim=${hclaim:-jenkins-slave-space} |
Alanny Lopez | 634ce36 | 2017-06-23 12:57:05 -0500 | [diff] [blame] | 72 | qclaim=${qclaim:-qemu-repo} |
Alanny Lopez | 5118688 | 2017-08-01 16:14:41 -0500 | [diff] [blame] | 73 | newimgname="${imgrepo}${imgname}" |
Alanny Lopez | 0e8ad99 | 2017-06-19 15:45:23 -0500 | [diff] [blame] | 74 | ;; |
| 75 | QEMU-launch) |
Alanny Lopez | 07b4d5b | 2017-08-01 16:24:07 -0500 | [diff] [blame] | 76 | deployname=${deployname:-qemu-launch-deployment} |
| 77 | podname=${podname:-qemu-instance} |
| 78 | replicas=${replicas:-5} |
Alanny Lopez | eba5ad4 | 2017-08-18 14:48:37 -0500 | [diff] [blame^] | 79 | hclaim=${hclaim:-jenkins-slave-space} |
Alanny Lopez | 07b4d5b | 2017-08-01 16:24:07 -0500 | [diff] [blame] | 80 | jenkins_subpath=${jenkins_subpath:-workspace/Openbmc-Build/build} |
Alanny Lopez | eba5ad4 | 2017-08-18 14:48:37 -0500 | [diff] [blame^] | 81 | newimgname="${imgrepo}qemu-instance" |
Alanny Lopez | 0e8ad99 | 2017-06-19 15:45:23 -0500 | [diff] [blame] | 82 | ;; |
| 83 | XCAT-launch) |
| 84 | ;; |
| 85 | generic) |
| 86 | ;; |
| 87 | *) |
| 88 | exit 1 |
| 89 | ;; |
| 90 | esac |
| 91 | |
Alanny Lopez | 09e1865 | 2017-04-24 15:50:33 -0500 | [diff] [blame] | 92 | |
Alanny Lopez | 5118688 | 2017-08-01 16:14:41 -0500 | [diff] [blame] | 93 | # Tag the image created by the invoker with the new image name that includes the imgrepo |
| 94 | docker tag ${imgname} ${newimgname} |
| 95 | imgname=${newimgname} |
Alanny Lopez | 09e1865 | 2017-04-24 15:50:33 -0500 | [diff] [blame] | 96 | |
| 97 | # Push the image that was built to the image repository |
| 98 | docker push ${imgname} |
| 99 | |
Alanny Lopez | eba5ad4 | 2017-08-18 14:48:37 -0500 | [diff] [blame^] | 100 | if [[ "$ARCH" == x86_64 ]]; then |
| 101 | ARCH=amd64 |
| 102 | fi |
Alanny Lopez | 634ce36 | 2017-06-23 12:57:05 -0500 | [diff] [blame] | 103 | yamlfile=$(eval "echo \"$(<./kubernetes/Templates/${invoker}-${launch}.yaml)\"" ) |
Alanny Lopez | 09e1865 | 2017-04-24 15:50:33 -0500 | [diff] [blame] | 104 | kubectl create -f - <<< "${yamlfile}" |
| 105 | |
Alanny Lopez | 09e1865 | 2017-04-24 15:50:33 -0500 | [diff] [blame] | 106 | # Once pod is running track logs |
Alanny Lopez | 0e8ad99 | 2017-06-19 15:45:23 -0500 | [diff] [blame] | 107 | if [[ "${log}" == true ]]; then |
| 108 | # Wait for Pod to be running |
Alanny Lopez | 5118688 | 2017-08-01 16:14:41 -0500 | [diff] [blame] | 109 | while [ -z "$(kubectl describe pod ${podname} -n ${namespace} | grep Status: | grep Running)" ] |
| 110 | do |
Alanny Lopez | 0e8ad99 | 2017-06-19 15:45:23 -0500 | [diff] [blame] | 111 | if [ ${timeout} -lt 0 ];then |
| 112 | kubectl delete -f - <<< "${yamlfile}" |
| 113 | echo "Timeout Occured: Job failed to start running in time" |
| 114 | exit 1 |
| 115 | else |
| 116 | sleep 1 |
| 117 | let timeout-=1 |
| 118 | fi |
| 119 | done |
| 120 | kubectl logs -f ${podname} -n ${namespace} |
| 121 | fi |
Alanny Lopez | 09e1865 | 2017-04-24 15:50:33 -0500 | [diff] [blame] | 122 | |
Alanny Lopez | 0e8ad99 | 2017-06-19 15:45:23 -0500 | [diff] [blame] | 123 | # Delete the object if purge is true |
| 124 | if [[ "${purge}" == true ]]; then |
| 125 | kubectl delete -f - <<< "${yamlfile}" |
| 126 | fi |