This repository has been archived by the owner on May 4, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
WIP Initial support for parametrization of descriptor files #116
Open
goldmann
wants to merge
3
commits into
jboss-dockerfiles:master
Choose a base branch
from
goldmann:params
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import hashlib | ||
import re | ||
import os | ||
import shutil | ||
import six | ||
import requests | ||
import yaml | ||
import tempfile | ||
|
@@ -29,6 +31,7 @@ def __init__(self, log, args, plugins=[]): | |
self.template = args.template | ||
self.scripts_path = args.scripts_path | ||
self.additional_scripts = args.additional_script | ||
self.params = args.params | ||
|
||
ssl_verify = None | ||
if args.skip_ssl_verification: | ||
|
@@ -203,7 +206,16 @@ def _validate_cfg(self): | |
plugin.extend_schema(schema) | ||
|
||
with open(self.descriptor, 'r') as stream: | ||
self.cfg = yaml.safe_load(stream) | ||
content = stream.read() | ||
|
||
for k, v in six.iteritems(self.params): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in python 2.7 we don need six for this. do we support python 2.6 and so? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't support 2.6. |
||
self.log.debug("Substituting '%s' with '%s' value..." % (k, v)) | ||
content = re.sub(r"{{%s.*}}" % k, v, content) | ||
|
||
# See if there any params without substitutions. If yes, use specified default values. | ||
content = re.sub(r"{{\w+:(.*)}}", '\g<1>', content) | ||
|
||
self.cfg = yaml.safe_load(content) | ||
|
||
c = Core(source_data=self.cfg, schema_data=schema) | ||
try: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,7 +31,7 @@ def setUp(self): | |
os.mkdir(self.target) | ||
self.args = argparse.Namespace(path=self.yaml, output=self.target, without_sources=None, | ||
template=None, scripts_path=None, additional_script=None, | ||
skip_ssl_verification=None) | ||
skip_ssl_verification=None, params={}) | ||
with open(self.yaml, 'wb') as f: | ||
f.write(self.basic_config.encode()) | ||
|
||
|
@@ -112,9 +112,6 @@ def test_set_entrypoint(self): | |
self.assertRegexpMatches(dockerfile, regex) | ||
|
||
def test_volumes(self): | ||
""" | ||
Test that cmd: is mapped into a CMD instruction | ||
""" | ||
with open(self.yaml, 'ab') as f: | ||
f.write("volumes:\n - '/var/lib'\n - '/usr/lib'".encode()) | ||
|
||
|
@@ -128,3 +125,53 @@ def test_volumes(self): | |
dockerfile = f.read() | ||
regex = re.compile(r'.*VOLUME \["/var/lib"\]\nVOLUME \["/usr/lib"\]', re.MULTILINE) | ||
self.assertRegexpMatches(dockerfile, regex) | ||
|
||
def test_default_substitution(self): | ||
with open(self.yaml, 'ab') as f: | ||
f.write("from: {{FROM:rhel:7}}\nversion: 1.0".encode()) | ||
|
||
generator = Generator(self.log, self.args) | ||
generator.configure() | ||
generator.render_from_template() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this code is copy pasted multiple times maybe it should be extracted to common setUp method There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably a good idea, yes. |
||
|
||
self.assertEqual(generator.cfg['from'], 'rhel:7') | ||
|
||
with open(os.path.join(self.target, "Dockerfile"), "r") as f: | ||
dockerfile = f.read() | ||
regex = re.compile(r'.*FROM rhel:7', re.MULTILINE) | ||
self.assertRegexpMatches(dockerfile, regex) | ||
|
||
def test_substitution_with_parameters(self): | ||
with open(self.yaml, 'ab') as f: | ||
f.write("from: {{FROM:rhel:7}}\nversion: 1.0".encode()) | ||
|
||
self.args.params = {'FROM': 'centos:7'} | ||
|
||
generator = Generator(self.log, self.args) | ||
generator.configure() | ||
generator.render_from_template() | ||
|
||
self.assertEqual(generator.cfg['from'], 'centos:7') | ||
|
||
with open(os.path.join(self.target, "Dockerfile"), "r") as f: | ||
dockerfile = f.read() | ||
regex = re.compile(r'.*FROM centos:7', re.MULTILINE) | ||
self.assertRegexpMatches(dockerfile, regex) | ||
|
||
def test_substitution_of_multiple_parameters(self): | ||
with open(self.yaml, 'ab') as f: | ||
f.write("from: rhel:7\ndescription: 'Image {{VERSION}}'\nenvs:\n information:\n - name: VERSION\n value: {{VERSION}}\n".encode()) | ||
|
||
self.args.params = {'VERSION': '1.0'} | ||
|
||
generator = Generator(self.log, self.args) | ||
generator.configure() | ||
generator.render_from_template() | ||
|
||
self.assertEqual(generator.cfg['description'], 'Image 1.0') | ||
self.assertEqual(generator.cfg['envs']['information'], [{'name': 'VERSION', 'value': 1.0}]) | ||
|
||
with open(os.path.join(self.target, "Dockerfile"), "r") as f: | ||
dockerfile = f.read() | ||
regex = re.compile(r'.*VERSION="1.0".*', re.MULTILINE) | ||
self.assertRegexpMatches(dockerfile, regex) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is very hacky - you should not mess with args object and store your values here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right. I guess the problem is that argparse doesn't support a dict directly. I could leave validation and just pass what I've got via CLI, but I guess I wanted to have the validation as close as possible to where argparse does it. I'm open for suggestions.