Exemplo n.º 1
0
 def _user_api(cls, api=None):
     api = api or ChefAPI.get_global()
     url = '/'.join(api.url.split('/')[:3])
     api_args = (url, api.key, api.client)
     api_kwargs = {
         'version': api.version,
         'headers': api.headers,
         'ssl_verify': api.ssl_verify
     }
     return ChefAPI(*api_args, **api_kwargs)
Exemplo n.º 2
0
 def create(cls, name, api=None, admin=False):
     api = api or ChefAPI.get_global()
     obj = cls(name, api, skip_load=True)
     obj.admin = admin
     d = api.api_request('POST', cls.url, data=obj)
     obj.private_key = d['private_key']
     return obj
 def create(cls, name, api=None, admin=False):
     api = api or ChefAPI.get_global()
     obj = cls(name, api, skip_load=True)
     obj.admin = admin
     d = api.api_request('POST', cls.url, data=obj)
     obj.private_key = d['private_key']
     return obj
Exemplo n.º 4
0
 def list(cls, api=None):
     """Return a :class:`ChefQuery` with the available objects of this type.
     """
     api = api or ChefAPI.get_global()
     cls._check_api_version(api)
     names = [name for name, url in api[cls.url].iteritems()]
     return ChefQuery(cls, names, api)
Exemplo n.º 5
0
def chef_roledefs(api=None, hostname_attr='fqdn'):
    """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 is the attribute in the chef node that holds the real hostname.
    to refer to a nested attribute, separate the levels with '.'.
    for example 'ec2.public_hostname'
    """
    api = api or ChefAPI.get_global() or autoconfigure()
    if not api:
        raise ChefError('Unable to load Chef API configuration')
    roledefs = {}
    for row in Search('role', api=api):
        name = row['name']
        roledefs[name] = Roledef(name, api, hostname_attr)
    return roledefs
Exemplo n.º 6
0
 def create(cls, name, api=None, **kwargs):
     api = api or ChefAPI.get_global()
     obj = cls(name, api, skip_load=True)
     for key, value in kwargs.iteritems():
         setattr(obj, key, value)
     api.api_request('POST', cls.url, data=obj)
     return obj
Exemplo n.º 7
0
    def get_api(self, mess):
        """Check to see if the user has selected a knife.rb

        If there is only one, it will be selected automatically

        :param mess: Errbot message object
        """
        configs = self.get_config_files()
        if len(configs) == 1:
            # there is only one config, so auto-select it
            self.set_config(mess, list(configs)[0])

        if self.USER_CONF.get(mess.frm.person):
            self.send(
                mess.frm,
                "/me Chef knife.rb: {}".format(self.USER_CONF[mess.frm.person]["knife_file"]),
                message_type=mess.type,
            )
        else:
            raise Exception(
                'You have not selected a knife.rb. Run "chef '
                'config list" to see a list of available '
                'configs and then use "chef config set '
                '<name>" to select one'
            )

        return ChefAPI.from_config_file(self.USER_CONF[mess.frm.person]["knife_file"])
Exemplo n.º 8
0
def chef_environment(name, api=None):
    """A Fabric task to set the current Chef environment context.

    This task works alongside :func:`~chef.fabric.chef_roledefs` to set the
    Chef environment to be used in future role queries.

    Example::

        from chef.fabric import chef_environment, chef_roledefs
        env.roledefs = chef_roledefs()

    .. code-block:: bash

        $ fab env:production deploy

    The task can be configured slightly via Fabric ``env`` values.

    ``env.chef_environment_task_alias`` sets the task alias, defaulting to "env".
    This value must be set **before** :mod:`chef.fabric` is imported.

    ``env.chef_environment_validate`` sets if :class:`~chef.Environment` names
    should be validated before use. Defaults to True.

    .. versionadded:: 0.2
    """
    if env.get('chef_environment_validate', True):
        api = api or ChefAPI.get_global() or autoconfigure()
        if not api:
            raise ChefError('Unable to load Chef API configuration')
        chef_env = Environment(name, api=api)
        if not chef_env.exists:
            raise ChefError('Unknown Chef environment: %s' % name)
    env['chef_environment'] = name
