Fixing 'render' function behavior

Files will be rendered directly to temporary directory and
only for matched images

Change-Id: I74431d5de90f7d001edf75a3ed41181b7b9321d1
This commit is contained in:
Andrey Pavlov
2016-11-17 11:33:47 +00:00
parent 2ec87d8f6f
commit bffb2aa43e
2 changed files with 19 additions and 12 deletions

View File

@@ -28,6 +28,7 @@ def render_dockerfile(path, name, config):
LOG.debug('%s: Rendering dockerfile', name)
sources = set()
parent = [] # Could've been None if we could use nonlocal
render_files = []
def copy_sources(source_name, cont_dir):
if source_name not in config['sources']:
@@ -42,24 +43,22 @@ def render_dockerfile(path, name, config):
return images.image_spec(image_name, add_address=CONF.builder.push)
def render(fname):
dirname = os.path.dirname(path)
fpath = os.path.join(dirname, fname)
if fname.endswith('.j2'):
oname = fname[:-3]
else:
oname = fname + '.rendered'
opath = os.path.join(dirname, oname)
content = jinja_utils.jinja_render(fpath, config['render'],
[copy_sources, image_spec, render])
with open(opath, 'wb') as f:
f.write(content.encode('utf-8'))
render_files.append({
'src': fname,
'dest': oname
})
return oname
content = jinja_utils.jinja_render(path, config['render'],
[copy_sources, image_spec, render])
return content, sources, parent[0] if parent else None
return content, sources, render_files, parent[0] if parent else None
def prepare_source(source_name, name, dest_dir, config):
@@ -92,6 +91,13 @@ def create_rendered_dockerfile(dockerfile, tmp_path, config):
for source_name in dockerfile['sources']:
prepare_source(source_name, dockerfile['name'], dest_dir, config)
for render_file in dockerfile['render_files']:
fpath = os.path.join(src_dir, render_file['src'])
opath = os.path.join(dest_dir, render_file['dest'])
content = jinja_utils.jinja_render(fpath, config['render'])
with open(opath, 'wb') as f:
f.write(content.encode('utf-8'))
for filename in os.listdir(src_dir):
if 'Dockerfile' in filename:
continue
@@ -141,11 +147,12 @@ def find_dockerfiles(repository_name, match=True):
def render_dockerfiles(dockerfiles, config):
for dockerfile in dockerfiles.values():
content, sources, parent = \
content, sources, render_files, parent = \
render_dockerfile(dockerfile['path'], dockerfile['name'], config)
dockerfile['content'] = content
dockerfile['sources'] = sources
dockerfile['parent'] = parent
dockerfile['render_files'] = render_files
IMAGE_FULL_NAME_RE = r"((?P<namespace>[\w:\.-]+)/){0,2}" \

View File

@@ -235,12 +235,12 @@ class TestRenderDockerfile(testscenarios.WithScenarios, base.TestCase):
('empty', {
'config': {'render': {}},
'source': '',
'result': ('', set(), None),
'result': ('', set(), [], None),
}),
('one_source', {
'config': {'render': {}, 'sources': {'one': {}}},
'source': '{{ copy_sources("one", "/tmp") }}',
'result': ('COPY one /tmp', {'one'}, None),
'result': ('COPY one /tmp', {'one'}, [], None),
}),
('wrong_source', {
'config': {'render': {}, 'sources': {'one': {}}},
@@ -250,7 +250,7 @@ class TestRenderDockerfile(testscenarios.WithScenarios, base.TestCase):
('one_from', {
'config': {'render': {}},
'source': 'FROM {{ image_spec("one") }}',
'result': ('FROM ccp/one:latest', set(), 'one'),
'result': ('FROM ccp/one:latest', set(), [], 'one'),
}),
]