|
@@ -36,6 +36,7 @@ import yaml
|
36
|
36
|
|
37
|
37
|
VERSION = "1.3.1"
|
38
|
38
|
REGISTRY_DIR = "REGISTRY_STORAGE_FILESYSTEM_ROOTREGISTRY_DIR"
|
|
39
|
+args = None
|
39
|
40
|
|
40
|
41
|
|
41
|
42
|
def dockerized():
|
|
@@ -59,8 +60,12 @@ def remove(path):
|
59
|
60
|
|
60
|
61
|
def clean_revisions(repo):
|
61
|
62
|
'''Remove the revision manifests that are not present in the tags directory'''
|
62
|
|
- revisions = set(os.listdir(repo + "/_manifests/revisions/sha256/"))
|
63
|
|
- manifests = set(map(os.path.basename, iglob(repo + "/_manifests/tags/*/*/sha256/*")))
|
|
63
|
+ revisions = set(
|
|
64
|
+ os.listdir(repo + "/_manifests/revisions/sha256/")
|
|
65
|
+ )
|
|
66
|
+ manifests = set(
|
|
67
|
+ map(os.path.basename, iglob(repo + "/_manifests/tags/*/*/sha256/*"))
|
|
68
|
+ )
|
64
|
69
|
revisions.difference_update(manifests)
|
65
|
70
|
for revision in revisions:
|
66
|
71
|
remove(repo + "/_manifests/revisions/sha256/" + revision)
|
|
@@ -133,8 +138,12 @@ def check_name(image):
|
133
|
138
|
# Note: Internally, distribution permits multiple dashes and up to 2 underscores as separators.
|
134
|
139
|
# See https://github.com/docker/distribution/blob/master/reference/regexp.go
|
135
|
140
|
|
136
|
|
- return len(image) < 256 and len(tag) < 129 and re.match('[a-zA-Z0-9_][a-zA-Z0-9_.-]*$', tag) and \
|
137
|
|
- all(re.match('[a-z0-9]+(?:(?:[._]|__|[-]*)[a-z0-9]+)*$', path) for path in repo.split("/"))
|
|
141
|
+ return len(image) < 256 and len(tag) < 129 and \
|
|
142
|
+ re.match('[a-zA-Z0-9_][a-zA-Z0-9_.-]*$', tag) and \
|
|
143
|
+ all(
|
|
144
|
+ re.match('[a-z0-9]+(?:(?:[._]|__|[-]*)[a-z0-9]+)*$', path)
|
|
145
|
+ for path in repo.split("/")
|
|
146
|
+ )
|
138
|
147
|
|
139
|
148
|
|
140
|
149
|
class RegistryCleaner():
|
|
@@ -185,7 +194,8 @@ class RegistryCleaner():
|
185
|
194
|
if self.container is not None:
|
186
|
195
|
self.docker.api.stop(self.container)
|
187
|
196
|
|
188
|
|
- images = args.images or map(os.path.dirname, iglob("**/_manifests", recursive=True))
|
|
197
|
+ images = args.images or \
|
|
198
|
+ map(os.path.dirname, iglob("**/_manifests", recursive=True))
|
189
|
199
|
|
190
|
200
|
exit_status = 0
|
191
|
201
|
for image in images:
|
|
@@ -204,9 +214,11 @@ class RegistryCleaner():
|
204
|
214
|
def get_file(self, path):
|
205
|
215
|
'''Returns the contents of the specified file from the container'''
|
206
|
216
|
try:
|
207
|
|
- with BytesIO(
|
208
|
|
- b"".join(_ for _ in self.docker.api.get_archive(self.container, path)[0])
|
209
|
|
- ) as buf, tarfile.open(fileobj=buf) as tar, tar.extractfile(os.path.basename(path)) as infile:
|
|
217
|
+ with BytesIO(b"".join(
|
|
218
|
+ _ for _ in self.docker.api.get_archive(self.container, path)[0]
|
|
219
|
+ )) as buf, tarfile.open(fileobj=buf) \
|
|
220
|
+ as tar, tar.extractfile(os.path.basename(path)) \
|
|
221
|
+ as infile:
|
210
|
222
|
data = infile.read()
|
211
|
223
|
except NotFound as err:
|
212
|
224
|
error(err)
|
|
@@ -230,7 +242,10 @@ class RegistryCleaner():
|
230
|
242
|
try:
|
231
|
243
|
registry_dir = data['storage']['filesystem']['rootdirectory']
|
232
|
244
|
except KeyError:
|
233
|
|
- driver = [k for k in 'azure gcs inmemory oss s3 swift'.split() if k in data['storage']][0]
|
|
245
|
+ driver = [
|
|
246
|
+ _ for _ in 'azure gcs inmemory oss s3 swift'.split()
|
|
247
|
+ if _ in data['storage']
|
|
248
|
+ ][0]
|
234
|
249
|
error("Unsupported storage driver: " + driver)
|
235
|
250
|
|
236
|
251
|
if dockerized():
|
|
@@ -239,13 +254,18 @@ class RegistryCleaner():
|
239
|
254
|
for item in self.info['Mounts']:
|
240
|
255
|
if item['Destination'] == registry_dir:
|
241
|
256
|
return item['Source']
|
|
257
|
+ return None
|
242
|
258
|
|
243
|
259
|
def get_image_version(self):
|
244
|
260
|
'''Gets the Docker distribution version running on the container'''
|
245
|
261
|
if self.info['State']['Running']:
|
246
|
|
- data = self.docker.containers.get(self.container).exec_run("/bin/registry --version").output
|
|
262
|
+ data = self.docker.containers.get(
|
|
263
|
+ self.container
|
|
264
|
+ ).exec_run("/bin/registry --version").output
|
247
|
265
|
else:
|
248
|
|
- data = self.docker.containers.run(self.info["Image"], command="--version", remove=True)
|
|
266
|
+ data = self.docker.containers.run(
|
|
267
|
+ self.info["Image"], command="--version", remove=True
|
|
268
|
+ )
|
249
|
269
|
return data.decode('utf-8').split()[2]
|
250
|
270
|
|
251
|
271
|
def garbage_collect(self):
|
|
@@ -253,13 +273,27 @@ class RegistryCleaner():
|
253
|
273
|
command = "garbage-collect " + "/etc/docker/registry/config.yml"
|
254
|
274
|
if dockerized():
|
255
|
275
|
command = "/bin/registry " + command
|
256
|
|
- with subprocess.Popen(command.split(), stdout=subprocess.PIPE, stderr=subprocess.STDOUT) as proc:
|
|
276
|
+ with subprocess.Popen(
|
|
277
|
+ command.split(),
|
|
278
|
+ stdout=subprocess.PIPE,
|
|
279
|
+ stderr=subprocess.STDOUT
|
|
280
|
+ ) as proc:
|
257
|
281
|
if not args.quiet:
|
258
|
282
|
print(proc.stdout.read().decode('utf-8'))
|
259
|
283
|
status = proc.wait()
|
260
|
284
|
else:
|
261
|
|
- cli = self.docker.containers.run("registry:2", command=command, detach=True, stderr=True,
|
262
|
|
- volumes={self.registry_dir: {'bind': "/var/lib/registry", 'mode': "rw"}})
|
|
285
|
+ cli = self.docker.containers.run(
|
|
286
|
+ "registry:2",
|
|
287
|
+ command=command,
|
|
288
|
+ detach=True,
|
|
289
|
+ stderr=True,
|
|
290
|
+ volumes={
|
|
291
|
+ self.registry_dir: {
|
|
292
|
+ 'bind': "/var/lib/registry",
|
|
293
|
+ 'mode': "rw"
|
|
294
|
+ }
|
|
295
|
+ }
|
|
296
|
+ )
|
263
|
297
|
if not args.quiet:
|
264
|
298
|
for line in cli.logs(stream=True):
|
265
|
299
|
print(line.decode('utf-8'), end="")
|
|
@@ -313,6 +347,7 @@ Options:
|
313
|
347
|
|
314
|
348
|
sys.exit(cleaner())
|
315
|
349
|
|
|
350
|
+
|
316
|
351
|
if __name__ == "__main__":
|
317
|
352
|
try:
|
318
|
353
|
main()
|