forked from silenceshell/docker_wrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker_wrapper.py
executable file
·65 lines (54 loc) · 1.92 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
#!/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 gcr.io/google_containers/kube-apiserver:v1.14.1
# k8s.gcr.io/xxx:yyy => gcr.io/google-containers/xxx:yyy -> gcr.azk8s.cn/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
converts = [
{
'prefix': 'gcr.io',
'replace': lambda x: x.replace('gcr.io', 'gcr.mirrors.ustc.edu.cn'),
},
{
'prefix': 'k8s.gcr.io',
'replace': lambda x: x.replace('k8s.gcr.io', 'gcr.mirrors.ustc.edu.cn/google-containers'),
},
{
'prefix': 'quay.io',
'replace': lambda x: x.replace('quay.io', 'quay.mirrors.ustc.edu.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)
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)