Skip to content
This repository has been archived by the owner on May 4, 2018. It is now read-only.

Commit

Permalink
Initial support for parametrization of descriptor files
Browse files Browse the repository at this point in the history
  • Loading branch information
goldmann committed May 2, 2017
1 parent 3238421 commit 6b74adf
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
14 changes: 14 additions & 0 deletions dogen/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def run(self):
parser.add_argument('--skip-ssl-verification', action='store_true', help='Should we skip SSL verification when retrieving data?')
parser.add_argument('--scripts-path', help='Location of the scripts directory containing script packages.')
parser.add_argument('--additional-script', action='append', help='Location of additional script (can be url). Can be specified multiple times.')
parser.add_argument('--param', action='append', dest='params', help='List of key and value pairs (format: KEY=value) used for substitution in the image descriptor. Can be specified multiple times.')
parser.add_argument('--template', help='Path to custom template (can be url)')

parser.add_argument('path', help="Path to yaml descriptor to process")
Expand All @@ -69,6 +70,19 @@ def run(self):
parser.epilog = epilog
args = parser.parse_args()

params = {}

if args.params:
for param in args.params:
if '=' not in param:
self.log.error("The --param argument with value '%s' could not be parsed. Please make sure you use the 'KEY=value' format" % param)
sys.exit(1)

k, v = param.split("=", 1)
params[k] = v

args.params = params

if args.verbose:
self.log.setLevel(logging.DEBUG)
else:
Expand Down
14 changes: 13 additions & 1 deletion dogen/generator.py
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
Expand All @@ -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:
Expand Down Expand Up @@ -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):
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:
Expand Down

0 comments on commit 6b74adf

Please sign in to comment.