From dbcc94ab659c861cc9af416073f235da22f21c8c Mon Sep 17 00:00:00 2001 From: Darko Poljak Date: Wed, 10 Aug 2016 23:56:56 +0200 Subject: [PATCH 1/4] Add derived env vars for target hostname and fqdn. --- cdist/config.py | 29 +++++++++++++++++-- cdist/core/code.py | 18 ++++++++++-- cdist/core/explorer.py | 6 ++-- cdist/core/manifest.py | 9 ++++-- cdist/emulator.py | 4 ++- cdist/exec/local.py | 9 ++++-- cdist/exec/remote.py | 15 ++++++---- cdist/shell.py | 11 +++++-- cdist/test/__init__.py | 6 +++- cdist/test/code/__init__.py | 16 ++++++++-- .../type/__dump_environment/gencode-local | 2 ++ cdist/test/config/__init__.py | 2 +- cdist/test/emulator/__init__.py | 12 ++++---- .../conf/type/__cdist_test_type/gencode-local | 2 ++ cdist/test/exec/local.py | 26 +++++++++++++---- cdist/test/exec/remote.py | 28 ++++++++++-------- cdist/test/explorer/__init__.py | 2 +- cdist/test/manifest/__init__.py | 18 +++++++++--- .../fixtures/conf/manifest/dump_environment | 2 ++ .../conf/type/__dump_environment/manifest | 2 ++ docs/changelog | 4 +-- docs/src/cdist-manifest.rst | 4 +-- docs/src/cdist-reference.rst.sh | 13 ++++++++- 23 files changed, 181 insertions(+), 59 deletions(-) diff --git a/cdist/config.py b/cdist/config.py index da560e91..dac5e923 100644 --- a/cdist/config.py +++ b/cdist/config.py @@ -28,6 +28,7 @@ import time import pprint import itertools import tempfile +import socket import cdist @@ -74,7 +75,7 @@ class Config(object): self.local = local self.remote = remote - self.log = logging.getLogger(self.local.target_host) + self.log = logging.getLogger(self.local.target_host[0]) self.dry_run = dry_run self.explorer = core.Explorer(self.local.target_host, self.local, @@ -229,15 +230,37 @@ class Config(object): log.debug("remote_copy for host \"{}\": {}".format( host, remote_copy)) + try: + # getaddrinfo returns a list of 5-tuples: + # (family, type, proto, canonname, sockaddr) + # where sockaddr is: + # (address, port) for AF_INET, + # (address, port, flow_info, scopeid) for AF_INET6 + ip_addr = socket.getaddrinfo( + host, None, type=socket.SOCK_STREAM)[0][4][0] + host_name = socket.gethostbyaddr(ip_addr)[0] + except socket.gaierror as e: + log.error("{}: {}".format(e[0], e[1])) + # in case of error provide empty value + host_name = None + + try: + host_fqdn = socket.getfqdn(host) + except socket.herror as e: + log.error("{}: {}".format(e[0], e[1])) + # in case of error provide empty value + host_fqdn = None + target_host = (host, host_name, host_fqdn) + local = cdist.exec.local.Local( - target_host=host, + target_host=target_host, base_root_path=host_base_path, host_dir_name=host_dir_name, initial_manifest=args.manifest, add_conf_dirs=args.conf_dir) remote = cdist.exec.remote.Remote( - target_host=host, + target_host=target_host, remote_exec=remote_exec, remote_copy=remote_copy) diff --git a/cdist/core/code.py b/cdist/core/code.py index 8e3cd63e..cfc1316a 100644 --- a/cdist/core/code.py +++ b/cdist/core/code.py @@ -36,6 +36,9 @@ common: env: PATH: prepend directory with type emulator symlinks == local.bin_path __target_host: the target host we are working on + __target_hostname: the target hostname provided from __target_host + __target_fqdn: the target's fully qualified domain name provided from + __target_host __cdist_manifest: full qualified path of the manifest == script __cdist_type_base_path: full qualified path to the directory where types are defined for use in type emulator @@ -46,6 +49,9 @@ gencode-local env: __target_host: the target host we are working on + __target_hostname: the target hostname provided from __target_host + __target_fqdn: the target's fully qualified domain name provided from + __target_host __global: full qualified path to the global output dir == local.out_path __object: full qualified path to the object's dir @@ -61,6 +67,9 @@ gencode-remote env: __target_host: the target host we are working on + __target_hostname: the target hostname provided from __target_host + __target_fqdn: the target's fully qualified domain name provided from + __target_host __global: full qualified path to the global output dir == local.out_path __object: full qualified path to the object's dir @@ -89,12 +98,17 @@ class Code(object): """Generates and executes cdist code scripts. """ + # target_host is tuple (target_host, target_hostname, target_fqdn) def __init__(self, target_host, local, remote): - self.target_host = target_host + self.target_host = target_host[0] + self.target_hostname = target_host[1] + self.target_fqdn = target_host[2] self.local = local self.remote = remote self.env = { - '__target_host': self.target_host, + '__target_host': self.target_host[0], + '__target_hostname': self.target_host[1], + '__target_fqdn': self.target_host[2], '__global': self.local.base_path, '__files': self.local.files_path, } diff --git a/cdist/core/explorer.py b/cdist/core/explorer.py index 345f45ff..faf1d23b 100644 --- a/cdist/core/explorer.py +++ b/cdist/core/explorer.py @@ -68,12 +68,14 @@ class Explorer(object): def __init__(self, target_host, local, remote): self.target_host = target_host - self.log = logging.getLogger(target_host) + self.log = logging.getLogger(target_host[0]) self.local = local self.remote = remote self.env = { - '__target_host': self.target_host, + '__target_host': self.target_host[0], + '__target_hostname': self.target_host[1], + '__target_fqdn': self.target_host[2], '__explorer': self.remote.global_explorer_path, } self._type_explorers_transferred = [] diff --git a/cdist/core/manifest.py b/cdist/core/manifest.py index ac322101..a16e9346 100644 --- a/cdist/core/manifest.py +++ b/cdist/core/manifest.py @@ -32,6 +32,9 @@ common: env: PATH: prepend directory with type emulator symlinks == local.bin_path __target_host: the target host we are working on + __target_hostname: the target hostname provided from __target_host + __target_fqdn: the target's fully qualified domain name provided from + __target_host __global: full qualified path to the global output dir == local.out_path __cdist_manifest: full qualified path of the manifest == script @@ -95,14 +98,16 @@ class Manifest(object): self.target_host = target_host self.local = local - self.log = logging.getLogger(self.target_host) + self.log = logging.getLogger(self.target_host[0]) self.env = { 'PATH': "%s:%s" % (self.local.bin_path, os.environ['PATH']), # for use in type emulator '__cdist_type_base_path': self.local.type_path, '__global': self.local.base_path, - '__target_host': self.target_host, + '__target_host': self.target_host[0], + '__target_hostname': self.target_host[1], + '__target_fqdn': self.target_host[2], '__files': self.local.files_path, } diff --git a/cdist/emulator.py b/cdist/emulator.py index 58ab7389..52dd6828 100644 --- a/cdist/emulator.py +++ b/cdist/emulator.py @@ -65,6 +65,8 @@ class Emulator(object): try: self.global_path = self.env['__global'] self.target_host = self.env['__target_host'] + self.target_hostname = self.env['__target_hostname'] + self.target_fqdn = self.env['__target_fqdn'] # Internal variables self.object_source = self.env['__cdist_manifest'] @@ -101,7 +103,7 @@ class Emulator(object): else: logging.root.setLevel(logging.INFO) - self.log = logging.getLogger(self.target_host) + self.log = logging.getLogger(self.target_host[0]) def commandline(self): """Parse command line""" diff --git a/cdist/exec/local.py b/cdist/exec/local.py index 4fdb5170..6876124b 100644 --- a/cdist/exec/local.py +++ b/cdist/exec/local.py @@ -83,7 +83,7 @@ class Local(object): return None def _init_log(self): - self.log = logging.getLogger(self.target_host) + self.log = logging.getLogger(self.target_host[0]) def _init_permissions(self): # Setup file permissions using umask @@ -184,8 +184,11 @@ class Local(object): if env is None: env = os.environ.copy() - # Export __target_host for use in __remote_{copy,exec} scripts - env['__target_host'] = self.target_host + # Export __target_host, __target_hostname, __target_fqdn + # for use in __remote_{copy,exec} scripts + env['__target_host'] = self.target_host[0] + env['__target_hostname'] = self.target_host[1] + env['__target_fqdn'] = self.target_host[2] # Export for emulator env['__cdist_object_marker'] = self.object_marker_name diff --git a/cdist/exec/remote.py b/cdist/exec/remote.py index b6ff8c1f..f157d65c 100644 --- a/cdist/exec/remote.py +++ b/cdist/exec/remote.py @@ -66,7 +66,7 @@ class Remote(object): self.type_path = os.path.join(self.conf_path, "type") self.global_explorer_path = os.path.join(self.conf_path, "explorer") - self.log = logging.getLogger(self.target_host) + self.log = logging.getLogger(self.target_host[0]) self._init_env() @@ -102,12 +102,12 @@ class Remote(object): command = self._copy.split() path = os.path.join(source, f) command.extend([path, '{0}:{1}'.format( - self.target_host, destination)]) + self.target_host[0], destination)]) self._run_command(command) else: command = self._copy.split() command.extend([source, '{0}:{1}'.format( - self.target_host, destination)]) + self.target_host[0], destination)]) self._run_command(command) def run_script(self, script, env=None, return_output=False): @@ -128,7 +128,7 @@ class Remote(object): """ # prefix given command with remote_exec cmd = self._exec.split() - cmd.append(self.target_host) + cmd.append(self.target_host[0]) # FIXME: replace this by -o SendEnv name -o SendEnv name ... to ssh? # can't pass environment to remote side, so prepend command with @@ -165,9 +165,12 @@ class Remote(object): assert isinstance(command, (list, tuple)), ( "list or tuple argument expected, got: %s" % command) - # export target_host for use in __remote_{exec,copy} scripts + # export target_host, target_hostname, target_fqdn + # for use in __remote_{exec,copy} scripts os_environ = os.environ.copy() - os_environ['__target_host'] = self.target_host + os_environ['__target_host'] = self.target_host[0] + os_environ['__target_hostname'] = self.target_host[1] + os_environ['__target_fqdn'] = self.target_host[2] self.log.debug("Remote run: %s", command) try: diff --git a/cdist/shell.py b/cdist/shell.py index dacff674..ffbc310a 100644 --- a/cdist/shell.py +++ b/cdist/shell.py @@ -38,7 +38,12 @@ class Shell(object): self.shell = shell - self.target_host = "cdist-shell-no-target-host" + self.target_host = ( + "cdist-shell-no-target-host", + "cdist-shell-no-target-host", + "cdist-shell-no-target-host", + ) + self.local = cdist.exec.local.Local( target_host=self.target_host) @@ -59,7 +64,9 @@ class Shell(object): '__cdist_type_base_path': self.local.type_path, '__cdist_manifest': "cdist shell", '__global': self.local.base_path, - '__target_host': self.target_host, + '__target_host': self.target_host[0], + '__target_hostname': self.target_host[1], + '__target_fqdn': self.target_host[2], '__manifest': self.local.manifest_path, '__explorer': self.local.global_explorer_path, '__files': self.local.files_path, diff --git a/cdist/test/__init__.py b/cdist/test/__init__.py index d9630f53..83b0c618 100644 --- a/cdist/test/__init__.py +++ b/cdist/test/__init__.py @@ -37,7 +37,11 @@ class CdistTestCase(unittest.TestCase): remote_exec = os.path.join(global_fixtures_dir, "remote", "exec") remote_copy = os.path.join(global_fixtures_dir, "remote", "copy") - target_host = 'cdisttesthost' + target_host = ( + 'cdisttesthost', + 'cdisttesthost', + 'cdisttesthost', + ) def mkdtemp(self, **kwargs): return tempfile.mkdtemp(prefix='tmp.cdist.test.', **kwargs) diff --git a/cdist/test/code/__init__.py b/cdist/test/code/__init__.py index 7f61c13f..83c93f8b 100644 --- a/cdist/test/code/__init__.py +++ b/cdist/test/code/__init__.py @@ -41,7 +41,7 @@ class CodeTestCase(test.CdistTestCase): def setUp(self): self.local_dir = self.mkdtemp() - self.hostdir = cdist.str_hash(self.target_host) + self.hostdir = cdist.str_hash(self.target_host[0]) self.host_base_path = os.path.join(self.local_dir, self.hostdir) self.local = local.Local( @@ -83,7 +83,12 @@ class CodeTestCase(test.CdistTestCase): junk, value = line.split(': ') key = junk.split(' ')[1] output_dict[key] = value - self.assertEqual(output_dict['__target_host'], self.local.target_host) + self.assertEqual(output_dict['__target_host'], + self.local.target_host[0]) + self.assertEqual(output_dict['__target_hostname'], + self.local.target_host[1]) + self.assertEqual(output_dict['__target_fqdn'], + self.local.target_host[2]) self.assertEqual(output_dict['__global'], self.local.base_path) self.assertEqual(output_dict['__type'], self.cdist_type.absolute_path) self.assertEqual(output_dict['__object'], @@ -101,7 +106,12 @@ class CodeTestCase(test.CdistTestCase): junk, value = line.split(': ') key = junk.split(' ')[1] output_dict[key] = value - self.assertEqual(output_dict['__target_host'], self.local.target_host) + self.assertEqual(output_dict['__target_host'], + self.local.target_host[0]) + self.assertEqual(output_dict['__target_hostname'], + self.local.target_host[1]) + self.assertEqual(output_dict['__target_fqdn'], + self.local.target_host[2]) self.assertEqual(output_dict['__global'], self.local.base_path) self.assertEqual(output_dict['__type'], self.cdist_type.absolute_path) self.assertEqual(output_dict['__object'], diff --git a/cdist/test/code/fixtures/conf/type/__dump_environment/gencode-local b/cdist/test/code/fixtures/conf/type/__dump_environment/gencode-local index 5883c268..7fa70342 100755 --- a/cdist/test/code/fixtures/conf/type/__dump_environment/gencode-local +++ b/cdist/test/code/fixtures/conf/type/__dump_environment/gencode-local @@ -1,6 +1,8 @@ #!/bin/sh echo "echo __target_host: $__target_host" +echo "echo __target_hostname: $__target_hostname" +echo "echo __target_fqdn: $__target_fqdn" echo "echo __global: $__global" echo "echo __type: $__type" echo "echo __object: $__object" diff --git a/cdist/test/config/__init__.py b/cdist/test/config/__init__.py index cfcc3c70..db753f41 100644 --- a/cdist/test/config/__init__.py +++ b/cdist/test/config/__init__.py @@ -55,7 +55,7 @@ class ConfigRunTestCase(test.CdistTestCase): self.temp_dir = self.mkdtemp() self.local_dir = os.path.join(self.temp_dir, "local") - self.hostdir = cdist.str_hash(self.target_host) + self.hostdir = cdist.str_hash(self.target_host[0]) self.host_base_path = os.path.join(self.local_dir, self.hostdir) os.makedirs(self.host_base_path) self.local = cdist.exec.local.Local( diff --git a/cdist/test/emulator/__init__.py b/cdist/test/emulator/__init__.py index 3fe9a4e5..4fd0ed40 100644 --- a/cdist/test/emulator/__init__.py +++ b/cdist/test/emulator/__init__.py @@ -48,7 +48,7 @@ class EmulatorTestCase(test.CdistTestCase): handle, self.script = self.mkstemp(dir=self.temp_dir) os.close(handle) base_path = self.temp_dir - hostdir = cdist.str_hash(self.target_host) + hostdir = cdist.str_hash(self.target_host[0]) host_base_path = os.path.join(base_path, hostdir) self.local = local.Local( @@ -151,7 +151,7 @@ class EmulatorConflictingRequirementsTestCase(test.CdistTestCase): handle, self.script = self.mkstemp(dir=self.temp_dir) os.close(handle) base_path = self.temp_dir - hostdir = cdist.str_hash(self.target_host) + hostdir = cdist.str_hash(self.target_host[0]) host_base_path = os.path.join(base_path, hostdir) self.local = local.Local( @@ -241,7 +241,7 @@ class AutoRequireEmulatorTestCase(test.CdistTestCase): def setUp(self): self.temp_dir = self.mkdtemp() base_path = os.path.join(self.temp_dir, "out") - hostdir = cdist.str_hash(self.target_host) + hostdir = cdist.str_hash(self.target_host[0]) host_base_path = os.path.join(base_path, hostdir) self.local = local.Local( @@ -274,7 +274,7 @@ class OverrideTestCase(test.CdistTestCase): handle, self.script = self.mkstemp(dir=self.temp_dir) os.close(handle) base_path = self.temp_dir - hostdir = cdist.str_hash(self.target_host) + hostdir = cdist.str_hash(self.target_host[0]) host_base_path = os.path.join(base_path, hostdir) self.local = local.Local( @@ -315,7 +315,7 @@ class ArgumentsTestCase(test.CdistTestCase): def setUp(self): self.temp_dir = self.mkdtemp() base_path = self.temp_dir - hostdir = cdist.str_hash(self.target_host) + hostdir = cdist.str_hash(self.target_host[0]) host_base_path = os.path.join(base_path, hostdir) handle, self.script = self.mkstemp(dir=self.temp_dir) os.close(handle) @@ -440,7 +440,7 @@ class StdinTestCase(test.CdistTestCase): self.temp_dir = self.mkdtemp() base_path = os.path.join(self.temp_dir, "out") - hostdir = cdist.str_hash(self.target_host) + hostdir = cdist.str_hash(self.target_host[0]) host_base_path = os.path.join(base_path, hostdir) self.local = local.Local( diff --git a/cdist/test/exec/fixtures/conf/type/__cdist_test_type/gencode-local b/cdist/test/exec/fixtures/conf/type/__cdist_test_type/gencode-local index 771894fb..3d568ad3 100755 --- a/cdist/test/exec/fixtures/conf/type/__cdist_test_type/gencode-local +++ b/cdist/test/exec/fixtures/conf/type/__cdist_test_type/gencode-local @@ -1,6 +1,8 @@ #!/bin/sh echo "echo __target_host: $__target_host" +echo "echo __target_hostname: $__target_hostname" +echo "echo __target_fqdn: $__target_fqdn" echo "echo __global: $__global" echo "echo __type: $__type" echo "echo __object: $__object" diff --git a/cdist/test/exec/local.py b/cdist/test/exec/local.py index f83fd6b7..0efdfa0a 100644 --- a/cdist/test/exec/local.py +++ b/cdist/test/exec/local.py @@ -44,10 +44,14 @@ class LocalTestCase(test.CdistTestCase): def setUp(self): - target_host = 'localhost' + target_host = ( + 'localhost', + 'localhost', + 'localhost', + ) self.temp_dir = self.mkdtemp() self.out_parent_path = self.temp_dir - self.hostdir = cdist.str_hash(target_host) + self.hostdir = cdist.str_hash(target_host[0]) self.host_base_path = op.join(self.out_parent_path, self.hostdir) self.out_path = op.join(self.host_base_path, "data") @@ -111,7 +115,11 @@ class LocalTestCase(test.CdistTestCase): test_type = "__file" link_test_local = local.Local( - target_host='localhost', + target_host=( + 'localhost', + 'localhost', + 'localhost', + ), base_root_path=self.host_base_path, host_dir_name=self.hostdir, exec_path=test.cdist_exec_path, @@ -130,7 +138,11 @@ class LocalTestCase(test.CdistTestCase): test_type = "__cdist_test_type" link_test_local = local.Local( - target_host='localhost', + target_host=( + 'localhost', + 'localhost', + 'localhost', + ), base_root_path=self.host_base_path, host_dir_name=self.hostdir, exec_path=test.cdist_exec_path, @@ -152,7 +164,11 @@ class LocalTestCase(test.CdistTestCase): os.environ['CDIST_PATH'] = conf_dir link_test_local = local.Local( - target_host='localhost', + target_host=( + 'localhost', + 'localhost', + 'localhost', + ), base_root_path=self.host_base_path, host_dir_name=self.hostdir, exec_path=test.cdist_exec_path, diff --git a/cdist/test/exec/remote.py b/cdist/test/exec/remote.py index 318e4c7b..1cb4490a 100644 --- a/cdist/test/exec/remote.py +++ b/cdist/test/exec/remote.py @@ -34,7 +34,11 @@ class RemoteTestCase(test.CdistTestCase): def setUp(self): self.temp_dir = self.mkdtemp() - self.target_host = 'localhost' + self.target_host = ( + 'localhost', + 'localhost', + 'localhost', + ) self.base_path = self.temp_dir user = getpass.getuser() remote_exec = "ssh -o User=%s -q" % user @@ -67,22 +71,22 @@ class RemoteTestCase(test.CdistTestCase): # /test api def test_run_success(self): - self.remote.run(['/bin/true']) + self.remote.run(['true']) def test_run_fail(self): - self.assertRaises(cdist.Error, self.remote.run, ['/bin/false']) + self.assertRaises(cdist.Error, self.remote.run, ['false']) def test_run_script_success(self): handle, script = self.mkstemp(dir=self.temp_dir) with os.fdopen(handle, "w") as fd: - fd.writelines(["#!/bin/sh\n", "/bin/true"]) + fd.writelines(["#!/bin/sh\n", "true"]) self.remote.run_script(script) def test_run_script_fail(self): handle, script = self.mkstemp(dir=self.temp_dir) with os.fdopen(handle, "w") as fd: - fd.writelines(["#!/bin/sh\n", "/bin/false"]) - self.assertRaises(remote.RemoteScriptError, self.remote.run_script, + fd.writelines(["#!/bin/sh\n", "false"]) + self.assertRaises(cdist.Error, self.remote.run_script, script) def test_run_script_get_output(self): @@ -121,8 +125,8 @@ class RemoteTestCase(test.CdistTestCase): # test if the payload file is in the target directory self.assertTrue(os.path.isfile(os.path.join(target, source_file_name))) - def test_create_directories(self): - self.remote.create_directories() + def test_create_files_dirs(self): + self.remote.create_files_dirs() self.assertTrue(os.path.isdir(self.remote.base_path)) self.assertTrue(os.path.isdir(self.remote.conf_path)) @@ -135,8 +139,8 @@ class RemoteTestCase(test.CdistTestCase): remote_copy = "echo" r = remote.Remote(self.target_host, base_path=self.base_path, remote_exec=remote_exec, remote_copy=remote_copy) - self.assertEqual(r.run('/bin/true', return_output=True), - "%s\n" % self.target_host) + self.assertEqual(r.run('true', return_output=True), + "%s\n" % self.target_host[0]) def test_run_script_target_host_in_env(self): handle, remote_exec_path = self.mkstemp(dir=self.temp_dir) @@ -149,9 +153,9 @@ class RemoteTestCase(test.CdistTestCase): remote_exec=remote_exec, remote_copy=remote_copy) handle, script = self.mkstemp(dir=self.temp_dir) with os.fdopen(handle, "w") as fd: - fd.writelines(["#!/bin/sh\n", "/bin/true"]) + fd.writelines(["#!/bin/sh\n", "true"]) self.assertEqual(r.run_script(script, return_output=True), - "%s\n" % self.target_host) + "%s\n" % self.target_host[0]) def test_run_script_with_env_target_host_in_env(self): handle, script = self.mkstemp(dir=self.temp_dir) diff --git a/cdist/test/explorer/__init__.py b/cdist/test/explorer/__init__.py index 9a4555b8..9efa1680 100644 --- a/cdist/test/explorer/__init__.py +++ b/cdist/test/explorer/__init__.py @@ -42,7 +42,7 @@ class ExplorerClassTestCase(test.CdistTestCase): def setUp(self): self.temp_dir = self.mkdtemp() self.local_path = os.path.join(self.temp_dir, "local") - hostdir = cdist.str_hash(self.target_host) + hostdir = cdist.str_hash(self.target_host[0]) base_root_path = os.path.join(self.local_path, hostdir) self.remote_base_path = os.path.join(self.temp_dir, "remote") os.makedirs(self.remote_base_path) diff --git a/cdist/test/manifest/__init__.py b/cdist/test/manifest/__init__.py index cfaefe5c..3e07c1a7 100644 --- a/cdist/test/manifest/__init__.py +++ b/cdist/test/manifest/__init__.py @@ -49,7 +49,7 @@ class ManifestTestCase(test.CdistTestCase): self.temp_dir = self.mkdtemp() out_path = self.temp_dir - hostdir = cdist.str_hash(self.target_host) + hostdir = cdist.str_hash(self.target_host[0]) base_root_path = os.path.join(out_path, hostdir) self.local = local.Local( target_host=self.target_host, @@ -60,7 +60,7 @@ class ManifestTestCase(test.CdistTestCase): self.local.create_files_dirs() self.manifest = manifest.Manifest(self.target_host, self.local) - self.log = logging.getLogger(self.target_host) + self.log = logging.getLogger(self.target_host[0]) def tearDown(self): os.environ = self.orig_environ @@ -82,7 +82,12 @@ class ManifestTestCase(test.CdistTestCase): key, value = line.split(': ') output_dict[key] = value self.assertTrue(output_dict['PATH'].startswith(self.local.bin_path)) - self.assertEqual(output_dict['__target_host'], self.local.target_host) + self.assertEqual(output_dict['__target_host'], + self.local.target_host[0]) + self.assertEqual(output_dict['__target_hostname'], + self.local.target_host[1]) + self.assertEqual(output_dict['__target_fqdn'], + self.local.target_host[2]) self.assertEqual(output_dict['__global'], self.local.base_path) self.assertEqual(output_dict['__cdist_type_base_path'], self.local.type_path) @@ -107,7 +112,12 @@ class ManifestTestCase(test.CdistTestCase): key, value = line.split(': ') output_dict[key] = value self.assertTrue(output_dict['PATH'].startswith(self.local.bin_path)) - self.assertEqual(output_dict['__target_host'], self.local.target_host) + self.assertEqual(output_dict['__target_host'], + self.local.target_host[0]) + self.assertEqual(output_dict['__target_hostname'], + self.local.target_host[1]) + self.assertEqual(output_dict['__target_fqdn'], + self.local.target_host[2]) self.assertEqual(output_dict['__global'], self.local.base_path) self.assertEqual(output_dict['__cdist_type_base_path'], self.local.type_path) diff --git a/cdist/test/manifest/fixtures/conf/manifest/dump_environment b/cdist/test/manifest/fixtures/conf/manifest/dump_environment index df901910..702145e2 100755 --- a/cdist/test/manifest/fixtures/conf/manifest/dump_environment +++ b/cdist/test/manifest/fixtures/conf/manifest/dump_environment @@ -3,6 +3,8 @@ cat > $__cdist_test_out << DONE PATH: $PATH __target_host: $__target_host +__target_hostname: $__target_hostname +__target_fqdn: $__target_fqdn __global: $__global __cdist_type_base_path: $__cdist_type_base_path __manifest: $__manifest diff --git a/cdist/test/manifest/fixtures/conf/type/__dump_environment/manifest b/cdist/test/manifest/fixtures/conf/type/__dump_environment/manifest index 13efe038..757d07b5 100755 --- a/cdist/test/manifest/fixtures/conf/type/__dump_environment/manifest +++ b/cdist/test/manifest/fixtures/conf/type/__dump_environment/manifest @@ -3,6 +3,8 @@ cat > $__cdist_test_out << DONE PATH: $PATH __target_host: $__target_host +__target_hostname: $__target_hostname +__target_fqdn: $__target_fqdn __global: $__global __cdist_type_base_path: $__cdist_type_base_path __type: $__type diff --git a/docs/changelog b/docs/changelog index 5dd49ef5..0906f2c0 100644 --- a/docs/changelog +++ b/docs/changelog @@ -1,8 +1,8 @@ Changelog --------- next: - * New type __filesystem: manage filesystems on devices ( Daniel Heule ) - + * Core: Add derived env vars for target hostname and fqdn (Darko Poljak) + * New type __filesystem: manage filesystems on devices (Daniel Heule) * New type: __locale_system (Steven Armstrong, Carlos Ortigoza, Nico Schottelius) * New type: __sysctl (Steven Armstrong) diff --git a/docs/src/cdist-manifest.rst b/docs/src/cdist-manifest.rst index 5c35b6a5..5655c6c2 100644 --- a/docs/src/cdist-manifest.rst +++ b/docs/src/cdist-manifest.rst @@ -53,8 +53,8 @@ Cdist expects the initial manifest at **cdist/conf/manifest/init**. Within this initial manifest you define, which objects should be created on which host. To distinguish between hosts, you can use the -environment variable **__target_host**. Let's have a look at a simple -example:: +environment variable **__target_host** and/or **__target_hostname** and/or +**__target_fqdn**. Let's have a look at a simple example:: __cdistmarker diff --git a/docs/src/cdist-reference.rst.sh b/docs/src/cdist-reference.rst.sh index 06842450..97b22473 100755 --- a/docs/src/cdist-reference.rst.sh +++ b/docs/src/cdist-reference.rst.sh @@ -226,7 +226,18 @@ __object_name The full qualified name of the current object. Available for: type manifest, type explorer, type gencode. __target_host - The host we are deploying to. + The host we are deploying to. This is primary variable. It's content is + literally the one user passed in. + Available for: explorer, initial manifest, type explorer, type manifest, type gencode, shell. +__target_hostname + The hostname of host we are deploying to. This variable is derived from + **__target_host** (using **socket.getaddrinfo(__target_host)** and then + **socket.gethostbyaddr()**). + Available for: explorer, initial manifest, type explorer, type manifest, type gencode, shell. +__target_fqdn + The fully qualified domain name of the host we are deploying to. + This variable is derived from **__target_host** + (using **socket.getfqdn()**). Available for: explorer, initial manifest, type explorer, type manifest, type gencode, shell. __type Path to the current type. From 45298b810c6293cbcd2c4d2fa379ece45040a75e Mon Sep 17 00:00:00 2001 From: Darko Poljak Date: Thu, 11 Aug 2016 00:04:37 +0200 Subject: [PATCH 2/4] Add more comments. --- cdist/config.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cdist/config.py b/cdist/config.py index dac5e923..17fb33ca 100644 --- a/cdist/config.py +++ b/cdist/config.py @@ -238,6 +238,8 @@ class Config(object): # (address, port, flow_info, scopeid) for AF_INET6 ip_addr = socket.getaddrinfo( host, None, type=socket.SOCK_STREAM)[0][4][0] + # gethostbyaddr returns triple + # (hostname, aliaslist, ipaddrlist) host_name = socket.gethostbyaddr(ip_addr)[0] except socket.gaierror as e: log.error("{}: {}".format(e[0], e[1])) From d96be90f26647c394707c2e17aea3057977c513a Mon Sep 17 00:00:00 2001 From: Darko Poljak Date: Sun, 14 Aug 2016 22:09:36 +0200 Subject: [PATCH 3/4] Fix self.target_host --- cdist/emulator.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/cdist/emulator.py b/cdist/emulator.py index 52dd6828..2c5e567b 100644 --- a/cdist/emulator.py +++ b/cdist/emulator.py @@ -64,9 +64,11 @@ class Emulator(object): try: self.global_path = self.env['__global'] - self.target_host = self.env['__target_host'] - self.target_hostname = self.env['__target_hostname'] - self.target_fqdn = self.env['__target_fqdn'] + self.target_host = ( + self.env['__target_host'], + self.env['__target_hostname'], + self.env['__target_fqdn'] + ) # Internal variables self.object_source = self.env['__cdist_manifest'] From 15d6524efde25f89a217539ff8b2c3469b346cf0 Mon Sep 17 00:00:00 2001 From: Darko Poljak Date: Sun, 14 Aug 2016 22:09:51 +0200 Subject: [PATCH 4/4] Fix cdist shell. --- cdist/shell.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/cdist/shell.py b/cdist/shell.py index ffbc310a..9378efc3 100644 --- a/cdist/shell.py +++ b/cdist/shell.py @@ -22,6 +22,7 @@ import logging import os import subprocess +import tempfile # initialise cdist import cdist.exec.local @@ -44,8 +45,14 @@ class Shell(object): "cdist-shell-no-target-host", ) + host_dir_name = cdist.str_hash(self.target_host[0]) + base_root_path = tempfile.mkdtemp() + host_base_path = os.path.join(base_root_path, host_dir_name) + self.local = cdist.exec.local.Local( - target_host=self.target_host) + target_host=self.target_host, + base_root_path=host_base_path, + host_dir_name=host_dir_name) def _init_shell(self): """Select shell to execute, if not specified by user"""