summaryrefslogtreecommitdiffstats
path: root/docker_files/buildbot_step
blob: 99eafcfa353920a3c9dbcd8735a03d00328906fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#! /bin/bash -e
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# This Source Code Form is "Incompatible With Secondary Licenses", as
# defined by the Mozilla Public License, v. 2.0.

buildbot_step_help() {
  echo "Run a command and wrap it in buildbot step format"
  echo "  $0 - <buildbot step> [bash args...]"
}

bb_time() {
  # Parser dies if this is longer then 6 chars long.
  nano=$(date +%N | cut -c1-6)
  echo "$(date '+%Y-%m-%d %H:%M:%S.')$nano"
}

bb_echo() {
  echo "========= $1 ========="
}

bb_line() {
  local type=$1 # Finished/Started
  local step=$2 # Name of step
  local code=$3 # Exit code
  local duration=$4 # Elapsed time in seconds
  local time=$(bb_time)

  bb_echo "$type $step (results: $code, elapsed: $duration secs) (at $time)"
}

# Intentionally the full name of this executable so sourcing this file also
# works as expected...
buildbot_step() {
  local step=$1
  local command=${@:2}
  local start_time=$(date +%s)
  local exit_code=0

  bb_line "Started" "$step" 0 0
  if eval "$command";
  then
    exit_code=$?
  else
    exit_code=$?
  fi

  local end_time=$(date +%s)
  local duration=$(($end_time-$start_time))

  bb_line "Finished" "$step" $exit_code $duration
  return $exit_code
}


# When this script is not being sourced invoke command directly.
if [ "$(basename $0)" == "buildbot_step" ];
then
  if [ $# -lt 2 ];
  then
    buildbot_step_help
    exit 1
  fi
  buildbot_step "$@"
  exit $?
fi