-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathgenerate_sync_yaml.py
465 lines (369 loc) · 13.1 KB
/
generate_sync_yaml.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
import os
import re
import yaml
import requests
from distutils.version import LooseVersion
# 基本配置
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
CONFIG_FILE = os.path.join(BASE_DIR, 'config.yaml')
SYNC_FILE = os.path.join(BASE_DIR, 'sync.yaml')
CUSTOM_SYNC_FILE = os.path.join(BASE_DIR, 'custom_sync.yaml')
def is_exclude_tag(tag):
"""
排除tag
:param tag:
:return:
"""
excludes = ['alpha', 'beta', 'rc', 'dev', 'test', 'amd64', 'ppc64le', 'arm64', 'arm', 's390x', 'SNAPSHOT', 'debug', 'master', 'latest', 'main']
for e in excludes:
if e.lower() in tag.lower():
return True
if str.isalpha(tag):
return True
if len(tag) >= 40:
return True
# 处理带有 - 字符的 tag
if re.search("-\d$", tag, re.M | re.I):
return False
# v20231011-8b53cabe0
if re.search("-\w{9}", tag, re.M | re.I):
return False
if '-' in tag:
return True
return False
def get_repo_aliyun_tags(image):
"""
获取 aliyuncs repo 最新的 tag
:param image:
:return:
"""
image_name = image.split('/')[-1]
tags = []
hearders = {
'User-Agent': 'docker/19.03.12 go/go1.13.10 git-commit/48a66213fe kernel/5.8.0-1.el7.elrepo.x86_64 os/linux arch/amd64 UpstreamClient(Docker-Client/19.03.12 \(linux\))'
}
token_url = "https://dockerauth.cn-hangzhou.aliyuncs.com/auth?scope=repository:kainstall/{image}:pull&service=registry.aliyuncs.com:cn-hangzhou:26842".format(
image=image_name)
try:
token_res = requests.get(url=token_url, headers=hearders)
token_data = token_res.json()
access_token = token_data['token']
except Exception as e:
print('[Get repo token]', e)
return tags
tag_url = "https://registry.cn-hangzhou.aliyuncs.com/v2/kainstall/{image}/tags/list".format(image=image_name)
hearders['Authorization'] = 'Bearer ' + access_token
try:
tag_res = requests.get(url=tag_url, headers=hearders)
tag_data = tag_res.json()
print('[aliyun tag]: ', tag_data)
except Exception as e:
print('[Get tag Error]', e)
return tags
tags = tag_data.get('tags', [])
return tags
def get_repo_gcr_tags(image, limit=5, host="k8s.gcr.io"):
"""
获取 gcr.io repo 最新的 tag
:param host:
:param image:
:param limit:
:return:
"""
hearders = {
'User-Agent': 'docker/19.03.12 go/go1.13.10 git-commit/48a66213fe kernel/5.8.0-1.el7.elrepo.x86_64 os/linux arch/amd64 UpstreamClient(Docker-Client/19.03.12 \(linux\))'
}
tag_url = "https://{host}/v2/{image}/tags/list".format(host=host, image=image)
tags = []
tags_data = []
manifest_data = []
try:
tag_rep = requests.get(url=tag_url, headers=hearders)
tag_req_json = tag_rep.json()
manifest_data = tag_req_json['manifest']
except Exception as e:
print('[Get tag Error]', e)
return tags
for manifest in manifest_data:
sha256_data = manifest_data[manifest]
sha256_tag = sha256_data.get('tag', [])
if len(sha256_tag) > 0:
# 排除 tag
if is_exclude_tag(sha256_tag[0]):
continue
tags_data.append({
'tag': sha256_tag[0],
'timeUploadedMs': sha256_data.get('timeUploadedMs')
})
tags_sort_data = sorted(tags_data, key=lambda i: i['timeUploadedMs'], reverse=True)
# limit tag
tags_limit_data = tags_sort_data[:limit]
image_aliyun_tags = get_repo_aliyun_tags(image)
for t in tags_limit_data:
# 去除同步过的
if t['tag'] in image_aliyun_tags:
continue
tags.append(t['tag'])
print('[repo tag]', tags)
return tags
def get_repo_quay_tags(image, limit=5):
"""
获取 quay.io repo 最新的 tag
:param image:
:param limit:
:return:
"""
hearders = {
'User-Agent': 'docker/19.03.12 go/go1.13.10 git-commit/48a66213fe kernel/5.8.0-1.el7.elrepo.x86_64 os/linux arch/amd64 UpstreamClient(Docker-Client/19.03.12 \(linux\))'
}
tag_url = "https://quay.io/api/v1/repository/{image}/tag/?onlyActiveTags=true&limit=100".format(image=image)
tags = []
tags_data = []
manifest_data = []
try:
tag_rep = requests.get(url=tag_url, headers=hearders)
tag_req_json = tag_rep.json()
manifest_data = tag_req_json['tags']
except Exception as e:
print('[Get tag Error]', e)
return tags
for manifest in manifest_data:
name = manifest.get('name', '')
# 排除 tag
if is_exclude_tag(name):
continue
tags_data.append({
'tag': name,
'start_ts': manifest.get('start_ts')
})
tags_sort_data = sorted(tags_data, key=lambda i: i['start_ts'], reverse=True)
# limit tag
tags_limit_data = tags_sort_data[:limit]
image_aliyun_tags = get_repo_aliyun_tags(image)
for t in tags_limit_data:
# 去除同步过的
if t['tag'] in image_aliyun_tags:
continue
tags.append(t['tag'])
print('[repo tag]', tags)
return tags
def get_repo_elastic_tags(image, limit=5):
"""
获取 elastic.io repo 最新的 tag
:param image:
:param limit:
:return:
"""
token_url = "https://docker-auth.elastic.co/auth?service=token-service&scope=repository:{image}:pull".format(
image=image)
tag_url = "https://docker.elastic.co/v2/{image}/tags/list".format(image=image)
tags = []
tags_data = []
manifest_data = []
hearders = {
'User-Agent': 'docker/19.03.12 go/go1.13.10 git-commit/48a66213fe kernel/5.8.0-1.el7.elrepo.x86_64 os/linux arch/amd64 UpstreamClient(Docker-Client/19.03.12 \(linux\))'
}
try:
token_res = requests.get(url=token_url, headers=hearders)
token_data = token_res.json()
access_token = token_data['token']
except Exception as e:
print('[Get repo token]', e)
return tags
hearders['Authorization'] = 'Bearer ' + access_token
try:
tag_rep = requests.get(url=tag_url, headers=hearders)
tag_req_json = tag_rep.json()
manifest_data = tag_req_json['tags']
except Exception as e:
print('[Get tag Error]', e)
return tags
for tag in manifest_data:
# 排除 tag
if is_exclude_tag(tag):
continue
tags_data.append(tag)
tags_sort_data = sorted(tags_data, key=LooseVersion, reverse=True)
# limit tag
tags_limit_data = tags_sort_data[:limit]
image_aliyun_tags = get_repo_aliyun_tags(image)
for t in tags_limit_data:
# 去除同步过的
if t in image_aliyun_tags:
continue
tags.append(t)
print('[repo tag]', tags)
return tags
def get_repo_ghcr_tags(image, limit=5):
"""
获取 ghcr.io repo 最新的 tag
:param image:
:param limit:
:return:
"""
token_url = "https://ghcr.io/token?service=ghcr.io&scope=repository:{image}:pull".format(
image=image)
tag_url = "https://ghcr.io/v2/{image}/tags/list".format(image=image)
tags = []
tags_data = []
hearders = {
'User-Agent': 'docker/19.03.12 go/go1.13.10 git-commit/48a66213fe kernel/5.8.0-1.el7.elrepo.x86_64 os/linux arch/amd64 UpstreamClient(Docker-Client/19.03.12 \(linux\))'
}
try:
token_res = requests.get(url=token_url, headers=hearders)
token_data = token_res.json()
print("token_data", token_url, token_data)
access_token = token_data['token']
except Exception as e:
print('[Get repo token]', e)
return tags
hearders['Authorization'] = 'Bearer ' + access_token
try:
tag_rep = requests.get(url=tag_url, headers=hearders)
tag_req_json = tag_rep.json()
manifest_data = tag_req_json['tags']
except Exception as e:
print('[Get tag Error]', e)
return tags
for tag in manifest_data:
# 排除 tag
if is_exclude_tag(tag):
continue
tags_data.append(tag)
tags_sort_data = sorted(tags_data, key=LooseVersion, reverse=True)
# limit tag
tags_limit_data = tags_sort_data[:limit]
image_aliyun_tags = get_repo_aliyun_tags(image)
for t in tags_limit_data:
# 去除同步过的
if t in image_aliyun_tags:
continue
tags.append(t)
print('[repo tag]', tags)
return tags
def get_docker_io_tags(image, limit=5):
hearders = {
'User-Agent':
'docker/19.03.12 go/go1.13.10 git-commit/48a66213fe kernel/5.8.0-1.el7.elrepo.x86_64 os/linux arch/amd64 UpstreamClient(Docker-Client/19.03.12 \(linux\))'
}
namespace_image = image.split('/')
tag_url = "https://hub.docker.com/v2/namespaces/{username}/repositories/{image}/tags".format(
username=namespace_image[0], image=namespace_image[1])
print(tag_url)
tags = []
tags_data = []
manifest_data = []
try:
tag_rep = requests.get(url=tag_url, headers=hearders)
tag_req_json = tag_rep.json()
manifest_data = tag_req_json['results']
except Exception as e:
print('[Get tag Error]', e)
return tags
for tag in manifest_data:
name = tag.get('name', '')
# 排除 tag
if is_exclude_tag(name):
continue
tags_data.append(name)
tags_sort_data = sorted(tags_data, key=LooseVersion, reverse=True)
# limit tag
tags_limit_data = tags_sort_data[:limit]
image_aliyun_tags = get_repo_aliyun_tags(namespace_image[1])
for t in tags_limit_data:
# 去除同步过的
if t in image_aliyun_tags:
continue
tags.append(t)
return tags
def get_repo_tags(repo, image, limit=5):
"""
获取 repo 最新的 tag
:param repo:
:param image:
:param limit:
:return:
"""
tags_data = []
if repo == 'gcr.io':
tags_data = get_repo_gcr_tags(image, limit, "gcr.io")
elif repo == 'k8s.gcr.io':
tags_data = get_repo_gcr_tags(image, limit, "k8s.gcr.io")
elif repo == 'registry.k8s.io':
tags_data = get_repo_gcr_tags(image, limit, "registry.k8s.io")
elif repo == 'quay.io':
tags_data = get_repo_quay_tags(image, limit)
elif repo == 'docker.elastic.co':
tags_data = get_repo_elastic_tags(image, limit)
elif repo == 'ghcr.io':
tags_data = get_repo_ghcr_tags(image, limit)
elif repo == "docker.io":
tags_data = get_docker_io_tags(image, limit)
return tags_data
def generate_dynamic_conf():
"""
生成动态同步配置
:return:
"""
print('[generate_dynamic_conf] start.')
config = None
with open(CONFIG_FILE, 'r') as stream:
try:
config = yaml.safe_load(stream)
except yaml.YAMLError as e:
print('[Get Config]', e)
exit(1)
print('[config]', config)
skopeo_sync_data = {}
for repo in config['images']:
if repo not in skopeo_sync_data:
skopeo_sync_data[repo] = {'images': {}}
if config['images'][repo] is None:
continue
for image in config['images'][repo]:
print("[image] {image}".format(image=image))
sync_tags = get_repo_tags(repo, image, config['last'])
if len(sync_tags) > 0:
skopeo_sync_data[repo]['images'][image] = sync_tags
# skopeo_sync_data[repo]['images'][image].append('latest')
else:
print('[{image}] no sync tag.'.format(image=image))
print('[sync data]', skopeo_sync_data)
with open(SYNC_FILE, 'w+') as f:
yaml.safe_dump(skopeo_sync_data, f, default_flow_style=False)
print('[generate_dynamic_conf] done.', end='\n\n')
def generate_custom_conf():
"""
生成自定义的同步配置
:return:
"""
print('[generate_custom_conf] start.')
custom_sync_config = None
with open(CUSTOM_SYNC_FILE, 'r') as stream:
try:
custom_sync_config = yaml.safe_load(stream)
except yaml.YAMLError as e:
print('[Get Config]', e)
exit(1)
print('[custom_sync config]', custom_sync_config)
custom_skopeo_sync_data = {}
for repo in custom_sync_config:
if repo not in custom_skopeo_sync_data:
custom_skopeo_sync_data[repo] = {'images': {}}
if custom_sync_config[repo]['images'] is None:
continue
for image in custom_sync_config[repo]['images']:
image_aliyun_tags = get_repo_aliyun_tags(image)
for tag in custom_sync_config[repo]['images'][image]:
if tag in image_aliyun_tags:
continue
if image not in custom_skopeo_sync_data[repo]['images']:
custom_skopeo_sync_data[repo]['images'][image] = [tag]
else:
custom_skopeo_sync_data[repo]['images'][image].append(tag)
print('[custom_sync data]', custom_skopeo_sync_data)
with open(CUSTOM_SYNC_FILE, 'w+') as f:
yaml.safe_dump(custom_skopeo_sync_data, f, default_flow_style=False)
print('[generate_custom_conf] done.', end='\n\n')
generate_dynamic_conf()
generate_custom_conf()