-
Notifications
You must be signed in to change notification settings - Fork 3
/
jobs
executable file
·124 lines (90 loc) · 3.33 KB
/
jobs
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env python3
#
# cli interface to manipulate a job
#
import argparse
import contextlib
import sqlite3
import sys
import carpetbag
from request_build import request_build, cancel_build
from utils import get_maintainer
def lookup_id(id):
with contextlib.closing(sqlite3.connect(carpetbag.dbfile)) as conn:
conn.row_factory = sqlite3.Row
cursor = conn.execute('SELECT * FROM jobs WHERE id = ?', (id,))
row = cursor.fetchone()
if not row:
sys.exit("job id {} not found".format(id))
return row
def owns_job(row):
# check for matching maintainer name
owner = row['user']
maintainer = get_maintainer()
if owner != maintainer:
sys.exit("job id {} is owned by maintainer {}, not {}".format(row['id'], owner, maintainer))
return True
#
# subcommands
#
def cancel(id):
row = lookup_id(id)
owns_job(row)
backend = row['backend']
bbid = row['backend_id']
# ask backend to cancel build
cancel_build(backend, bbid)
def deploy(id):
row = lookup_id(id)
owns_job(row)
if row['status'] != 'build succeeded':
sys.exit("job id {} isn't deployable from status '{}'".format(row['id'], row['status']))
# convert db row to an Update object
u = carpetbag.Update()
for k in row.keys():
attr = k
if k == 'ref':
attr = 'reference'
elif k == 'srcpkg':
attr = 'package'
elif k == 'id':
attr = 'buildnumber'
setattr(u, attr, row[k])
# if deployable, update to 'fetching' status, irrespective of token
if not carpetbag.deploy(u, force=True):
sys.exit("job id {} isn't deployable due to reference or package name restrictions")
def rerun(id, override_tokens):
row = lookup_id(id)
commit = row['hash']
ref = row['ref']
package = row['srcpkg']
maintainer = row['user']
tokens = row['tokens']
if override_tokens is not None:
tokens = ' '.join(override_tokens)
print(commit, ref, package, maintainer, tokens)
request_build(commit, ref, package, maintainer, tokens)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='build job control')
subparsers = parser.add_subparsers(title='subcommands', dest='subcommand')
# required=True not supported till python 3.7
parser_cancel = subparsers.add_parser('cancel', help='cancel job')
parser_cancel.add_argument('id', metavar='ID', type=int, help='job id')
parser_deploy = subparsers.add_parser('deploy', help='deploy job')
parser_deploy.add_argument('id', metavar='ID', type=int, help='job id')
parser_help = subparsers.add_parser('help', help='this help')
parser_rerun = subparsers.add_parser('rerun', help='re-run job')
parser_rerun.add_argument('id', metavar='ID', type=int, help='job id')
parser_rerun.add_argument('--token', metavar='TOKEN', action='append', help='tokens (default: as previous run)', default=None)
args = parser.parse_args()
if args.subcommand == 'help' or args.subcommand is None:
parser.print_help()
elif args.subcommand == 'deploy':
deploy(args.id)
elif args.subcommand == 'cancel':
cancel(args.id)
elif args.subcommand == 'rerun':
rerun(args.id, args.token)
else:
print("Unknown subcommand '{}'".format(args.subcommand))
sys.exit(1)