-
Notifications
You must be signed in to change notification settings - Fork 490
Expand file tree
/
Copy pathDockerfileUtility.py
More file actions
255 lines (210 loc) · 8.58 KB
/
DockerfileUtility.py
File metadata and controls
255 lines (210 loc) · 8.58 KB
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# (c) 2016 WebDevOps.io
#
# This file is part of Dockerfile Repository.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
# documentation files (the "Software"), to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
# to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or substantial portions
# of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
# THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
# OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import os
import re
DOCKERFILE_STATEMENT_FROM_RE = re.compile(r'FROM\s+(?P<image>[^\s:]+)(:(?P<tag>[^\s:]+))?(?!.*\s+AS)', re.MULTILINE)
DOCKERFILE_STATEMENT_FROM_MULTISTAGE_RE = re.compile(r'FROM\s+(?P<image>[^\s:]+)(:(?P<tag>[^\s:]+))?(\s+AS)', re.MULTILINE)
def find_file_in_path(dockerfile_path, filename="Dockerfile", whitelist=False, blacklist=False):
"""
Search all file un dockerfile_path with filename ends with "filename"
And match filter
:param dockerfile_path: path where to search file
:type dockerfile_path: str
:param filename: pattern which the file must be validate
:type filename: str
:param whitelist: list of term must be match in path
:type whitelist: list
:param blacklist: list of term must not be match in path
:type blacklist: list
:return: list of path
:rtype: list
"""
file_list = []
# build list of files
for root, dirs, files in os.walk(dockerfile_path):
for file in files:
if file.endswith(filename):
file_list.append(os.path.join(root, file))
# filter by whitelist
if whitelist:
tmp = []
for file in file_list:
for term in whitelist:
if term in file:
tmp.append(file)
break
file_list = tmp
if blacklist:
for term in blacklist:
file_list = list(filter(lambda x: term not in x, file_list))
return file_list
def find_dockerfiles_in_path(base_path, path_regex, image_prefix, whitelist=False, blacklist=False):
"""
Find all Dockerfiles in path (and even in symlinks and build dependencies)
"""
def parse_docker_info_from_path(path):
image_name_info = ([m.groupdict() for m in path_regex.finditer(os.path.abspath(path))])[0]
image_repository = (image_name_info['repository'] if 'repository' in image_name_info else '')
image_name = (image_name_info['image'] if 'image' in image_name_info else '')
image_tag = (image_name_info['tag'] if 'tag' in image_name_info else '')
image_is_duplicate = False
# check if path is linked
if os.path.islink(os.path.dirname(path)):
linked_image_name_info = ([m.groupdict() for m in path_regex.finditer(os.path.realpath(path))])[0]
linked_image_repository = (linked_image_name_info['repository'] if 'repository' in linked_image_name_info else '')
linked_image_name = (linked_image_name_info['image'] if 'image' in linked_image_name_info else '')
linked_image_tag = (linked_image_name_info['tag'] if 'tag' in linked_image_name_info else '')
image_from = image_prefix + linked_image_repository + '/' + linked_image_name + ':' + linked_image_tag
image_is_duplicate = True
else:
image_from = parse_dockerfile_from_statement(path)
imageInfo = {
'fullname': image_prefix + image_repository + '/' + image_name + ':' + image_tag,
'name': image_prefix + image_repository + '/' + image_name,
'tag': image_tag,
'repository': image_prefix + image_repository,
'imageName': image_name,
'from': image_from,
'duplicate': image_is_duplicate,
'multiStageImages': parse_dockerfile_multistage_images(path)
}
return imageInfo
ret = []
for path in find_dockerfile_in_path_recursive(base_path):
base_path = os.path.dirname(path)
if os.path.isfile(path) and os.path.basename(path) == 'Dockerfile':
dockerfile = {
'path': path,
'basePath': base_path,
'abspath': os.path.abspath(path),
'image': parse_docker_info_from_path(path),
}
ret.append(dockerfile)
ret = filter_dockerfile(
dockerfile_list=ret,
whitelist=whitelist,
blacklist = blacklist
)
return ret
def filter_dockerfile(dockerfile_list, whitelist=False, blacklist=False):
"""
Filter Dockerfiles by white- and blacklist
"""
if whitelist:
tmp = []
for dockerfile in dockerfile_list:
for term in whitelist:
if term in dockerfile['image']['fullname']:
tmp.append(dockerfile)
break
dockerfile_list = tmp
if blacklist:
for term in blacklist:
dockerfile_list = list(filter(lambda x: term not in x['image']['fullname'], dockerfile_list))
return dockerfile_list
def find_dockerfile_in_path_recursive(basePath):
"""
Find all Dockerfiles paths recursive in path
"""
ret = []
for root, subFolders, files in os.walk(basePath, followlinks=True):
for file in files:
if os.path.basename(file) == 'Dockerfile':
ret.append(os.path.join(root, file))
return ret
def create_imagename_from_regex_result(data):
"""
Create imagename from regex result (parsed Dockerfile)
"""
ret = data['image']
if data['tag']:
ret += ':%s' % data['tag']
ret = image_full_name(ret)
return ret
def parse_dockerfile_from_statement(path):
"""
Extract docker image name from FROM statement
"""
with open(path, 'r') as fileInput:
DockerfileContent = fileInput.read()
data = ([m.groupdict() for m in DOCKERFILE_STATEMENT_FROM_RE.finditer(DockerfileContent)])[0]
ret = create_imagename_from_regex_result(data)
return ret
def parse_dockerfile_multistage_images(path):
"""
Extract docker image name from FROM statement
"""
ret = []
with open(path, 'r') as fileInput:
DockerfileContent = fileInput.read()
for data in ([m.groupdict() for m in DOCKERFILE_STATEMENT_FROM_MULTISTAGE_RE.finditer(DockerfileContent)]):
ret.append(create_imagename_from_regex_result(data))
return ret
def image_full_name(image_name):
"""
Return full name image if just short form (eg. alpine instead of alpine:latest)
"""
if not re.search(':[^:]+$', image_name):
image_name = '%s:latest' % image_name
return image_name
def generate_image_name_with_tag_latest(image_name):
"""
Prepare dockerfile list with dependency and also add "auto latest tag" images
"""
if re.search(':[^:]+$', image_name):
ret = re.sub('(:[^:]+)$', ':latest', image_name)
else:
ret = '%s:latest' % image_name
return ret
def image_basename(image_name):
"""
Get image name without tag
"""
if re.search(':[^:]+$', image_name):
image_name = re.sub('(:[^:]+)$', '', image_name)
return image_name
def extract_image_name_tag(image_name):
"""
Get tag from image name
"""
if re.search('^(.*):', image_name):
ret = re.sub('^(.*):', '', image_name)
else:
ret = 'latest'
return ret
def check_if_base_image_needs_pull(image, configuration):
ret = False
if configuration.get('docker.autoPull'):
if configuration.get('docker.autoPullWhitelist') and configuration.get('docker.autoPullWhitelist').search(image):
"""
Matched whitelist
"""
ret = True
else:
"""
No whitelist, we need to pull every image
"""
ret = True
if configuration.get('docker.autoPullBlacklist') and configuration.get('docker.autoPullBlacklist').match(image):
"""
Matched blacklist
"""
ret = False
return ret