This repository was archived by the owner on May 4, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathgenerator.py
More file actions
318 lines (240 loc) · 11.2 KB
/
Copy pathgenerator.py
File metadata and controls
318 lines (240 loc) · 11.2 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
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
# -*- coding: utf-8 -*-
import hashlib
import glob
import os
import shutil
import requests
import yaml
import tempfile
from jinja2 import FileSystemLoader, Environment
from pykwalify.core import Core
from pykwalify.errors import SchemaError
from dogen.template_helper import TemplateHelper
from dogen.tools import Tools
from dogen import version, DEFAULT_SCRIPT_EXEC, DEFAULT_SCRIPT_USER
from dogen.errors import Error
class Generator(object):
def __init__(self, log, args, plugins=[]):
self.log = log
self.pwd = os.path.realpath(os.path.dirname(os.path.realpath(__file__)))
self.descriptor = os.path.realpath(args.path)
self.without_sources = args.without_sources
self.output = args.output
self.dockerfile = os.path.join(self.output, "Dockerfile")
self.template = args.template
self.scripts_path = args.scripts_path
self.additional_scripts = args.additional_script
self.ignore_version = args.ignore_version
ssl_verify = None
if args.skip_ssl_verification:
ssl_verify = False
self.ssl_verify = ssl_verify
self.plugins = []
for plugin in plugins:
self.plugins.append(plugin(self, args))
def _fetch_file(self, location, output=None):
"""
Fetches remote file and saves it under output. If no
output path is provided, a temporary file is created
and path to this file is returned.
SSL verification could be disabled by setting
self.ssl_verify to False.
"""
self.log.info("Fetching '%s' file..." % location)
if not output:
output = tempfile.mktemp("-dogen")
self.log.info("Fetched file will be saved as '%s'..." % os.path.basename(output))
with open(output, 'wb') as f:
f.write(requests.get(location, verify=self.ssl_verify).content)
return output
def _handle_custom_template(self):
"""
Fetches custom template (if provided) and saves as temporary
file. This file is removed later in the process.
"""
if not self.template:
return
self.log.info("Using custom provided template file: '%s'" % self.template)
if Tools.is_url(self.template):
self.template = self._fetch_file(self.template)
if not os.path.exists(self.template):
raise Error("Template file '%s' could not be found. Please make sure you specified correct path or check if the file was successfully fetched." % self.template)
def configure(self):
"""
Reads configuration values from the descriptor, if provided.
Some Dogen configuration values can be set in the YAML
descriptor file using the 'dogen' section.
"""
self._validate_cfg()
if not self.scripts_path:
# If scripts directory is not provided, see if there is a "scripts"
# directory next to the descriptor. If found - assume that's the
# directory containing scripts.
scripts = os.path.join(os.path.dirname(self.descriptor), "scripts")
if os.path.exists(scripts) and os.path.isdir(scripts):
self.scripts_path = scripts
if 'user' not in self.cfg:
self.cfg['user'] = 0
dogen_cfg = self.cfg.get('dogen')
if not dogen_cfg:
return
required_version = dogen_cfg.get('version')
if required_version:
# Check if the current runnig version of Dogen
# is the one the descriptor is expecting.
if required_version != version:
message = "You try to parse descriptor that requires Dogen version %s, but you run version %s" % (required_version, version)
if self.ignore_version:
self.log.warn(message)
else:
raise Error(message)
ssl_verify = dogen_cfg.get('ssl_verify')
if self.ssl_verify is None and ssl_verify is not None:
self.ssl_verify = ssl_verify
template = dogen_cfg.get('template')
if template and not self.template:
self.template = template
scripts = dogen_cfg.get('scripts_path')
if scripts and not self.scripts_path:
self.scripts_path = scripts
additional_scripts = dogen_cfg.get('additional_scripts')
if additional_scripts and not self.additional_scripts:
self.additional_scripts = additional_scripts
if self.scripts_path and not os.path.exists(self.scripts_path):
raise Error("Provided scripts directory '%s' does not exist" % self.scripts_path)
def _handle_scripts(self):
if not self.cfg.get('scripts'):
return
for script in self.cfg['scripts']:
package = script['package']
src_path = os.path.join(self.scripts_path, package)
output_path = os.path.join(self.output, "scripts", package)
possible_exec = os.getenv('DOGEN_SCRIPT_EXEC', DEFAULT_SCRIPT_EXEC)
if "exec" not in script and os.path.exists(os.path.join(src_path, possible_exec)):
script['exec'] = possible_exec
if "user" not in script:
script['user'] = os.getenv('DOGEN_SCRIPT_USER', DEFAULT_SCRIPT_USER)
# Poor-man's workaround for not copying multiple times the same thing
if not os.path.exists(output_path):
self.log.info("Copying package '%s'..." % package)
shutil.copytree(src=src_path, dst=output_path)
self.log.debug("Done.")
def _handle_additional_scripts(self):
self.log.info("Additional scripts provided, installing them...")
output_scripts = os.path.join(self.output, "scripts")
if not os.path.exists(output_scripts):
os.makedirs(output_scripts)
for f in self.additional_scripts:
self.log.debug("Handling '%s' file..." % f)
if Tools.is_url(f):
self._fetch_file(f, os.path.join(output_scripts, os.path.basename(f)))
else:
if not (os.path.exists(f) and os.path.isfile(f)):
raise Error("File '%s' does not exist. Please make sure you specified correct path to a file when specifying additional scripts." % f)
self.log.debug("Copying '%s' file to target scripts directory..." % f)
shutil.copy(f, output_scripts)
def _validate_cfg(self):
"""
Open and parse the YAML configuration file and ensure it matches
our Schema for a Dogen configuration.
"""
# Fail early if descriptor file is not found
if not os.path.exists(self.descriptor):
raise Error("Descriptor file '%s' could not be found. Please make sure you specified correct path." % self.descriptor)
schema_path = os.path.join(self.pwd, "schema", "kwalify_schema.yaml")
schema = {}
with open(schema_path, 'r') as fh:
schema = yaml.safe_load(fh)
if schema is None:
raise Error("couldn't read a valid schema at %s" % schema_path)
for plugin in self.plugins:
plugin.extend_schema(schema)
with open(self.descriptor, 'r') as stream:
self.cfg = yaml.safe_load(stream)
c = Core(source_data=self.cfg, schema_data=schema)
try:
c.validate(raise_exception=True)
except SchemaError as e:
raise Error(e)
def run(self):
# Set Dogen settings if provided in descriptor
self.configure()
# Special case for ssl_verify setting. Setting it to None
# in CLI if --skip-ssl-verification is not set to make it
# possible to determine which setting should be used.
# This means that we need to se the ssl_verify to the
# default value of True is not set.
if self.ssl_verify is None:
self.ssl_verify = True
for plugin in self.plugins:
plugin.prepare(cfg=self.cfg)
if self.template:
self._handle_custom_template()
# Remove the target scripts directory
shutil.rmtree(os.path.join(self.output, "scripts"), ignore_errors=True)
if not os.path.exists(self.output):
os.makedirs(self.output)
if self.scripts_path:
self._handle_scripts()
else:
self.log.warn("No scripts will be copied, mistake?")
# Additional scripts (not package scripts)
if self.additional_scripts:
self._handle_additional_scripts()
self.render_from_template()
sources = self.handle_sources()
for plugin in self.plugins:
plugin.after_sources(files=sources)
self.log.info("Finished!")
def render_from_template(self):
if self.template:
template_file = self.template
else:
self.log.debug("Using dogen provided template file")
template_file = os.path.join(self.pwd, "templates", "template.jinja")
self.log.info("Rendering Dockerfile...")
loader = FileSystemLoader(os.path.dirname(template_file))
env = Environment(loader=loader, trim_blocks=True, lstrip_blocks=True)
env.globals['helper'] = TemplateHelper()
template = env.get_template(os.path.basename(template_file))
with open(self.dockerfile, 'wb') as f:
f.write(template.render(self.cfg).encode('utf-8'))
self.log.debug("Done")
if self.template and Tools.is_url(self.template):
self.log.debug("Removing temporary template file...")
os.remove(self.template)
def handle_sources(self):
if 'sources' not in self.cfg or self.without_sources:
return []
files = []
for source in self.cfg['sources']:
url = source['url']
target = source.get('target')
basename = os.path.basename(url)
# In case we specify target name for the artifact - use it
if not target:
target = basename
files.append(target)
filename = ("%s/%s" % (self.output, target))
passed = False
try:
if os.path.exists(filename):
self.check_sum(filename, source['md5sum'])
passed = True
except Exception as e:
self.log.warn(str(e))
passed = False
if not passed:
sources_cache = os.environ.get("DOGEN_SOURCES_CACHE")
if sources_cache:
self.log.info("Using '%s' as cached location for sources" % sources_cache)
url = "%s/%s" % (sources_cache, basename)
self._fetch_file(url, filename)
self.check_sum(filename, source['md5sum'])
return files
def check_sum(self, filename, checksum):
self.log.info("Checking '%s' MD5 hash..." % os.path.basename(filename))
filesum = hashlib.md5(open(filename, 'rb').read()).hexdigest()
if filesum != checksum:
raise Exception("The md5sum computed for the '%s' file ('%s') doesn't match the '%s' value" % (filename, filesum, checksum))
self.log.debug("MD5 hash is correct.")