do not assume target_host is anything that can be used as a directory name

Signed-off-by: Steven Armstrong <steven@icarus.ethz.ch>
This commit is contained in:
Steven Armstrong 2016-07-07 14:38:36 +02:00
parent ce26deb706
commit 13d315d718

View file

@ -29,6 +29,7 @@ import subprocess
import shutil
import logging
import tempfile
import hashlib
import cdist
import cdist.message
@ -55,8 +56,10 @@ class Local(object):
base_path_parent = base_path
else:
base_path_parent = tempfile.mkdtemp()
import atexit
atexit.register(lambda: shutil.rmtree(base_path_parent))
# TODO: the below atexit hook nukes any debug info we would have
# if cdist exits with error.
#import atexit
#atexit.register(lambda: shutil.rmtree(base_path_parent))
self.hostdir = self._hostdir()
self.base_path = os.path.join(base_path_parent, self.hostdir)
@ -89,11 +92,9 @@ class Local(object):
return None
def _hostdir(self):
if os.path.isabs(self.target_host):
hostdir = self.target_host[1:]
else:
hostdir = self.target_host
return hostdir
# Do not assume target_host is anything that can be used as a directory name.
# Instead use a hash, which is know to work as directory name.
return hashlib.md5(self.target_host.encode('utf-8')).hexdigest()
def _init_log(self):
self.log = logging.getLogger(self.target_host)
@ -227,7 +228,7 @@ class Local(object):
def save_cache(self):
destination = os.path.join(self.cache_path, self.hostdir)
destination = os.path.join(self.cache_path, self.target_host)
self.log.debug("Saving " + self.base_path + " to " + destination)
try: