Skip to content

Commit d834a6d

Browse files
committed
Box base name depends on --profile now
1 parent 098343e commit d834a6d

1 file changed

Lines changed: 28 additions & 11 deletions

File tree

malboxes/malboxes.py

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ def init_parser():
8484
parser_build.add_argument('--force', action='store_true',
8585
help='Force the build to happen. Overwrites '
8686
'pre-existing builds or vagrant boxes.')
87+
parser_build.add_argument('--profile', dest='profile', action='store',
88+
help='Override the profile setting')
8789
parser_build.add_argument('--skip-packer-build', action='store_true',
8890
help='Skip packer build phase. '
8991
'Only useful for debugging.')
@@ -100,6 +102,8 @@ def init_parser():
100102
parser_spin.add_argument('name', help='Name of the target VM. '
101103
'Must be unique on your system. '
102104
'Ex: Cryptolocker_XYZ.')
105+
parser_spin.add_argument('--profile', dest='profile', action='store',
106+
help='Override the profile setting')
103107
parser_spin.set_defaults(func=spin)
104108

105109
# no command
@@ -126,14 +130,17 @@ def prepare_autounattend(config):
126130
f.close()
127131

128132

129-
def prepare_packer_template(config, template_name):
133+
def prepare_packer_template(config, args):
130134
"""
131135
Prepares a packer template JSON file according to configuration and writes
132136
it into a temporary location where packer later expects it.
133137
134138
Uses jinja2 template syntax to generate the resulting JSON file.
135139
Templates are in templates/ and snippets in templates/snippets/.
136140
"""
141+
template_name = config["template"]
142+
packer_template_name = config["template_name"]
143+
137144
try:
138145
template_fd = resource_stream(__name__,
139146
'templates/{}.json'.format(template_name))
@@ -147,7 +154,7 @@ def prepare_packer_template(config, template_name):
147154
template = env.get_template("{}.json".format(template_name))
148155

149156
# write to temporary file
150-
f = create_cachefd('{}.json'.format(template_name))
157+
f = create_cachefd('{}.json'.format(packer_template_name))
151158
f.write(template.render(config)) # pylint: disable=no-member
152159
f.close()
153160
return f.name
@@ -167,7 +174,7 @@ def _prepare_vagrantfile(config, source, fd_dest):
167174
fd_dest.close()
168175

169176

170-
def prepare_config(template):
177+
def prepare_config(args):
171178
"""
172179
Prepares Malboxes configuration and merge with Packer template configuration
173180
@@ -190,15 +197,15 @@ def prepare_config(template):
190197
shutil.copy(resource_filename(__name__, 'config-example.js'),
191198
config_file)
192199

193-
config = load_config(config_file, template)
200+
config = load_config(config_file, args)
194201

195202
if "profile" in config.keys():
196-
profile_config = prepare_profile(template, config)
203+
profile_config = prepare_profile(config, args)
197204

198205
# profile_config might contain a profile not in the config file
199206
config.update(profile_config)
200207

201-
packer_tmpl = prepare_packer_template(config, template)
208+
packer_tmpl = prepare_packer_template(config, args)
202209

203210
# merge/update with template config
204211
with open(packer_tmpl, 'r') as f:
@@ -207,19 +214,28 @@ def prepare_config(template):
207214
return config, packer_tmpl
208215

209216

210-
def load_config(config_filename, template):
217+
def load_config(config_filename, args):
211218
"""Loads the minified JSON config. Returns a dict."""
212219

213220
config = {}
214221
with open(config_filename, 'r') as config_file:
215222
# minify then load as JSON
216223
config = json.loads(jsmin(config_file.read()))
217224

225+
if getattr(args, 'profile', None):
226+
config["profile"] = args.profile
227+
228+
229+
config["template"] = args.template
230+
if "profile" in config.keys():
231+
config['template_name'] = "{}_{}".format(args.template, config["profile"])
232+
else:
233+
config['template_name'] = args.template
234+
218235
# add packer required variables
219236
# Note: Backslashes are replaced with forward slashes (Packer on Windows)
220237
config['cache_dir'] = DIRS.user_cache_dir.replace('\\', '/')
221238
config['dir'] = resource_filename(__name__, "").replace('\\', '/')
222-
config['template_name'] = template
223239
config['config_dir'] = DIRS.user_config_dir.replace('\\', '/')
224240

225241
# add default values
@@ -387,7 +403,7 @@ def list_templates(parser, args):
387403
def build(parser, args):
388404

389405
print("Generating configuration files...")
390-
config, packer_tmpl = prepare_config(args.template)
406+
config, packer_tmpl = prepare_config(args)
391407
prepare_autounattend(config)
392408
_prepare_vagrantfile(config, "box_win.rb", create_cachefd('box_win.rb'))
393409
print("Configuration files are ready")
@@ -436,7 +452,7 @@ def spin(parser, args):
436452
print("Vagrantfile already exists. Please move it away. Exiting...")
437453
sys.exit(5)
438454

439-
config, _ = prepare_config(args.template)
455+
config, _ = prepare_config(args)
440456

441457
config['template'] = args.template
442458
config['name'] = args.name
@@ -452,8 +468,9 @@ def spin(parser, args):
452468
"and issue a `vagrant up` to get started with your VM.")
453469

454470

455-
def prepare_profile(template, config):
471+
def prepare_profile(config, args):
456472
"""Converts the profile to a powershell script."""
473+
template = args.template
457474

458475
profile_name = config["profile"]
459476

0 commit comments

Comments
 (0)