Ejemplo n.º 1
0
    def get_recipe(self, name):
        path = os.path.join(self.path, "recipes", name + ".py")
        if not os.path.exists(path):
            raise Fail("Recipe %s in cookbook %s not found" % (name, self.name))

        with open(path, "rb") as fp:
            return fp.read()
Ejemplo n.º 2
0
    def override(self, **kwargs):
        for key, value in kwargs.items():
            try:
                arg = self._arguments[key]
            except KeyError:
                raise Fail("%s received unsupported argument %s" % (self, key))
            else:
                if value != self.arguments.get(key):
                    if not arg.allow_override:
                        raise Fail(
                            "%s doesn't allow overriding argument '%s'" %
                            (self, key))

                    try:
                        self.arguments[key] = arg.validate(value)
                    except InvalidArgument, exc:
                        raise InvalidArgument("%s %s" % (self, exc))
Ejemplo n.º 3
0
 def get_content(self):
     try:
         cookbook, name = self.name.split('/', 1)
     except ValueError:
         raise Fail(
             "[StaticFile(%s)] Path must include cookbook name (e.g. 'nginx/nginx.conf')"
             % self.name)
     cb = self.env.cookbooks[cookbook]
     path = os.path.join(cb.path, "files", name)
     with open(path, "rb") as fp:
         return fp.read()
Ejemplo n.º 4
0
 def get_source(self, environment, template):
     try:
         cookbook, name = template.split('/', 1)
     except ValueError:
         raise Fail(
             "[Template(%s)] Path must include cookbook name (e.g. 'nginx/nginx.conf.j2')"
             % template)
     cb = self.env.cookbooks[cookbook]
     path = os.path.join(cb.path, "templates", name)
     if not os.path.exists(path):
         raise TemplateNotFound("%s at %s" % (template, path))
     mtime = os.path.getmtime(path)
     with open(path, "rb") as fp:
         source = fp.read().decode('utf-8')
     return source, path, lambda: mtime == os.path.getmtime(path)
Ejemplo n.º 5
0
def find_provider(env, resource, class_path=None):
    if not class_path:
        try:
            class_path = PROVIDERS[env.system.platform][resource]
        except KeyError:
            class_path = PROVIDERS["default"][resource]

    if class_path.startswith('*'):
        cookbook, classname = class_path[1:].split('.')
        return getattr(env.cookbooks[cookbook], classname)

    try:
        mod_path, class_name = class_path.rsplit('.', 1)
    except ValueError:
        raise Fail("Unable to find provider for %s as %s" % (resource, class_path))
    mod = __import__(mod_path, {}, {}, [class_name])
    return getattr(mod, class_name)
Ejemplo n.º 6
0
    def run_action(self, resource, action):
        self.log.debug("Performing action %s on %s" % (action, resource))

        provider_class = find_provider(self, resource.__class__.__name__, resource.provider)
        provider = provider_class(resource)
        try:
            provider_action = getattr(provider, 'action_%s' % action)
        except AttributeError:
            raise Fail("%r does not implement action %s" % (provider, action))
        provider_action()

        if resource.is_updated:
            for action, res in resource.subscriptions['immediate']:
                self.log.info("%s sending %s action to %s (immediate)" % (resource, action, res))
                self.run_action(res, action)
            for action, res in resource.subscriptions['delayed']:
                self.log.info("%s sending %s action to %s (delayed)" % (resource, action, res))
            self.delayed_actions |= resource.subscriptions['delayed']
Ejemplo n.º 7
0
    def __init__(self, name, env=None, provider=None, **kwargs):
        if hasattr(self, 'name'):
            return

        self.name = name
        self.env = env or Environment.get_instance()
        self.provider = provider or getattr(self, 'provider', None)
        self.log = logging.getLogger("kokki.resource")

        self.arguments = {}
        for key, value in kwargs.items():
            try:
                arg = self._arguments[key]
            except KeyError:
                raise Fail("%s received unsupported argument %s" % (self, key))
            else:
                try:
                    self.arguments[key] = arg.validate(value)
                except InvalidArgument, exc:
                    raise InvalidArgument("%s %s" % (self, exc))
Ejemplo n.º 8
0
    def __new__(cls, name, env=None, provider=None, **kwargs):
        env = env or Environment.get_instance()
        provider = provider or getattr(cls, 'provider', None)

        r_type = cls.__name__
        if r_type not in env.resources:
            env.resources[r_type] = {}
        if name not in env.resources[r_type]:
            obj = super(Resource, cls).__new__(cls)
            env.resources[r_type][name] = obj
            env.resource_list.append(obj)
            return obj

        obj = env.resources[r_type][name]
        if obj.provider != provider:
            raise Fail(
                "Duplicate resource %r with a different provider %r != %r" %
                (obj, provider, obj.provider))

        obj.override(**kwargs)
        return obj