Exemplo n.º 9
0
 def __init__(self, index, q='*:*', sort='X_CHEF_id_CHEF_X asc', rows=1000, start=0, api=None):
     self.name = index
     self.api = api or ChefAPI.get_global()
     if not (sort.endswith(' asc') or sort.endswith(' desc')):
         sort += ' asc'
     self._args = dict(q=q, sort=sort, rows=rows, start=start)
     self.url = self.__class__.url + '/' + self.name + '?' + urllib.urlencode(self._args)
Exemplo n.º 10
0
 def list(cls, api=None):
     """Return a :class:`ChefQuery` with the available objects of this type.
     """
     api = api or ChefAPI.get_global()
     cls._check_api_version(api)
     names = [name for name, url in six.iteritems(api[cls.url])]
     return ChefQuery(cls, names, api)
Exemplo n.º 11
0
def chef_roledefs(api=None, hostname_attr = 'fqdn'):
    """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 is the attribute in the chef node that holds the real hostname.
    to refer to a nested attribute, separate the levels with '.'.
    for example 'ec2.public_hostname'
    """
    api = api or ChefAPI.get_global() or autoconfigure()
    if not api:
        raise ChefError('Unable to load Chef API configuration')
    roledefs = {}
    for row in Search('role', api=api):
        name = row['name']
        roledefs[name] =  Roledef(name, api, hostname_attr)
    return roledefs
Exemplo n.º 12
0
 def start(cls, command, nodes, run_timeout=300, api=None):
     data = {
         'command': command,
         'nodes': nodes,
         'run_timeout': run_timeout
     }
     api = api or ChefAPI.get_global()
     return api.api_request('POST', cls.url, data=data)
Exemplo n.º 13
0
 def save(self, api=None):
     api = api or ChefAPI.get_global()
     try:
         api.api_request('PUT', self.url, data=self)
     except ChefServerNotFoundError, e:
         # If you get a 404 during a save, just create it instead
         # This mirrors the logic in the Chef code
         api.api_request('POST', self.__class__.url, data=self)
Exemplo n.º 14
0
 def create(cls, name, api=None, **kwargs):
     """Create a new object of this type. Pass the initial value for any
     attributes as keyword arguments.
     """
     api = api or ChefAPI.get_global()
     obj = cls(name, api, skip_load=True)
     for key, value in kwargs.iteritems():
         setattr(obj, key, value)
     api.api_request('POST', cls.url, data=obj)
     return obj
Exemplo n.º 15
0
 def create(cls, name, api=None, **kwargs):
     """Create a new object of this type. Pass the initial value for any
     attributes as keyword arguments.
     """
     api = api or ChefAPI.get_global()
     obj = cls(name, api, skip_load=True)
     for key, value in kwargs.iteritems():
         setattr(obj, key, value)
     api.api_request("POST", cls.url, data=obj)
     return obj
Exemplo n.º 16
0
 def _user_api(cls, api=None):
     api = api or ChefAPI.get_global()
     url = '/'.join(api.url.split('/')[:3])
     api_args = (url, api.key, api.client)
     api_kwargs = {
         'version': api.version,
         'headers': api.headers,
         'ssl_verify': api.ssl_verify
     }
     return ChefAPI(*api_args, **api_kwargs)
Exemplo n.º 17
0
 def __init__(self,
              index,
              q='*:*',
              rows=1000,
              start=0,
              api=None,
              filter_result=None):
     self.name = index
     self.api = api or ChefAPI.get_global()
     self._args = dict(q=q, rows=rows, start=start)
     self.url = self.__class__.url + '/' + self.name + '?' + six.moves.urllib.parse.urlencode(
         self._args)
     self._filter_result = filter_result
Exemplo n.º 18
0
 def create(cls, bag, name, api=None, **kwargs):
     """Create a new data bag item. Pass the initial value for any keys as
     keyword arguments."""
     api = api or ChefAPI.get_global()
     obj = cls(bag, name, api, skip_load=True)
     for key, value in six.iteritems(kwargs):
         obj[key] = value
     obj.save()
     if isinstance(bag, Vault) and name not in bag.names:
         # Mutate the bag in-place if possible, so it will return the new
         # item instantly
         bag.names.append(name)
     return obj
