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 | ############################################################################### |
Alanny Lopez | e08e870 | 2018-02-24 18:07:13 -0600 | [diff] [blame^] | 22 | # 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: |
| 27 | # imgplsec The image pull secret used to access registry if needed |
| 28 | # Default: "regkey" |
| 29 | # imgrepo The registry to use to pull and push images |
| 30 | # Default: "master.cfc:8500/openbmc/"" |
| 31 | # jobtimeout The amount of time in seconds that the build will wait for |
| 32 | # 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" |
| 36 | # podtimeout The amount of time in seconds that the build will wait for |
| 37 | # the pod to start running on the cluster. |
| 38 | # Default: "600" |
| 39 | # |
| 40 | # YAML File Variables (No Defaults): |
| 41 | # imgname The name the image that will be passed to the kubernetes |
| 42 | # api to build the containers. The image with the tag |
| 43 | # imgname will be built in the invoker script. This script |
| 44 | # will then tag it to include the registry in the name, push |
| 45 | # it, and update the imgname to be what was pushed to the |
| 46 | # registry. Users should not include the registry in the |
| 47 | # original imgname. |
| 48 | # podname The name of the pod, needed to trace down the logs. |
| 49 | # |
| 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 Lopez | 09e1865 | 2017-04-24 15:50:33 -0500 | [diff] [blame] | 68 | # |
| 69 | ############################################################################### |
Alanny Lopez | e08e870 | 2018-02-24 18:07:13 -0600 | [diff] [blame^] | 70 | build_scripts_dir=${build_scripts_dir:-"$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/.."} |
Alanny Lopez | 09e1865 | 2017-04-24 15:50:33 -0500 | [diff] [blame] | 71 | |
| 72 | # Kubernetes Variables |
| 73 | namespace=${namespace:-openbmc} |
Alanny Lopez | 09e1865 | 2017-04-24 15:50:33 -0500 | [diff] [blame] | 74 | imgrepo=${imgrepo:-master.cfc:8500/openbmc/} |
| 75 | imgplsec=${imgplsec:-regkey} |
Alanny Lopez | 26b2bc5 | 2017-09-29 16:46:00 -0500 | [diff] [blame] | 76 | jobtimeout=${jobtimeout:-60} |
| 77 | podtimeout=${podtimeout:-600} |
Alanny Lopez | 09e1865 | 2017-04-24 15:50:33 -0500 | [diff] [blame] | 78 | |
Alanny Lopez | 0e8ad99 | 2017-06-19 15:45:23 -0500 | [diff] [blame] | 79 | # Options which decide script behavior |
| 80 | invoker=${invoker:-${1}} |
| 81 | log=${log:-${2}} |
| 82 | purge=${purge:-${3}} |
| 83 | launch=${launch:-${4}} |
Alanny Lopez | d1bb5b3 | 2017-09-20 11:32:40 -0500 | [diff] [blame] | 84 | workaround=${workaround:-${log}} |
Alanny Lopez | 0e8ad99 | 2017-06-19 15:45:23 -0500 | [diff] [blame] | 85 | |
| 86 | # 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] | 87 | # Other variables in the template not declared here are declared by invoker |
Alanny Lopez | 0e8ad99 | 2017-06-19 15:45:23 -0500 | [diff] [blame] | 88 | case ${invoker} in |
| 89 | OpenBMC-build) |
Alanny Lopez | a6b7d4b | 2017-10-19 09:58:25 -0500 | [diff] [blame] | 90 | wclaim=${wclaim:-jenkins-slave-space} |
Alanny Lopez | 0e8ad99 | 2017-06-19 15:45:23 -0500 | [diff] [blame] | 91 | sclaim=${sclaim:-shared-state-cache} |
| 92 | oclaim=${oclaim:-openbmc-reference-repo} |
Alanny Lopez | 5118688 | 2017-08-01 16:14:41 -0500 | [diff] [blame] | 93 | newimgname=${newimgname:-${imgrepo}${distro}:${imgtag}-${ARCH}} |
Alanny Lopez | 0e8ad99 | 2017-06-19 15:45:23 -0500 | [diff] [blame] | 94 | podname=${podname:-openbmc${BUILD_ID}-${target}-builder} |
| 95 | ;; |
| 96 | QEMU-build) |
Alanny Lopez | 634ce36 | 2017-06-23 12:57:05 -0500 | [diff] [blame] | 97 | podname=${podname:-qemubuild${BUILD_ID}} |
Alanny Lopez | a6b7d4b | 2017-10-19 09:58:25 -0500 | [diff] [blame] | 98 | wclaim=${wclaim:-jenkins-slave-space} |
Alanny Lopez | 634ce36 | 2017-06-23 12:57:05 -0500 | [diff] [blame] | 99 | qclaim=${qclaim:-qemu-repo} |
Alanny Lopez | 5118688 | 2017-08-01 16:14:41 -0500 | [diff] [blame] | 100 | newimgname="${imgrepo}${imgname}" |
Alanny Lopez | 0e8ad99 | 2017-06-19 15:45:23 -0500 | [diff] [blame] | 101 | ;; |
| 102 | QEMU-launch) |
Alanny Lopez | 07b4d5b | 2017-08-01 16:24:07 -0500 | [diff] [blame] | 103 | deployname=${deployname:-qemu-launch-deployment} |
| 104 | podname=${podname:-qemu-instance} |
| 105 | replicas=${replicas:-5} |
Alanny Lopez | a6b7d4b | 2017-10-19 09:58:25 -0500 | [diff] [blame] | 106 | wclaim=${wclaim:-jenkins-slave-space} |
| 107 | jenkins_subpath=${jenkins_subpath:-Openbmc-Build/openbmc/build} |
Alanny Lopez | eba5ad4 | 2017-08-18 14:48:37 -0500 | [diff] [blame] | 108 | newimgname="${imgrepo}qemu-instance" |
Alanny Lopez | 0e8ad99 | 2017-06-19 15:45:23 -0500 | [diff] [blame] | 109 | ;; |
| 110 | XCAT-launch) |
| 111 | ;; |
| 112 | generic) |
| 113 | ;; |
| 114 | *) |
| 115 | exit 1 |
| 116 | ;; |
| 117 | esac |
| 118 | |
Alanny Lopez | 26b2bc5 | 2017-09-29 16:46:00 -0500 | [diff] [blame] | 119 | # 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] | 120 | docker tag ${imgname} ${newimgname} |
| 121 | imgname=${newimgname} |
Alanny Lopez | 09e1865 | 2017-04-24 15:50:33 -0500 | [diff] [blame] | 122 | |
| 123 | # Push the image that was built to the image repository |
| 124 | docker push ${imgname} |
| 125 | |
Alanny Lopez | eba5ad4 | 2017-08-18 14:48:37 -0500 | [diff] [blame] | 126 | if [[ "$ARCH" == x86_64 ]]; then |
| 127 | ARCH=amd64 |
| 128 | fi |
Alanny Lopez | 26b2bc5 | 2017-09-29 16:46:00 -0500 | [diff] [blame] | 129 | |
Alanny Lopez | d1bb5b3 | 2017-09-20 11:32:40 -0500 | [diff] [blame] | 130 | extras="" |
| 131 | if [[ "${workaround}" == "true" ]]; then |
| 132 | extras+="-v2" |
| 133 | fi |
| 134 | |
Alanny Lopez | e08e870 | 2018-02-24 18:07:13 -0600 | [diff] [blame^] | 135 | yamlfile=$(eval "echo \"$(<${build_scripts_dir}/kubernetes/Templates/${invoker}-${launch}${extras}.yaml)\"") |
Alanny Lopez | 09e1865 | 2017-04-24 15:50:33 -0500 | [diff] [blame] | 136 | kubectl create -f - <<< "${yamlfile}" |
| 137 | |
Alanny Lopez | 26b2bc5 | 2017-09-29 16:46:00 -0500 | [diff] [blame] | 138 | # If launch is a job we have to find the podname with identifiers |
| 139 | if [[ "${launch}" == "job" ]]; then |
| 140 | while [ -z ${replace} ] |
| 141 | do |
| 142 | if [ ${jobtimeout} -lt 0 ]; then |
| 143 | kubectl delete -f - <<< "${yamlfile}" |
Gunnar Mills | 5f81180 | 2017-10-25 16:10:27 -0500 | [diff] [blame] | 144 | echo "Timeout occurred before job was present in the API" |
Alanny Lopez | 26b2bc5 | 2017-09-29 16:46:00 -0500 | [diff] [blame] | 145 | exit 1 |
| 146 | else |
| 147 | sleep 1 |
| 148 | let jobtimeout-=1 |
| 149 | fi |
| 150 | replace=$(kubectl get pods -n ${namespaces} | grep ${podname} | awk 'print $1') |
| 151 | done |
| 152 | podname=${replace} |
| 153 | fi |
| 154 | |
| 155 | |
Alanny Lopez | 09e1865 | 2017-04-24 15:50:33 -0500 | [diff] [blame] | 156 | # Once pod is running track logs |
Alanny Lopez | 0e8ad99 | 2017-06-19 15:45:23 -0500 | [diff] [blame] | 157 | if [[ "${log}" == true ]]; then |
| 158 | # Wait for Pod to be running |
Alanny Lopez | 259185c | 2017-10-12 15:39:03 -0500 | [diff] [blame] | 159 | checkstatus="kubectl describe pod ${podname} -n ${namespace}" |
| 160 | status=$( ${checkstatus} | grep Status: ) |
Alanny Lopez | 26b2bc5 | 2017-09-29 16:46:00 -0500 | [diff] [blame] | 161 | while [ -z "$( echo ${status} | grep Running)" ] |
Alanny Lopez | 5118688 | 2017-08-01 16:14:41 -0500 | [diff] [blame] | 162 | do |
Alanny Lopez | 26b2bc5 | 2017-09-29 16:46:00 -0500 | [diff] [blame] | 163 | if [ ${podtimeout} -lt 0 ]; then |
Alanny Lopez | 0e8ad99 | 2017-06-19 15:45:23 -0500 | [diff] [blame] | 164 | kubectl delete -f - <<< "${yamlfile}" |
Gunnar Mills | 5f81180 | 2017-10-25 16:10:27 -0500 | [diff] [blame] | 165 | echo "Timeout occurred before pod was Running" |
Alanny Lopez | 0e8ad99 | 2017-06-19 15:45:23 -0500 | [diff] [blame] | 166 | exit 1 |
| 167 | else |
| 168 | sleep 1 |
Alanny Lopez | 26b2bc5 | 2017-09-29 16:46:00 -0500 | [diff] [blame] | 169 | let podtimeout-=1 |
Alanny Lopez | 0e8ad99 | 2017-06-19 15:45:23 -0500 | [diff] [blame] | 170 | fi |
Alanny Lopez | 259185c | 2017-10-12 15:39:03 -0500 | [diff] [blame] | 171 | status=$( ${checkstatus} | grep Status: ) |
Alanny Lopez | 0e8ad99 | 2017-06-19 15:45:23 -0500 | [diff] [blame] | 172 | done |
Alanny Lopez | d1bb5b3 | 2017-09-20 11:32:40 -0500 | [diff] [blame] | 173 | # Tail the logs of the pod, if workaround enabled start executing build script instead. |
| 174 | if [[ "${workaround}" == "true" ]]; then |
| 175 | kubectl exec -it ${podname} -n ${namespace} ${WORKSPACE}/build.sh |
| 176 | else |
| 177 | kubectl logs -f ${podname} -n ${namespace} |
| 178 | fi |
Alanny Lopez | 0e8ad99 | 2017-06-19 15:45:23 -0500 | [diff] [blame] | 179 | fi |
Alanny Lopez | 09e1865 | 2017-04-24 15:50:33 -0500 | [diff] [blame] | 180 | |
Alanny Lopez | 0e8ad99 | 2017-06-19 15:45:23 -0500 | [diff] [blame] | 181 | # Delete the object if purge is true |
| 182 | if [[ "${purge}" == true ]]; then |
| 183 | kubectl delete -f - <<< "${yamlfile}" |
| 184 | fi |