Beispiel #1
0
    def insert(self, thing):
        # insert into self and also add attributes that will help with  allocation
        if thing.type != BasicServer._clusto_type:
            raise ResourceException("Only servers can be inserted into "
                                    "this manager but %s is of type %s."
                                    % (thing.name, thing.type))

        
        memory = thing.attr_value('system', subkey='memory')
        disk = thing.attr_value('system', subkey='disk')
        cpucount = thing.attr_value('system', subkey='cpucount')

        if not memory and not disk and not cpucount:
            raise ResourceException("Server must have attributes for "
                                    "key='system' and subkey='disk',"
                                    "'memory', and 'cpucount' set to be "
                                    "inserted into this manager.")

        d = self.ensure_driver(thing,
                               "Can only insert an Entity or a Driver. "
                               "Tried to insert %s." % str(type(thing)))

        if d in self:
            raise PoolException("%s is already in %s." % (d, self))

        self.add_attr("_contains", d, number=True)
Beispiel #2
0
    def get_ip_manager(cls, ip):
        """return a valid ip manager for the given ip.

        @param ip: the ip
        @type ip: integer, string, or IPy object

        @return: the appropriate IP manager from the clusto database
        """
        ipman = cls.get_ip_managers(ip)
        if not ipman:
            raise ResourceException("No resource manager for %s exists." % str(ip))
        if len(ipman) > 1:
            raise ResourceException("More than one resource manager matches %s" % str(ip))
        ipman = ipman[0]
        return ipman
Beispiel #3
0
    def get_ip_manager(cls, ip):
        """return a valid ip manager for the given ip.

        @param ip: the ip
        @type ip: integer, string, or IPy object

        @return: the appropriate IP manager from the clusto database
        """

        ipman = None
        if isinstance(ip, Attribute):
            ipman = ip.entity
            return Driver(ipman)

        for ipmantest in clusto.get_entities(clusto_drivers=[cls]):
            try:
                ipmantest.ensure_type(ip)
            except ResourceTypeException:
                continue

            ipman = Driver(ipmantest)
            break

        if not ipman:
            raise ResourceException(u"No resource manager for %s exists." %
                                    str(ip))

        return ipman
Beispiel #4
0
    def allocate(self, thing, resource=(), number=True, force=False):
        """allocates a resource element to the given thing.

        resource - is passed as an argument it will be checked 
                   before assignment.  

        refattr - the attribute name on the entity that will refer back
                  this resource manager.

        returns the resource that was either passed in and processed 
        or generated.
        """

        try:
            clusto.begin_transaction()
            if not isinstance(thing, Driver):
                raise TypeError("thing is not of type Driver")

            if resource is ():
                # allocate a new resource
                resource, number = self.allocator(thing)
                auto_allocated = True
            else:
                auto_allocated = False
                resource, number = self.ensure_type(resource, number, thing)
                if not force and not self.available(resource, number, thing):
                    raise ResourceException(
                        "Requested resource is not available.")

            if self._record_allocations:
                if number == True:
                    c = Counter.get(ClustoMeta().entity, self._attr_name)
                    attr = thing.add_attr(self._attr_name,
                                          resource,
                                          number=c.value)
                    c.next()
                else:
                    attr = thing.add_attr(self._attr_name,
                                          resource,
                                          number=number)

                clusto.flush()

                a = thing.add_attr(
                    self._attr_name,
                    self.entity,
                    number=attr.number,
                    subkey='manager',
                )

                clusto.flush()
                self.additional_attrs(thing, resource, attr.number)

            else:
                attr = None
            clusto.commit()
        except Exception, x:
            clusto.rollback_transaction()
            raise
Beispiel #5
0
    def ensure_type(self, resource, number=True, thing=None):

        if isinstance(resource, basestring):
            resource = clusto.get_by_name(resource)

        if resource not in self:
            raise ResourceException("%s is not managed by this VM manager"
                                    % resource.name)

        return (resource, number)
Beispiel #6
0
    def remove(self, thing):
        # check if thing is in use by a VM
        # error if yes
        # remove if no and clear attributes related to helping allocation

        vms = self.owners(thing)
        if vms:
            raise ResourceException("%s is still allocated to VMs: %s"
                                    % (thing.name, str(vms)))

        super(VMManager, self).remove(thing)
Beispiel #7
0
    def get_resource_number(self, thing, resource):
        """Get the number for a resource on a given entity."""
        
        resource, number = self.ensure_type(resource, thing=thing)
        
        attrs = thing.attrs(self._attr_name, value=resource)

        if attrs:
            return attrs[0].number
        else:
            raise ResourceException("%s isn't assigned resource %s"
                                    % (thing.name, str(resource)))
Beispiel #8
0
    def add_ip(self, ip=None, ipman=None):

        if not ip and not ipman:
            raise ResourceException('If no ip is specified then an ipmanager must be specified')

        elif ip:
            
            if not ipman:
                ipman = IPManager.get_ip_manager(ip)

            return ipman.allocate(self, ip)
        else:
            return ipman.allocate(self)
Beispiel #9
0
    def allocate(self, thing, resource=(), number=True, **kwargs):
        """Allocate resources for VMs

        pass off normal allocation to the parent but also keep track of
        available host-resources
        """
        
        for res in self.resources(thing):
            raise ResourceException("%s is already assigned to %s"
                                    % (thing.name, res.value))

        attr = super(VMManager, self).allocate(thing, resource, number, **kwargs)

        return attr
Beispiel #10
0
    def allocator(self, thing):
        """Allocate a host server for a given virtual server. """

        for res in self.resources(thing):
            raise ResourceException("%s is already assigned to %s"
                                    % (thing.name, res.value))

        hosts = self.contents(clusto_types=[BasicServer])

        hosts = sorted(hosts,
                       key=lambda x: x.attr_value('system', subkey='disk'))
        hosts = sorted(hosts,
                       key=lambda x: x.attr_value('system', subkey='cpucount'))
        hosts = sorted(hosts,
                       key=lambda x: x.attr_value('system', subkey='memory'))
                       
        
        for i in hosts:
            if self._has_capacity(i, thing):
                return (i, True)

                
        raise ResourceException("No hosts available.")