예제 #1
0
class LibvirtDecorator(object):
    def __init__(self):
        self.netmanager = NetManager()
        self.imagemanager = ImageManager()

    def _connection(self, host):
        if not host:
            conn = libvirt.open("qemu:///system")
        else:
            conn = libvirt.open("qemu+ssh://%s/system" % host)
        return conn


    def define_validate(self, argv):
        print argv
        #set name
        if argv.has_key('name'):
            if type(argv['name']) != str:
                return False, 'vm name must be a string'
            if len(argv['name']) > VM_NAME_LEN_LIMIT:
                return False, 'vm name is too long'
        else:
            argv['name'] = argv['uuid']

        #mamory validation
        if argv.has_key('memory'):
            if type(argv['memory']) != int:
                return False, 'memory must be a integer' 
        else:
            return False, 'memory is needed'

        #cpu validation
        if argv.has_key('vcpu'):
            if type(argv['vcpu']) != int:
                return False, 'vcpu must be a integer'
        else:
            return False, 'vcpu is needed'

        return True, argv

    def define(self, group, image_snap, argv, local_net=True):
        #generate uuid
        argv['uuid'] = uuid.uuid4()

        #set diskname
        argv['diskname'] = argv['uuid']
        
        res, info = self.define_validate(argv)
        if res:
            argv = info
        else:
            return res, 'Argv error: %s' % info


        if type(group) != Group:
            group = Group.objects.filter(pk=group)
            if group:
                group = group[0]
        if type(group) != Group:
            return False, 'Group error'

        #host filter
        host = host_filter(group, argv['vcpu'], argv['memory'], local_net)
        if not host:
            return False, "Host filter: Host not find."

        vlan = self.netmanager.vlan_filter(host.vlan.all())
        if not vlan:
            return False, "Vlan filter: No available Vlan."
        argv['bridge'] = vlan.br

        
        #allocate mac address
        mac = self.netmanager.allocate_mac(vlan, argv['uuid'])
        if not mac:
            return False, 'Allocate mac error: %s' % self.netmanager.error
        argv['mac'] = mac
        
        #resource claim
        host.claim(argv['vcpu'], argv['memory'])

        # print xmldesc
        diskinfo = self.imagemanager.init_disk(image_snap, argv['diskname'])
        if not diskinfo:
            host.release(argv['vcpu'], argv['memory'])
            self.netmanager.release_mac(argv['mac'], argv['uuid'])
            return False, 'Disk init error: %s' % self.imagemanager.error

        argv.update(diskinfo)
        
        try:
            xml = DomainXML(argv['image'])
            xmldesc = xml.render(argv)
            # print xmldesc
            conn = self._connection(host.ipv4)
            vm = conn.defineXML(xmldesc)

            obj = Vm()
            obj.host    = host
            obj.image   = argv['image']
            obj.image_name = argv['image_name']
            obj.uuid    = str(argv['uuid'])
            obj.name    = str(argv['name'])
            obj.vcpu    = argv['vcpu']
            obj.mem     = argv['memory']
            obj.disk    = str(argv['diskname'])
            obj.deleted = False
            obj.save()
            
        except Exception,e:
            print e
            # release resource
            host.release(argv['vcpu'], argv['memory'])
            # release mac address
            self.netmanager.release_mac(argv['mac'], argv['uuid'])
            return False, 'Define error: %s' % e          
        return True, obj
예제 #2
0
class Domain(object):
    def __init__(self, obj):
        self.netmanager = NetManager()
        self.imagemanager = ImageManager()
        self._obj = obj
        try:
            self._conn = libvirt.open("qemu+ssh://%s/system" % self._obj.host.ipv4) 
        except:
            self._conn = None
        else:
            try:
                self._vm = self._conn.lookupByUUIDString(self._obj.uuid)
            except:
                self._vm = None

    def __getattr__(self, name):
        if hasattr(self._obj, name):
            return getattr(self._obj, name)
        if self._vm:
            return getattr(self._vm, name)

    def delete(self):
        if self.exists():
            try:
                self.destroy()
            except:
                pass
            self.undefine()
            res = self.imagemanager.archive_disk(self._obj.get_ceph(), self._obj.uuid)
            print  'archive res:', res

        if not self._obj.get_domain():
            archive = VmArchive()
            if archive.archiveFromVm(self._obj):
                self.netmanager.release_mac(archive.mac, archive.uuid)
                self._obj.host.release(self._obj.vcpu, self._obj.mem)
                self._obj.delete()
                return True
        return False

    def exists(self):
        if self._vm:
            return True
        return False

    def _get_createxml_argv(self):
        ceph = self._obj.get_ceph()
        argv = {}
        argv['name'] = self._obj.name
        argv['uuid'] = self._obj.uuid
        argv['memory'] = self._obj.mem
        argv['vcpu'] = self._obj.vcpu
        argv['ceph_uuid'] = ceph.uuid
        argv['ceph_pool'] = ceph.pool
        argv['diskname'] = self._obj.disk
        argv['ceph_host'] = ceph.host
        argv['ceph_port'] = ceph.port
        argv['mac'] = self._obj.mac
        argv['bridge'] = self._obj.br
        return argv

    def set_cpu(self, cpu):
        try:
            cpu = int(cpu)
        except:
            return False

        if self.status == 'running':
            return False

        if cpu == self._obj.vcpu:
            return True
        org_cpu = self._obj.vcpu

        xml = XMLEditor()
        xml.set_xml(self.XMLDesc())
        root = xml.get_root()
        try:
            root.getElementsByTagName('vcpu')[0].firstChild.data = cpu
        except:
            return False

        xmldesc = root.toxml()
        if self._obj.update_cpu(cpu):
            try:
                print xmldesc
                res = self._conn.defineXML(xmldesc)
            except Exception, e:
                self._obj.update_cpu(org_cpu)
            else:
                return True
        return False