2828import signal
2929import subprocess
3030import sys
31+ import tempfile
3132import textwrap
3233
3334from appdirs import AppDirs
3839
3940DIRS = AppDirs ("malboxes" )
4041DEBUG = False
42+ tmp_cache_dir = "/tmp/malboxes"
4143
4244def 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
277287tempfiles = []
278288def 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
283292def 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
290298def 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
451459def spin (parser , args ):
0 commit comments