コード例 #1
0
ファイル: vtAggregateController.py プロジェクト: cargious/ocf
def askForAggregateResources(vtPlugin, projectUUID='None', sliceUUID='None'):
    "asks the VT AM for all the resources under it."
    serversInAggregate = []
    try:
        client = xmlrpclib.Server('https://' + vtPlugin.client.username + ':' +
                                  vtPlugin.client.password + '@' +
                                  vtPlugin.client.url[8:])
    except Exception as e:
        print "Can't connect to server"
        print e
        return

    try:
        rHashObject = resourcesHash.objects.get(vtamID=vtPlugin.id,
                                                projectUUID=projectUUID,
                                                sliceUUID=sliceUUID)
    except:
        rHashObject = resourcesHash(hashValue='0',
                                    vtamID=vtPlugin.id,
                                    projectUUID=projectUUID,
                                    sliceUUID=sliceUUID)
        rHashObject.save()
    try:
        remoteHashValue, resourcesString = client.listResources(
            rHashObject.hashValue, projectUUID, sliceUUID)
        print remoteHashValue
    except Exception as e:
        print "Can't retrieve resources"
        print e
        return

    if remoteHashValue == rHashObject.hashValue:
        print "Same HASH, no changes in resources"
        return
    else:
        print remoteHashValue
        oldHashValue = rHashObject.hashValue
        rHashObject.hashValue = remoteHashValue
        rHashObject.save()
        try:
            xmlClass = XmlHelper.parseXmlString(resourcesString)
        except Exception as e:
            print "Can't parse rspec"
            print e
            return
        try:
            for server in xmlClass.response.information.resources.server:
                vmsInAggregate = []
                for vm in server.virtual_machine:
                    #Translate and register VM present in the AM
                    VMmodel = Translator.VMtoModel(vm,
                                                   vtPlugin.id,
                                                   save="save")
                    Translator.PopulateNewVMifaces(vm, VMmodel)
                    vmsInAggregate.append(VMmodel.uuid)
                #Translate The whole server with the vms updated. There may be still VMs in the models
                #which are not in the AM
                serverModel = Translator.ServerClassToModel(
                    server, vtPlugin.id)
                # Delete VMs in the model that are not in the linked to the server in the AM
                for vmuuid in serverModel.vms.filter(
                        sliceId=sliceUUID,
                        projectId=projectUUID).values_list('uuid', flat=True):
                    if vmuuid not in vmsInAggregate:
                        vmToDelete = VM.objects.get(uuid=vmuuid)
                        serverModel.vms.remove(vmToDelete)
                        vmToDelete.completeDelete()
                serversInAggregate.append(server.uuid)
                #Update VMs in the server model
                serverModel.setVMs()
            serversInExpedient = VTServer.objects.filter(
                aggregate=vtPlugin.id).values_list('uuid', flat=True)
            #Delete servers in Expedient that are not any more in the AM. VMs in that server included
            for s in serversInExpedient:
                if s not in serversInAggregate:
                    delServer = VTServer.objects.get(uuid=s)
                    delServer.completeDelete()

            return xmlClass

        except Exception as e:
            print e
            rHashObject.hashValue = oldHashValue
            rHashObject.save()
コード例 #2
0
def askForAggregateResources(vtPlugin, projectUUID = 'None', sliceUUID = 'None'):
    "asks the VT AM for all the resources under it."
    serversInAggregate = []
    try:
        client = xmlrpclib.Server('https://'+vtPlugin.client.username+':'+vtPlugin.client.password+'@'+vtPlugin.client.url[8:])
    except Exception as e:
        print "Can't connect to server: %s" % str(e)
        return
    
    try:
        rHashObject =  resourcesHash.objects.get(vtamID = vtPlugin.id, projectUUID = projectUUID, sliceUUID = sliceUUID)
    except:
        rHashObject = resourcesHash(hashValue = '0', vtamID = vtPlugin.id, projectUUID= projectUUID, sliceUUID = sliceUUID)
        rHashObject.save()
    try:
        remoteHashValue ,resourcesString = client.listResources(rHashObject.hashValue, projectUUID, sliceUUID)
        print remoteHashValue
    except Exception as e:
        print "Can't retrieve resources: %s" % str(e)
        return

    if remoteHashValue == rHashObject.hashValue:
        print "Same HASH, no changes in resources"
        return
    else:
        print remoteHashValue
        oldHashValue = rHashObject.hashValue
        rHashObject.hashValue = remoteHashValue
        rHashObject.save() 
        try:
            xmlClass = XmlHelper.parseXmlString(resourcesString)
        except Exception as e:
            print "Can't parse rspec: %s" % str(e)
            return
        try:
            for server in xmlClass.response.information.resources.server:
                vmsInAggregate = []
                for vm in server.virtual_machine:
                    #Translate and register VM present in the AM
                    VMmodel = Translator.VMtoModel(vm, vtPlugin.id, save="save")
                    Translator.PopulateNewVMifaces(vm, VMmodel)
                    vmsInAggregate.append(VMmodel.uuid)
                #Translate The whole server with the vms updated. There may be still VMs in the models
                #which are not in the AM
                serverModel = Translator.ServerClassToModel(server, vtPlugin.id)
                # Delete VMs in the model that are not in the linked to the server in the AM
                for vmuuid in serverModel.vms.filter(sliceId=sliceUUID, projectId=projectUUID).values_list('uuid', flat=True):                

                    try:
                        action_present = False
                        action_create =  Action.objects.get(vm__uuid=vmuuid)
                        if action_create.type=="create" and action_create.status=="SUCCESS":
                            action_present = True
                    except:
                        print "VM Manager returned a VM which has not provisioning create action associated"
                    if vmuuid not in vmsInAggregate and action_present:
                        vmToDelete = VM.objects.get(uuid=vmuuid)
                        serverModel.vms.remove(vmToDelete)
                        vmToDelete.completeDelete() 
                serversInAggregate.append(server.uuid)
                #Update VMs in the server model
                serverModel.setVMs()
            serversInExpedient  = VTServer.objects.filter(aggregate=vtPlugin.id).values_list('uuid', flat=True)
            #Delete servers in Expedient that are not any more in the AM. VMs in that server included
            for s in serversInExpedient:
                if s not in serversInAggregate:
                    delServer = VTServer.objects.get(uuid = s)
                    delServer.completeDelete()
                
            return xmlClass


        except Exception as e:
            print e
            rHashObject.hashValue = oldHashValue
            rHashObject.save()