Add derived env vars for target hostname and fqdn.

This commit is contained in:
Darko Poljak 2016-08-10 23:56:56 +02:00
parent 9ce8f6bbb0
commit dbcc94ab65
23 changed files with 181 additions and 59 deletions

View file

@ -28,6 +28,7 @@ import time
import pprint import pprint
import itertools import itertools
import tempfile import tempfile
import socket
import cdist import cdist
@ -74,7 +75,7 @@ class Config(object):
self.local = local self.local = local
self.remote = remote 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.dry_run = dry_run
self.explorer = core.Explorer(self.local.target_host, self.local, self.explorer = core.Explorer(self.local.target_host, self.local,
@ -229,15 +230,37 @@ class Config(object):
log.debug("remote_copy for host \"{}\": {}".format( log.debug("remote_copy for host \"{}\": {}".format(
host, remote_copy)) 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( local = cdist.exec.local.Local(
target_host=host, target_host=target_host,
base_root_path=host_base_path, base_root_path=host_base_path,
host_dir_name=host_dir_name, host_dir_name=host_dir_name,
initial_manifest=args.manifest, initial_manifest=args.manifest,
add_conf_dirs=args.conf_dir) add_conf_dirs=args.conf_dir)
remote = cdist.exec.remote.Remote( remote = cdist.exec.remote.Remote(
target_host=host, target_host=target_host,
remote_exec=remote_exec, remote_exec=remote_exec,
remote_copy=remote_copy) remote_copy=remote_copy)

View file

@ -36,6 +36,9 @@ common:
env: env:
PATH: prepend directory with type emulator symlinks == local.bin_path PATH: prepend directory with type emulator symlinks == local.bin_path
__target_host: the target host we are working on __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_manifest: full qualified path of the manifest == script
__cdist_type_base_path: full qualified path to the directory where __cdist_type_base_path: full qualified path to the directory where
types are defined for use in type emulator types are defined for use in type emulator
@ -46,6 +49,9 @@ gencode-local
env: env:
__target_host: the target host we are working on __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 __global: full qualified path to the global
output dir == local.out_path output dir == local.out_path
__object: full qualified path to the object's dir __object: full qualified path to the object's dir
@ -61,6 +67,9 @@ gencode-remote
env: env:
__target_host: the target host we are working on __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 __global: full qualified path to the global
output dir == local.out_path output dir == local.out_path
__object: full qualified path to the object's dir __object: full qualified path to the object's dir
@ -89,12 +98,17 @@ class Code(object):
"""Generates and executes cdist code scripts. """Generates and executes cdist code scripts.
""" """
# target_host is tuple (target_host, target_hostname, target_fqdn)
def __init__(self, target_host, local, remote): 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.local = local
self.remote = remote self.remote = remote
self.env = { 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, '__global': self.local.base_path,
'__files': self.local.files_path, '__files': self.local.files_path,
} }

View file

@ -68,12 +68,14 @@ class Explorer(object):
def __init__(self, target_host, local, remote): def __init__(self, target_host, local, remote):
self.target_host = target_host self.target_host = target_host
self.log = logging.getLogger(target_host) self.log = logging.getLogger(target_host[0])
self.local = local self.local = local
self.remote = remote self.remote = remote
self.env = { 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, '__explorer': self.remote.global_explorer_path,
} }
self._type_explorers_transferred = [] self._type_explorers_transferred = []

View file

@ -32,6 +32,9 @@ common:
env: env:
PATH: prepend directory with type emulator symlinks == local.bin_path PATH: prepend directory with type emulator symlinks == local.bin_path
__target_host: the target host we are working on __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 __global: full qualified path to the global
output dir == local.out_path output dir == local.out_path
__cdist_manifest: full qualified path of the manifest == script __cdist_manifest: full qualified path of the manifest == script
@ -95,14 +98,16 @@ class Manifest(object):
self.target_host = target_host self.target_host = target_host
self.local = local self.local = local
self.log = logging.getLogger(self.target_host) self.log = logging.getLogger(self.target_host[0])
self.env = { self.env = {
'PATH': "%s:%s" % (self.local.bin_path, os.environ['PATH']), 'PATH': "%s:%s" % (self.local.bin_path, os.environ['PATH']),
# for use in type emulator # for use in type emulator
'__cdist_type_base_path': self.local.type_path, '__cdist_type_base_path': self.local.type_path,
'__global': self.local.base_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, '__files': self.local.files_path,
} }

View file

@ -65,6 +65,8 @@ class Emulator(object):
try: try:
self.global_path = self.env['__global'] self.global_path = self.env['__global']
self.target_host = self.env['__target_host'] self.target_host = self.env['__target_host']
self.target_hostname = self.env['__target_hostname']
self.target_fqdn = self.env['__target_fqdn']
# Internal variables # Internal variables
self.object_source = self.env['__cdist_manifest'] self.object_source = self.env['__cdist_manifest']
@ -101,7 +103,7 @@ class Emulator(object):
else: else:
logging.root.setLevel(logging.INFO) logging.root.setLevel(logging.INFO)
self.log = logging.getLogger(self.target_host) self.log = logging.getLogger(self.target_host[0])
def commandline(self): def commandline(self):
"""Parse command line""" """Parse command line"""

View file

@ -83,7 +83,7 @@ class Local(object):
return None return None
def _init_log(self): def _init_log(self):
self.log = logging.getLogger(self.target_host) self.log = logging.getLogger(self.target_host[0])
def _init_permissions(self): def _init_permissions(self):
# Setup file permissions using umask # Setup file permissions using umask
@ -184,8 +184,11 @@ class Local(object):
if env is None: if env is None:
env = os.environ.copy() env = os.environ.copy()
# Export __target_host for use in __remote_{copy,exec} scripts # Export __target_host, __target_hostname, __target_fqdn
env['__target_host'] = self.target_host # 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 # Export for emulator
env['__cdist_object_marker'] = self.object_marker_name env['__cdist_object_marker'] = self.object_marker_name

View file

@ -66,7 +66,7 @@ class Remote(object):
self.type_path = os.path.join(self.conf_path, "type") self.type_path = os.path.join(self.conf_path, "type")
self.global_explorer_path = os.path.join(self.conf_path, "explorer") 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() self._init_env()
@ -102,12 +102,12 @@ class Remote(object):
command = self._copy.split() command = self._copy.split()
path = os.path.join(source, f) path = os.path.join(source, f)
command.extend([path, '{0}:{1}'.format( command.extend([path, '{0}:{1}'.format(
self.target_host, destination)]) self.target_host[0], destination)])
self._run_command(command) self._run_command(command)
else: else:
command = self._copy.split() command = self._copy.split()
command.extend([source, '{0}:{1}'.format( command.extend([source, '{0}:{1}'.format(
self.target_host, destination)]) self.target_host[0], destination)])
self._run_command(command) self._run_command(command)
def run_script(self, script, env=None, return_output=False): def run_script(self, script, env=None, return_output=False):
@ -128,7 +128,7 @@ class Remote(object):
""" """
# prefix given command with remote_exec # prefix given command with remote_exec
cmd = self._exec.split() 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? # FIXME: replace this by -o SendEnv name -o SendEnv name ... to ssh?
# can't pass environment to remote side, so prepend command with # can't pass environment to remote side, so prepend command with
@ -165,9 +165,12 @@ class Remote(object):
assert isinstance(command, (list, tuple)), ( assert isinstance(command, (list, tuple)), (
"list or tuple argument expected, got: %s" % command) "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 = 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) self.log.debug("Remote run: %s", command)
try: try:

View file

@ -38,7 +38,12 @@ class Shell(object):
self.shell = shell 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( self.local = cdist.exec.local.Local(
target_host=self.target_host) target_host=self.target_host)
@ -59,7 +64,9 @@ class Shell(object):
'__cdist_type_base_path': self.local.type_path, '__cdist_type_base_path': self.local.type_path,
'__cdist_manifest': "cdist shell", '__cdist_manifest': "cdist shell",
'__global': self.local.base_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],
'__manifest': self.local.manifest_path, '__manifest': self.local.manifest_path,
'__explorer': self.local.global_explorer_path, '__explorer': self.local.global_explorer_path,
'__files': self.local.files_path, '__files': self.local.files_path,

View file

@ -37,7 +37,11 @@ class CdistTestCase(unittest.TestCase):
remote_exec = os.path.join(global_fixtures_dir, "remote", "exec") remote_exec = os.path.join(global_fixtures_dir, "remote", "exec")
remote_copy = os.path.join(global_fixtures_dir, "remote", "copy") remote_copy = os.path.join(global_fixtures_dir, "remote", "copy")
target_host = 'cdisttesthost' target_host = (
'cdisttesthost',
'cdisttesthost',
'cdisttesthost',
)
def mkdtemp(self, **kwargs): def mkdtemp(self, **kwargs):
return tempfile.mkdtemp(prefix='tmp.cdist.test.', **kwargs) return tempfile.mkdtemp(prefix='tmp.cdist.test.', **kwargs)

View file

@ -41,7 +41,7 @@ class CodeTestCase(test.CdistTestCase):
def setUp(self): def setUp(self):
self.local_dir = self.mkdtemp() 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.host_base_path = os.path.join(self.local_dir, self.hostdir)
self.local = local.Local( self.local = local.Local(
@ -83,7 +83,12 @@ class CodeTestCase(test.CdistTestCase):
junk, value = line.split(': ') junk, value = line.split(': ')
key = junk.split(' ')[1] key = junk.split(' ')[1]
output_dict[key] = value 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['__global'], self.local.base_path)
self.assertEqual(output_dict['__type'], self.cdist_type.absolute_path) self.assertEqual(output_dict['__type'], self.cdist_type.absolute_path)
self.assertEqual(output_dict['__object'], self.assertEqual(output_dict['__object'],
@ -101,7 +106,12 @@ class CodeTestCase(test.CdistTestCase):
junk, value = line.split(': ') junk, value = line.split(': ')
key = junk.split(' ')[1] key = junk.split(' ')[1]
output_dict[key] = value 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['__global'], self.local.base_path)
self.assertEqual(output_dict['__type'], self.cdist_type.absolute_path) self.assertEqual(output_dict['__type'], self.cdist_type.absolute_path)
self.assertEqual(output_dict['__object'], self.assertEqual(output_dict['__object'],

View file

@ -1,6 +1,8 @@
#!/bin/sh #!/bin/sh
echo "echo __target_host: $__target_host" echo "echo __target_host: $__target_host"
echo "echo __target_hostname: $__target_hostname"
echo "echo __target_fqdn: $__target_fqdn"
echo "echo __global: $__global" echo "echo __global: $__global"
echo "echo __type: $__type" echo "echo __type: $__type"
echo "echo __object: $__object" echo "echo __object: $__object"

View file

@ -55,7 +55,7 @@ class ConfigRunTestCase(test.CdistTestCase):
self.temp_dir = self.mkdtemp() self.temp_dir = self.mkdtemp()
self.local_dir = os.path.join(self.temp_dir, "local") 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) self.host_base_path = os.path.join(self.local_dir, self.hostdir)
os.makedirs(self.host_base_path) os.makedirs(self.host_base_path)
self.local = cdist.exec.local.Local( self.local = cdist.exec.local.Local(

View file

@ -48,7 +48,7 @@ class EmulatorTestCase(test.CdistTestCase):
handle, self.script = self.mkstemp(dir=self.temp_dir) handle, self.script = self.mkstemp(dir=self.temp_dir)
os.close(handle) os.close(handle)
base_path = self.temp_dir 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) host_base_path = os.path.join(base_path, hostdir)
self.local = local.Local( self.local = local.Local(
@ -151,7 +151,7 @@ class EmulatorConflictingRequirementsTestCase(test.CdistTestCase):
handle, self.script = self.mkstemp(dir=self.temp_dir) handle, self.script = self.mkstemp(dir=self.temp_dir)
os.close(handle) os.close(handle)
base_path = self.temp_dir 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) host_base_path = os.path.join(base_path, hostdir)
self.local = local.Local( self.local = local.Local(
@ -241,7 +241,7 @@ class AutoRequireEmulatorTestCase(test.CdistTestCase):
def setUp(self): def setUp(self):
self.temp_dir = self.mkdtemp() self.temp_dir = self.mkdtemp()
base_path = os.path.join(self.temp_dir, "out") 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) host_base_path = os.path.join(base_path, hostdir)
self.local = local.Local( self.local = local.Local(
@ -274,7 +274,7 @@ class OverrideTestCase(test.CdistTestCase):
handle, self.script = self.mkstemp(dir=self.temp_dir) handle, self.script = self.mkstemp(dir=self.temp_dir)
os.close(handle) os.close(handle)
base_path = self.temp_dir 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) host_base_path = os.path.join(base_path, hostdir)
self.local = local.Local( self.local = local.Local(
@ -315,7 +315,7 @@ class ArgumentsTestCase(test.CdistTestCase):
def setUp(self): def setUp(self):
self.temp_dir = self.mkdtemp() self.temp_dir = self.mkdtemp()
base_path = self.temp_dir 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) host_base_path = os.path.join(base_path, hostdir)
handle, self.script = self.mkstemp(dir=self.temp_dir) handle, self.script = self.mkstemp(dir=self.temp_dir)
os.close(handle) os.close(handle)
@ -440,7 +440,7 @@ class StdinTestCase(test.CdistTestCase):
self.temp_dir = self.mkdtemp() self.temp_dir = self.mkdtemp()
base_path = os.path.join(self.temp_dir, "out") 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) host_base_path = os.path.join(base_path, hostdir)
self.local = local.Local( self.local = local.Local(

View file

@ -1,6 +1,8 @@
#!/bin/sh #!/bin/sh
echo "echo __target_host: $__target_host" echo "echo __target_host: $__target_host"
echo "echo __target_hostname: $__target_hostname"
echo "echo __target_fqdn: $__target_fqdn"
echo "echo __global: $__global" echo "echo __global: $__global"
echo "echo __type: $__type" echo "echo __type: $__type"
echo "echo __object: $__object" echo "echo __object: $__object"

View file

@ -44,10 +44,14 @@ class LocalTestCase(test.CdistTestCase):
def setUp(self): def setUp(self):
target_host = 'localhost' target_host = (
'localhost',
'localhost',
'localhost',
)
self.temp_dir = self.mkdtemp() self.temp_dir = self.mkdtemp()
self.out_parent_path = self.temp_dir 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.host_base_path = op.join(self.out_parent_path, self.hostdir)
self.out_path = op.join(self.host_base_path, "data") self.out_path = op.join(self.host_base_path, "data")
@ -111,7 +115,11 @@ class LocalTestCase(test.CdistTestCase):
test_type = "__file" test_type = "__file"
link_test_local = local.Local( link_test_local = local.Local(
target_host='localhost', target_host=(
'localhost',
'localhost',
'localhost',
),
base_root_path=self.host_base_path, base_root_path=self.host_base_path,
host_dir_name=self.hostdir, host_dir_name=self.hostdir,
exec_path=test.cdist_exec_path, exec_path=test.cdist_exec_path,
@ -130,7 +138,11 @@ class LocalTestCase(test.CdistTestCase):
test_type = "__cdist_test_type" test_type = "__cdist_test_type"
link_test_local = local.Local( link_test_local = local.Local(
target_host='localhost', target_host=(
'localhost',
'localhost',
'localhost',
),
base_root_path=self.host_base_path, base_root_path=self.host_base_path,
host_dir_name=self.hostdir, host_dir_name=self.hostdir,
exec_path=test.cdist_exec_path, exec_path=test.cdist_exec_path,
@ -152,7 +164,11 @@ class LocalTestCase(test.CdistTestCase):
os.environ['CDIST_PATH'] = conf_dir os.environ['CDIST_PATH'] = conf_dir
link_test_local = local.Local( link_test_local = local.Local(
target_host='localhost', target_host=(
'localhost',
'localhost',
'localhost',
),
base_root_path=self.host_base_path, base_root_path=self.host_base_path,
host_dir_name=self.hostdir, host_dir_name=self.hostdir,
exec_path=test.cdist_exec_path, exec_path=test.cdist_exec_path,

View file

@ -34,7 +34,11 @@ class RemoteTestCase(test.CdistTestCase):
def setUp(self): def setUp(self):
self.temp_dir = self.mkdtemp() self.temp_dir = self.mkdtemp()
self.target_host = 'localhost' self.target_host = (
'localhost',
'localhost',
'localhost',
)
self.base_path = self.temp_dir self.base_path = self.temp_dir
user = getpass.getuser() user = getpass.getuser()
remote_exec = "ssh -o User=%s -q" % user remote_exec = "ssh -o User=%s -q" % user
@ -67,22 +71,22 @@ class RemoteTestCase(test.CdistTestCase):
# /test api # /test api
def test_run_success(self): def test_run_success(self):
self.remote.run(['/bin/true']) self.remote.run(['true'])
def test_run_fail(self): 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): 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", "true"])
self.remote.run_script(script) self.remote.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", "false"])
self.assertRaises(remote.RemoteScriptError, self.remote.run_script, self.assertRaises(cdist.Error, self.remote.run_script,
script) script)
def test_run_script_get_output(self): 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 # test if the payload file is in the target directory
self.assertTrue(os.path.isfile(os.path.join(target, source_file_name))) self.assertTrue(os.path.isfile(os.path.join(target, source_file_name)))
def test_create_directories(self): def test_create_files_dirs(self):
self.remote.create_directories() self.remote.create_files_dirs()
self.assertTrue(os.path.isdir(self.remote.base_path)) self.assertTrue(os.path.isdir(self.remote.base_path))
self.assertTrue(os.path.isdir(self.remote.conf_path)) self.assertTrue(os.path.isdir(self.remote.conf_path))
@ -135,8 +139,8 @@ class RemoteTestCase(test.CdistTestCase):
remote_copy = "echo" remote_copy = "echo"
r = remote.Remote(self.target_host, base_path=self.base_path, r = remote.Remote(self.target_host, base_path=self.base_path,
remote_exec=remote_exec, remote_copy=remote_copy) remote_exec=remote_exec, remote_copy=remote_copy)
self.assertEqual(r.run('/bin/true', return_output=True), self.assertEqual(r.run('true', return_output=True),
"%s\n" % self.target_host) "%s\n" % self.target_host[0])
def test_run_script_target_host_in_env(self): def test_run_script_target_host_in_env(self):
handle, remote_exec_path = self.mkstemp(dir=self.temp_dir) 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) remote_exec=remote_exec, remote_copy=remote_copy)
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", "true"])
self.assertEqual(r.run_script(script, return_output=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): def test_run_script_with_env_target_host_in_env(self):
handle, script = self.mkstemp(dir=self.temp_dir) handle, script = self.mkstemp(dir=self.temp_dir)

View file

@ -42,7 +42,7 @@ class ExplorerClassTestCase(test.CdistTestCase):
def setUp(self): def setUp(self):
self.temp_dir = self.mkdtemp() self.temp_dir = self.mkdtemp()
self.local_path = os.path.join(self.temp_dir, "local") 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) base_root_path = os.path.join(self.local_path, hostdir)
self.remote_base_path = os.path.join(self.temp_dir, "remote") self.remote_base_path = os.path.join(self.temp_dir, "remote")
os.makedirs(self.remote_base_path) os.makedirs(self.remote_base_path)

View file

@ -49,7 +49,7 @@ class ManifestTestCase(test.CdistTestCase):
self.temp_dir = self.mkdtemp() self.temp_dir = self.mkdtemp()
out_path = self.temp_dir 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) base_root_path = os.path.join(out_path, hostdir)
self.local = local.Local( self.local = local.Local(
target_host=self.target_host, target_host=self.target_host,
@ -60,7 +60,7 @@ class ManifestTestCase(test.CdistTestCase):
self.local.create_files_dirs() self.local.create_files_dirs()
self.manifest = manifest.Manifest(self.target_host, self.local) 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): def tearDown(self):
os.environ = self.orig_environ os.environ = self.orig_environ
@ -82,7 +82,12 @@ class ManifestTestCase(test.CdistTestCase):
key, value = line.split(': ') key, value = line.split(': ')
output_dict[key] = value output_dict[key] = value
self.assertTrue(output_dict['PATH'].startswith(self.local.bin_path)) 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['__global'], self.local.base_path)
self.assertEqual(output_dict['__cdist_type_base_path'], self.assertEqual(output_dict['__cdist_type_base_path'],
self.local.type_path) self.local.type_path)
@ -107,7 +112,12 @@ class ManifestTestCase(test.CdistTestCase):
key, value = line.split(': ') key, value = line.split(': ')
output_dict[key] = value output_dict[key] = value
self.assertTrue(output_dict['PATH'].startswith(self.local.bin_path)) 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['__global'], self.local.base_path)
self.assertEqual(output_dict['__cdist_type_base_path'], self.assertEqual(output_dict['__cdist_type_base_path'],
self.local.type_path) self.local.type_path)

View file

@ -3,6 +3,8 @@
cat > $__cdist_test_out << DONE cat > $__cdist_test_out << DONE
PATH: $PATH PATH: $PATH
__target_host: $__target_host __target_host: $__target_host
__target_hostname: $__target_hostname
__target_fqdn: $__target_fqdn
__global: $__global __global: $__global
__cdist_type_base_path: $__cdist_type_base_path __cdist_type_base_path: $__cdist_type_base_path
__manifest: $__manifest __manifest: $__manifest

View file

@ -3,6 +3,8 @@
cat > $__cdist_test_out << DONE cat > $__cdist_test_out << DONE
PATH: $PATH PATH: $PATH
__target_host: $__target_host __target_host: $__target_host
__target_hostname: $__target_hostname
__target_fqdn: $__target_fqdn
__global: $__global __global: $__global
__cdist_type_base_path: $__cdist_type_base_path __cdist_type_base_path: $__cdist_type_base_path
__type: $__type __type: $__type

View file

@ -1,8 +1,8 @@
Changelog Changelog
--------- ---------
next: 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: __locale_system (Steven Armstrong, Carlos Ortigoza, Nico Schottelius)
* New type: __sysctl (Steven Armstrong) * New type: __sysctl (Steven Armstrong)

View file

@ -53,8 +53,8 @@ Cdist expects the initial manifest at **cdist/conf/manifest/init**.
Within this initial manifest you define, which objects should be Within this initial manifest you define, which objects should be
created on which host. To distinguish between hosts, you can use the created on which host. To distinguish between hosts, you can use the
environment variable **__target_host**. Let's have a look at a simple environment variable **__target_host** and/or **__target_hostname** and/or
example:: **__target_fqdn**. Let's have a look at a simple example::
__cdistmarker __cdistmarker

View file

@ -226,7 +226,18 @@ __object_name
The full qualified name of the current object. The full qualified name of the current object.
Available for: type manifest, type explorer, type gencode. Available for: type manifest, type explorer, type gencode.
__target_host __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. Available for: explorer, initial manifest, type explorer, type manifest, type gencode, shell.
__type __type
Path to the current type. Path to the current type.