-
Notifications
You must be signed in to change notification settings - Fork 38
/
docker_wrapper.py
executable file
·74 lines (60 loc) · 2.18 KB
/
docker_wrapper.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
#!/usr/bin/python
# coding=utf8
# utf8 without BOM
import os
import sys
# gcr.io/xxx/yyy:zzz -> gcr.azk8s.cn/xxx/yyy:zzz,
# for example registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.14.1
# k8s.gcr.io/xxx:yyy -> registry.cn-hangzhou.aliyuncs.com/google-containers/xxx:yyy,
# for example k8s.gcr.io/kube-apiserver:v1.14.1
# quay.io/xxx/yyy:zzz -> quay.azk8s.cn/xxx/yyy:zzz,
# for example quay.io/coreos/flannel:v0.10.0-amd64 -- not support for now.
converts = [
{
'prefix': 'gcr.io',
'replace': lambda x: x.replace('gcr.io', 'registry.cn-hangzhou.aliyuncs.com'),
},
{
'prefix': 'k8s.gcr.io',
'replace': lambda x: x.replace('k8s.gcr.io', 'registry.cn-hangzhou.aliyuncs.com/google_containers'),
},
{
'prefix': 'quay.io',
'replace': lambda x: x.replace('quay.io', 'quay.azk8s.cn'),
}
]
def execute_sys_cmd(cmd):
result = os.system(cmd)
if result != 0:
print(cmd + " failed.")
sys.exit(-1)
def usage():
print("Usage: " + sys.argv[0] + " pull ${image}")
if __name__ == "__main__":
if len(sys.argv) != 3:
usage()
sys.exit(-1)
image = sys.argv[2]
imageArray = image.split("/")
newImage = ''
for cvt in converts:
if imageArray[0] == cvt['prefix']:
newImage = cvt['replace'](image)
# this is a special case: the tag of coredns image on aliyun repo has no "v" prefix.
if imageArray[1] == "coredns":
newImage = newImage.replace("/coredns:v", ":")
break
if newImage:
print("-- pull {image} from {newimage} instead --".format(image=image, newimage=newImage))
cmd = "docker pull {image}".format(image=newImage)
execute_sys_cmd(cmd)
cmd = "docker tag {newImage} {image}".format(newImage=newImage, image=image)
execute_sys_cmd(cmd)
cmd = "docker rmi {newImage}".format(newImage=newImage)
execute_sys_cmd(cmd)
print("-- pull {image} done --".format(image=image))
sys.exit(0)
else:
cmd = "docker pull {image}".format(image=image)
execute_sys_cmd(cmd)
sys.exit(0)