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 |
| 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 Lopez | 0e8ad99 | 2017-06-19 15:45:23 -0500 | [diff] [blame^] | 33 | # 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 Lopez | 09e1865 | 2017-04-24 15:50:33 -0500 | [diff] [blame] | 42 | |
| 43 | # Kubernetes Variables |
| 44 | namespace=${namespace:-openbmc} |
Alanny Lopez | 09e1865 | 2017-04-24 15:50:33 -0500 | [diff] [blame] | 45 | imgrepo=${imgrepo:-master.cfc:8500/openbmc/} |
| 46 | imgplsec=${imgplsec:-regkey} |
| 47 | timeout=${timeout:-60} |
| 48 | |
Alanny Lopez | 0e8ad99 | 2017-06-19 15:45:23 -0500 | [diff] [blame^] | 49 | # Options which decide script behavior |
| 50 | invoker=${invoker:-${1}} |
| 51 | log=${log:-${2}} |
| 52 | purge=${purge:-${3}} |
| 53 | launch=${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. |
| 57 | case ${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 | ;; |
| 76 | esac |
| 77 | |
Alanny Lopez | 09e1865 | 2017-04-24 15:50:33 -0500 | [diff] [blame] | 78 | |
| 79 | # Build the Docker image, using the Dockerfile carried from build-setup.sh |
| 80 | docker build -t ${imgname} - <<< "${Dockerfile}" |
| 81 | |
| 82 | # Push the image that was built to the image repository |
| 83 | docker push ${imgname} |
| 84 | |
Alanny Lopez | 0e8ad99 | 2017-06-19 15:45:23 -0500 | [diff] [blame^] | 85 | yamlfile=$(eval "echo \"$(<./Templates/${invoker}-${launch}.yaml)\"" ) |
Alanny Lopez | 09e1865 | 2017-04-24 15:50:33 -0500 | [diff] [blame] | 86 | kubectl create -f - <<< "${yamlfile}" |
| 87 | |
Alanny Lopez | 09e1865 | 2017-04-24 15:50:33 -0500 | [diff] [blame] | 88 | # Once pod is running track logs |
Alanny Lopez | 0e8ad99 | 2017-06-19 15:45:23 -0500 | [diff] [blame^] | 89 | if [[ "${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} |
| 102 | fi |
Alanny Lopez | 09e1865 | 2017-04-24 15:50:33 -0500 | [diff] [blame] | 103 | |
Alanny Lopez | 0e8ad99 | 2017-06-19 15:45:23 -0500 | [diff] [blame^] | 104 | # Delete the object if purge is true |
| 105 | if [[ "${purge}" == true ]]; then |
| 106 | kubectl delete -f - <<< "${yamlfile}" |
| 107 | fi |