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