forked from gdraheim/docker-systemctl-replacement
-
Notifications
You must be signed in to change notification settings - Fork 1
/
docker_mirror.py
executable file
·675 lines (665 loc) · 28.8 KB
/
docker_mirror.py
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
#! /usr/bin/python3
# from __future__ import print_function
__copyright__ = "(C) 2020 Guido Draheim"
__contact__ = "https://github.com/gdraheim/docker-mirror-packages-repo"
__license__ = "CC0 Creative Commons Zero (Public Domain)"
__version__ = "1.6.2504"
from collections import OrderedDict, namedtuple
import os.path
import sys
import re
import json
import logging
import subprocess
import tempfile
import shutil
if sys.version[0] != '2':
xrange = range
basestring = str
logg = logging.getLogger("mirror")
DOCKER = "docker"
ADDHOSTS = False
ADDEPEL = False
LEAP = "opensuse/leap"
SUSE = "opensuse"
OPENSUSE_VERSIONS = {"42.2": SUSE, "42.3": SUSE, "15.0": LEAP, "15.1": LEAP, "15.2": LEAP, "15.3": LEAP}
UBUNTU_LTS = {"16": "16.04", "18": "18.04", "20": "20.04"}
UBUNTU_VERSIONS = {"12.04": "precise", "14.04": "trusty", "16.04": "xenial", "17.10": "artful",
"18.04": "bionic", "18.10": "cosmic", "19.04": "disco", "19.10": "eoan",
"20.04": "focal", "20.10": "groovy"}
CENTOS_VERSIONS = {"7.0": "7.0.1406", "7.1": "7.1.1503", "7.2": "7.2.1511", "7.3": "7.3.1611",
"7.4": "7.4.1708", "7.5": "7.5.1804", "7.6": "7.6.1810", "7.7": "7.7.1908",
"7.8": "7.8.2003", "7.9": "7.9.2009",
"8.0": "8.0.1905", "8.1": "8.1.1911", "8.2": "8.2.2004", "8.3": "8.3.2011"}
def decodes(text):
if text is None: return None
return decodes_(text)
def decodes_(text):
if isinstance(text, bytes):
encoded = sys.getdefaultencoding()
if encoded in ["ascii"]:
encoded = "utf-8"
try:
return text.decode(encoded)
except:
return text.decode("latin-1")
return text
def output3(cmd, shell=True, debug=True):
if isinstance(cmd, basestring):
if debug: logg.debug("run: %s", cmd)
else:
if debug: logg.debug("run: %s", " ".join(["'%s'" % item for item in cmd]))
run = subprocess.Popen(cmd, shell=shell, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = run.communicate()
return decodes_(out), decodes_(err), run.returncode
def major(version):
if version:
return version[0]
return version
def onlyversion(image):
if ":" in image:
return image.split(":")[-1]
return image
class DockerMirror:
def __init__(self, cname, image, hosts):
self.cname = cname # name of running container
self.image = image # image used to start the container
self.hosts = hosts # domain names for the container
class DockerMirrorPackagesRepo:
def __init__(self, image=None):
self._image = image
def host_system_image(self):
""" returns the docker image name which corresponds to the
operating system distribution of the host system. This
image name is the key for the other mirror functions. """
distro, version = self.detect_etc_image("/etc")
logg.info(":%s:%s host system image detected", distro, version)
if distro and version:
return "%s:%s" % (distro, version)
return ""
def detect_etc_image(self, etc):
distro, version = "", ""
os_release = os.path.join(etc, "os-release")
if os.path.exists(os_release):
# rhel:7.4 # VERSION="7.4 (Maipo)" ID="rhel" VERSION_ID="7.4"
# centos:7.3 # VERSION="7 (Core)" ID="centos" VERSION_ID="7"
# centos:7.4 # VERSION="7 (Core)" ID="centos" VERSION_ID="7"
# centos:7.7.1908 # VERSION="7 (Core)" ID="centos" VERSION_ID="7"
# opensuse:42.3 # VERSION="42.3" ID=opensuse VERSION_ID="42.3"
# opensuse/leap:15.0 # VERSION="15.0" ID="opensuse-leap" VERSION_ID="15.0"
# ubuntu:16.04 # VERSION="16.04.3 LTS (Xenial Xerus)" ID=ubuntu VERSION_ID="16.04"
# ubuntu:18.04 # VERSION="18.04.1 LTS (Bionic Beaver)" ID=ubuntu VERSION_ID="18.04"
for line in open(os_release):
key, value = "", ""
m = re.match('^([_\\w]+)=([^"].*).*', line.strip())
if m:
key, value = m.group(1), m.group(2)
m = re.match('^([_\\w]+)="([^"]*)".*', line.strip())
if m:
key, value = m.group(1), m.group(2)
# logg.debug("%s => '%s' '%s'", line.strip(), key, value)
if key in ["ID"]:
distro = value.replace("-", "/")
if key in ["VERSION_ID"]:
version = value
redhat_release = os.path.join(etc, "redhat-release")
if os.path.exists(redhat_release):
for line in open(redhat_release):
m = re.search("release (\\d+[.]\\d+).*", line)
if m:
distro = "rhel"
version = m.group(1)
centos_release = os.path.join(etc, "centos-release")
if os.path.exists(centos_release):
# CentOS Linux release 7.5.1804 (Core)
for line in open(centos_release):
m = re.search("release (\\d+[.]\\d+).*", line)
if m:
distro = "centos"
version = m.group(1)
return distro, version
def detect_base_image(self, image):
""" returns the docker image name which corresponds to the
operating system distribution of the image provided. This
image name is the key for the other mirror functions. """
docker = DOCKER
distro, version = "", ""
cname = "docker_mirror_detect." + os.path.basename(image).replace(":", ".")
cmd = "{docker} rm -f {cname}"
out, err, end = output3(cmd.format(**locals()))
cmd = "{docker} create --name={cname} {image}"
out, err, end = output3(cmd.format(**locals()))
if end:
logg.info("%s --name %s : %s", image, cname, err.strip())
tempdir = tempfile.mkdtemp("docker_mirror_detect")
try:
distro, version = self.detect_base_image_from(cname, tempdir)
logg.info(":%s:%s base image detected", distro, version)
if distro and version:
return "%s:%s" % (distro, version)
finally:
shutil.rmtree(tempdir)
cmd = "{docker} rm {cname}"
out, err, end = output3(cmd.format(**locals()))
return ""
def detect_base_image_from(self, cname, tempdir):
debug = False
docker = DOCKER
cmd = "{docker} cp {cname}:/usr/lib/os-release {tempdir}/os-release"
out, err, end = output3(cmd.format(**locals()), debug=debug)
if not end:
logg.debug("get: /usr/lib/os-release copied")
else:
logg.debug("get: /usr/lib/os-release: %s", err.strip().replace(cname, "{cname}"))
cmd = "{docker} cp {cname}:/etc/os-release {tempdir}/os-release"
out, err, end = output3(cmd.format(**locals()), debug=debug)
if not end:
logg.debug("get: /etc/os-release copied")
else:
logg.debug("get: /etc/os-release: %s", err.strip().replace(cname, "{cname}"))
cmd = "{docker} cp {cname}:/etc/redhat-release {tempdir}/redhat-release"
out, err, end = output3(cmd.format(**locals()), debug=debug)
if not end:
logg.debug("get: /etc/redhat-release copied")
else:
logg.debug("get: /etc/redhat-release: %s", err.strip().replace(cname, "{cname}"))
cmd = "{docker} cp {cname}:/etc/centos-release {tempdir}/centos-release"
out, err, end = output3(cmd.format(**locals()), debug=debug)
if not end:
logg.debug("get: /etc/centos-release copied")
else:
logg.debug("get: /etc/centos-release: %s", err.strip().replace(cname, "{cname}"))
return self.detect_etc_image(tempdir)
def get_docker_latest_image(self, image):
""" converts a shorthand version into the version string used on an image name. """
if image.startswith("centos:"):
return self.get_centos_latest(image)
if image.startswith("opensuse/leap:"):
return self.get_opensuse_latest(image)
if image.startswith("opensuse:"):
return self.get_opensuse_latest(image)
if image.startswith("ubuntu:"):
return self.get_ubuntu_latest(image)
return ""
def get_docker_latest_version(self, image):
""" converts a shorthand version into the version string used on an image name. """
if image.startswith("centos:"):
version = image[len("centos:"):]
return self.get_centos_latest_version(version)
if image.startswith("opensuse/leap:"):
version = image[len("opensuse/leap:"):]
return self.get_opensuse_latest_version(version)
if image.startswith("opensuse:"):
version = image[len("opensuse:"):]
return self.get_opensuse_latest_version(version)
if image.startswith("ubuntu:"):
version = image[len("ubuntu:"):]
return self.get_ubuntu_latest_version(version)
return ""
def get_docker_mirror(self, image):
""" attach local centos-repo / opensuse-repo to docker-start enviroment.
Effectivly when it is required to 'docker start centos:x.y' then do
'docker start centos-repo:x.y' before and extend the original to
'docker start --add-host mirror...:centos-repo centos:x.y'. """
if image.startswith("centos:"):
return self.get_centos_docker_mirror(image)
if image.startswith("opensuse/leap:"):
return self.get_opensuse_docker_mirror(image)
if image.startswith("opensuse:"):
return self.get_opensuse_docker_mirror(image)
if image.startswith("ubuntu:"):
return self.get_ubuntu_docker_mirror(image)
return None
def get_docker_mirrors(self, image):
""" attach local centos-repo / opensuse-repo to docker-start enviroment.
Effectivly when it is required to 'docker start centos:x.y' then do
'docker start centos-repo:x.y' before and extend the original to
'docker start --add-host mirror...:centos-repo centos:x.y'. """
mirrors = []
if image.startswith("centos:"):
mirrors = self.get_centos_docker_mirrors(image)
if ADDEPEL:
if "centos" in image:
mirrors += self.get_epel_docker_mirrors(image)
if image.startswith("opensuse/leap:"):
mirrors = self.get_opensuse_docker_mirrors(image)
if image.startswith("opensuse:"):
mirrors = self.get_opensuse_docker_mirrors(image)
if image.startswith("ubuntu:"):
mirrors = self.get_ubuntu_docker_mirrors(image)
logg.info(" %s -> %s", image, " ".join([mirror.cname for mirror in mirrors]))
return mirrors
def get_ubuntu_latest(self, image, default=None):
if image.startswith("ubuntu:"):
distro = "ubuntu"
version = image[len("ubuntu:"):]
latest = self.get_ubuntu_latest_version(version)
if latest:
return "{distro}:{latest}".format(**locals())
if default is not None:
return default
return image
def get_ubuntu_latest_version(self, version):
""" allows to use 'ubuntu:18' or 'ubuntu:bionic' """
ver = version
if ver in ["latest"]:
ver = ""
if "." not in ver:
latest = ""
for release in UBUNTU_VERSIONS:
codename = UBUNTU_VERSIONS[release]
if len(ver) >= 3 and codename.startswith(ver):
logg.debug("release (%s) %s", release, codename)
if latest < release:
latest = release
elif release.startswith(ver):
logg.debug("release %s (%s)", release, codename)
if latest < release:
latest = release
if latest:
ver = latest
return ver or version
def get_ubuntu_docker_mirror(self, image):
""" detects a local ubuntu mirror or starts a local
docker container with a ubunut repo mirror. It
will return the extra_hosts setting to start
other docker containers"""
rmi = "localhost:5000/mirror-packages"
rep = "ubuntu-repo"
ver = self.get_ubuntu_latest_version(onlyversion(image))
return self.docker_mirror(rmi, rep, ver, "archive.ubuntu.com", "security.ubuntu.com")
def get_ubuntu_docker_mirrors(self, image):
main = self.get_ubuntu_docker_mirror(image)
return [main]
def get_centos_latest(self, image, default=None):
if image.startswith("centos:"):
distro = "centos"
version = image[len("centos:"):]
latest = self.get_centos_latest_version(version)
if latest:
return "{distro}:{latest}".format(**locals())
if default is not None:
return default
return image
def get_centos_latest_version(self, version):
""" allows to use 'centos:7' or 'centos:7.9' making 'centos:7.9.2009' """
ver = version
if ver in ["latest"]:
ver = ""
if "." not in ver:
latest = ""
for release in CENTOS_VERSIONS:
if release.startswith(ver):
fullrelease = CENTOS_VERSIONS[release]
logg.debug("release %s (%s)", release, fullrelease)
if latest < fullrelease:
latest = fullrelease
if latest:
ver = latest
if ver in CENTOS_VERSIONS:
ver = CENTOS_VERSIONS[ver]
return ver or version
def get_centos_docker_mirror(self, image):
""" detects a local centos mirror or starts a local
docker container with a centos repo mirror. It
will return the setting for extrahosts"""
rmi = "localhost:5000/mirror-packages"
rep = "centos-repo"
ver = self.get_centos_latest_version(onlyversion(image))
return self.docker_mirror(rmi, rep, ver, "mirrorlist.centos.org")
def get_centos_docker_mirrors(self, image):
main = self.get_centos_docker_mirror(image)
return [main]
def get_opensuse_latest(self, image, default=None):
if image.startswith("opensuse/leap:"):
distro = "opensuse/leap"
version = image[len("opensuse/leap:"):]
latest = self.get_opensuse_latest_version(version)
if latest:
if latest in OPENSUSE_VERSIONS:
distro = OPENSUSE_VERSIONS[latest]
return "{distro}:{latest}".format(**locals())
if image.startswith("opensuse:"):
distro = "opensuse"
version = image[len("opensuse:"):]
latest = self.get_opensuse_latest_version(version)
if latest:
if latest in OPENSUSE_VERSIONS:
distro = OPENSUSE_VERSIONS[latest]
return "{distro}:{latest}".format(**locals())
if default is not None:
return default
return image
def get_opensuse_latest_version(self, version):
""" allows to use 'opensuse:42' making 'opensuse:42.3' """
ver = version
if ver in ["latest"]:
ver = ""
if "." not in ver:
latest = ""
for release in OPENSUSE_VERSIONS:
if release.startswith(ver):
logg.debug("release %s", release)
# opensuse:42.0 was before opensuse/leap:15.0
release42 = release.replace("42.", "14.")
latest42 = latest.replace("42.", "14.")
if latest42 < release42:
latest = release
ver = latest or ver
return ver or version
def get_opensuse_docker_mirror(self, image):
""" detects a local opensuse mirror or starts a local
docker container with a centos repo mirror. It
will return the extra_hosts setting to start
other docker containers"""
rmi = "localhost:5000/mirror-packages"
rep = "opensuse-repo"
ver = self.get_opensuse_latest_version(onlyversion(image))
return self.docker_mirror(rmi, rep, ver, "download.opensuse.org")
def get_opensuse_docker_mirrors(self, image):
main = self.get_opensuse_docker_mirror(image)
return [main]
def docker_mirror(self, rmi, rep, ver, *hosts):
image = "{rmi}/{rep}:{ver}".format(**locals())
cname = "{rep}-{ver}".format(**locals())
return DockerMirror(cname, image, list(hosts))
#
def get_extra_mirrors(self, image):
mirrors = []
if image.startswith("centos:"):
version = image[len("centos:"):]
mirrors = self.get_epel_docker_mirrors(version)
return mirrors
def get_epel_docker_mirrors(self, image):
main = self.get_epel_docker_mirror(image)
return [main]
def get_epel_docker_mirror(self, image):
""" detects a local epel mirror or starts a local
docker container with a epel repo mirror. It
will return the setting for extrahosts"""
docker = DOCKER
rmi = "localhost:5000/mirror-packages"
rep = "epel-repo"
ver = onlyversion(image)
version = self.get_centos_latest_version(ver)
# cut the yymm date part from the centos release
released = version.split(".")[-1]
later = ""
before = ""
# and then check for actual images around
cmd = docker + " images --format '{{.Repository}}:{{.Tag}}'"
out, err, end = output3(cmd)
if end:
logg.error("docker images [%s]\n\t", end, cmd)
for line in out.split("\n"):
if "/epel-repo:" not in line:
continue
tagline = re.sub(".*/epel-repo:", "", line)
tagname = re.sub(" .*", "", tagline)
created = tagname.split(".")[-1]
accepts = tagname.startswith(major(version))
logg.debug(": %s (%s) (%s) %s:%s", line.strip(), created, released, major(version), accepts and "x" or "ignore")
if created >= released and accepts:
if not later or later > tagname:
later = tagname
elif created < released and accepts:
if not before or before < tagname:
before = tagname
if later:
ver = later
elif before:
ver = before
return self.docker_mirror(rmi, rep, ver, "mirrors.fedoraproject.org")
#
def ip_container(self, name):
docker = DOCKER
cmd = "{docker} inspect {name}"
out, err, rc = output3(cmd.format(**locals()))
if rc:
logg.info("%s : %s", cmd, err)
logg.debug("no address for %s", name)
return None
values = json.loads(out)
if not values or "NetworkSettings" not in values[0]:
logg.critical(" docker inspect %s => %s ", name, values)
addr = values[0]["NetworkSettings"]["IPAddress"]
assert isinstance(addr, basestring)
logg.debug("address %s for %s", addr, name)
return addr
def start_containers(self, image):
mirrors = self.get_docker_mirrors(image)
done = {}
for mirror in mirrors:
addr = self.start_container(mirror.image, mirror.cname)
done[mirror.cname] = addr
return done
def start_container(self, image, container):
docker = DOCKER
cmd = "{docker} inspect {image}"
out, err, ok = output3(cmd.format(**locals()))
image_found = json.loads(out)
if not image_found:
logg.info("image not found: %s", image)
return None
cmd = "{docker} inspect {container}"
out, err, rc = output3(cmd.format(**locals()))
container_found = json.loads(out)
if not rc and container_found:
container_status = container_found[0]["State"]["Status"]
logg.info("::: %s -> %s", container, container_status)
latest_image_id = image_found[0]["Id"]
container_image_id = container_found[0]["Image"]
if latest_image_id != container_image_id or container_status not in ["running"]:
cmd = "{docker} rm --force {container}"
out, err, rc = output3(cmd.format(**locals()))
if rc:
logg.debug("%s : %s", cmd, err)
container_found = []
if not container_found:
cmd = "{docker} run --rm=true --detach --name {container} {image}"
out, err, rc = output3(cmd.format(**locals()))
if rc:
logg.error("%s : %s", cmd, err)
addr = self.ip_container(container)
logg.info("%s : %s", container, addr)
return addr
def stop_containers(self, image):
mirrors = self.get_docker_mirrors(image)
done = {}
for mirror in mirrors:
info = self.stop_container(mirror.image, mirror.cname)
done[mirror.cname] = info
return done
def stop_container(self, image, container):
docker = DOCKER
cmd = "{docker} inspect {container}"
out, err, rc = output3(cmd.format(**locals()))
container_found = json.loads(out)
if not rc and container_found:
cmd = "{docker} rm --force {container}"
out, err, ok = output3(cmd.format(**locals()))
status = container_found[0].get("State", {})
started = status.get("StartedAt", "(was not started)")
assert isinstance(started, basestring)
return started
return "(did not exist)"
def info_containers(self, image):
mirrors = self.get_docker_mirrors(image)
done = {}
for mirror in mirrors:
info = self.info_container(mirror.image, mirror.cname)
done[mirror.cname] = info
return done
def info_container(self, image, container):
addr = self.ip_container(container)
return addr
def get_containers(self, image):
mirrors = self.get_docker_mirrors(image)
done = []
for mirror in mirrors:
done.append(mirror.cname)
return done
def inspect_containers(self, image):
mirrors = self.get_docker_mirrors(image)
docker = DOCKER
done = OrderedDict()
for mirror in mirrors:
addr = self.ip_container(mirror.cname)
done[mirror.cname] = addr
return done
#
def add_hosts(self, image, done={}):
mirrors = self.get_docker_mirrors(image)
args = []
for mirror in mirrors:
name = mirror.cname
logg.info("name = %s (%s)", name, done)
if name in done:
addr = done[name]
if addr:
for host in mirror.hosts:
args += ["--add-host", "%s:%s" % (host, addr)]
return args
def helps(self):
return """helper to start/stop mirror-container with the packages-repo
help this help screen
image|detect the image name matching the local system
facts [image] the json data used to start or stop the containers
start [image] starts the container(s) with the mirror-packages-repo
stop [image] stops the containers(s) with the mirror-packages-repo
addhosts [image] shows the --add-hosts string for the client container
"""
def detect(self, image=None):
if not image and self._image:
image = self._image
if not image or image in ["host", "system"]:
return self.host_system_image()
latest = self.get_docker_latest_image(image)
if latest:
return latest
else:
# actually create a container and look into it
return self.detect_base_image(image)
def epel(self, image=None):
image = self.detect(image)
mirrors = self.get_extra_mirrors(image)
for mirror in mirrors:
return mirror.image
return ""
def repo(self, image=None):
image = self.detect(image)
mirrors = self.get_docker_mirrors(image)
for mirror in mirrors:
if ADDHOSTS:
refer = mirror.image
host = mirror.hosts[0]
return "--add-host={host}:({refer})".format(**locals())
else:
return mirror.image
return ""
def repos(self, image=None):
image = self.detect(image)
mirrors = self.get_docker_mirrors(image)
shown = ""
for mirror in mirrors:
if ADDHOSTS:
if shown: shown += " "
for host in mirror.hosts:
refer = mirror.image
shown += "--add-host={host}:({refer})".format(**locals())
else:
shown += mirror.image + "\n"
return shown
def facts(self, image=None):
image = self.detect(image)
mirrors = self.get_docker_mirrors(image)
data = {}
for mirror in mirrors:
data[mirror.cname] = {"image": mirror.image, "name": mirror.cname,
"hosts": mirror.hosts}
return json.dumps(data, indent=2)
def starts(self, image=None):
image = self.detect(image)
done = self.start_containers(image)
if ADDHOSTS:
return " ".join(self.add_hosts(image, done))
else:
return json.dumps(done, indent=2)
def stops(self, image=None):
image = self.detect(image)
done = self.stop_containers(image)
if ADDHOSTS:
names = sorted(done.keys())
return " ".join(names)
else:
return json.dumps(done, indent=2)
def infos(self, image=None):
image = self.detect(image)
done = self.info_containers(image)
if ADDHOSTS:
return " ".join(self.add_hosts(image, done))
else:
return json.dumps(done, indent=2)
def containers(self, image=None):
image = self.detect(image)
done = self.get_containers(image)
if ADDHOSTS:
return " ".join(done)
else:
return json.dumps(done, indent=2)
def inspects(self, image=None):
image = self.detect(image)
done = self.inspect_containers(image)
if ADDHOSTS:
return " ".join(self.add_hosts(image, done))
else:
return json.dumps(done, indent=2)
if __name__ == "__main__":
from argparse import ArgumentParser
_o = ArgumentParser(description="""starts local containers representing mirrors of package repo repositories
which are required by a container type. Subsequent 'docker run' can use the '--add-hosts' from this
helper script to divert 'pkg install' calls to a local docker container as the real source.""")
_o.add_argument("-v", "--verbose", action="count", default=0, help="more logging")
_o.add_argument("-a", "--add-hosts", "--add-host", action="store_true", default=ADDHOSTS,
help="show addhost options for 'docker run' [%(default)s]")
_o.add_argument("--epel", action="store_true", default=ADDEPEL,
help="addhosts for epel as well [%(default)s]")
commands = ["help", "detect", "image", "repo", "info", "facts", "start", "stop"]
_o.add_argument("command", nargs="?", default="detect", help="|".join(commands))
_o.add_argument("image", nargs="?", default=None, help="defaults to image name of the local host system")
opt = _o.parse_args()
logging.basicConfig(level=max(0, logging.WARNING - opt.verbose * 10))
ADDHOSTS = opt.add_hosts
ADDEPEL = opt.epel # centos epel-repo
command = "detect"
repo = DockerMirrorPackagesRepo()
if opt.command in ["?", "help"]:
print(repo.helps())
elif opt.command in ["detect", "image"]:
print(repo.detect(opt.image))
elif opt.command in ["repo", "from"]:
print(repo.repo(opt.image))
elif opt.command in ["repos", "for"]:
print(repo.repos(opt.image))
elif opt.command in ["latest"]:
print(repo.get_docker_latest_version(opt.image))
elif opt.command in ["epel"]:
print(repo.epel(opt.image))
elif opt.command in ["facts"]:
print(repo.facts(opt.image))
elif opt.command in ["start", "starts"]:
print(repo.starts(opt.image))
elif opt.command in ["stop", "stops"]:
print(repo.stops(opt.image))
elif opt.command in ["show", "shows", "info", "infos"]:
print(repo.infos(opt.image))
elif opt.command in ["addhost", "add-host", "addhosts", "add-hosts"]:
ADDHOSTS = True
print(repo.infos(opt.image))
elif opt.command in ["inspect"]:
print(repo.inspects(opt.image))
elif opt.command in ["containers"]:
print(repo.containers(opt.image))
else:
print("unknown command", opt.command)
sys.exit(1)