Exemplo n.º 1
0
    def removeISO(self):
        """Removes ISO attached to the disk drive of a VM"""

        # Import cdrom XML template
        cdrom_xml = ET.parse(DirectoryLocation.TEMPLATE_DIR + '/cdrom.xml')

        # Add iso image path to cdrom XML
        cdrom_xml = cdrom_xml.getroot()
        source_xml = cdrom_xml.find('source')

        if (source_xml is not None):
            cdrom_xml.remove(source_xml)
            cdrom_xml_string = ET.tostring(cdrom_xml,
                                           encoding='utf8',
                                           method='xml')

            # Update the libvirt cdrom device
            if (self.vm_object._getLibvirtDomainObject().updateDeviceFlags(
                    cdrom_xml_string)):
                raise LibvirtException(
                    'An error occurred whilst detaching ISO')
Exemplo n.º 2
0
    def delete(self):
        """Delete a network from the node"""
        # Ensure user has permission to manage networks
        self._get_registered_object('auth').assert_permission(
            PERMISSIONS.MANAGE_HOST_NETWORKS)

        # Ensure network is not connected to any VMs
        connected_vms = self._get_connected_virtual_machines()
        if len(connected_vms):
            connected_vm_name_string = ', '.join(vm.get_name()
                                                 for vm in connected_vms)
            raise NetworkUtilizedException(
                'Network \'%s\' cannot be removed as it is used by the following VMs: %s'
                % (self.get_name(), connected_vm_name_string))

        # Undefine object from libvirt
        try:
            self._get_libvirt_object().destroy()
            self._get_libvirt_object().undefine()
        except:
            raise LibvirtException('Failed to delete network from libvirt')

        # Update MCVirt config
        def update_config(config):
            del config['networks'][self.get_name()]

        from mcvirt.mcvirt_config import MCVirtConfig
        MCVirtConfig().update_config(
            update_config, 'Deleted network \'%s\'' % self.get_name())

        if self._is_cluster_master:

            def remove_remote(node):
                network_factory = node.get_connection('network_factory')
                network = network_factory.get_network_by_name(self.name)
                node.annotate_object(network)
                network.delete()

            cluster = self._get_registered_object('cluster')
            cluster.run_remote_command(remove_remote)
Exemplo n.º 3
0
    def attachISO(self, iso_object, live=False):
        """Attaches an ISO image to the disk drive of the VM"""
        iso_object = self._convert_remote_object(iso_object)

        # Ensure that the user has permissions to modifiy the VM
        self._get_registered_object('auth').assert_permission(
            PERMISSIONS.MODIFY_VM, self.vm_object)

        # Import cdrom XML template
        cdrom_xml = ET.parse(DirectoryLocation.TEMPLATE_DIR + '/cdrom.xml')

        # Add iso image path to cdrom XML
        cdrom_xml.find('source').set('file', iso_object.get_path())
        cdrom_xml_string = ET.tostring(cdrom_xml.getroot(),
                                       encoding='utf8',
                                       method='xml')

        flags = libvirt.VIR_DOMAIN_AFFECT_LIVE if live else 0

        # Update the libvirt cdrom device
        libvirt_object = self.vm_object._getLibvirtDomainObject()
        if libvirt_object.updateDeviceFlags(cdrom_xml_string, flags):
            raise LibvirtException('An error occurred whilst attaching ISO')
Exemplo n.º 4
0
    def create(self, name, physical_interface):
        """Create a network on the node"""
        # Ensure user has permission to manage networks
        self._get_registered_object('auth').assert_permission(PERMISSIONS.MANAGE_HOST_NETWORKS)

        # Validate name
        ArgumentValidator.validate_network_name(name)

        # Ensure network does not already exist
        if self.check_exists(name):
            raise NetworkAlreadyExistsException('Network already exists: %s' % name)

        # Ensure that the physical interface eixsts
        if not self._interface_exists(physical_interface):
            raise InterfaceDoesNotExist(
                'Physical interface %s does not exist' % physical_interface
            )

        # Create XML for network
        network_xml = ET.Element('network')
        network_xml.set('ipv6', 'no')
        network_name_xml = ET.SubElement(network_xml, 'name')
        network_name_xml.text = name

        # Create 'forward'
        network_forward_xml = ET.SubElement(network_xml, 'forward')
        network_forward_xml.set('mode', 'bridge')

        # Set interface bridge
        network_bridge_xml = ET.SubElement(network_xml, 'bridge')
        network_bridge_xml.set('name', physical_interface)

        # Convert XML object to string
        network_xml_string = ET.tostring(network_xml, encoding='utf8', method='xml')

        # Attempt to register network with LibVirt
        try:
            self._get_registered_object('libvirt_connector').get_connection(
            ).networkDefineXML(network_xml_string)
        except:
            raise LibvirtException('An error occurred whilst registering network with LibVirt')

        # Update MCVirt config
        def update_config(config):
            config['networks'][name] = physical_interface
        from mcvirt.mcvirt_config import MCVirtConfig
        MCVirtConfig().update_config(update_config, 'Created network \'%s\'' % name)

        # Obtain instance of the network object
        network_instance = self.get_network_by_name(name)

        # Start network
        network_instance._get_libvirt_object().create()

        # Set network to autostart
        network_instance._get_libvirt_object().setAutostart(True)

        if self._is_cluster_master:
            def remote_add(node):
                network_factory = node.get_connection('network_factory')
                network_factory.create(name, physical_interface)
            cluster = self._get_registered_object('cluster')
            cluster.run_remote_command(remote_add)

        return network_instance