本文整理汇总了Python中chef.api.ChefAPI类的典型用法代码示例。如果您正苦于以下问题:Python ChefAPI类的具体用法?Python ChefAPI怎么用?Python ChefAPI使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ChefAPI类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: chef_roledefs
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
示例2: create
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
示例3: get_api
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"])
示例4: __init__
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)
示例5: chef_environment
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
示例6: create
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
示例7: list
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)
示例8: start
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)
示例9: save
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)
示例10: _user_api
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)
示例11: create
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
示例12: __init__
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()
示例13: create
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
示例14: __init__
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)
示例15: fetch
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'}
)