This repository has been archived by the owner on Dec 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathcheck_image.py
97 lines (76 loc) · 2.34 KB
/
check_image.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
"""
Checks the existence of a tag in a registry by calling skopeo
Usage: check_image.py file [regex]
If regex is supplied the path of the image must match this regex
"""
import os
import re
import subprocess
import sys
import time
import anymarkup
if os.environ.get('AUTH_FILE'):
AUTH_FILE = os.environ.get('AUTH_FILE')
else:
AUTH_FILE = os.path.expanduser('~/skopeo.json')
SKOPEO_USER = os.environ.get('SKOPEO_USER')
SKOPEO_PASS = os.environ.get('SKOPEO_PASS')
def skopeo_inspect(image):
# try without authentication first
cmd = ['skopeo', 'inspect', 'docker://{}'.format(image)]
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = p.communicate()
if p.returncode == 0:
return (p.returncode, stdout, stderr)
# try with authentication
if SKOPEO_USER and SKOPEO_PASS:
cmd += ['--creds', '{}:{}'.format(SKOPEO_USER, SKOPEO_PASS)]
else:
cmd += ['--authfile', AUTH_FILE]
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = p.communicate()
return (p.returncode, stdout, stderr)
try:
OPENSHIFT_TEMPLATE = sys.argv[1]
except IndexError:
print "Provide path to the processed OpenShift template"
sys.exit(1)
try:
image_path_pattern = re.compile(sys.argv[2])
except IndexError:
image_path_pattern = None
images = set()
for i in anymarkup.parse_file(OPENSHIFT_TEMPLATE, force_types=None)["items"]:
try:
for c in i["spec"]["template"]["spec"]["containers"]:
images.add(c["image"])
except KeyError:
pass
try:
images.add(i["spec"]["image"])
except KeyError:
pass
success = True
for image in images:
if image_path_pattern and not image_path_pattern.search(image):
print >>sys.stderr, ["ERROR_NO_MATCH",
image, image_path_pattern.pattern]
success = False
continue
attempt = 1
max_attemps = 5
while True:
status_code, stdout, stderr = skopeo_inspect(image)
if status_code == 0:
print ["OK", image]
break
print >>sys.stderr, ["ERROR", image, stderr]
if attempt < max_attemps:
time.sleep(attempt)
attempt += 1
else:
success = False
break
if not success:
sys.exit(1)
sys.exit(0)