forked from libvirt/libvirt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper
executable file
·240 lines (199 loc) · 7.8 KB
/
helper
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#!/usr/bin/env python3
#
# Copyright (C) 2021 Red Hat, Inc.
# SPDX-License-Identifier: LGPL-2.1-or-later
import argparse
import os
import pathlib
import pty
import subprocess
import sys
import textwrap
import util
class Parser:
def __init__(self):
# Options that are common to all actions that use containers
containerparser = argparse.ArgumentParser(add_help=False)
containerparser.add_argument(
"target",
help="perform action on target OS",
)
containerparser.add_argument(
"--engine",
choices=["auto", "podman", "docker"],
default="auto",
help="container engine to use",
)
containerparser.add_argument(
"--login",
default=os.getlogin(), # exempt from syntax-check
help="login to use inside the container",
)
containerparser.add_argument(
"--image-prefix",
default="registry.gitlab.com/libvirt/libvirt/ci-",
help="use container images from non-default location",
)
containerparser.add_argument(
"--image-tag",
default=":latest",
help="use container images with non-default tags",
)
# Options that are common to all actions that call the
# project's build system
mesonparser = argparse.ArgumentParser(add_help=False)
mesonparser.add_argument(
"--meson-args",
default="",
help="additional arguments passed to meson "
"(eg --meson-args='-Dopt1=enabled -Dopt2=disabled')",
)
mesonparser.add_argument(
"--ninja-args",
default="",
help="additional arguments passed to ninja",
)
# Options that are common to actions communicating with a GitLab
# instance
gitlabparser = argparse.ArgumentParser(add_help=False)
gitlabparser.add_argument(
"--namespace",
default="libvirt/libvirt",
help="GitLab project namespace"
)
gitlabparser.add_argument(
"--gitlab-uri",
default="https://gitlab.com",
help="base GitLab URI"
)
# Main parser
self._parser = argparse.ArgumentParser()
subparsers = self._parser.add_subparsers(
dest="action",
metavar="ACTION",
)
subparsers.required = True
# build action
buildparser = subparsers.add_parser(
"build",
help="run a build in a container",
parents=[containerparser, mesonparser],
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
buildparser.set_defaults(func=Application._action_build)
# test action
testparser = subparsers.add_parser(
"test",
help="run a build in a container (including tests)",
parents=[containerparser, mesonparser],
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
testparser.set_defaults(func=Application._action_test)
# shell action
shellparser = subparsers.add_parser(
"shell",
help="start a shell in a container",
parents=[containerparser],
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
shellparser.set_defaults(func=Application._action_shell)
# list-images action
listimagesparser = subparsers.add_parser(
"list-images",
help="list known container images",
parents=[gitlabparser],
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
listimagesparser.set_defaults(func=Application._action_list_images)
# check_stale action
check_staleparser = subparsers.add_parser(
"check-stale",
help="check for existence of stale images on the GitLab instance",
parents=[gitlabparser],
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
check_staleparser.set_defaults(func=Application._action_check_stale)
def parse(self):
return self._parser.parse_args()
class Application:
def __init__(self):
self._basedir = pathlib.Path(__file__).resolve().parent
self._args = Parser().parse()
def _make_run(self, target):
args = [
"-C",
self._basedir,
target,
]
if self._args.action in ["build", "test", "shell"]:
args.extend([
f"CI_ENGINE={self._args.engine}",
f"CI_USER_LOGIN={self._args.login}",
f"CI_IMAGE_PREFIX={self._args.image_prefix}",
f"CI_IMAGE_TAG={self._args.image_tag}",
])
if self._args.action in ["build", "test"]:
args.extend([
f"CI_MESON_ARGS={self._args.meson_args}",
f"CI_NINJA_ARGS={self._args.ninja_args}",
])
if pty.spawn(["make"] + args) != 0:
sys.exit("error: 'make' failed")
def _lcitool_run(self, args):
output = subprocess.check_output([self._args.lcitool] + args)
return output.decode("utf-8")
def _lcitool_get_targets(self):
output = self._lcitool_run(["targets"])
return output.splitlines()
def _check_stale_images(self):
namespace = self._args.namespace
gitlab_uri = self._args.gitlab_uri
registry_uri = util.get_registry_uri(namespace, gitlab_uri)
stale_images = util.get_registry_stale_images(registry_uri, self._basedir)
if stale_images:
spacing = "\n" + 4 * " "
stale_fmt = [f"{k} (ID: {v})" for k, v in stale_images.items()]
stale_details = spacing.join(stale_fmt)
stale_ids = ' '.join([str(id) for id in stale_images.values()])
registry_uri = util.get_registry_uri(namespace, gitlab_uri)
msg = textwrap.dedent(f"""
The following images are stale and can be purged from the registry:
STALE_DETAILS
You can delete the images listed above using this shell snippet:
$ for image_id in {stale_ids}; do
curl --request DELETE --header "PRIVATE-TOKEN: <access_token>" \\
{registry_uri}/$image_id;
done
You can generate a personal access token here:
{gitlab_uri}/-/profile/personal_access_tokens
""")
print(msg.replace("STALE_DETAILS", stale_details))
def _action_build(self):
self._make_run(f"ci-build@{self._args.target}")
def _action_test(self):
self._make_run(f"ci-test@{self._args.target}")
def _action_shell(self):
self._make_run(f"ci-shell@{self._args.target}")
def _action_list_images(self):
registry_uri = util.get_registry_uri(self._args.namespace,
self._args.gitlab_uri)
images = util.get_registry_images(registry_uri)
# skip the "ci-" prefix each of our container images' name has
name_prefix = "ci-"
names = [i["name"][len(name_prefix):] for i in images]
names.sort()
native = [name for name in names if "-cross-" not in name]
cross = [name for name in names if "-cross-" in name]
spacing = 4 * " "
print("Available x86 container images:\n")
print(spacing + ("\n" + spacing).join(native))
if cross:
print()
print("Available cross-compiler container images:\n")
print(spacing + ("\n" + spacing).join(cross))
def _action_check_stale(self):
self._check_stale_images()
def run(self):
self._args.func(self)
if __name__ == "__main__":
Application().run()