Пример #1
0
def getOvfStartup(ovf):
    """
    Returns a schedule representing the startup order for a virtual
    appliance from an ovf.

    @param ovf: Ovf file
    @type ovf: DOM Document

    @rtype: dictionary
    @return: startup dictionary of domains
    """
    startupDict = dict(boot='',
                       entities=dict())

    systems = startupDict['entities']

    # Create a list of all startup sections
    startupSections = Ovf.getNodes(ovf, (Ovf.hasTagName, 'StartupSection'))

    # Create an entry in startup dictionary for each entry in Startup sections
    for section in startupSections:
        for item in section.getElementsByTagName('Item'):
            attributes = []
            for i in range(item.attributes.length):
                attr = item.attributes.item(i)
                if attr.name == 'ovf:id':
                    sysId = attr.value
                elif(attr.name == 'ovf:order' or
                   attr.name == 'ovf:startDelay'):
                    attributes.append((attr.name, attr.value))

            # Store attribute pairs in dicitonary
            virtualSys = dict(attributes)
            if not virtualSys.has_key('ovf:order'):
                virtualSys['ovf:order'] = '0'
            if not virtualSys.has_key('ovf:startDelay'):
                virtualSys['ovf:startDelay'] = '0'

            parentId = section.parentNode.getAttribute('ovf:id')
            if not systems.has_key(parentId):
                systems[parentId] = dict(systems=[])
            systems[parentId]['systems'].append(sysId)
            systems[sysId] = virtualSys

    # Create a default entry for each system not in a startup section
    for each in Ovf.getNodes(ovf, (Ovf.hasTagName, 'VirtualSystem')):
        sysId = each.getAttribute('ovf:id')

        # If parentNode is Envelope, set as root system
        # else, trace back and fill in missing parent info
        if each.parentNode.tagName == 'Envelope':
            startupDict['boot'] = sysId
            systems[sysId] = {'ovf:order':'0', 'ovf:startDelay':'0'}
        else:
            parent = each.parentNode
            parentId = parent.getAttribute('ovf:id')
            if systems.has_key(parentId) and \
                not systems.has_key(sysId):
                systems[parentId]['systems'].append(sysId)
                systems[sysId] = {'ovf:order':'0', 'ovf:startDelay':'0'}

            # set parent info
            if parent.parentNode.tagName == 'Envelope':
                startupDict['boot'] = parentId

            while(not systems.has_key(parentId)):
                systems[parentId] = {'systems':[]}
                systems[parentId]['systems'].append(sysId)
                systems[sysId] = {'ovf:order':'0', 'ovf:startDelay':'0'}

                # Increment, if not at root
                if parent.parentNode.tagName == 'Envelope':
                    startupDict['boot'] = parentId
                else:
                    parent = parent.parentNode
                    sysId = parentId
                    parentId = parent.getAttribute('ovf:id')

    return startupDict
Пример #2
0
def getOvfDomains(ovf, path, hypervisor=None, configId=None, envDirectory=None):
    """
    Returns a dictionary with all of the VirtualSystems in an ovf
    listed as keys with the libvirt domain, for the specified configuration,
    stored as the value.

    @param ovf: Ovf file
    @type ovf: DOM Document

    @param path: path to Ovf file
    @type path: String

    @param configId: configuration name
    @type configId: String

    @todo: needs work, very basic, assumes hypervisor type
    """
    domains = dict()
    #directory = os.path.abspath(path.rsplit("/", 1)[0])
    directory = path

    if configId == None:
        configId = Ovf.getDefaultConfiguration(ovf)
    else:
        if not Ovf.isConfiguration(ovf, configId):
            raise RuntimeError("OvfLibvirt.getOvfDomains: configuration " +
                                configId + " not found.")

    # Get Nodes
    references = Ovf.getElementsByTagName(ovf, 'References')
    diskSection = Ovf.getElementsByTagName(ovf, 'DiskSection')

    if len(references) is not 1:
        raise NotImplementedError("OvfLibvirt.getOvfDomain: Unable to locate" +
                                  " a single References node.")
    elif len(diskSection) is not 1:
        raise NotImplementedError("OvfLibvirt.getOvfDomain: Unable to locate" +
                                  " a single DiskSection node.")
    else:
        refs = references[0]
        disks = diskSection[0]

    # For each system, create libvirt domain description
    for system in Ovf.getNodes(ovf, (Ovf.hasTagName, 'VirtualSystem')):
        ovfId = system.getAttribute('ovf:id')

        # Get VirtualHardwareSection
        virtualHardwareSection = Ovf.getElementsByTagName(system,
                                                          'VirtualHardwareSection')

        if len(virtualHardwareSection) is not 1:
            raise NotImplementedError("OvfLibvirt.getOvfDomain: Unable to locate" +
                                      " a single VirtualHardwareSection node.")
        else:
            virtualHardware = virtualHardwareSection[0]

            #metadata
            name = nameElement(ovfId)

            #resources
            memory = memoryElement(getOvfMemory(virtualHardware, configId))
            vcpu = vcpuElement(getOvfVcpu(virtualHardware, configId))

            #domain
            if not hypervisor:
                hypervisor = OvfPlatform.getVsSystemType(system)
            else:
                hypervisor = hypervisor.lower()
            domainType = getDomainTypeForVsType(hypervisor)
            domain = domainElement(domainType)

            #boot
            bootElements(domain, hypervisor)

            #time
            clock = clockElement('utc')

            #features
            features = featuresElement(acpi=True)

            #life cycle
            onPowerOff = onPowerOffElement('destroy')
            onReboot = onRebootElement('restart')
            onCrash = onCrashElement('destroy')

            #devices - graphics
            graphics = graphicsElement('vnc', 'localhost', '-1')

            #devices - console
            console = consoleElement('pty', '0')

            #devices
            devices = devicesElement(graphics, console)

            #disks
            envFile = None
            if envDirectory:
                envFile = os.path.join(envDirectory, ovfId + '.iso')
            diskDicts = getOvfDisks(virtualHardware, directory, refs,
                                    disks, configId, envFile)
            for dsk in diskDicts:
                addDevice(devices, diskElement(dsk))

            #network
            netDicts = getOvfNetworks(virtualHardware, configId)
            for networkDict in netDicts:
                network = networkElement(networkDict)
                addDevice(devices, network)

            #document
            document = libvirtDocument(domain, name, memory, vcpu,
                                       clock, features, onPowerOff,
                                       onReboot, onCrash, devices)

        domains[ovfId] = Ovf.xmlString(document)

    # Delete any extra copies of disk images
    cleanupExtraDiskImages(directory, refs, disks)

    return domains