Skip to content

Commit 5d9dd38

Browse files
committed
Move everything in its own dir in cache dir
Now creates a directory in the cache directory. Every file is moved under that temp directory. It should allow packer to build multiple machines concurently
1 parent 2ad003e commit 5d9dd38

File tree

7 files changed

+24
-16
lines changed

7 files changed

+24
-16
lines changed

malboxes/malboxes.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import signal
2929
import subprocess
3030
import sys
31+
import tempfile
3132
import textwrap
3233

3334
from appdirs import AppDirs
@@ -38,8 +39,10 @@
3839

3940
DIRS = AppDirs("malboxes")
4041
DEBUG = False
42+
tmp_cache_dir = "/tmp/malboxes"
4143

4244
def initialize():
45+
global tmp_cache_dir
4346
# create appdata directories if they don't exist
4447
if not os.path.exists(DIRS.user_config_dir):
4548
os.makedirs(DIRS.user_config_dir)
@@ -52,7 +55,13 @@ def initialize():
5255
if not os.path.exists(DIRS.user_cache_dir):
5356
os.makedirs(DIRS.user_cache_dir)
5457

55-
cache_scripts_dir = os.path.join(DIRS.user_cache_dir, "scripts", "user")
58+
temp_cache = tempfile.mkdtemp(dir=DIRS.user_cache_dir)
59+
tmp_cache_dir = temp_cache
60+
61+
if not os.path.exists(tmp_cache_dir):
62+
os.makedirs(tmp_cache_dir)
63+
64+
cache_scripts_dir = os.path.join(tmp_cache_dir, "scripts", "user")
5665

5766
if not (os.path.exists(cache_scripts_dir)):
5867
os.makedirs(cache_scripts_dir)
@@ -235,6 +244,7 @@ def load_config(config_filename, args):
235244
# add packer required variables
236245
# Note: Backslashes are replaced with forward slashes (Packer on Windows)
237246
config['cache_dir'] = DIRS.user_cache_dir.replace('\\', '/')
247+
config['packer_dir'] = tmp_cache_dir.replace('\\', '/')
238248
config['dir'] = resource_filename(__name__, "").replace('\\', '/')
239249
config['config_dir'] = DIRS.user_config_dir.replace('\\', '/')
240250

@@ -276,15 +286,13 @@ def _get_os_type(config):
276286

277287
tempfiles = []
278288
def create_cachefd(filename):
279-
tempfiles.append(filename)
280-
return open(os.path.join(DIRS.user_cache_dir, filename), 'w')
289+
return open(os.path.join(tmp_cache_dir, filename), 'w')
281290

282291

283292
def cleanup():
284293
"""Removes temporary files. Keep them in debug mode."""
285294
if not DEBUG:
286-
for f in tempfiles:
287-
os.remove(os.path.join(DIRS.user_cache_dir, f))
295+
shutil.rmtree(tmp_cache_dir)
288296

289297

290298
def run_foreground(command, env=None):
@@ -318,7 +326,7 @@ def run_packer(packer_tmpl, args):
318326
print("----------------------------------")
319327

320328
prev_cwd = os.getcwd()
321-
os.chdir(DIRS.user_cache_dir)
329+
os.chdir(tmp_cache_dir)
322330

323331
try:
324332
# packer or packer-io?
@@ -331,7 +339,7 @@ def run_packer(packer_tmpl, args):
331339
return 254
332340

333341
# run packer with relevant config minified
334-
# (removes "profiles" as packer do not support arrays in var-file)
342+
# (removes "profile_config" as packer do not support arrays in var-file)
335343
configfile = os.path.join(DIRS.user_config_dir, 'config.js')
336344
with open(configfile, 'r') as config:
337345
config = json.loads(jsmin(config.read()))
@@ -344,7 +352,7 @@ def run_packer(packer_tmpl, args):
344352
flags = ['-var-file={}'.format(f.name)]
345353

346354
special_env = {'PACKER_CACHE_DIR': DIRS.user_cache_dir}
347-
special_env['TMPDIR'] = DIRS.user_cache_dir
355+
special_env['TMPDIR'] = tmp_cache_dir
348356
if DEBUG:
349357
special_env['PACKER_LOG'] = '1'
350358
flags.append('-on-error=abort')
@@ -370,7 +378,7 @@ def add_box(config, args):
370378
print("--------------------------")
371379

372380
box = config['post-processors'][0]['output']
373-
box = os.path.join(DIRS.user_cache_dir, box)
381+
box = os.path.join(tmp_cache_dir, box)
374382
box = box.replace('{{user `name`}}', args.template)
375383

376384
flags = ['--name={}'.format(args.template)]
@@ -445,7 +453,7 @@ def build(parser, args):
445453
You can re-use this base box several times by using `malboxes
446454
spin`. Each VM will be independent of each other.
447455
===============================================================""")
448-
.format(args.template, DIRS.user_cache_dir))
456+
.format(args.template, tmp_cache_dir))
449457

450458

451459
def spin(parser, args):
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"post-processors": [{
22
"type": "vagrant",
33
"output": "boxes/{{ template_name }}.box",
4-
"vagrantfile_template": "{{ cache_dir }}/box_win.rb"
4+
"vagrantfile_template": "{{ packer_dir }}/box_win.rb"
55
}]

malboxes/templates/snippets/provision_powershell.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
{
3434
"type": "powershell",
3535
"scripts": [
36-
"{{ cache_dir }}/profile-{{ profile }}.ps1"
36+
"{{ packer_dir }}/profile-{{ profile }}.ps1"
3737
]
3838
}
3939
{% endif %}

malboxes/templates/win10_32_analyst.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"iso_checksum_type": "sha1",
1818

1919
"floppy_files": [
20-
"{{ cache_dir }}/Autounattend.xml",
20+
"{{ packer_dir }}/Autounattend.xml",
2121
"{{ dir }}/installconfig/windows10/enablewinrm.ps1"
2222
]
2323
}],

malboxes/templates/win10_64_analyst.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"iso_checksum_type": "sha1",
1818

1919
"floppy_files": [
20-
"{{ cache_dir }}/Autounattend.xml",
20+
"{{ packer_dir }}/Autounattend.xml",
2121
"{{ dir }}/installconfig/windows10_64/enablewinrm.ps1"
2222
]
2323
}],

malboxes/templates/win7_32_analyst.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
{% endif %}
1818

1919
"floppy_files": [
20-
"{{ cache_dir }}/Autounattend.xml",
20+
"{{ packer_dir }}/Autounattend.xml",
2121
{% if proxy %}"{{ dir }}/scripts/windows/refresh-proxy.ps1",{% endif %}
2222
"{{ dir }}/installconfig/windows7/enablewinrm.ps1"
2323
]

malboxes/templates/win7_64_analyst.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
{% endif %}
1818

1919
"floppy_files": [
20-
"{{ cache_dir }}/Autounattend.xml",
20+
"{{ packer_dir }}/Autounattend.xml",
2121
{% if proxy %}"{{ dir }}/scripts/windows/refresh-proxy.ps1",{% endif %}
2222
"{{ dir }}/installconfig/windows7_64/enablewinrm.ps1"
2323
]

0 commit comments

Comments
 (0)