2011-09-26 09:18:36 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2015-03-05 14:02:26 +00:00
|
|
|
# 2011-2015 Nico Schottelius (nico-cdist at schottelius.org)
|
2013-08-29 20:32:26 +00:00
|
|
|
# 2012-2013 Steven Armstrong (steven-cdist at armstrong.cc)
|
2014-01-27 15:19:01 +00:00
|
|
|
# 2014 Daniel Heule (hda at sfs.biz)
|
2011-09-26 09:18:36 +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 argparse
|
|
|
|
import logging
|
|
|
|
import os
|
2012-06-04 12:11:34 +00:00
|
|
|
import sys
|
2020-08-22 17:53:42 +00:00
|
|
|
import re
|
2011-09-26 09:18:36 +00:00
|
|
|
|
2011-09-26 22:49:12 +00:00
|
|
|
import cdist
|
2011-10-11 10:27:08 +00:00
|
|
|
from cdist import core
|
2017-08-11 13:04:26 +00:00
|
|
|
from cdist import flock
|
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
|
|
|
from cdist.core.manifest import Manifest
|
2011-09-26 09:21:04 +00:00
|
|
|
|
2016-07-05 18:44:24 +00:00
|
|
|
|
2013-08-19 11:34:29 +00:00
|
|
|
class MissingRequiredEnvironmentVariableError(cdist.Error):
|
|
|
|
def __init__(self, name):
|
|
|
|
self.name = name
|
2016-07-05 18:44:24 +00:00
|
|
|
self.message = ("Emulator requires the environment variable %s to be "
|
|
|
|
"setup" % self.name)
|
2013-08-19 11:34:29 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.message
|
|
|
|
|
2013-08-19 10:03:25 +00:00
|
|
|
|
2013-12-19 22:33:43 +00:00
|
|
|
class DefaultList(list):
|
|
|
|
"""Helper class to allow default values for optional_multiple parameters.
|
|
|
|
|
2019-11-27 13:59:25 +00:00
|
|
|
@see https://groups.google.com/forum/#!msg/comp.lang.python/sAUvkJEDpRc/RnRymrzJVDYJ # noqa
|
2013-12-19 22:33:43 +00:00
|
|
|
"""
|
|
|
|
def __copy__(self):
|
|
|
|
return []
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def create(cls, initial=None):
|
|
|
|
if initial:
|
2013-12-20 09:56:46 +00:00
|
|
|
return cls(initial.split('\n'))
|
2013-12-19 22:33:43 +00:00
|
|
|
|
|
|
|
|
2020-06-20 19:16:23 +00:00
|
|
|
class Emulator:
|
2012-11-19 11:04:07 +00:00
|
|
|
def __init__(self, argv, stdin=sys.stdin.buffer, env=os.environ):
|
2016-07-05 18:44:24 +00:00
|
|
|
self.argv = argv
|
|
|
|
self.stdin = stdin
|
|
|
|
self.env = env
|
2012-11-07 09:49:11 +00:00
|
|
|
|
2016-07-05 18:44:24 +00:00
|
|
|
self.object_id = ''
|
2011-10-15 00:31:40 +00:00
|
|
|
|
2013-08-19 10:03:25 +00:00
|
|
|
try:
|
2016-07-05 18:44:24 +00:00
|
|
|
self.global_path = self.env['__global']
|
2016-08-14 20:09:36 +00:00
|
|
|
self.target_host = (
|
|
|
|
self.env['__target_host'],
|
|
|
|
self.env['__target_hostname'],
|
|
|
|
self.env['__target_fqdn']
|
|
|
|
)
|
2013-08-19 10:03:25 +00:00
|
|
|
|
2015-03-05 14:02:26 +00:00
|
|
|
# Internal variables
|
2016-07-05 18:44:24 +00:00
|
|
|
self.object_source = self.env['__cdist_manifest']
|
2013-08-19 10:03:25 +00:00
|
|
|
self.type_base_path = self.env['__cdist_type_base_path']
|
2016-07-05 18:44:24 +00:00
|
|
|
self.object_marker = self.env['__cdist_object_marker']
|
2012-02-12 00:27:25 +00:00
|
|
|
|
2013-08-19 11:34:29 +00:00
|
|
|
except KeyError as e:
|
|
|
|
raise MissingRequiredEnvironmentVariableError(e.args[0])
|
2011-10-18 11:32:36 +00:00
|
|
|
|
2011-10-15 00:31:40 +00:00
|
|
|
self.object_base_path = os.path.join(self.global_path, "object")
|
2014-01-17 22:35:02 +00:00
|
|
|
self.typeorder_path = os.path.join(self.global_path, "typeorder")
|
2011-10-15 00:31:40 +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
|
|
|
self.typeorder_dep_path = os.path.join(self.global_path,
|
|
|
|
Manifest.TYPEORDER_DEP_NAME)
|
|
|
|
self.order_dep_state_path = os.path.join(self.global_path,
|
|
|
|
Manifest.ORDER_DEP_STATE_NAME)
|
|
|
|
|
2016-07-05 18:44:24 +00:00
|
|
|
self.type_name = os.path.basename(argv[0])
|
|
|
|
self.cdist_type = core.CdistType(self.type_base_path, self.type_name)
|
2011-10-15 00:31:40 +00:00
|
|
|
|
|
|
|
self.__init_log()
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
"""Emulate type commands (i.e. __file and co)"""
|
|
|
|
|
|
|
|
self.commandline()
|
2017-08-11 13:04:26 +00:00
|
|
|
self.init_object()
|
|
|
|
|
|
|
|
# locking for parallel execution
|
2017-08-22 08:58:30 +00:00
|
|
|
with flock.Flock(self.flock_path):
|
2017-08-11 13:04:26 +00:00
|
|
|
self.setup_object()
|
|
|
|
self.save_stdin()
|
|
|
|
self.record_requirements()
|
|
|
|
self.record_auto_requirements()
|
|
|
|
self.log.trace("Finished %s %s" % (
|
|
|
|
self.cdist_object.path, self.parameters))
|
2011-10-15 00:31:40 +00:00
|
|
|
|
|
|
|
def __init_log(self):
|
|
|
|
"""Setup logging facility"""
|
|
|
|
|
2017-09-09 19:17:29 +00:00
|
|
|
if '__cdist_log_level' in self.env:
|
2017-08-12 21:02:31 +00:00
|
|
|
try:
|
2017-09-09 19:17:29 +00:00
|
|
|
loglevel = self.env['__cdist_log_level']
|
2017-09-11 07:06:47 +00:00
|
|
|
level = int(loglevel)
|
2017-08-22 08:58:30 +00:00
|
|
|
except ValueError:
|
2017-08-12 21:02:31 +00:00
|
|
|
level = logging.WARNING
|
2011-10-15 00:31:40 +00:00
|
|
|
else:
|
2017-08-12 21:02:31 +00:00
|
|
|
level = logging.WARNING
|
2017-09-07 14:36:23 +00:00
|
|
|
try:
|
|
|
|
logging.root.setLevel(level)
|
|
|
|
except (ValueError, TypeError):
|
2017-09-09 19:17:29 +00:00
|
|
|
# if invalid __cdist_log_level value
|
2017-09-07 14:36:23 +00:00
|
|
|
logging.root.setLevel(logging.WARNING)
|
2011-10-15 00:31:40 +00:00
|
|
|
|
2020-06-03 19:45:04 +00:00
|
|
|
colored_log = self.env.get('__cdist_colored_log', 'false')
|
2020-06-11 12:16:37 +00:00
|
|
|
cdist.log.CdistFormatter.USE_COLORS = colored_log == 'true'
|
2020-06-01 17:11:58 +00:00
|
|
|
|
2016-08-10 21:56:56 +00:00
|
|
|
self.log = logging.getLogger(self.target_host[0])
|
2011-10-15 00:31:40 +00:00
|
|
|
|
|
|
|
def commandline(self):
|
|
|
|
"""Parse command line"""
|
|
|
|
|
2016-07-05 18:44:24 +00:00
|
|
|
parser = argparse.ArgumentParser(add_help=False,
|
|
|
|
argument_default=argparse.SUPPRESS)
|
2011-10-15 00:31:40 +00:00
|
|
|
|
2012-06-04 20:01:32 +00:00
|
|
|
for parameter in self.cdist_type.required_parameters:
|
|
|
|
argument = "--" + parameter
|
2016-07-05 18:44:24 +00:00
|
|
|
parser.add_argument(argument, dest=parameter, action='store',
|
|
|
|
required=True)
|
2012-06-04 20:01:32 +00:00
|
|
|
for parameter in self.cdist_type.required_multiple_parameters:
|
|
|
|
argument = "--" + parameter
|
2016-07-05 18:44:24 +00:00
|
|
|
parser.add_argument(argument, dest=parameter, action='append',
|
|
|
|
required=True)
|
2011-10-15 00:31:40 +00:00
|
|
|
for parameter in self.cdist_type.optional_parameters:
|
|
|
|
argument = "--" + parameter
|
2016-07-05 18:44:24 +00:00
|
|
|
default = self.cdist_type.parameter_defaults.get(parameter, None)
|
|
|
|
parser.add_argument(argument, dest=parameter, action='store',
|
|
|
|
required=False, default=default)
|
2012-06-04 20:01:32 +00:00
|
|
|
for parameter in self.cdist_type.optional_multiple_parameters:
|
2011-10-15 00:31:40 +00:00
|
|
|
argument = "--" + parameter
|
2016-07-05 18:44:24 +00:00
|
|
|
default = DefaultList.create(
|
|
|
|
self.cdist_type.parameter_defaults.get(
|
|
|
|
parameter, None))
|
|
|
|
parser.add_argument(argument, dest=parameter, action='append',
|
|
|
|
required=False, default=default)
|
2012-02-15 13:44:16 +00:00
|
|
|
for parameter in self.cdist_type.boolean_parameters:
|
|
|
|
argument = "--" + parameter
|
2016-07-05 18:44:24 +00:00
|
|
|
parser.add_argument(argument, dest=parameter,
|
|
|
|
action='store_const', const='')
|
2011-10-15 00:31:40 +00:00
|
|
|
|
|
|
|
# If not singleton support one positional parameter
|
|
|
|
if not self.cdist_type.is_singleton:
|
|
|
|
parser.add_argument("object_id", nargs=1)
|
|
|
|
|
|
|
|
# And finally parse/verify parameter
|
|
|
|
self.args = parser.parse_args(self.argv[1:])
|
2017-06-28 21:36:42 +00:00
|
|
|
self.log.trace('Args: %s' % self.args)
|
2011-10-15 00:31:40 +00:00
|
|
|
|
2017-08-11 13:04:26 +00:00
|
|
|
def init_object(self):
|
|
|
|
# Initialize object - and ensure it is not in args
|
2015-03-05 14:02:26 +00:00
|
|
|
if self.cdist_type.is_singleton:
|
2015-03-05 14:23:53 +00:00
|
|
|
self.object_id = ''
|
2015-03-05 14:02:26 +00:00
|
|
|
else:
|
2011-10-15 00:31:40 +00:00
|
|
|
self.object_id = self.args.object_id[0]
|
|
|
|
del self.args.object_id
|
2011-10-14 13:52:53 +00:00
|
|
|
|
2011-10-15 00:31:40 +00:00
|
|
|
# Instantiate the cdist object we are defining
|
2016-07-05 18:44:24 +00:00
|
|
|
self.cdist_object = core.CdistObject(
|
|
|
|
self.cdist_type, self.object_base_path, self.object_marker,
|
|
|
|
self.object_id)
|
2017-08-11 13:04:26 +00:00
|
|
|
lockfname = ('.' + self.cdist_type.name +
|
|
|
|
self.object_id + '_' +
|
|
|
|
self.object_marker + '.lock')
|
|
|
|
lockfname = lockfname.replace(os.sep, '_')
|
|
|
|
self.flock_path = os.path.join(self.object_base_path, lockfname)
|
2011-10-14 13:52:53 +00:00
|
|
|
|
2018-10-13 14:11:51 +00:00
|
|
|
def _object_params_in_context(self):
|
|
|
|
''' Get cdist_object parameters dict adopted by context.
|
|
|
|
Context consists of cdist_type boolean, optional, required,
|
|
|
|
optional_multiple and required_multiple parameters. If parameter
|
|
|
|
is multiple parameter then its value is a list.
|
|
|
|
This adaptation works on cdist_object.parameters which are read from
|
|
|
|
directory based dict where it is unknown what kind of data is in
|
|
|
|
file. If there is only one line in the file it is unknown if this
|
|
|
|
is a value of required/optional parameter or if it is one value of
|
|
|
|
multiple values parameter.
|
|
|
|
'''
|
|
|
|
params = {}
|
|
|
|
if self.cdist_object.exists:
|
|
|
|
for param in self.cdist_object.parameters:
|
|
|
|
value = ('' if param in self.cdist_type.boolean_parameters
|
|
|
|
else self.cdist_object.parameters[param])
|
|
|
|
if ((param in self.cdist_type.required_multiple_parameters or
|
|
|
|
param in self.cdist_type.optional_multiple_parameters) and
|
|
|
|
not isinstance(value, list)):
|
|
|
|
value = [value]
|
|
|
|
params[param] = value
|
|
|
|
return params
|
|
|
|
|
2017-08-11 13:04:26 +00:00
|
|
|
def setup_object(self):
|
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
|
|
|
# CDIST_ORDER_DEPENDENCY state
|
|
|
|
order_dep_on = self._order_dep_on()
|
|
|
|
order_dep_defined = "CDIST_ORDER_DEPENDENCY" in self.env
|
|
|
|
if not order_dep_defined and order_dep_on:
|
|
|
|
self._set_order_dep_state_off()
|
|
|
|
if order_dep_defined and not order_dep_on:
|
|
|
|
self._set_order_dep_state_on()
|
|
|
|
|
2011-10-15 00:31:40 +00:00
|
|
|
# Create object with given parameters
|
|
|
|
self.parameters = {}
|
2016-07-05 18:44:24 +00:00
|
|
|
for key, value in vars(self.args).items():
|
2011-10-15 00:31:40 +00:00
|
|
|
if value is not None:
|
|
|
|
self.parameters[key] = value
|
2011-10-18 11:32:36 +00:00
|
|
|
|
2016-07-05 18:44:24 +00:00
|
|
|
if self.cdist_object.exists and 'CDIST_OVERRIDE' not in self.env:
|
2018-10-13 14:11:51 +00:00
|
|
|
obj_params = self._object_params_in_context()
|
|
|
|
if obj_params != self.parameters:
|
2016-06-10 05:50:07 +00:00
|
|
|
errmsg = ("Object %s already exists with conflicting "
|
2016-07-05 18:44:24 +00:00
|
|
|
"parameters:\n%s: %s\n%s: %s" % (
|
|
|
|
self.cdist_object.name,
|
|
|
|
" ".join(self.cdist_object.source),
|
2018-10-13 14:11:51 +00:00
|
|
|
obj_params,
|
2016-07-05 18:44:24 +00:00
|
|
|
self.object_source,
|
|
|
|
self.parameters))
|
2016-06-10 05:50:07 +00:00
|
|
|
raise cdist.Error(errmsg)
|
2011-10-15 00:31:40 +00:00
|
|
|
else:
|
2014-01-27 15:19:01 +00:00
|
|
|
if self.cdist_object.exists:
|
2016-07-05 18:44:24 +00:00
|
|
|
self.log.debug(('Object %s override forced with '
|
|
|
|
'CDIST_OVERRIDE'), self.cdist_object.name)
|
2014-01-31 16:56:55 +00:00
|
|
|
self.cdist_object.create(True)
|
2014-01-31 16:59:56 +00:00
|
|
|
else:
|
2014-01-31 16:56:55 +00:00
|
|
|
self.cdist_object.create()
|
2011-10-15 00:31:40 +00:00
|
|
|
self.cdist_object.parameters = self.parameters
|
2020-07-01 12:05:45 +00:00
|
|
|
# Do the following recording even if object exists, but with
|
|
|
|
# different requirements.
|
|
|
|
|
|
|
|
# record the created object in typeorder file
|
|
|
|
with open(self.typeorder_path, 'a') as typeorderfile:
|
|
|
|
print(self.cdist_object.name, file=typeorderfile)
|
|
|
|
# record the created object in parent object typeorder file
|
|
|
|
__object_name = self.env.get('__object_name', None)
|
|
|
|
depname = self.cdist_object.name
|
|
|
|
if __object_name:
|
|
|
|
parent = self.cdist_object.object_from_name(__object_name)
|
|
|
|
parent.typeorder.append(self.cdist_object.name)
|
|
|
|
if self._order_dep_on():
|
|
|
|
self.log.trace(('[ORDER_DEP] Adding %s to typeorder dep'
|
|
|
|
' for %s'), depname, parent.name)
|
|
|
|
parent.typeorder_dep.append(depname)
|
|
|
|
elif self._order_dep_on():
|
|
|
|
self.log.trace('[ORDER_DEP] Adding %s to global typeorder dep',
|
|
|
|
depname)
|
|
|
|
self._add_typeorder_dep(depname)
|
2011-10-15 00:31:40 +00:00
|
|
|
|
2012-02-13 06:47:33 +00:00
|
|
|
# Record / Append source
|
|
|
|
self.cdist_object.source.append(self.object_source)
|
|
|
|
|
2012-11-19 11:04:07 +00:00
|
|
|
chunk_size = 65536
|
2016-07-05 18:44:24 +00:00
|
|
|
|
2012-11-19 11:04:07 +00:00
|
|
|
def _read_stdin(self):
|
|
|
|
return self.stdin.read(self.chunk_size)
|
|
|
|
|
2012-06-04 12:11:34 +00:00
|
|
|
def save_stdin(self):
|
|
|
|
"""If something is written to stdin, save it in the object as
|
|
|
|
$__object/stdin so it can be accessed in manifest and gencode-*
|
|
|
|
scripts.
|
|
|
|
"""
|
2012-11-07 08:58:47 +00:00
|
|
|
if not self.stdin.isatty():
|
2012-06-04 12:11:34 +00:00
|
|
|
try:
|
|
|
|
# go directly to file instead of using CdistObject's api
|
|
|
|
# as that does not support streaming
|
|
|
|
path = os.path.join(self.cdist_object.absolute_path, 'stdin')
|
2012-11-19 11:04:07 +00:00
|
|
|
with open(path, 'wb') as fd:
|
|
|
|
chunk = self._read_stdin()
|
|
|
|
while chunk:
|
|
|
|
fd.write(chunk)
|
|
|
|
chunk = self._read_stdin()
|
2012-06-04 12:11:34 +00:00
|
|
|
except EnvironmentError as e:
|
|
|
|
raise cdist.Error('Failed to read from stdin: %s' % e)
|
|
|
|
|
2016-06-30 18:31:01 +00:00
|
|
|
def record_requirement(self, requirement):
|
|
|
|
"""record requirement and return recorded requirement"""
|
|
|
|
|
|
|
|
# Raises an error, if object cannot be created
|
|
|
|
try:
|
|
|
|
cdist_object = self.cdist_object.object_from_name(requirement)
|
2017-11-02 12:57:36 +00:00
|
|
|
except core.cdist_type.InvalidTypeError as e:
|
2016-06-30 18:31:01 +00:00
|
|
|
self.log.error(("%s requires object %s, but type %s does not"
|
2016-07-05 18:44:24 +00:00
|
|
|
" exist. Defined at %s" % (
|
|
|
|
self.cdist_object.name,
|
|
|
|
requirement, e.name, self.object_source)))
|
2016-06-30 18:31:01 +00:00
|
|
|
raise
|
2018-10-13 14:11:51 +00:00
|
|
|
except core.cdist_object.MissingObjectIdError:
|
2016-06-30 18:31:01 +00:00
|
|
|
self.log.error(("%s requires object %s without object id."
|
2016-07-05 18:44:24 +00:00
|
|
|
" Defined at %s" % (self.cdist_object.name,
|
|
|
|
requirement,
|
|
|
|
self.object_source)))
|
2016-06-30 18:31:01 +00:00
|
|
|
raise
|
|
|
|
|
2019-04-14 14:54:59 +00:00
|
|
|
self.log.debug("Recording requirement %s for %s",
|
|
|
|
requirement, self.cdist_object.name)
|
2016-06-30 18:31:01 +00:00
|
|
|
|
|
|
|
# Save the sanitised version, not the user supplied one
|
|
|
|
# (__file//bar => __file/bar)
|
|
|
|
# This ensures pattern matching is done against sanitised list
|
|
|
|
self.cdist_object.requirements.append(cdist_object.name)
|
|
|
|
|
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 _order_dep_on(self):
|
|
|
|
return os.path.exists(self.order_dep_state_path)
|
|
|
|
|
|
|
|
def _set_order_dep_state_on(self):
|
|
|
|
self.log.trace('[ORDER_DEP] Setting order dep state on')
|
|
|
|
with open(self.order_dep_state_path, 'w'):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def _set_order_dep_state_off(self):
|
|
|
|
self.log.trace('[ORDER_DEP] Setting order dep state off')
|
|
|
|
# remove order dep state file
|
|
|
|
try:
|
|
|
|
os.remove(self.order_dep_state_path)
|
|
|
|
except FileNotFoundError:
|
|
|
|
pass
|
|
|
|
# remove typeorder dep file
|
|
|
|
try:
|
|
|
|
os.remove(self.typeorder_dep_path)
|
|
|
|
except FileNotFoundError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def _add_typeorder_dep(self, name):
|
|
|
|
with open(self.typeorder_dep_path, 'a') as f:
|
|
|
|
print(name, file=f)
|
|
|
|
|
|
|
|
def _read_typeorder_dep(self):
|
|
|
|
try:
|
|
|
|
with open(self.typeorder_dep_path, 'r') as f:
|
|
|
|
return f.readlines()
|
|
|
|
except FileNotFoundError:
|
|
|
|
return []
|
|
|
|
|
2011-10-15 00:31:40 +00:00
|
|
|
def record_requirements(self):
|
2016-10-08 09:40:32 +00:00
|
|
|
"""Record requirements."""
|
2014-01-18 18:23:21 +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_on = self._order_dep_on()
|
|
|
|
|
2016-07-05 18:44:24 +00:00
|
|
|
# Inject the predecessor, but not if its an override
|
|
|
|
# (this would leed to an circular dependency)
|
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
|
|
|
if (order_dep_on and 'CDIST_OVERRIDE' not in self.env):
|
|
|
|
try:
|
|
|
|
# __object_name is the name of the object whose type
|
|
|
|
# manifest is currently executed
|
|
|
|
__object_name = self.env.get('__object_name', None)
|
|
|
|
# load object name created befor this one from typeorder
|
|
|
|
# dep file
|
|
|
|
if __object_name:
|
|
|
|
parent = self.cdist_object.object_from_name(
|
|
|
|
__object_name)
|
|
|
|
typeorder = parent.typeorder_dep
|
|
|
|
else:
|
|
|
|
typeorder = self._read_typeorder_dep()
|
|
|
|
# get the type created before this one
|
|
|
|
lastcreatedtype = typeorder[-2].strip()
|
|
|
|
if 'require' in self.env:
|
|
|
|
if lastcreatedtype not in self.env['require']:
|
|
|
|
self.env['require'] += " " + lastcreatedtype
|
|
|
|
else:
|
|
|
|
self.env['require'] = lastcreatedtype
|
|
|
|
self.log.debug(("Injecting require for "
|
|
|
|
"CDIST_ORDER_DEPENDENCY: %s for %s"),
|
|
|
|
lastcreatedtype,
|
|
|
|
self.cdist_object.name)
|
|
|
|
except IndexError:
|
|
|
|
# if no second last line, we are on the first type,
|
|
|
|
# so do not set a requirement
|
|
|
|
pass
|
2014-01-18 18:23:21 +00:00
|
|
|
|
2012-11-07 09:25:47 +00:00
|
|
|
if "require" in self.env:
|
|
|
|
requirements = self.env['require']
|
2012-11-19 14:17:46 +00:00
|
|
|
self.log.debug("reqs = " + requirements)
|
2020-08-22 17:53:42 +00:00
|
|
|
for requirement in self._parse_require(requirements):
|
2011-10-15 21:23:57 +00:00
|
|
|
# Ignore empty fields - probably the only field anyway
|
2016-07-05 18:44:24 +00:00
|
|
|
if len(requirement) == 0:
|
|
|
|
continue
|
2020-07-01 12:05:45 +00:00
|
|
|
self.record_requirement(requirement)
|
2016-06-10 05:50:07 +00:00
|
|
|
|
2020-08-22 17:53:42 +00:00
|
|
|
def _parse_require(self, require):
|
|
|
|
return re.split(r'[ \t\n]+', require)
|
|
|
|
|
2011-11-02 22:58:18 +00:00
|
|
|
def record_auto_requirements(self):
|
2016-07-05 18:44:24 +00:00
|
|
|
"""An object shall automatically depend on all objects that it
|
|
|
|
defined in it's type manifest.
|
2011-11-02 22:58:18 +00:00
|
|
|
"""
|
2016-07-05 18:44:24 +00:00
|
|
|
# __object_name is the name of the object whose type manifest is
|
|
|
|
# currently executed
|
2012-11-07 09:25:47 +00:00
|
|
|
__object_name = self.env.get('__object_name', None)
|
2011-11-02 22:58:18 +00:00
|
|
|
if __object_name:
|
2012-04-25 15:18:16 +00:00
|
|
|
# The object whose type manifest is currently run
|
|
|
|
parent = self.cdist_object.object_from_name(__object_name)
|
|
|
|
# The object currently being defined
|
|
|
|
current_object = self.cdist_object
|
2016-07-05 18:44:24 +00:00
|
|
|
# As parent defined current_object it shall automatically
|
|
|
|
# depend on it.
|
2012-04-25 15:18:16 +00:00
|
|
|
# But only if the user hasn't said otherwise.
|
|
|
|
# Must prevent circular dependencies.
|
2016-07-05 18:44:24 +00:00
|
|
|
if parent.name not in current_object.requirements:
|
2019-04-14 14:54:59 +00:00
|
|
|
self.log.debug("Recording autorequirement %s for %s",
|
|
|
|
current_object.name, parent.name)
|
2012-05-03 08:16:08 +00:00
|
|
|
parent.autorequire.append(current_object.name)
|