Exemplo n.º 19
0
    def __init__(self, object_type, name, api, skip_load=False):
        self._check_object_type(object_type)

        self.object_type = object_type
        self.name = name
        self.url = "/%s/%s/_acl/" % (object_type, name)
        self.api = api or ChefAPI.get_global()

        self.attributes_map = {}
        for t in self.ace_types:
            self.attributes_map[t] = Permissions()

        if (not skip_load) and self.is_supported():
            self.reload()
Exemplo n.º 20
0
 def __init__(self, name, api=None, skip_load=False):
     self.name = name
     self.api = api or ChefAPI.get_global()
     self.url = self.__class__.url + '/' + self.name
     self.exists = False
     data = {}
     if not skip_load:
         try:
             data = self.api[self.url]
         except ChefServerNotFoundError:
             pass
         else:
             self.exists = True
     self._populate(data)
Exemplo n.º 21
0
    def __init__(self, object_type, name, api, skip_load=False):
        self._check_object_type(object_type)

        self.object_type = object_type
        self.name = name
        self.url = "/%s/%s/_acl/" % (object_type, name)
        self.api = api or ChefAPI.get_global()

        self.attributes_map = {}
        for t in self.ace_types:
            self.attributes_map[t] = Permissions()

        if (not skip_load) and self.is_supported():
            self.reload()
Exemplo n.º 22
0
 def fetch(cls, node=None, start_time=None, end_time=None, status=None, rows=10, api=None):
     search = node and 'nodes/%s' % node or 'org'
     api = api or ChefAPI.get_global()
     full_url = '/reports/%s/runs?' % (search)
     if start_time:
         full_url = full_url + ('from=%s' % cls._unix_stamp(start_time))
         full_url = full_url + ('&until=%s' % cls._unix_stamp(
             end_time or (datetime.now() + timedelta(days=1))))
     if rows:
         full_url = full_url + ('&rows=%s' % rows)
     return api.api_request('GET',
         full_url,
         headers={'X-Ops-Reporting-Protocol-Version': '0.1.0'}
     )
Exemplo n.º 23
0
 def create(cls, bag, name, api=None, **kwargs):
     """Create a new data bag item. Pass the initial value for any keys as
     keyword arguments."""
     api = api or ChefAPI.get_global()
     obj = cls(bag, name, api, skip_load=True)
     for key, value in kwargs.iteritems():
         obj[key] = value
     obj['id'] = name
     api.api_request('POST', cls.url + '/' + str(bag), data=obj.raw_data)
     if isinstance(bag, DataBag) and name not in bag.names:
         # Mutate the bag in-place if possible, so it will return the new
         # item instantly
         bag.names.append(name)
     return obj
Exemplo n.º 24
0
 def __init__(self, name, api=None, skip_load=False):
     self.name = name
     self.api = api or ChefAPI.get_global()
     self.url = self.__class__.url + "/" + self.name
     self.exists = False
     data = {}
     if not skip_load:
         try:
             data = self.api[self.url]
         except ChefServerNotFoundError:
             pass
         else:
             self.exists = True
     self._populate(data)
Exemplo n.º 25
0
 def create(cls, bag, name, api=None, **kwargs):
     """Create a new data bag item. Pass the initial value for any keys as
     keyword arguments."""
     api = api or ChefAPI.get_global()
     obj = cls(bag, name, api, skip_load=True)
     for key, value in kwargs.iteritems():
         obj[key] = value
     obj['id'] = name
     api.api_request('POST', cls.url+'/'+str(bag), data=obj.raw_data)
     if isinstance(bag, DataBag) and name not in bag.names:
         # Mutate the bag in-place if possible, so it will return the new
         # item instantly
         bag.names.append(name)
     return obj
