-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgitlab-pipeline-runners
executable file
·64 lines (53 loc) · 2.05 KB
/
gitlab-pipeline-runners
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
#! /usr/bin/env python3
# List the runners used for a specific pipeline
import argparse
import gitlab
import collections
import humanize
parser = argparse.ArgumentParser()
parser.add_argument("--server", nargs="?")
parser.add_argument("--use-config", "-c", action="store_true", help="Use configuration stored in the current git repository")
parser.add_argument("--project", "-p")
parser.add_argument("--pipeline", "-l", required=True, help="pipeline ID, or branch name for latest pipeline")
args = parser.parse_args()
# If we're using a configuration, load it into args and re-parse to override the settings
if args.use_config:
import configparser, git
repo = git.Repo(search_parent_directories=True)
with repo.config_reader() as config:
for key in ("project", "stage"):
try:
value = config.get("ross-tools", key)
setattr(args, key, value)
except configparser.NoOptionError:
pass
args = parser.parse_args(namespace=args)
# TODO check variables are set
gl = gitlab.Gitlab.from_config(args.server)
gl.auth()
project = gl.projects.get(args.project)
if args.pipeline.isdigit():
pipeline = project.pipelines.get(args.pipeline)
else:
pipeline = project.pipelines.list(ref=args.pipeline, page=1, per_page=1)[0]
pipeline = project.pipelines.get(pipeline.id)
print(f"Found pipeline {pipeline.web_url}")
print()
runners = collections.Counter()
durations = collections.Counter()
print("Pipeline:")
if pipeline.duration:
print(f" Pipeline took {humanize.precisedelta(pipeline.duration)}")
else:
print(f"Pipeline started at {pipeline.started_at}")
print()
print("Jobs:")
for job in pipeline.jobs.list(iterator=True):
runner = job.runner["description"]
runners[runner] += 1
durations[runner] += job.duration
print(f" {job.name}: {runner} (ran for {humanize.precisedelta(job.duration)})")
print()
print("Runners:")
for runner, count in sorted(runners.items()):
print(f" {runner}: {count} jobs (cumulative runtime {humanize.precisedelta(durations[runner])})")