2011-10-12 15:20:47 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2018-01-09 08:31:40 +00:00
|
|
|
# 2011-2013 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
|
|
|
|
|
|
|
"""
|
Redefine/reimplement CDIST_ORDER_DEPENDENCY
CDIST_ORDER_DEPENDENCY now defines type order dependency context.
cdist (emulator) maintains global state variables, as files,
order_dep_state and typeorder_dep, and per object state variable,
as file, typeorder_dep.
If order_dep_state exists then this defines that order dependency is
turned on.
If order_dep_state does not exist then order dependency is turned off.
If order dependency is on then objects created after it is turned on are
recorded into:
* global typeorder_dep, in case of init manifest
* object's typeorder_dep, in case of type's manifest.
If order dependency is on then requirement is injected, where object
created before current, is read from:
* global typeorder_dep, in case of init manifest
* object's typeorder_dep, in case of type's manifest.
Every time order dependency is turned off, typeorder_dep files are
removed, which means that type order list is cleared, context is
cleaned.
In the end cdist cleans after itself, i.e. mentioned files are removed.
When running type manifest is finished typeorder_dep file is removed.
When running config finishes global typeorder_dep and order_dep_state
files are removed.
Global type order recording is untouched.
Furthermore, for completeness, type order is now recorded for each object
too.
2019-11-22 23:25:51 +00:00
|
|
|
|
|
|
|
ORDER_DEP_STATE_NAME = 'order_dep_state'
|
|
|
|
TYPEORDER_DEP_NAME = 'typeorder_dep'
|
|
|
|
|
2019-05-16 19:58:47 +00:00
|
|
|
def __init__(self, target_host, local, dry_run=False):
|
2011-10-12 15:20:47 +00:00
|
|
|
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),
|
2017-09-11 07:06:47 +00:00
|
|
|
'__cdist_log_level_name': util.log_level_name_env_var_val(
|
|
|
|
self.log),
|
2020-06-11 12:16:37 +00:00
|
|
|
'__cdist_colored_log': str(
|
|
|
|
cdist.log.CdistFormatter.USE_COLORS).lower(),
|
2011-10-12 15:20:47 +00:00
|
|
|
}
|
2011-10-14 21:21:22 +00:00
|
|
|
|
2019-05-16 19:58:47 +00:00
|
|
|
if dry_run:
|
|
|
|
self.env['__cdist_dry_run'] = '1'
|
|
|
|
|
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)
|
2018-01-09 08:31:40 +00:00
|
|
|
which = "init"
|
2018-02-07 17:12:15 +00:00
|
|
|
if self.local.save_output_streams:
|
|
|
|
stderr_path = os.path.join(self.local.stderr_base_path, which)
|
|
|
|
stdout_path = os.path.join(self.local.stdout_base_path, which)
|
|
|
|
with open(stderr_path, 'ba+') as stderr, \
|
|
|
|
open(stdout_path, 'ba+') as stdout:
|
|
|
|
self.local.run_script(
|
|
|
|
initial_manifest,
|
|
|
|
env=self.env_initial_manifest(initial_manifest),
|
|
|
|
message_prefix=message_prefix,
|
|
|
|
stdout=stdout, stderr=stderr)
|
|
|
|
else:
|
2018-01-09 08:31:40 +00:00
|
|
|
self.local.run_script(
|
|
|
|
initial_manifest,
|
|
|
|
env=self.env_initial_manifest(initial_manifest),
|
2018-02-07 17:12:15 +00:00
|
|
|
message_prefix=message_prefix)
|
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
|
2018-01-09 08:31:40 +00:00
|
|
|
which = 'manifest'
|
2012-11-07 08:07:18 +00:00
|
|
|
if os.path.isfile(type_manifest):
|
2017-09-17 18:30:07 +00:00
|
|
|
self.log.verbose("Running type manifest %s for object %s",
|
|
|
|
type_manifest, cdist_object.name)
|
2018-02-07 17:12:15 +00:00
|
|
|
if self.local.save_output_streams:
|
|
|
|
stderr_path = os.path.join(cdist_object.stderr_path, which)
|
|
|
|
stdout_path = os.path.join(cdist_object.stdout_path, which)
|
|
|
|
with open(stderr_path, 'ba+') as stderr, \
|
|
|
|
open(stdout_path, 'ba+') as stdout:
|
|
|
|
self.local.run_script(
|
|
|
|
type_manifest,
|
|
|
|
env=self.env_type_manifest(cdist_object),
|
|
|
|
message_prefix=message_prefix,
|
|
|
|
stdout=stdout, stderr=stderr)
|
|
|
|
else:
|
2018-01-09 08:31:40 +00:00
|
|
|
self.local.run_script(
|
|
|
|
type_manifest,
|
|
|
|
env=self.env_type_manifest(cdist_object),
|
2018-02-07 17:12:15 +00:00
|
|
|
message_prefix=message_prefix)
|
Redefine/reimplement CDIST_ORDER_DEPENDENCY
CDIST_ORDER_DEPENDENCY now defines type order dependency context.
cdist (emulator) maintains global state variables, as files,
order_dep_state and typeorder_dep, and per object state variable,
as file, typeorder_dep.
If order_dep_state exists then this defines that order dependency is
turned on.
If order_dep_state does not exist then order dependency is turned off.
If order dependency is on then objects created after it is turned on are
recorded into:
* global typeorder_dep, in case of init manifest
* object's typeorder_dep, in case of type's manifest.
If order dependency is on then requirement is injected, where object
created before current, is read from:
* global typeorder_dep, in case of init manifest
* object's typeorder_dep, in case of type's manifest.
Every time order dependency is turned off, typeorder_dep files are
removed, which means that type order list is cleared, context is
cleaned.
In the end cdist cleans after itself, i.e. mentioned files are removed.
When running type manifest is finished typeorder_dep file is removed.
When running config finishes global typeorder_dep and order_dep_state
files are removed.
Global type order recording is untouched.
Furthermore, for completeness, type order is now recorded for each object
too.
2019-11-22 23:25:51 +00:00
|
|
|
|
|
|
|
def cleanup(self):
|
|
|
|
def _rm_file(fname):
|
|
|
|
try:
|
|
|
|
self.log.trace("[ORDER_DEP] Removing %s", fname)
|
|
|
|
os.remove(os.path.join(self.local.base_path, fname))
|
|
|
|
except FileNotFoundError:
|
|
|
|
pass
|
|
|
|
_rm_file(Manifest.ORDER_DEP_STATE_NAME)
|
|
|
|
_rm_file(Manifest.TYPEORDER_DEP_NAME)
|