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

Commit 6b74adf

Browse files
committed
Initial support for parametrization of descriptor files
1 parent 3238421 commit 6b74adf

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

dogen/cli.py

+14
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ def run(self):
5656
parser.add_argument('--skip-ssl-verification', action='store_true', help='Should we skip SSL verification when retrieving data?')
5757
parser.add_argument('--scripts-path', help='Location of the scripts directory containing script packages.')
5858
parser.add_argument('--additional-script', action='append', help='Location of additional script (can be url). Can be specified multiple times.')
59+
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.')
5960
parser.add_argument('--template', help='Path to custom template (can be url)')
6061

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

73+
params = {}
74+
75+
if args.params:
76+
for param in args.params:
77+
if '=' not in param:
78+
self.log.error("The --param argument with value '%s' could not be parsed. Please make sure you use the 'KEY=value' format" % param)
79+
sys.exit(1)
80+
81+
k, v = param.split("=", 1)
82+
params[k] = v
83+
84+
args.params = params
85+
7286
if args.verbose:
7387
self.log.setLevel(logging.DEBUG)
7488
else:

dogen/generator.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# -*- coding: utf-8 -*-
22

33
import hashlib
4+
import re
45
import os
56
import shutil
7+
import six
68
import requests
79
import yaml
810
import tempfile
@@ -29,6 +31,7 @@ def __init__(self, log, args, plugins=[]):
2931
self.template = args.template
3032
self.scripts_path = args.scripts_path
3133
self.additional_scripts = args.additional_script
34+
self.params = args.params
3235

3336
ssl_verify = None
3437
if args.skip_ssl_verification:
@@ -203,7 +206,16 @@ def _validate_cfg(self):
203206
plugin.extend_schema(schema)
204207

205208
with open(self.descriptor, 'r') as stream:
206-
self.cfg = yaml.safe_load(stream)
209+
content = stream.read()
210+
211+
for k, v in six.iteritems(self.params):
212+
self.log.debug("Substituting '%s' with '%s' value..." % (k, v))
213+
content = re.sub(r"{{%s.*}}" % k, v, content)
214+
215+
# See if there any params without substitutions. If yes, use specified default values.
216+
content = re.sub(r"{{\w+:(.*)}}", '\g<1>', content)
217+
218+
self.cfg = yaml.safe_load(content)
207219

208220
c = Core(source_data=self.cfg, schema_data=schema)
209221
try:

0 commit comments

Comments
 (0)