Exemplo n.º 26
0
    def test__set_item__(self):
        api = ChefAPI('https://*****:*****@t@ b@g'

        self.assertEqual(item['id'], 'test')

        self.assertIsInstance(item.raw_data['pychef_test_ver1'], dict)
        self.assertEqual(item.raw_data['pychef_test_ver1']['version'], 1)
        self.assertEqual(item.raw_data['pychef_test_ver1']['cipher'], 'aes-256-cbc')
        self.assertIsNotNone(item.raw_data['pychef_test_ver1']['iv'])
        self.assertIsNotNone(item.raw_data['pychef_test_ver1']['encrypted_data'])

        self.assertIsInstance(item.raw_data['pychef_test_ver2'], dict)
        self.assertEqual(item.raw_data['pychef_test_ver2']['version'], 2)
        self.assertEqual(item.raw_data['pychef_test_ver2']['cipher'], 'aes-256-cbc')
        self.assertIsNotNone(item.raw_data['pychef_test_ver2']['iv'])
        self.assertIsNotNone(item.raw_data['pychef_test_ver2']['hmac'])
        self.assertIsNotNone(item.raw_data['pychef_test_ver2']['encrypted_data'])
Exemplo n.º 27
0
def chef_roledefs(api=None, hostname_attr=['cloud.public_hostname', 'fqdn'], 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 or ChefAPI.get_global() or autoconfigure()
    if not api:
        raise ChefError('Unable to load Chef API configuration')
    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(name, api, hostname_attr, environment)
    return roledefs
Exemplo n.º 28
0
    def __init__(self, name, api=None, skip_load=False):
        self.name = name
        self.api = api or ChefAPI.get_global()

        if not is_version_compatible(self.api_version, self.api):
            raise ChefApiVersionError, "Class %s is not compatible with API version %s" % (self.__class__.__name__, self.api.version)

        self.url = self.__class__.url + '/' + self.name
        self.exists = False
        data = {}
        if not skip_load:
            try:
                data = self.api[self.url]
            except ChefServerNotFoundError:
                pass
            else:
                self.exists = True
        self._populate(data)
Exemplo n.º 29
0
    def __init__(self, name, api=None, skip_load=False):
        self.name = name
        self.api = api or ChefAPI.get_global()
        self._check_api_version(self.api)

        self.url = self.__class__.url + '/' + self.name
        self.exists = False
        data = {}
        if not skip_load:
            try:
                data = self.api[self.url]
            except ChefServerNotFoundError:
                pass
            except TypeError:
                raise RuntimeError(
                    "Unable to load api object. Are we running in a thread?")
            else:
                self.exists = True
        self._populate(data)
Exemplo n.º 30
0
 def fetch(cls,
           node=None,
           start_time=None,
           end_time=None,
           status=None,
           rows=10,
           api=None):
     search = node and 'nodes/%s' % node or 'org'
     api = api or ChefAPI.get_global()
     full_url = '/reports/%s/runs?' % (search)
     if start_time:
         full_url = full_url + ('from=%s' % cls._unix_stamp(start_time))
         full_url = full_url + ('&until=%s' % cls._unix_stamp(
             end_time or (datetime.now() + timedelta(days=1))))
     if rows:
         full_url = full_url + ('&rows=%s' % rows)
     return api.api_request(
         'GET',
         full_url,
         headers={'X-Ops-Reporting-Protocol-Version': '0.1.0'})
Exemplo n.º 31
0
def chef_roledefs(api=None, hostname_attr = 'fqdn', environment = '_default'):
    """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 real hostname.
    Or it can be a function that takes a Node object as a parameter and returns the attribute.
    
    To refer to a nested attribute, separate the levels with '.' e.g. 'ec2.public_hostname'

    For example:
        def use_ec2_hostname(node):
            if node.attributes.has_dotted('fqdn'):
              return 'fqdn'
            else:
              return 'ec2.public_hostname'
      
        env.roledefs = chef_roledefs(hostname_attr = use_ec2_hostname)

    environment is the chef environment whose nodes will be fetched. The default environment is "_default".
    """
    api = api or ChefAPI.get_global() or autoconfigure()
    if not api:
        raise ChefError('Unable to load Chef API configuration')
    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(name, api, hostname_attr, environment)
    return roledefs
Exemplo n.º 32
0
def chef_roledefs(api=None, hostname_attr=['cloud.public_hostname', 'fqdn'], environment='_default'):
    """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 environment whose nodes will be fetched. The
    default environment is '_default'.

    .. versionadded:: 0.1

    .. versionadded:: 0.2
        Support for iterable and callable values for  the``hostname_attr``
        argument, and the ``environment`` argument.
    """
    api = api or ChefAPI.get_global() or autoconfigure()
    if not api:
        raise ChefError('Unable to load Chef API configuration')
    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(name, api, hostname_attr, environment)
    return roledefs
Exemplo n.º 33
0
def chef_roledefs(api=None):
    """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')
    """
    api = api or ChefAPI.get_global() or autoconfigure()
    if not api:
        raise ChefError('Unable to load Chef API configuration')
    roledefs = {}
    for row in Search('role', api=api):
        name = row['name']
        roledefs[name] =  Roledef(name, api)
    return roledefs
Exemplo n.º 34
0
    def __init__(self, name, version='_latest', api=None, skip_load=False):
        self.name = name
        self.api = api or ChefAPI.get_global()
        self._check_api_version(self.api)

        # Cookbooks need some special handling, and we default to the 
        # latest revision if version goes unspecified.
        if type(self).__name__ == 'Cookbook':
            self.version = version
            self.versions = self.versions(name)
            self.url = "{0}/{1}/{2}".format(self.__class__.url, self.name, self.version)
        else:
            self.url = self.__class__.url + '/' + self.name

        self.exists = False
        data = {}
        if not skip_load:
            try:
                data = self.api[self.url]
            except ChefServerNotFoundError:
                pass
            else:
                self.exists = True
        self._populate(data)
Exemplo n.º 35
0
 def list(cls, api=None):
     api = api or ChefAPI.get_global()
     for name, url in api["/nodes"].iteritems():
         yield cls(name, api=api)
 def list(cls, api=None):
     api = api or ChefAPI.get_global()
     names = [name for name, url in api[cls.url].iteritems()]
     return ChefQuery(cls, names, api)
 def __init__(self, index, q='*:*', rows=1000, start=0, api=None):
     self.name = index
     self.api = api or ChefAPI.get_global()
     self._args = dict(q=q, rows=rows, start=start)
     self.url = self.__class__.url + '/' + self.name + '?' + urllib.urlencode(
         self._args)
Exemplo n.º 38
0
 def list(cls, api=None):
     api = api or ChefAPI.get_global()
     names = [name for name, url in six.iteritems(api[cls.url])]
     return ChefQuery(cls, names, api, data=self.keys)
Exemplo n.º 39
0
 def jobs(cls):
     api = api or ChefAPI.get_global()
     return api.api_request('GET', cls.url)
Exemplo n.º 40
0
def test_chef_api(**kwargs):
    return ChefAPI('https://api.opscode.com/organizations/pycheftest',
                   os.path.join(TEST_ROOT, 'client.pem'), 'unittests',
                   **kwargs)
Exemplo n.º 41
0
 def list(cls, api=None):
     """Return a :class:`ChefQuery` with the available objects of this type.
     """
     api = api or ChefAPI.get_global()
     names = [name for name, url in api[cls.url].iteritems()]
     return ChefQuery(cls, names, api)
Exemplo n.º 42
0
 def __init__(self, name, api=None):
     self.name = name
     self.api = api or ChefAPI.get_global()
     self._populated = False
Exemplo n.º 43
0
def _api(api):
    api = api or ChefAPI.get_global() or autoconfigure()
    if not api:
        raise ChefError('Unable to load Chef API configuration')
    return api
Exemplo n.º 44
0
 def load(self, path):
     path = os.path.join(os.path.dirname(__file__), 'configs', path)
     return ChefAPI.from_config_file(path)
 def list(cls, api=None):
     api = api or ChefAPI.get_global()
     names = [name for name, url in six.iteritems(api[cls.url])]
     return ChefQuery(cls, names, api)
Exemplo n.º 46
0
 def delete(self, api=None):
     api = api or ChefAPI.get_global()
     api.api_request("DELETE", "/nodes/" + self.name)
Exemplo n.º 47
0
 def load(self, path):
     path = os.path.join(os.path.dirname(__file__), 'configs', path)
     return ChefAPI.from_config_file(path)
Exemplo n.º 48
0
 def setUp(self):
     super(UserTestCase, self).setUp()
     self.api = ChefAPI.get_global()
     self.user_api = User._user_api(api=self.api)
     User._user_api = mock.MagicMock(return_value=self.user_api)
 def __init__(self, index, q='*:*', rows=1000, start=0, api=None):
     self.name = index
     self.api = api or ChefAPI.get_global()
     self._args = dict(q=q, rows=rows, start=start)
     self.url = self.__class__.url + '/' + self.name + '?' + six.moves.urllib.parse.urlencode(self._args)
Exemplo n.º 50
0
 def save(self, api=None):
     api = api or ChefAPI.get_global()
     api.api_request("PUT", "/nodes/" + self.name, data=self)