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)
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 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)
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
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
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"])
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
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)
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)
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
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)
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)
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
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
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
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
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()
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)
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'} )
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
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)
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
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'])
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
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)
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)
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'})
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
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
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
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)
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)
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)
def jobs(cls): api = api or ChefAPI.get_global() return api.api_request('GET', cls.url)
def test_chef_api(**kwargs): return ChefAPI('https://api.opscode.com/organizations/pycheftest', os.path.join(TEST_ROOT, 'client.pem'), 'unittests', **kwargs)
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)
def __init__(self, name, api=None): self.name = name self.api = api or ChefAPI.get_global() self._populated = False
def _api(api): api = api or ChefAPI.get_global() or autoconfigure() if not api: raise ChefError('Unable to load Chef API configuration') return api
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)
def delete(self, api=None): api = api or ChefAPI.get_global() api.api_request("DELETE", "/nodes/" + self.name)
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)
def save(self, api=None): api = api or ChefAPI.get_global() api.api_request("PUT", "/nodes/" + self.name, data=self)