Ejemplo n.º 1
0
def getPropertyDefaultValue(propertyNode, configuration):
    """
    Get the default value for a given Property node.  The value returned is
    based on this priority:
    1) value node for given configuration
    2) ovf:value attribute
    3) first Value node
    @type propertyNode: DOM node
    @param propertyNode: Property node
    @type configuration: String
    @param configuration: Configuration used to select the default value

    @rtype: String
    @return: Default value for node.  May be empty string.  None if no default
             was specified
    """
    if not configuration:
        docNode = propertyNode.parentNode
        while docNode.nodeType != Node.DOCUMENT_NODE:
            docNode = docNode.parentNode
            if not docNode:
                raise RuntimeError, "Unable to find document node"

        configuration = Ovf.getDefaultConfiguration(docNode)

    if configuration:
        valueNodes = Ovf.getChildNodes(propertyNode,
                                       (Ovf.hasTagName, 'Value'),
                                       (Ovf.hasAttribute, 'ovf:configuration',
                                        configuration))
        if valueNodes:
            attributes = Ovf.getAttributes(valueNodes[0])
            if attributes.has_key('ovf:value'):
                return attributes['ovf:value']

    attributes = Ovf.getAttributes(propertyNode)
    if attributes.has_key('ovf:value'):
        return attributes['ovf:value']

    valueNodes = Ovf.getChildNodes(propertyNode, (Ovf.hasTagName, 'Value'))
    if valueNodes:
        attributes = Ovf.getAttributes(valueNodes[0])
        if attributes.has_key('ovf:value'):
            return attributes['ovf:value']

    return None
Ejemplo n.º 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