Merge pull request #427 from darko-poljak/fix-parallel-outpath

Fix bug with parallel hosts operation and specified output path.
This commit is contained in:
Nico Schottelius 2016-05-02 11:47:14 +02:00
commit 48087e6b02
5 changed files with 52 additions and 21 deletions

View file

@ -2,6 +2,7 @@
# #
# 2011 Steven Armstrong (steven-cdist at armstrong.cc) # 2011 Steven Armstrong (steven-cdist at armstrong.cc)
# 2011-2015 Nico Schottelius (nico-cdist at schottelius.org) # 2011-2015 Nico Schottelius (nico-cdist at schottelius.org)
# 2016 Darko Poljak (darko.poljak at gmail.com)
# #
# This file is part of cdist. # This file is part of cdist.
# #
@ -51,9 +52,18 @@ class Local(object):
# FIXME: stopped: create base that does not require moving later # FIXME: stopped: create base that does not require moving later
if base_path: if base_path:
self.base_path = base_path base_path_parent = base_path
else: else:
self.base_path = tempfile.mkdtemp() base_path_parent = tempfile.mkdtemp()
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)
self._init_log()
self._init_permissions()
self.mkdir(self.base_path)
# FIXME: as well # FIXME: as well
self._init_cache_dir(None) self._init_cache_dir(None)
@ -63,8 +73,6 @@ class Local(object):
self._add_conf_dirs = add_conf_dirs self._add_conf_dirs = add_conf_dirs
self._init_log()
self._init_permissions()
self._init_paths() self._init_paths()
self._init_object_marker() self._init_object_marker()
self._init_conf_dirs() self._init_conf_dirs()
@ -80,6 +88,13 @@ class Local(object):
else: else:
return None return None
def _hostdir(self):
if os.path.isabs(self.target_host):
hostdir = self.target_host[1:]
else:
hostdir = self.target_host
return hostdir
def _init_log(self): def _init_log(self):
self.log = logging.getLogger(self.target_host) self.log = logging.getLogger(self.target_host)
@ -210,13 +225,9 @@ class Local(object):
return self.run(command=command, env=env, return_output=return_output, message_prefix=message_prefix) return self.run(command=command, env=env, return_output=return_output, message_prefix=message_prefix)
def save_cache(self):
if os.path.isabs(self.target_host):
hostdir = self.target_host[1:]
else:
hostdir = self.target_host
destination = os.path.join(self.cache_path, hostdir) def save_cache(self):
destination = os.path.join(self.cache_path, self.hostdir)
self.log.debug("Saving " + self.base_path + " to " + destination) self.log.debug("Saving " + self.base_path + " to " + destination)
try: try:

View file

@ -0,0 +1,8 @@
#!/bin/sh
echo "echo __target_host: $__target_host"
echo "echo __global: $__global"
echo "echo __type: $__type"
echo "echo __object: $__object"
echo "echo __object_id: $__object_id"
echo "echo __object_name: $__object_name"

View file

@ -0,0 +1 @@
gencode-local

View file

