Example #1
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"
        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()
Example #2
0
    def processProvisioning(provisioning):

        #go through all actions in provisioning class
        for action in provisioning.action:
            #translate action to actionModel
            actionModel = Translator.ActionToModel(action,"provisioning")
            actionModel.requestUser = threading.currentThread().requestUser
            if actionModel.type == "create":
                if Action.objects.filter (uuid = actionModel.uuid):
                    #if action already exists we raise exception. It shouldn't exist because it is create action!
                    try:
                        raise Exception
                    except Exception as e:
                        print "Action already exists"
                        print e
                        return                
                else:
                    actionModel.save()

                try:
                    Server = VTServer.objects.get(uuid =  action.server.uuid)
                    VMmodel = Translator.VMtoModel(action.server.virtual_machines[0], Server.aggregate_id, "save")
                    Server.vms.add(VMmodel)
                    actionModel.vm = VMmodel
                    actionModel.save()
                except Exception as e:
                    print "Not possible to translate to VM model\n"
                    print e
                    DatedMessage.objects.post_message_to_user(
                        "Not possible to translate VM %s to a proper app model" % VMmodel.name,
                        threading.currentThread().requestUser, msg_type=DatedMessage.TYPE_ERROR,
                    )
                    Server.vms.remove(VMmodel)
                    VMmodel.completeDelete()
                    return
                
                client = Server.aggregate.as_leaf_class().client
                
                ProvisioningDispatcher.connectAndSend('https://'+client.username+':'+client.password+'@'+client.url[8:], action)                

            elif actionModel.type == "delete" :

                #ProvisioningDispatcher.checkVMisPresent(action)
                VMmodel =  VM.objects.get(uuid = action.server.virtual_machines[0].uuid)
                if not  VMmodel:
                    try:
                        raise Exception
                    except Exception as e:
                        print "No VM found to start it\n"
                        print e
                        return
                    
                #ProvisioningDispatcher.checkActionIsPresent(actionModel)
                if Action.objects.filter(uuid = actionModel.uuid):
                    try:
                        raise Exception
                    except Exception as e:
                        print "Action already exists"
                        print e
                        return
                else:
                    print "ACTION delete is going to be saved"
                    actionModel.vm = VMmodel
                    actionModel.save()
                
                try:	
                    Server = VTServer.objects.get(uuid = VMmodel.getServerID() )
                    client = Server.aggregate.as_leaf_class().client
                    ProvisioningDispatcher.connectAndSend('https://'+client.username+':'+client.password+'@'+client.url[8:], action)  
                except:
                    print "Could not connect to AM"
                    print e
                    DatedMessage.objects.post_message_to_user(
                        "Could not connect to AM",
                        threading.currentThread().requestUser, msg_type=DatedMessage.TYPE_ERROR,
                    )


            #elif actionModel.type == "start":
            else:
                
                #ProvisioningDispatcher.checkVMisPresent(action)
                VMmodel = VM.objects.get(uuid = action.server.virtual_machines[0].uuid)
                    
                if not VMmodel:
                    try:
                        raise Exception
                    except Exception as e:
                        print "No VM found to start it\n"
                        print e
                        return
                #AGENT_URL = VTServer.objects.get(name = VMmodel.getServerID() ).getAgentURL()                
                #ProvisioningDispatcher.checkActionIsPresent(actionModel)            
                if Action.objects.filter (uuid = actionModel.uuid):
                    try:
                        raise Exception
                    except Exception as e:
                        print "Action already exists"
                        print e
                        return
                else:
                    actionModel.vm = VMmodel
                    actionModel.save()
               
		try: 
                    Server = VTServer.objects.get(uuid = VMmodel.getServerID() )
                    client = Server.aggregate.as_leaf_class().client
                    ProvisioningDispatcher.connectAndSend('https://'+client.username+':'+client.password+'@'+client.url[8:], action)                
                except:
                    print "Could not connect to AM"
                    print e
                    DatedMessage.objects.post_message_to_user(
                        "Could not connect to AM",
                        threading.currentThread().requestUser, msg_type=DatedMessage.TYPE_ERROR,
                    )