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 |
| 5 | # as an supplemental script to the build-setup.sh script as such will use some |
| 6 | # of the variables it expects to carry over from that script. |
| 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 |
| 24 | # hclaim = name of the Jenkins slave home PVC on Kubernetes cluster |
| 25 | # sclaim = name of the shared state cache PVC on Kubernetes cluster |
| 26 | # oclaim = name of OpenBMC cache repo PVC on the Kubernetes cluster |
| 27 | # registry = the registry to use to pull and push images |
| 28 | # imgplsec = the image pull secret used to access registry if needed |
| 29 | # timeout = the amount of time in seconds that the build will wait for |
| 30 | # the pod to start running on the cluster |
| 31 | # imgname = the name the image will be given when built, must include |
| 32 | # the repo in name for the push command to work. |
| 33 | # podname = the name of the pod, will be needed to trace down the logs |
| 34 | # |
| 35 | ############################################################################### |
| 36 | |
| 37 | # Kubernetes Variables |
| 38 | namespace=${namespace:-openbmc} |
| 39 | hclaim=${hclaim:-jenkins} |
| 40 | sclaim=${sclaim:-shared-state-cache} |
| 41 | oclaim=${oclaim:-openbmc-reference-repo} |
| 42 | imgrepo=${imgrepo:-master.cfc:8500/openbmc/} |
| 43 | imgplsec=${imgplsec:-regkey} |
| 44 | timeout=${timeout:-60} |
| 45 | |
| 46 | # Give the Docker image and the pod a name |
| 47 | imgname=${imgname:-${imgrepo}${distro}:${imgtag}-${ARCH}} |
| 48 | podname=${podname:-openbmc${BUILD_ID}-${target}-builder} |
| 49 | |
| 50 | # Build the Docker image, using the Dockerfile carried from build-setup.sh |
| 51 | docker build -t ${imgname} - <<< "${Dockerfile}" |
| 52 | |
| 53 | # Push the image that was built to the image repository |
| 54 | docker push ${imgname} |
| 55 | |
| 56 | if [[ "${launch}" == "pod" ]]; then |
| 57 | yamlfile=$(cat << EOF |
| 58 | apiVersion: v1 |
| 59 | kind: Pod |
| 60 | metadata: |
| 61 | name: ${podname} |
| 62 | namespace: ${namespace} |
| 63 | spec: |
| 64 | nodeSelector: |
| 65 | worker: "true" |
| 66 | arch: ${ARCH} |
| 67 | volumes: |
| 68 | - name: home |
| 69 | persistentVolumeClaim: |
| 70 | claimName: ${hclaim} |
| 71 | - name: sscdir |
| 72 | persistentVolumeClaim: |
| 73 | claimName: ${sclaim} |
| 74 | - name: obmccache |
| 75 | persistentVolumeClaim: |
| 76 | claimName: ${oclaim} |
| 77 | hostNetwork: True |
| 78 | containers: |
| 79 | - image: ${imgname} |
| 80 | name: builder |
| 81 | command: ["${WORKSPACE}/build.sh"] |
| 82 | workingDir: ${HOME} |
| 83 | env: |
| 84 | - name: WORKSPACE |
| 85 | value: ${WORKSPACE} |
| 86 | - name: obmcdir |
| 87 | value: ${obmcdir} |
| 88 | securityContext: |
| 89 | capabilities: |
| 90 | add: |
| 91 | - SYS_ADMIN |
| 92 | volumeMounts: |
| 93 | - name: home |
| 94 | mountPath: ${HOME} |
| 95 | - name: sscdir |
| 96 | mountPath: ${sscdir} |
| 97 | - name: obmccache |
| 98 | mountPath: ${ocache} |
| 99 | imagePullSecrets: |
| 100 | - name: ${imgplsec} |
| 101 | EOF |
| 102 | ) |
| 103 | |
| 104 | elif [[ "${launch}" == "job" ]]; then |
| 105 | yamlfile=$(cat << EOF |
| 106 | apiVersion: batch/v1 |
| 107 | kind: Job |
| 108 | metadata: |
| 109 | name: openbmc${BUILD_ID}-${target} |
| 110 | namespace: ${namespace} |
| 111 | labels: |
| 112 | app: openbmc |
| 113 | stage: build |
| 114 | spec: |
| 115 | template: |
| 116 | metadata: |
| 117 | name: ${podname} |
| 118 | labels: |
| 119 | target: ${target} |
| 120 | spec: |
| 121 | nodeSelector: |
| 122 | worker: "true" |
| 123 | arch: ${ARCH} |
| 124 | volumes: |
| 125 | - name: home |
| 126 | persistentVolumeClaim: |
| 127 | claimName: ${hclaim} |
| 128 | - name: sscdir |
| 129 | persistentVolumeClaim: |
| 130 | claimName: ${sclaim} |
| 131 | - name: obmccache |
| 132 | persistentVolumeClaim: |
| 133 | claimName: ${oclaim} |
| 134 | restartPolicy: Never |
| 135 | hostNetwork: True |
| 136 | containers: |
| 137 | - image: ${imgname} |
| 138 | name: builder |
| 139 | command: ["${WORKSPACE}/build.sh"] |
| 140 | workingDir: ${HOME} |
| 141 | env: |
| 142 | - name: WORKSPACE |
| 143 | value: ${WORKSPACE} |
| 144 | - name: obmcdir |
| 145 | value: ${obmcdir} |
| 146 | securityContext: |
| 147 | capabilities: |
| 148 | add: |
| 149 | - SYS_ADMIN |
| 150 | volumeMounts: |
| 151 | - name: home |
| 152 | mountPath: ${HOME} |
| 153 | - name: sscdir |
| 154 | mountPath: ${sscdir} |
| 155 | - name: obmccache |
| 156 | mountPath: ${ocache} |
| 157 | imagePullSecrets: |
| 158 | - name: ${imgplsec} |
| 159 | EOF |
| 160 | ) |
| 161 | fi |
| 162 | |
| 163 | kubectl create -f - <<< "${yamlfile}" |
| 164 | |
| 165 | # Wait for Pod to be running before tailing log file |
| 166 | while [ -z "$(kubectl describe pod ${podname} -n ${namespace} | grep Status: | grep Running)" ]; do |
| 167 | if [ ${timeout} -lt 0 ];then |
| 168 | kubectl delete -f - <<< "${yamlfile}" |
| 169 | echo "Timeout Occured: Job failed to start running in time" |
| 170 | exit 1 |
| 171 | else |
| 172 | sleep 1 |
| 173 | let timeout-=1 |
| 174 | fi |
| 175 | done |
| 176 | |
| 177 | # Once pod is running track logs |
| 178 | kubectl logs -f ${podname} -n ${namespace} |
| 179 | |
| 180 | # When job is completed wipe the job |
| 181 | kubectl delete -f - <<< "${yamlfile}" |