commit
22874bfa84
@ -1,175 +0,0 @@ |
||||
# -*- coding: utf-8 -*- |
||||
# |
||||
# 2011 Steven Armstrong (steven-cdist at armstrong.cc) |
||||
# |
||||
# 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 itertools |
||||
import fnmatch |
||||
import pprint |
||||
|
||||
import cdist |
||||
|
||||
log = logging.getLogger(__name__) |
||||
|
||||
|
||||
class CircularReferenceError(cdist.Error): |
||||
def __init__(self, cdist_object, required_object): |
||||
self.cdist_object = cdist_object |
||||
self.required_object = required_object |
||||
|
||||
def __str__(self): |
||||
return 'Circular reference detected: %s -> %s' % (self.cdist_object.name, self.required_object.name) |
||||
|
||||
|
||||
class RequirementNotFoundError(cdist.Error): |
||||
def __init__(self, requirement): |
||||
self.requirement = requirement |
||||
|
||||
def __str__(self): |
||||
return 'Requirement could not be found: %s' % self.requirement |
||||
|
||||
|
||||
class DependencyResolver(object): |
||||
"""Cdist's dependency resolver. |
||||
|
||||
Usage: |
||||
>> resolver = DependencyResolver(list_of_objects) |
||||
# Easy access to the objects we are working with |
||||
>> resolver.objects['__some_type/object_id'] |
||||
<CdistObject __some_type/object_id> |
||||
# Easy access to a specific objects dependencies |
||||
>> resolver.dependencies['__some_type/object_id'] |
||||
[<CdistObject __other_type/dependency>, <CdistObject __some_type/object_id>] |
||||
# Pretty print the dependency graph |
||||
>> from pprint import pprint |
||||
>> pprint(resolver.dependencies) |
||||
# Iterate over all existing objects in the correct order |
||||
>> for cdist_object in resolver: |
||||
>> do_something_with(cdist_object) |
||||
""" |
||||
def __init__(self, objects, logger=None): |
||||
self.objects = dict((o.name, o) for o in objects) |
||||
self._dependencies = None |
||||
self.log = logger or log |
||||
|
||||
@property |
||||
def dependencies(self): |
||||
"""Build the dependency graph. |
||||
|
||||
Returns a dict where the keys are the object names and the values are |
||||
lists of all dependencies including the key object itself. |
||||
""" |
||||
if self._dependencies is None: |
||||
self.log.info("Resolving dependencies...") |
||||
self._dependencies = {} |
||||
self._preprocess_requirements() |
||||
for name,cdist_object in self.objects.items(): |
||||
resolved = [] |
||||
unresolved = [] |
||||
self._resolve_object_dependencies(cdist_object, resolved, unresolved) |
||||
self._dependencies[name] = resolved |
||||
self.log.debug(self._dependencies) |
||||
return self._dependencies |
||||
|
||||
def find_requirements_by_name(self, requirements): |
||||
"""Takes a list of requirement patterns and returns a list of matching object instances. |
||||
|
||||
Patterns are expected to be Unix shell-style wildcards for use with fnmatch.filter. |
||||
|
||||
find_requirements_by_name(['__type/object_id', '__other_type/*']) -> |
||||
[<Object __type/object_id>, <Object __other_type/any>, <Object __other_type/match>] |
||||
""" |
||||
object_names = self.objects.keys() |
||||
for pattern in requirements: |
||||
found = False |
||||
for requirement in fnmatch.filter(object_names, pattern): |
||||
found = True |
||||
yield self.objects[requirement] |
||||
if not found: |
||||
# FIXME: get rid of the singleton object_id, it should be invisible to the code -> hide it in Object |
||||
singleton = os.path.join(pattern, 'singleton') |
||||
if singleton in self.objects: |
||||
yield self.objects[singleton] |
||||
else: |
||||
raise RequirementNotFoundError(pattern) |
||||
|
||||
def _preprocess_requirements(self): |
||||
"""Find all autorequire dependencies and merge them to be just requirements |
||||
for further processing. |
||||
""" |
||||
for cdist_object in self.objects.values(): |
||||
if cdist_object.autorequire: |
||||
# The objects (children) that this cdist_object (parent) defined |
||||
# in it's type manifest shall inherit all explicit requirements |
||||
# that the parent has so that user defined requirements are |
||||
# fullfilled and processed in the expected order. |
||||
for auto_requirement in self.find_requirements_by_name(cdist_object.autorequire): |
||||
for requirement in self.find_requirements_by_name(cdist_object.requirements): |
||||
requirement_object_all_requirements = list(requirement.requirements) + list(requirement.autorequire) |
||||
if (requirement.name not in auto_requirement.requirements |
||||
and auto_requirement.name not in requirement_object_all_requirements): |
||||
self.log.debug('Adding %s to %s.requirements', requirement.name, auto_requirement) |
||||
auto_requirement.requirements.append(requirement.name) |
||||
# On the other hand the parent shall depend on all the children |
||||
# it created so that the user can setup dependencies on it as a |
||||
# whole without having to know anything about the parents |
||||
# internals. |
||||
cdist_object.requirements.extend(cdist_object.autorequire) |
||||
# As we changed the object on disc, we have to ensure it is not |
||||
# preprocessed again if someone would call us multiple times. |
||||
cdist_object.autorequire = [] |
||||
|
||||
def _resolve_object_dependencies(self, cdist_object, resolved, unresolved): |
||||
"""Resolve all dependencies for the given cdist_object and store them |
||||
in the list which is passed as the 'resolved' arguments. |
||||
|
||||
e.g. |
||||
resolved = [] |
||||
unresolved = [] |
||||
resolve_object_dependencies(some_object, resolved, unresolved) |
||||
print("Dependencies for %s: %s" % (some_object, resolved)) |
||||
""" |
||||
self.log.debug('Resolving dependencies for: %s' % cdist_object.name) |
||||
try: |
||||
unresolved.append(cdist_object) |
||||
for required_object in self.find_requirements_by_name(cdist_object.requirements): |
||||
self.log.debug("Object %s requires %s", cdist_object, required_object) |
||||
if required_object not in resolved: |
||||
if required_object in unresolved: |
||||
error = CircularReferenceError(cdist_object, required_object) |
||||
self.log.error('%s: %s', error, pprint.pformat(self._dependencies)) |
||||
raise error |
||||
self._resolve_object_dependencies(required_object, resolved, unresolved) |
||||
resolved.append(cdist_object) |
||||
unresolved.remove(cdist_object) |
||||
except RequirementNotFoundError as e: |
||||
raise cdist.CdistObjectError(cdist_object, "requires non-existing " + e.requirement) |
||||
|
||||
def __iter__(self): |
||||
"""Iterate over all unique objects and yield them in the correct order. |
||||
""" |
||||
iterable = itertools.chain(*self.dependencies.values()) |
||||
# Keep record of objects that have already been seen |
||||
seen = set() |
||||
seen_add = seen.add |
||||
for cdist_object in itertools.filterfalse(seen.__contains__, iterable): |
||||
seen_add(cdist_object) |
||||
yield cdist_object |
@ -1,91 +0,0 @@ |
||||
# -*- coding: utf-8 -*- |
||||
# |
||||
# 2010-2011 Steven Armstrong (steven-cdist at armstrong.cc) |
||||
# 2012 Nico Schottelius (nico-cdist at schottelius.org) |
||||
# |
||||
# 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 os |
||||
import shutil |
||||
|
||||
import cdist |
||||
from cdist import test |
||||
from cdist.exec import local |
||||
from cdist import core |
||||
from cdist.core import manifest |
||||
from cdist import resolver |
||||
from cdist import config |
||||
import cdist.context |
||||
|
||||
import os.path as op |
||||
my_dir = op.abspath(op.dirname(__file__)) |
||||
fixtures = op.join(my_dir, 'fixtures') |
||||
add_conf_dir = op.join(fixtures, 'conf') |
||||
|
||||
class AutorequireTestCase(test.CdistTestCase): |
||||
|
||||
def setUp(self): |
||||
self.orig_environ = os.environ |
||||
os.environ = os.environ.copy() |
||||
self.temp_dir = self.mkdtemp() |
||||
|
||||
self.out_dir = os.path.join(self.temp_dir, "out") |
||||
self.remote_out_dir = os.path.join(self.temp_dir, "remote") |
||||
|
||||
os.environ['__cdist_out_dir'] = self.out_dir |
||||
os.environ['__cdist_remote_out_dir'] = self.remote_out_dir |
||||
|
||||
self.context = cdist.context.Context( |
||||
target_host=self.target_host, |
||||
remote_copy=self.remote_copy, |
||||
remote_exec=self.remote_exec, |
||||
add_conf_dirs=[add_conf_dir], |
||||
exec_path=test.cdist_exec_path, |
||||
debug=False) |
||||
|
||||
self.config = config.Config(self.context) |
||||
|
||||
def tearDown(self): |
||||
os.environ = self.orig_environ |
||||
shutil.rmtree(self.temp_dir) |
||||
|
||||
def test_implicit_dependencies(self): |
||||
self.context.initial_manifest = os.path.join(self.context.local.manifest_path, 'implicit_dependencies') |
||||
self.config.stage_prepare() |
||||
|
||||
objects = core.CdistObject.list_objects(self.context.local.object_path, self.context.local.type_path) |
||||
dependency_resolver = resolver.DependencyResolver(objects) |
||||
expected_dependencies = [ |
||||
dependency_resolver.objects['__package_special/b'], |
||||
dependency_resolver.objects['__package/b'], |
||||
dependency_resolver.objects['__package_special/a'] |
||||
] |
||||
resolved_dependencies = dependency_resolver.dependencies['__package_special/a'] |
||||
self.assertEqual(resolved_dependencies, expected_dependencies) |
||||
|
||||
def test_circular_dependency(self): |
||||
self.context.initial_manifest = os.path.join(self.context.local.manifest_path, 'circular_dependency') |
||||
self.config.stage_prepare() |
||||
# raises CircularDependecyError |
||||
self.config.stage_run() |
||||
|
||||
def test_recursive_type(self): |
||||
self.context.initial_manifest = os.path.join(self.config.local.manifest_path, 'recursive_type') |
||||
self.config.stage_prepare() |
||||
# raises CircularDependecyError |
||||
self.config.stage_run() |
Binary file not shown.
After Width: | Height: | Size: 10 KiB |
Binary file not shown.
Binary file not shown.
@ -0,0 +1,44 @@ |
||||
Old: |
||||
|
||||
- global explores (all) |
||||
- initial manifest |
||||
- for each object |
||||
execute type explorers |
||||
execute manifest |
||||
|
||||
continue until all objects (including newly created) |
||||
have their type explorers/manifests run |
||||
- build dependency tree |
||||
- for each object |
||||
execute gencode-* |
||||
execute code-* |
||||
|
||||
New: |
||||
- run all global explorers |
||||
- run initial manifest |
||||
creates zero or more cdist_objects |
||||
- for each cdist_object |
||||
if not cdist_object.has_unfullfilled_requirements: |
||||
execute type explorers |
||||
execute manifest |
||||
may create new objects, resulting in autorequirements |
||||
|
||||
# Gained requirements during manifest run |
||||
if object.has_auto_requirements(): |
||||
continue |
||||
|
||||
cdist_object.execute gencode-* |
||||
cdist_object.execute code-* |
||||
|
||||
|
||||
Requirements / Test cases for requirments / resolver: |
||||
|
||||
- omnipotence |
||||
- |
||||
|
||||
|
||||
-------------------------------------------------------------------------------- |
||||
ERROR: localhost: The following objects could not be resolved: __cdistmarker/singleton requires autorequires ; __directory/etc/sudoers.d requires autorequires ; __file/etc/sudoers.d/nico requires __directory/etc/sudoers.d autorequires ; __file/etc/motd requires autorequires ; __package_pacman/atop requires autorequires ; __package_pacman/screen requires autorequires ; __package_pacman/strace requires autorequires ; __package_pacman/vim requires autorequires ; __package_pacman/zsh requires autorequires ; __package_pacman/lftp requires autorequires ; __package_pacman/nmap requires autorequires ; __package_pacman/ntp requires autorequires ; __package_pacman/rsync requires autorequires ; __package_pacman/rtorrent requires autorequires ; __package_pacman/wget requires autorequires ; __package_pacman/nload requires autorequires ; __package_pacman/iftop requires autorequires ; __package_pacman/mosh requires autorequires ; __package_pacman/git requires autorequires ; __package_pacman/mercurial requires autorequires ; __package_pacman/netcat requires autorequires ; __package_pacman/python-virtualenv requires autorequires ; __package_pacman/wireshark-cli requires autorequires ; __package_pacman/sudo requires autorequires |
||||
INFO: Total processing time for 1 host(s): 32.30426597595215 |
||||
ERROR: Failed to deploy to the following hosts: localhost |
||||
|
Binary file not shown.
Loading…
Reference in new issue