2011-10-12 15:20:47 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# 2011 Steven Armstrong (steven-cdist at armstrong.cc)
|
2013-11-25 22:18:19 +00:00
|
|
|
# 2011-2013 Nico Schottelius (nico-cdist at schottelius.org)
|
2011-10-12 15:20:47 +00:00
|
|
|
#
|
|
|
|
# This file is part of cdist.
|
|
|
|
#
|
|
|
|
# cdist is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# cdist is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
|
|
|
|
import cdist
|
2017-09-09 19:17:29 +00:00
|
|
|
from . import util
|
2011-10-12 15:20:47 +00:00
|
|
|
|
|
|
|
'''
|
|
|
|
common:
|
|
|
|
runs only locally, does not need remote
|
|
|
|
|
|
|
|
env:
|
|
|
|
PATH: prepend directory with type emulator symlinks == local.bin_path
|
|
|
|
__target_host: the target host we are working on
|
2016-08-10 21:56:56 +00:00
|
|
|
__target_hostname: the target hostname provided from __target_host
|
|
|
|
__target_fqdn: the target's fully qualified domain name provided from
|
|
|
|
__target_host
|
2016-07-05 18:44:24 +00:00
|
|
|
__global: full qualified path to the global
|
|
|
|
output dir == local.out_path
|
2011-10-12 15:20:47 +00:00
|
|
|
__cdist_manifest: full qualified path of the manifest == script
|
2016-07-05 18:44:24 +00:00
|
|
|
__cdist_type_base_path: full qualified path to the directory where
|
|
|
|
types are defined for use in type emulator
|
2011-10-12 22:35:14 +00:00
|
|
|
== local.type_path
|
2016-06-11 10:02:13 +00:00
|
|
|
__files: full qualified path to the files dir
|
2017-07-20 20:04:44 +00:00
|
|
|
__target_host_tags: comma spearated list of host tags
|
2011-10-12 15:20:47 +00:00
|
|
|
|
|
|
|
initial manifest is:
|
|
|
|
script: full qualified path to the initial manifest
|
|
|
|
|
|
|
|
env:
|
|
|
|
__manifest: path to .../conf/manifest/ == local.manifest_path
|
|
|
|
|
|
|
|
creates: new objects through type emulator
|
|
|
|
|
|
|
|
type manifeste is:
|
|
|
|
script: full qualified path to the type manifest
|
|
|
|
|
|
|
|
env:
|
|
|
|
__object: full qualified path to the object's dir
|
|
|
|
__object_id: the objects id
|
|
|
|
__object_fq: full qualified object id, iow: $type.name + / + object_id
|
|
|
|
__type: full qualified path to the type's dir
|
|
|
|
|
|
|
|
creates: new objects through type emulator
|
|
|
|
'''
|
|
|
|
|
2016-07-05 18:44:24 +00:00
|
|
|
|
2012-11-07 09:48:30 +00:00
|
|
|
class NoInitialManifestError(cdist.Error):
|
|
|
|
"""
|
|
|
|
Display missing initial manifest:
|
|
|
|
- Display path if user given
|
|
|
|
- try to resolve link if it is a link
|
|
|
|
- Omit path if default (is a linked path in temp directory without
|
|
|
|
much help)
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, manifest_path, user_supplied):
|
|
|
|
msg_header = "Initial manifest missing"
|
|
|
|
|
|
|
|
if user_supplied:
|
|
|
|
if os.path.islink(manifest_path):
|
2016-07-05 18:44:24 +00:00
|
|
|
self.message = "%s: %s -> %s" % (
|
|
|
|
msg_header, manifest_path,
|
|
|
|
os.path.realpath(manifest_path))
|
2012-11-07 09:48:30 +00:00
|
|
|
else:
|
2012-11-07 10:05:29 +00:00
|
|
|
self.message = "%s: %s" % (msg_header, manifest_path)
|
2012-11-07 09:48:30 +00:00
|
|
|
else:
|
2012-11-07 10:05:29 +00:00
|
|
|
self.message = "%s" % (msg_header)
|
2012-11-07 09:48:30 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return repr(self.message)
|
|
|
|
|
2011-10-12 15:20:47 +00:00
|
|
|
|
|
|
|
class Manifest(object):
|
2011-10-13 09:05:39 +00:00
|
|
|
"""Executes cdist manifests.
|
2011-10-12 15:20:47 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
def __init__(self, target_host, local):
|
|
|
|
self.target_host = target_host
|
|
|
|
self.local = local
|
2011-10-18 11:32:36 +00:00
|
|
|
|
2016-12-08 16:36:57 +00:00
|
|
|
self._open_logger()
|
2011-10-14 23:38:22 +00:00
|
|
|
|
2011-10-12 15:20:47 +00:00
|
|
|
self.env = {
|
|
|
|
'PATH': "%s:%s" % (self.local.bin_path, os.environ['PATH']),
|
2016-07-05 18:44:24 +00:00
|
|
|
# for use in type emulator
|
|
|
|
'__cdist_type_base_path': self.local.type_path,
|
2013-08-19 10:03:25 +00:00
|
|
|
'__global': self.local.base_path,
|
2016-08-10 21:56:56 +00:00
|
|
|
'__target_host': self.target_host[0],
|
|
|
|
'__target_hostname': self.target_host[1],
|
|
|
|
'__target_fqdn': self.target_host[2],
|
2016-06-11 10:02:13 +00:00
|
|
|
'__files': self.local.files_path,
|
2017-07-20 20:04:44 +00:00
|
|
|
'__target_host_tags': self.local.target_host_tags,
|
2017-09-09 19:17:29 +00:00
|
|
|
'__cdist_log_level': util.log_level_env_var_val(self.log),
|
2011-10-12 15:20:47 +00:00
|
|
|
}
|
2011-10-14 21:21:22 +00:00
|
|
|
|
2016-12-08 16:36:57 +00:00
|
|
|
def _open_logger(self):
|
|
|
|
self.log = logging.getLogger(self.target_host[0])
|
|
|
|
|
|
|
|
# logger is not pickable, so remove it when we pickle
|
|
|
|
def __getstate__(self):
|
|
|
|
state = self.__dict__.copy()
|
|
|
|
if 'log' in state:
|
|
|
|
del state['log']
|
|
|
|
return state
|
|
|
|
|
|
|
|
# recreate logger when we unpickle
|
|
|
|
def __setstate__(self, state):
|
|
|
|
self.__dict__.update(state)
|
|
|
|
self._open_logger()
|
|
|
|
|
2012-11-07 09:48:30 +00:00
|
|
|
def env_initial_manifest(self, initial_manifest):
|
2011-10-12 15:20:47 +00:00
|
|
|
env = os.environ.copy()
|
|
|
|
env.update(self.env)
|
2012-11-07 09:48:30 +00:00
|
|
|
env['__cdist_manifest'] = initial_manifest
|
2011-10-12 15:20:47 +00:00
|
|
|
env['__manifest'] = self.local.manifest_path
|
2013-03-31 22:47:57 +00:00
|
|
|
env['__explorer'] = self.local.global_explorer_out_path
|
2012-11-06 21:32:18 +00:00
|
|
|
|
|
|
|
return env
|
|
|
|
|
2012-11-07 08:07:18 +00:00
|
|
|
def run_initial_manifest(self, initial_manifest=None):
|
|
|
|
if not initial_manifest:
|
|
|
|
initial_manifest = self.local.initial_manifest
|
2012-11-07 09:48:30 +00:00
|
|
|
user_supplied = False
|
|
|
|
else:
|
|
|
|
user_supplied = True
|
2012-11-06 20:32:37 +00:00
|
|
|
|
2012-11-07 08:07:18 +00:00
|
|
|
if not os.path.isfile(initial_manifest):
|
2012-11-07 09:48:30 +00:00
|
|
|
raise NoInitialManifestError(initial_manifest, user_supplied)
|
2012-11-07 08:07:18 +00:00
|
|
|
|
2016-07-05 18:44:24 +00:00
|
|
|
message_prefix = "initialmanifest"
|
2017-07-03 11:32:39 +00:00
|
|
|
self.log.verbose("Running initial manifest " + initial_manifest)
|
2016-07-05 18:44:24 +00:00
|
|
|
self.local.run_script(initial_manifest,
|
|
|
|
env=self.env_initial_manifest(initial_manifest),
|
2017-01-20 14:22:25 +00:00
|
|
|
message_prefix=message_prefix,
|
|
|
|
save_output=False)
|
2012-11-06 21:32:18 +00:00
|
|
|
|
|
|
|
def env_type_manifest(self, cdist_object):
|
2016-07-05 18:44:24 +00:00
|
|
|
type_manifest = os.path.join(self.local.type_path,
|
|
|
|
cdist_object.cdist_type.manifest_path)
|
2012-11-06 21:32:18 +00:00
|
|
|
env = os.environ.copy()
|
|
|
|
env.update(self.env)
|
|
|
|
env.update({
|
2012-11-07 09:48:30 +00:00
|
|
|
'__cdist_manifest': type_manifest,
|
2012-11-06 21:32:18 +00:00
|
|
|
'__manifest': self.local.manifest_path,
|
|
|
|
'__object': cdist_object.absolute_path,
|
|
|
|
'__object_id': cdist_object.object_id,
|
|
|
|
'__object_name': cdist_object.name,
|
|
|
|
'__type': cdist_object.cdist_type.absolute_path,
|
|
|
|
})
|
|
|
|
|
|
|
|
return env
|
2011-10-12 15:20:47 +00:00
|
|
|
|
|
|
|
def run_type_manifest(self, cdist_object):
|
2016-07-05 18:44:24 +00:00
|
|
|
type_manifest = os.path.join(self.local.type_path,
|
|
|
|
cdist_object.cdist_type.manifest_path)
|
2013-11-26 00:17:37 +00:00
|
|
|
message_prefix = cdist_object.name
|
2012-11-07 08:07:18 +00:00
|
|
|
if os.path.isfile(type_manifest):
|
2017-07-03 11:32:39 +00:00
|
|
|
self.log.verbose("Running type manifest " + type_manifest)
|
2016-07-05 18:44:24 +00:00
|
|
|
self.local.run_script(type_manifest,
|
|
|
|
env=self.env_type_manifest(cdist_object),
|
2017-01-20 14:22:25 +00:00
|
|
|
message_prefix=message_prefix,
|
|
|
|
save_output=False)
|