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.
This commit is contained in:
parent
f3bd439c43
commit
da274e5ef3
5 changed files with 157 additions and 38 deletions
|
|
@ -29,6 +29,7 @@ import sys
|
|||
import cdist
|
||||
from cdist import core
|
||||
from cdist import flock
|
||||
from cdist.core.manifest import Manifest
|
||||
|
||||
|
||||
class MissingRequiredEnvironmentVariableError(cdist.Error):
|
||||
|
|
@ -82,6 +83,11 @@ class Emulator(object):
|
|||
self.object_base_path = os.path.join(self.global_path, "object")
|
||||
self.typeorder_path = os.path.join(self.global_path, "typeorder")
|
||||
|
||||
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)
|
||||
|
||||
self.type_name = os.path.basename(argv[0])
|
||||
self.cdist_type = core.CdistType(self.type_base_path, self.type_name)
|
||||
|
||||
|
|
@ -206,6 +212,14 @@ class Emulator(object):
|
|||
return params
|
||||
|
||||
def setup_object(self):
|
||||
# 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()
|
||||
|
||||
# Create object with given parameters
|
||||
self.parameters = {}
|
||||
for key, value in vars(self.args).items():
|
||||
|
|
@ -237,6 +251,20 @@ class Emulator(object):
|
|||
# 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)
|
||||
|
||||
# Record / Append source
|
||||
self.cdist_object.source.append(self.object_source)
|
||||
|
|
@ -293,45 +321,73 @@ class Emulator(object):
|
|||
|
||||
return cdist_object.name
|
||||
|
||||
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 []
|
||||
|
||||
def record_requirements(self):
|
||||
"""Record requirements."""
|
||||
|
||||
order_dep_on = self._order_dep_on()
|
||||
|
||||
# Inject the predecessor, but not if its an override
|
||||
# (this would leed to an circular dependency)
|
||||
if ("CDIST_ORDER_DEPENDENCY" in self.env and
|
||||
'CDIST_OVERRIDE' not in self.env):
|
||||
# load object name created befor this one from typeorder file ...
|
||||
with open(self.typeorder_path, 'r') as typecreationfile:
|
||||
typecreationorder = typecreationfile.readlines()
|
||||
# get the type created before this one ...
|
||||
try:
|
||||
lastcreatedtype = typecreationorder[-2].strip()
|
||||
# __object_name is the name of the object whose type
|
||||
# manifest is currently executed
|
||||
__object_name = self.env.get('__object_name', None)
|
||||
if lastcreatedtype == __object_name:
|
||||
self.log.debug(("Not injecting require for "
|
||||
"CDIST_ORDER_DEPENDENCY: %s for %s,"
|
||||
" %s's type manifest is currently"
|
||||
" being executed"),
|
||||
lastcreatedtype,
|
||||
self.cdist_object.name,
|
||||
lastcreatedtype)
|
||||
else:
|
||||
if 'require' in self.env:
|
||||
appendix = " " + lastcreatedtype
|
||||
if appendix not in self.env['require']:
|
||||
self.env['require'] += appendix
|
||||
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
|
||||
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
|
||||
|
||||
reqs = set()
|
||||
if "require" in self.env:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue