-
Notifications
You must be signed in to change notification settings - Fork 0
/
replace-with-mirror.py
executable file
·68 lines (45 loc) · 1.63 KB
/
replace-with-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
#!/usr/bin/env python3
def replace_registry(imageUrl):
with open("domain_mirror.txt",'r',encoding='utf-8') as fh:
mirror_lists=fh.readlines()
fh.close()
registry_mirror_dict={}
for line in mirror_lists:
line = line.rstrip()
pos= line.find("=")
if pos < 0:
print("error: line must be like 'a=b'")
return
src_registry = line[0:pos]
dest_registry = line[pos+1:len(line)].rstrip()
registry_mirror_dict[src_registry] = dest_registry
str_list= imageUrl.split(":")
# handle: docker.m.daocloud.io/calico/cni
if len(str_list) == 1 :
imageUrl +=":latest"
name = str_list[0].split("/")
pos2 = name[0].find('.')
# handle: alpine:3.0
if pos2 < 0:
new_imageUrl = "m.daocloud.io/"+ imageUrl
else:
for src_registry,dest_registry in registry_mirror_dict.items():
if name[0] == src_registry:
new_imageUrl= imageUrl.replace(src_registry,dest_registry)
continue
return new_imageUrl
with open("image-list.txt",'r',encoding='utf-8') as fh:
lines = fh.readlines()
mirrored_image_list =[]
for image in lines:
image = image.rstrip()
mirrored_image = replace_registry(image)
mirrored_image_list.append(mirrored_image)
fh.close()
with open("mirrored-image-list.txt",'w+',encoding='utf-8') as mfh:
for i, image in enumerate(mirrored_image_list):
if i != len(mirrored_image_list)-1:
mfh.write(image+"\n")
else:
mfh.write(image)
mfh.close()