forked from coreos/fedora-coreos-pipeline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
devel-up
executable file
·166 lines (133 loc) · 5.63 KB
/
devel-up
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/python3
'''
Tiny convenient and *safe* wrapper around `oc process/apply` for devel
pipelines. Can be run multiple times; subsequent runs will replace existing
resources.
Example usage:
./devel-up --update \
--pipeline https://github.com/jlebon/fedora-coreos-pipeline \
--config https://github.com/jlebon/fedora-coreos-config@wip \
--cosa-img quay.io/jlebon/coreos-assembler:master
# OR
--cosa https://github.com/jlebon/coreos-assembler
Deleting devel pipeline:
./devel-up --delete
'''
import os
import sys
import json
import argparse
import subprocess
def main():
args = parse_args()
resources = process_template(args)
if args.update:
update_resources(args, resources)
if args.start:
print("Starting:")
out = subprocess.run(['oc', 'start-build',
# hack the name; should extract from manifest
f'{args.prefix}fedora-coreos-pipeline'],
check=True, stdout=subprocess.PIPE,
encoding='utf-8')
print(f" {out.stdout.strip()}")
print()
else:
print("You may start your devel pipeline with:")
print(f" oc start-build {args.prefix}fedora-coreos-pipeline")
print()
else:
assert args.delete
delete_resources(args, resources)
def parse_args():
parser = argparse.ArgumentParser()
action = parser.add_mutually_exclusive_group(required=True)
action.add_argument("--update", action='store_true',
help="Create or update devel pipeline")
action.add_argument("--delete", action='store_true',
help="Delete devel pipeline")
parser.add_argument("--start", action='store_true',
help="Start build after updating it")
parser.add_argument("--prefix", help="Devel prefix to use for resources",
default=get_username())
parser.add_argument("--pipeline", metavar='<URL>[@REF]',
help="Repo and ref to use for pipeline code")
parser.add_argument("--config", metavar='<URL>[@REF]',
help="Repo and ref to use for FCOS config")
parser.add_argument("--bucket", metavar='BUCKET',
help="AWS S3 bucket to use")
parser.add_argument("--cosa-img", metavar='FQIN',
help="Pullspec to use for COSA image")
# XXX: to add as a mutually exclusive option with above
# parser.add_argument("--cosa", metavar='<URL>[@REF]',
# help="Repo and ref to use for COSA image",
# default=DEFAULT_COSA_IMG)
args = parser.parse_args()
# just sanity check we have a prefix
assert len(args.prefix)
assert not args.prefix.endswith('-'), "Prefix must not end with dash"
# e.g. jlebon --> jlebon-fedora-coreos-pipeline
args.prefix += '-'
return args
def get_username():
import pwd
return pwd.getpwuid(os.getuid()).pw_name
def process_template(args):
params = [f'DEVEL_PREFIX={args.prefix}']
if args.pipeline:
params += params_from_git_refspec(args.pipeline, 'PIPELINE_REPO')
if args.config:
params += params_from_git_refspec(args.config, 'PIPELINE_FCOS_CONFIG')
if args.bucket:
params += [f'S3_BUCKET={args.bucket}']
if args.cosa_img:
params += [f'COREOS_ASSEMBLER_IMAGE={args.cosa_img}']
print("Parameters:")
for param in params:
print(f" {param}")
params = [q for p in params for q in ['--param', p]]
print()
resources = subprocess.check_output(
['oc', 'process', '--filename', 'manifests/pipeline.yaml'] + params)
return json.loads(resources)
def update_resources(args, resources):
# final safety check: only actually create/update prefixed resources
print("Updating:")
for resource in resources['items']:
if resource['metadata']['name'].startswith(args.prefix):
out = subprocess.run(['oc', 'apply', '--filename', '-'],
input=json.dumps(resource), encoding='utf-8',
check=True, stdout=subprocess.PIPE)
print(f" {out.stdout.strip()}")
print()
def delete_resources(args, resources):
# only delete prefixed resources
print("Deleting:")
for resource in resources['items']:
if resource['metadata']['name'].startswith(args.prefix):
out = subprocess.run(['oc', 'delete', '--filename', '-'],
input=json.dumps(resource), encoding='utf-8',
check=True, stdout=subprocess.PIPE)
print(f" {out.stdout.strip()}")
print()
def params_from_git_refspec(refspec, param_prefix):
url, ref = parse_git_refspec(refspec)
return [f'{param_prefix}_URL={url}',
f'{param_prefix}_REF={ref}']
def parse_git_refspec(refspec):
if '@' not in refspec:
return (refspec, get_default_branch(refspec))
return tuple(refspec.split('@'))
def get_default_branch(repo):
output = subprocess.check_output(['git', 'ls-remote', '--symref',
repo, 'HEAD'],
encoding='utf-8')
for line in output.splitlines():
if line.startswith('ref: '):
ref, symref = line[len('ref: '):].split()
if symref != "HEAD":
continue
assert ref.startswith("refs/heads/")
return ref[len("refs/heads/"):]
if __name__ == "__main__":
sys.exit(main())