Esempio n. 1
0
 def _search_inst(self, index, q='*:*', *args, **kwargs):
     data = self.search_data[index, q]
     if not isinstance(data, dict):
         data = {'total': len(data), 'rows': data}
     search = Search(index, q, *args, **kwargs)
     search._data = data
     return search
 def _search_inst(self, index, q='*:*', *args, **kwargs):
     data = self.search_data[index, q]
     if not isinstance(data, dict):
         data = {'total': len(data), 'rows': data}
     search = Search(index, q, *args, **kwargs)
     search._data = data
     return search
Esempio n. 3
0
 def __call__(self):
     query = self.query
     environment = self.environment
     if environment is _default_environment:
         environment = env.get('chef_environment', DEFAULT_ENVIRONMENT)
     if environment:
         query += ' AND chef_environment:%s' % environment
     for row in Search('node', query, api=self.api):
         if row:
             if callable(self.hostname_attr):
                 val = self.hostname_attr(row.object)
                 if val:
                     yield val
             else:
                 for attr in self.hostname_attr:
                     try:
                         val = row.object.attributes.get_dotted(attr)
                         if val:  # Don't ever give out '' or None, since it will error anyway
                             yield val
                             break
                     except KeyError:
                         pass  # Move on to the next
                 else:
                     raise ChefError(
                         'Cannot find a usable hostname attribute for node %s',
                         row.object)
Esempio n. 4
0
def chef_roledefs(api=None,
                  hostname_attr=DEFAULT_HOSTNAME_ATTR,
                  environment=_default_environment):
    """Build a Fabric roledef dictionary from a Chef server.

    Example::

        from fabric.api import env, run, roles
        from chef.fabric import chef_roledefs

        env.roledefs = chef_roledefs()

        @roles('web_app')
        def mytask():
            run('uptime')

    ``hostname_attr`` can either be a string that is the attribute in the chef
    node that holds the hostname or IP to connect to, an array of such keys to
    check in order (the first which exists will be used), or a callable which
    takes a :class:`~chef.Node` and returns the hostname or IP to connect to.

    To refer to a nested attribute, separate the levels with ``'.'`` e.g. ``'ec2.public_hostname'``

    ``environment`` is the Chef :class:`~chef.Environment` name in which to
    search for nodes. If set to ``None``, no environment filter is added. If
    set to a string, it is used verbatim as a filter string. If not passed as
    an argument at all, the value in the Fabric environment dict is used,
    defaulting to ``'_default'``.

    .. note::

        ``environment`` must be set to ``None`` if you are emulating Chef API
        version 0.9 or lower.

    .. versionadded:: 0.1

    .. versionadded:: 0.2
        Support for iterable and callable values for  the``hostname_attr``
        argument, and the ``environment`` argument.
    """
    api = _api(api)
    if api.version_parsed < Environment.api_version_parsed and environment is not None:
        raise ChefAPIVersionError(
            'Environment support requires Chef API 0.10 or greater')
    roledefs = {}
    for row in Search('role', api=api):
        name = row['name']
        roledefs[name] = Roledef('roles:%s' % name, api, hostname_attr,
                                 environment)
    return roledefs
Esempio n. 5
0
 def __call__(self):
     query = self.query
     environment = self.environment
     if environment is _default_environment:
         environment = env.get('chef_environment', DEFAULT_ENVIRONMENT)
     if environment:
         query += ' AND chef_environment:%s' % environment
     for row in Search('node', query, api=self.api):
         if row:
             if callable(self.hostname_attr):
                 yield self.hostname_attr(row.object)
             else:
                 for attr in self.hostname_attr:
                     try:
                         yield row.object.attributes.get_dotted(attr)
                         break
                     except KeyError:
                         continue
                 else:
                     raise ChefError('Cannot find a usable hostname attribute for node %s', row.object)