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 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 scalarizr.externals.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 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 scalarizr.externals.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): """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, 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 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): """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, 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)