@ -2,6 +2,7 @@
# #
# 2010-2011 Steven Armstrong (steven-cdist at armstrong.cc) # 2010-2011 Steven Armstrong (steven-cdist at armstrong.cc)
# 2012 Nico Schottelius (nico-cdist at schottelius.org) # 2012 Nico Schottelius (nico-cdist at schottelius.org)
# 2016 Darko Poljak (darko.poljak at gmail.com)
# #
# This file is part of cdist. # This file is part of cdist.
# #
@ -35,17 +36,21 @@ my_dir = op.abspath(op.dirname(__file__))
fixtures = op.join(my_dir, 'fixtures') fixtures = op.join(my_dir, 'fixtures')
conf_dir = op.join(fixtures, "conf") conf_dir = op.join(fixtures, "conf")
bin_true = "true"
bin_false = "false"
class LocalTestCase(test.CdistTestCase): class LocalTestCase(test.CdistTestCase):
def setUp(self): def setUp(self):
target_host = 'localhost' target_host = 'localhost'
self.temp_dir = self.mkdtemp() self.temp_dir = self.mkdtemp()
self.out_path = self.temp_dir self.out_parent_path = self.temp_dir
self.out_path = op.join(self.out_parent_path, target_host)
self.local = local.Local( self.local = local.Local(
target_host=target_host, target_host=target_host,
out_path=self.out_path, base_path=self.out_parent_path,
exec_path=test.cdist_exec_path exec_path=test.cdist_exec_path
) )
@ -63,7 +68,7 @@ class LocalTestCase(test.CdistTestCase):
self.assertEqual(self.local.conf_path, os.path.join(self.out_path, "conf")) self.assertEqual(self.local.conf_path, os.path.join(self.out_path, "conf"))
def test_out_path(self): def test_out_path(self):
self.assertEqual(self.local.out_path, self.out_path) self.assertEqual(self.local.base_path, self.out_path)
def test_bin_path(self): def test_bin_path(self):
self.assertEqual(self.local.bin_path, os.path.join(self.out_path, "bin")) self.assertEqual(self.local.bin_path, os.path.join(self.out_path, "bin"))
@ -94,7 +99,7 @@ class LocalTestCase(test.CdistTestCase):
link_test_local = local.Local( link_test_local = local.Local(
target_host='localhost', target_host='localhost',
out_path=self.out_path, base_path=self.out_parent_path,
exec_path=test.cdist_exec_path, exec_path=test.cdist_exec_path,
) )
@ -111,7 +116,7 @@ class LocalTestCase(test.CdistTestCase):
link_test_local = local.Local( link_test_local = local.Local(
target_host='localhost', target_host='localhost',
out_path=self.out_path, base_path=self.out_parent_path,
exec_path=test.cdist_exec_path, exec_path=test.cdist_exec_path,
add_conf_dirs=[conf_dir] add_conf_dirs=[conf_dir]
) )
@ -131,7 +136,7 @@ class LocalTestCase(test.CdistTestCase):
link_test_local = local.Local( link_test_local = local.Local(
target_host='localhost', target_host='localhost',
out_path=self.out_path, base_path=self.out_parent_path,
exec_path=test.cdist_exec_path, exec_path=test.cdist_exec_path,
) )
@ -144,21 +149,21 @@ class LocalTestCase(test.CdistTestCase):
### other tests ### other tests
def test_run_success(self): def test_run_success(self):
self.local.run(['/bin/true']) self.local.run([bin_true])
def test_run_fail(self): def test_run_fail(self):
self.assertRaises(cdist.Error, self.local.run, ['/bin/false']) self.assertRaises(cdist.Error, self.local.run, [bin_false])
def test_run_script_success(self): def test_run_script_success(self):
handle, script = self.mkstemp(dir=self.temp_dir) handle, script = self.mkstemp(dir=self.temp_dir)
with os.fdopen(handle, "w") as fd: with os.fdopen(handle, "w") as fd:
fd.writelines(["#!/bin/sh\n", "/bin/true"]) fd.writelines(["#!/bin/sh\n", bin_true])
self.local.run_script(script) self.local.run_script(script)
def test_run_script_fail(self): def test_run_script_fail(self):
handle, script = self.mkstemp(dir=self.temp_dir) handle, script = self.mkstemp(dir=self.temp_dir)
with os.fdopen(handle, "w") as fd: with os.fdopen(handle, "w") as fd:
fd.writelines(["#!/bin/sh\n", "/bin/false"]) fd.writelines(["#!/bin/sh\n", bin_false])
self.assertRaises(cdist.Error, self.local.run_script, script) self.assertRaises(cdist.Error, self.local.run_script, script)
def test_run_script_get_output(self): def test_run_script_get_output(self):
@ -180,6 +185,11 @@ class LocalTestCase(test.CdistTestCase):
def test_create_files_dirs(self): def test_create_files_dirs(self):
self.local.create_files_dirs() self.local.create_files_dirs()
self.assertTrue(os.path.isdir(self.local.out_path)) self.assertTrue(os.path.isdir(self.local.base_path))
self.assertTrue(os.path.isdir(self.local.bin_path)) self.assertTrue(os.path.isdir(self.local.bin_path))
self.assertTrue(os.path.isdir(self.local.conf_path)) self.assertTrue(os.path.isdir(self.local.conf_path))
if __name__ == "__main__":
import unittest
unittest.main()

View file

@ -2,6 +2,7 @@ Changelog
--------- ---------
next: next:
* Core: Fix bug with parallel hosts operation when output path is specifed (Darko Poljak)
* Type __package_pip: Add support for running as specified user (useful for pip in venv) (Darko Poljak) * Type __package_pip: Add support for running as specified user (useful for pip in venv) (Darko Poljak)
* New type: __pyvenv: Manage python virtualenv (Darko Poljak) * New type: __pyvenv: Manage python virtualenv (Darko Poljak)
* Core: Add CDIST_REMOTE_COPY/EXEC env variables and multiplexing options for default scp/ssh (Darko Poljak) * Core: Add CDIST_REMOTE_COPY/EXEC env variables and multiplexing options for default scp/ssh (Darko Poljak)