-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitlab-runner-top
executable file
·54 lines (44 loc) · 1.75 KB
/
gitlab-runner-top
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
#! /usr/bin/env python3
# List all active jobs on the runners associated with this project
import argparse
import gitlab
import humanize
parser = argparse.ArgumentParser()
parser.add_argument("--server", nargs="?")
parser.add_argument("--project", "-p")
parser.add_argument("--use-config", "-c", action="store_true")
parser.add_argument("--tail", "-t", type=int, const=1, nargs="?")
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",):
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)
runners = project.runners.list()
for runner in runners:
print(f"Runner {runner.description} ({runner.status})")
runner = gl.runners.get(runner.id, lazy=True)
jobs = runner.jobs.list(status="running")
for job in sorted(jobs, key=lambda j: j.duration, reverse=True):
print(f"- {job.name} (running for {humanize.precisedelta(job.duration)})")
print(f" From {job.ref} for {job.user['name']}")
print(f" {job.web_url}")
if args.tail:
print(" ---")
job = project.jobs.get(job.id, lazy=True)
trace = job.trace().decode("utf-8").splitlines()
print("\n".join(f" {s.strip()}" for s in trace[-args.tail :]))
print(" ---")
print()
print()