blob: 5b48870f5f01cd8d5b7a1469b44efde08fca4c65 [file] [log] [blame]
Zack Williams704f70a2018-07-18 15:22:35 -07001#!/usr/bin/env bash
2
3# Copyright 2018-present Open Networking Foundation
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
Zack Williamscd8c6072018-08-31 15:24:56 -070017# wait_for_jobs.sh
Zack Williams704f70a2018-07-18 15:22:35 -070018# waits for all kubernetes jobs to complete before exiting
19# inspired by similar scripts in Kolla-Kubernetes and Openstack Helm
20
Zack Williamscd8c6072018-08-31 15:24:56 -070021set -eu -o pipefail
Zack Williams704f70a2018-07-18 15:22:35 -070022fail_wfj=0
23
24# Set these to configure maximum timeout, and interval for checks
25JOBS_TIMEOUT=${JOBS_TIMEOUT:-600}
26CHECK_INTERVAL=${CHECK_INTERVAL:-5}
27KUBECTL_ARGS=${KUBECTL_ARGS:-}
28
29# calculate timeout time
30START_TIME=$(date +%s)
31END_TIME=$((START_TIME + JOBS_TIMEOUT))
32
33echo "wait_for_jobs.sh - Waiting up to ${JOBS_TIMEOUT} seconds for all Kubernetes jobs to complete"
34echo "Number printed is number of currently active jobs"
35
36prev_job_count=0
37
38while true; do
39 NOW=$(date +%s)
40
41 # handle timeout without completion
42 if [ "$NOW" -gt "$END_TIME" ]
43 then
Zack Williamscd8c6072018-08-31 15:24:56 -070044 echo "Jobs didn't complete before timeout of ${JOBS_TIMEOUT} seconds"
Zack Williams704f70a2018-07-18 15:22:35 -070045 fail_wfj=1
46 break
47 fi
48
49 # get list of active jobs, and count of them
50 # jsonpath is picky about string vs comparison quoting, so have to have:
Zack Williamscd8c6072018-08-31 15:24:56 -070051 # shellcheck disable=SC2026,SC2086
Zack Williams704f70a2018-07-18 15:22:35 -070052 active_jobs=$(kubectl get jobs $KUBECTL_ARGS -o=jsonpath='{range .items[?(@.status.active=='1')]}{.metadata.name}{"\n"}{end}')
Zack Williamscd8c6072018-08-31 15:24:56 -070053
54 # this always is 1 or more, as echo leaves a newline in the output which wc
55 # counts as a line
56 active_job_count=$(echo -n "${active_jobs}" | wc -l)
Zack Williams704f70a2018-07-18 15:22:35 -070057
58 # if no jobs active, print runtime and break
59 if [ -z "$active_jobs" ]
60 then
61 runtime=$((NOW - START_TIME))
62 echo ""
63 echo "All jobs completed in $runtime seconds"
64 break
65 fi
66
67 # deal with changes in number of jobs
68 if [ "$active_job_count" -ne "$prev_job_count" ]
69 then
70 echo ""
71 echo "Number of active jobs changed - current jobs:"
72 echo "$active_jobs"
73 fi
74 prev_job_count=$active_job_count
75
76 # print number of remaining jobs every $CHECK_INTERVAL
Zack Williamscd8c6072018-08-31 15:24:56 -070077 echo -n "$active_job_count "
Zack Williams704f70a2018-07-18 15:22:35 -070078 sleep "$CHECK_INTERVAL"
79done
80
81echo ""
82echo "Job Status - Name | Start Time | Completion Time"
Zack Williamscd8c6072018-08-31 15:24:56 -070083# shellcheck disable=SC2086
Zack Williams704f70a2018-07-18 15:22:35 -070084kubectl get jobs $KUBECTL_ARGS -o=jsonpath='{range .items[*]}{.metadata.name}{"\t| "}{.status.startTime}{" | "}{.status.completionTime}{"\n"}{end}'
85
86exit ${fail_wfj}