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 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)

View File

@ -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,
}

View File

@ -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 = []

View File

@ -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,
}

View File

@ -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"""

View File

@ -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

View File

@ -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:

View File

@ -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,

View File

@ -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)

View File

@ -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'],

View File

@ -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"

View File

@ -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(

View File

@ -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(

View File

@ -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"

View File

@ -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,

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -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)

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
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

View File

@ -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.