예제 #1
0
    def set_boot_device(self, bootdevice):
        LOG.debug('Set boot device called for %(domain)s with boot '
                  'device "%(bootdev)s"', {'domain': self.domain_name,
                                           'bootdev': bootdevice})
        device = SET_BOOT_DEVICES_MAP.get(bootdevice)
        if device is None:
            # Invalid data field in request
            return 0xcc

        try:
            with utils.libvirt_open(**self._conn_args) as conn:
                domain = utils.get_libvirt_domain(conn, self.domain_name)
                tree = ET.fromstring(domain.XMLDesc())

                for device_element in tree.findall('devices'):
                    for disk_element in device_element.findall('disk'):
                        for boot_ele in disk_element.findall('boot'):
                            disk_element.remove(boot_ele)
                for os_element in tree.findall('os'):
                    # Remove all "boot" elements
                    for boot_element in os_element.findall('boot'):
                        os_element.remove(boot_element)

                    # Add a new boot element with the request boot device
                    boot_element = ET.SubElement(os_element, 'boot')
                    boot_element.set('dev', device)

                conn.defineXML(ET.tostring(tree))
        except libvirt.libvirtError:
            LOG.error('Failed setting the boot device  %(bootdev)s for '
                      'domain %(domain)s', {'bootdev': device,
                                            'domain': self.domain_name})
            # Command not supported in present state
            return 0xd5
예제 #2
0
    def set_boot_device(self, bootdevice):
        LOG.debug('Set boot device called for %(domain)s with boot '
                  'device "%(bootdev)s"', {'domain': self.domain_name,
                                           'bootdev': bootdevice})
        device = SET_BOOT_DEVICES_MAP.get(bootdevice)
        if device is None:
            # Invalid data field in request
            return IPMI_INVALID_DATA

        try:
            with utils.libvirt_open(**self._conn_args) as conn:
                domain = utils.get_libvirt_domain(conn, self.domain_name)
                tree = ET.fromstring(domain.XMLDesc())

                # Remove all "boot" element under "devices"
                # They are mutually exclusive with "os/boot"
                for device_element in tree.findall('devices/*'):
                    self._remove_boot_elements(device_element)

                for os_element in tree.findall('os'):
                    # Remove all "boot" elements under "os"
                    self._remove_boot_elements(os_element)

                    # Add a new boot element with the request boot device
                    boot_element = ET.SubElement(os_element, 'boot')
                    boot_element.set('dev', device)

                conn.defineXML(ET.tostring(tree))
        except libvirt.libvirtError:
            LOG.error('Failed setting the boot device  %(bootdev)s for '
                      'domain %(domain)s', {'bootdev': device,
                                            'domain': self.domain_name})
            # Command failed, but let client to retry
            return IPMI_COMMAND_NODE_BUSY
예제 #3
0
    def set_boot_device(self, bootdevice):
        LOG.debug('Set boot device called for %(domain)s with boot '
                  'device "%(bootdev)s"', {'domain': self.domain_name,
                                           'bootdev': bootdevice})
        device = SET_BOOT_DEVICES_MAP.get(bootdevice)
        if device is None:
            # Invalid data field in request
            return 0xcc

        try:
            with utils.libvirt_open(**self._conn_args) as conn:
                domain = utils.get_libvirt_domain(conn, self.domain_name)
                tree = ET.fromstring(domain.XMLDesc())

                for os_element in tree.findall('os'):
                    # Remove all "boot" elements
                    for boot_element in os_element.findall('boot'):
                        os_element.remove(boot_element)

                    # Add a new boot element with the request boot device
                    boot_element = ET.SubElement(os_element, 'boot')
                    boot_element.set('dev', device)

                conn.defineXML(ET.tostring(tree))
        except libvirt.libvirtError:
            LOG.error('Failed setting the boot device  %(bootdev)s for '
                      'domain %(domain)s', {'bootdev': device,
                                            'domain': self.domain_name})
            # Command not supported in present state
            return 0xd5
예제 #4
0
    def activate_payload(self, request, session):
        """Connect VM serial console to the IPMI session

        :param request: IPMI request packet
        :type request: dict
        :param session: IPMI session
        :type session: ServerSession object
        :rtype: None
        """
        LOG.debug('Activate payload called for domain %s', self.domain_name)
        if self.activated:
            LOG.error('Error activating payload the domain %(domain)s. '
                      'Error: Payload already activated' %
                      {'domain': self.domain_name})
            return
        super(VirtualBMC, self).activate_payload(request, session)
        try:
            self._conn = utils.libvirt_open(**self._conn_args).get_conn()
            self._conn.domainEventRegister(lifecycle_callback, self)
            self._domain = utils.get_libvirt_domain(self._conn,
                                                    self.domain_name)
            self._state = self._domain.state(0)
            self._run_console = True
            self._sol_thread = threading.Thread(target=self.loop)
            self._sol_thread.start()
        except libvirt.libvirtError as e:
            LOG.error('Error activating payload the domain %(domain)s. '
                      'Error: %(error)s' % {
                          'domain': self.domain_name,
                          'error': e
                      })
            super(VirtualBMC, self).deactivate_payload(request, session)
예제 #5
0
 def get_boot_device(self):
     LOG.debug('Get boot device called for %s', self.domain_name)
     with utils.libvirt_open(readonly=True, **self._conn_args) as conn:
         domain = utils.get_libvirt_domain(conn, self.domain_name)
         boot_element = ET.fromstring(domain.XMLDesc()).find('.//os/boot')
         boot_dev = None
         if boot_element is not None:
             boot_dev = boot_element.attrib.get('dev')
         return GET_BOOT_DEVICES_MAP.get(boot_dev, 0)
예제 #6
0
 def get_boot_device(self):
     LOG.debug('Get boot device called for %s', self.domain_name)
     with utils.libvirt_open(readonly=True, **self._conn_args) as conn:
         domain = utils.get_libvirt_domain(conn, self.domain_name)
         boot_element = ET.fromstring(domain.XMLDesc()).find('.//os/boot')
         boot_dev = None
         if boot_element is not None:
             boot_dev = boot_element.attrib.get('dev')
         return GET_BOOT_DEVICES_MAP.get(boot_dev, 0)
예제 #7
0
    def set_boot_device(self, bootdevice):
        LOG.debug('Set boot device called for %(domain)s with boot '
                  'device "%(bootdev)s"', {'domain': self.domain_name,
                                           'bootdev': bootdevice})
        device = SET_BOOT_DEVICES_MAP.get(bootdevice)
        if device is None:
            # Invalid data field in request
            return IPMI_INVALID_DATA

        if CONF['default']['kubevirt'] != 'true':
            try:
                with utils.libvirt_open(**self._conn_args) as conn:
                    domain = utils.get_libvirt_domain(conn, self.domain_name)
                    tree = ET.fromstring(domain.XMLDesc())

                    # Remove all "boot" element under "devices"
                    # They are mutually exclusive with "os/boot"
                    for device_element in tree.findall('devices/*'):
                        self._remove_boot_elements(device_element)

                    for os_element in tree.findall('os'):
                        # Remove all "boot" elements under "os"
                        self._remove_boot_elements(os_element)

                        # Add a new boot element with the request boot device
                        boot_element = ET.SubElement(os_element, 'boot')
                        boot_element.set('dev', device)

                    # conn.defineXML can't take bytes but
                    # in py3 ET.tostring returns bytes unless "unicode"
                    # in py2 "unicode" is unknown so specify "utf8" instead
                    enc = sys.version_info[0] < 3 and "utf8" or "unicode"
                    conn.defineXML(ET.tostring(tree, encoding=enc))
                    self.get_boot_device()
            except libvirt.libvirtError:
                LOG.error('Failed setting the boot device  %(bootdev)s for '
                          'domain %(domain)s', {'bootdev': device,
                                                'domain': self.domain_name})
                # Command failed, but let client to retry
                return IPMI_COMMAND_NODE_BUSY
        else:
            api = get_client()
            vm = api.read_namespaced_virtual_machine(self.name,self.namespace)
            self._remove_boot_order(vm.spec.template.spec.domain.devices.disks)
            self._remove_boot_order(vm.spec.template.spec.domain.devices.interfaces)

            if device == 'network':
                self._set_boot_order(vm.spec.template.spec.domain.devices.interfaces)

            if device == 'hd':
                self._set_boot_order(vm.spec.template.spec.domain.devices.disks)

            api.replace_namespaced_virtual_machine(vm,self.namespace,self.name)

            return IPMI_COMMAND_NODE_BUSY
예제 #8
0
 def is_active(self):
     try:
         with utils.libvirt_open(**self._conn_args) as conn:
             domain = utils.get_libvirt_domain(conn, self.domain_name)
             return domain.isActive()
     except libvirt.libvirtError as e:
         LOG.error('Error checking domain %(domain)s is alive. '
                   'Error: %(error)s' % {
                       'domain': self.domain_name,
                       'error': e
                   })
예제 #9
0
 def pulse_diag(self):
     LOG.debug('Power diag called for domain %s', self.domain_name)
     try:
         with utils.libvirt_open(**self._conn_args) as conn:
             domain = utils.get_libvirt_domain(conn, self.domain_name)
             if domain.isActive():
                 domain.injectNMI()
     except libvirt.libvirtError as e:
         LOG.error('Error powering diag the domain %(domain)s. '
                   'Error: %(error)s' % {'domain': self.domain_name,
                                         'error': e})
         # Command not supported in present state
         return 0xd5
예제 #10
0
 def power_shutdown(self):
     LOG.debug('Soft power off called for domain %s', self.domain_name)
     try:
         with utils.libvirt_open(**self._conn_args) as conn:
             domain = utils.get_libvirt_domain(conn, self.domain_name)
             if domain.isActive():
                 domain.shutdown()
     except libvirt.libvirtError as e:
         LOG.error('Error soft powering off the domain %(domain)s. '
                   'Error: %(error)s' % {'domain': self.domain_name,
                                         'error': e})
         # Command not supported in present state
         return 0xd5
예제 #11
0
 def power_reset(self):
     LOG.debug('Soft power reset called for domain %s', self.domain_name)
     try:
         with utils.libvirt_open(**self._conn_args) as conn:
             domain = utils.get_libvirt_domain(conn, self.domain_name)
             if domain.isActive():
                 domain.destroy()
                 domain.create()
     except libvirt.libvirtError as e:
         LOG.error('Error soft powering reset the domain %(domain)s. '
                   'Error: %(error)s' % {'domain': self.domain_name,
                                         'error': e})
         return 0xd5
예제 #12
0
 def power_shutdown(self):
     LOG.debug('Soft power off called for domain %s', self.domain_name)
     try:
         with utils.libvirt_open(**self._conn_args) as conn:
             domain = utils.get_libvirt_domain(conn, self.domain_name)
             if domain.isActive():
                 domain.shutdown()
     except libvirt.libvirtError as e:
         LOG.error('Error soft powering off the domain %(domain)s. '
                   'Error: %(error)s' % {'domain': self.domain_name,
                                         'error': e})
         # Command failed, but let client to retry
         return IPMI_COMMAND_NODE_BUSY
예제 #13
0
 def get_boot_device(self):
     LOG.debug('Get boot device called for %(domain)s',
               {'domain': self.domain_name})
     if CONF['default']['kubevirt'] != 'true':
         with utils.libvirt_open(readonly=True, **self._conn_args) as conn:
             domain = utils.get_libvirt_domain(conn, self.domain_name)
             boot_element = ET.fromstring(domain.XMLDesc()).find('.//os/boot')
             boot_dev = None
             if boot_element is not None:
                 boot_dev = boot_element.attrib.get('dev')
             return GET_BOOT_DEVICES_MAP.get(boot_dev, 0)
     else:
             return IPMI_COMMAND_NODE_BUSY
예제 #14
0
 def power_reset(self):
     LOG.debug('Power reset called for domain %s', self.domain_name)
     try:
         with utils.libvirt_open(**self._conn_args) as conn:
             domain = utils.get_libvirt_domain(conn, self.domain_name)
             if domain.isActive():
                 domain.reset()
     except libvirt.libvirtError as e:
         LOG.error('Error reseting the domain %(domain)s. '
                   'Error: %(error)s' % {'domain': self.domain_name,
                                         'error': e})
         # Command not supported in present state
         return 0xd5
예제 #15
0
 def pulse_diag(self):
     LOG.debug('Power diag called for domain %s', self.domain_name)
     try:
         with utils.libvirt_open(**self._conn_args) as conn:
             domain = utils.get_libvirt_domain(conn, self.domain_name)
             if domain.isActive():
                 domain.injectNMI()
     except libvirt.libvirtError as e:
         LOG.error('Error powering diag the domain %(domain)s. '
                   'Error: %(error)s' % {'domain': self.domain_name,
                                         'error': e})
         # Command failed, but let client to retry
         return IPMI_COMMAND_NODE_BUSY
예제 #16
0
 def pulse_diag(self):
     LOG.debug('Power diag called for domain %(domain)s',
               {'domain': self.domain_name})
     try:
         with utils.libvirt_open(**self._conn_args) as conn:
             domain = utils.get_libvirt_domain(conn, self.domain_name)
             if domain.isActive():
                 domain.injectNMI()
     except libvirt.libvirtError as e:
         LOG.error('Error powering diag the domain %(domain)s. '
                   'Error: %(error)s', {'domain': self.domain_name,
                                        'error': e})
         # Command failed, but let client to retry
         return IPMI_COMMAND_NODE_BUSY
예제 #17
0
 def power_shutdown(self):
     LOG.debug('Soft power off called for domain %(domain)s',
               {'domain': self.domain_name})
     try:
         with utils.libvirt_open(**self._conn_args) as conn:
             domain = utils.get_libvirt_domain(conn, self.domain_name)
             if domain.isActive():
                 domain.shutdown()
     except libvirt.libvirtError as e:
         LOG.error('Error soft powering off the domain %(domain)s. '
                   'Error: %(error)s', {'domain': self.domain_name,
                                        'error': e})
         # Command failed, but let client to retry
         return IPMI_COMMAND_NODE_BUSY
예제 #18
0
 def power_on(self):
     LOG.debug('Power on called for domain %(domain)s',
               {'domain': self.domain_name})
     try:
         with utils.libvirt_open(**self._conn_args) as conn:
             domain = utils.get_libvirt_domain(conn, self.domain_name)
             if not domain.isActive():
                 domain.create()
     except libvirt.libvirtError as e:
         LOG.error('Error powering on the domain %(domain)s. '
                   'Error: %(error)s', {'domain': self.domain_name,
                                        'error': e})
         # Command failed, but let client to retry
         return IPMI_COMMAND_NODE_BUSY
예제 #19
0
 def power_reset(self):
     LOG.debug('Power reset called for domain %(domain)s',
               {'domain': self.domain_name})
     try:
         with utils.libvirt_open(**self._conn_args) as conn:
             domain = utils.get_libvirt_domain(conn, self.domain_name)
             if domain.isActive():
                 domain.reset()
     except libvirt.libvirtError as e:
         LOG.error('Error reseting the domain %(domain)s. '
                   'Error: %(error)s', {'domain': self.domain_name,
                                        'error': e})
         # Command not supported in present state
         return IPMI_COMMAND_NODE_BUSY
예제 #20
0
 def power_reset(self):
     LOG.debug('Soft power reset called for domain %s', self.domain_name)
     try:
         with utils.libvirt_open(**self._conn_args) as conn:
             domain = utils.get_libvirt_domain(conn, self.domain_name)
             if domain.isActive():
                 domain.destroy()
                 domain.create()
     except libvirt.libvirtError as e:
         LOG.error('Error soft powering reset the domain %(domain)s. '
                   'Error: %(error)s' % {
                       'domain': self.domain_name,
                       'error': e
                   })
         return 0xd5
예제 #21
0
 def power_shutdown(self):
     LOG.debug('Soft power off called for domain %s', self.domain_name)
     try:
         with utils.libvirt_open(**self._conn_args) as conn:
             domain = utils.get_libvirt_domain(conn, self.domain_name)
             if domain.isActive():
                 domain.shutdown()
     except libvirt.libvirtError as e:
         LOG.error('Error soft powering off the domain %(domain)s. '
                   'Error: %(error)s' % {
                       'domain': self.domain_name,
                       'error': e
                   })
         # Command not supported in present state
         return IPMI_COMMAND_NOT_SUPPORTED
예제 #22
0
    def get_power_state(self):
        LOG.debug('Get power state called for domain %s', self.domain_name)
        try:
            with utils.libvirt_open(readonly=True, **self._conn_args) as conn:
                domain = utils.get_libvirt_domain(conn, self.domain_name)
                if domain.isActive():
                    return POWERON
        except libvirt.libvirtError as e:
            LOG.error('Error getting the power state of domain %(domain)s. '
                      'Error: %(error)s', {'domain': self.domain_name,
                                           'error': e})
            # Command failed, but let client to retry
            return IPMI_COMMAND_NODE_BUSY

        return POWEROFF
예제 #23
0
    def get_power_state(self):
        LOG.debug('Get power state called for domain %s', self.domain_name)
        try:
            with utils.libvirt_open(readonly=True, **self._conn_args) as conn:
                domain = utils.get_libvirt_domain(conn, self.domain_name)
                if domain.isActive():
                    return POWERON
        except libvirt.libvirtError as e:
            LOG.error('Error getting the power state of domain %(domain)s. '
                      'Error: %(error)s', {'domain': self.domain_name,
                                           'error': e})
            # Command not supported in present state
            return 0xd5

        return POWEROFF
예제 #24
0
    def get_power_state(self):
        LOG.debug('Get power state called for domain %s', self.domain_name)
        try:
            with utils.libvirt_open(readonly=True, **self._conn_args) as conn:
                domain = utils.get_libvirt_domain(conn, self.domain_name)
                if domain.isActive():
                    return POWERON
        except libvirt.libvirtError as e:
            LOG.error('Error getting the power state of domain %(domain)s. '
                      'Error: %(error)s', {'domain': self.domain_name,
                                           'error': e})
            # Command not supported in present state
            return 0xd5

        return POWEROFF
예제 #25
0
    def get_power_state(self):
        LOG.debug('Get power state called for domain %(domain)s',
                  {'domain': self.domain_name})
        try:
            with utils.libvirt_open(readonly=True, **self._conn_args) as conn:
                domain = utils.get_libvirt_domain(conn, self.domain_name)
                if domain.isActive():
                    return POWERON
        except libvirt.libvirtError as e:
            msg = ('Error getting the power state of domain %(domain)s. '
                   'Error: %(error)s' % {'domain': self.domain_name,
                                         'error': e})
            LOG.error(msg)
            raise exception.VirtualBMCError(message=msg)

        return POWEROFF
예제 #26
0
 def pulse_diag(self):
     LOG.debug('Power diag called for domain %(domain)s',
               {'domain': self.domain_name})
     if CONF['default']['kubevirt'] != 'true':
         try:
             with utils.libvirt_open(**self._conn_args) as conn:
                 domain = utils.get_libvirt_domain(conn, self.domain_name)
                 if domain.isActive():
                     domain.injectNMI()
         except libvirt.libvirtError as e:
             LOG.error('Error powering diag the domain %(domain)s. '
                       'Error: %(error)s', {'domain': self.domain_name,
                                            'error': e})
             # Command failed, but let client to retry
             return IPMI_COMMAND_NODE_BUSY
     else:
         LOG.info('reset option not supported for domain %(domain)s', {'domain': self.domain_name} )
         return IPMI_COMMAND_NODE_BUSY
예제 #27
0
 def power_off(self):
     LOG.debug('Power off called for domain %(domain)s',
               {'domain': self.domain_name})
     if CONF['default']['kubevirt'] != 'true':
         try:
             with utils.libvirt_open(**self._conn_args) as conn:
                 domain = utils.get_libvirt_domain(conn, self.domain_name)
                 if domain.isActive():
                     domain.destroy()
         except libvirt.libvirtError as e:
             LOG.error('Error powering off the domain %(domain)s. '
                       'Error: %(error)s', {'domain': self.domain_name,
                                            'error': e})
             # Command failed, but let client to retry
             return IPMI_COMMAND_NODE_BUSY
     else:
         api = get_client()
         api.stop(self.namespace,self.name)
예제 #28
0
 def power_reset(self):
     LOG.debug('Power reset called for domain %(domain)s',
               {'domain': self.domain_name})
     if CONF['default']['kubevirt'] != 'true':
         try:
             with utils.libvirt_open(**self._conn_args) as conn:
                 domain = utils.get_libvirt_domain(conn, self.domain_name)
                 if domain.isActive():
                     domain.reset()
         except libvirt.libvirtError as e:
             LOG.error('Error reseting the domain %(domain)s. '
                       'Error: %(error)s', {'domain': self.domain_name,
                                            'error': e})
             # Command not supported in present state
             return IPMI_COMMAND_NODE_BUSY
     else:
         LOG.info('reset option not supported for domain %(domain)s', {'domain': self.domain_name} )
         return IPMI_COMMAND_NODE_BUSY
예제 #29
0
    def get_power_state(self):
        LOG.debug('Get power state called for domain %(domain)s',
                  {'domain': self.domain_name})

        if CONF['default']['kubevirt'] != 'true':
            try:
                with utils.libvirt_open(readonly=True, **self._conn_args) as conn:
                    domain = utils.get_libvirt_domain(conn, self.domain_name)
                    if domain.isActive():
                        return POWERON
            except libvirt.libvirtError as e:
                msg = ('Error getting the power state of domain %(domain)s. '
                       'Error: %(error)s' % {'domain': self.domain_name,
                                             'error': e})
                LOG.error(msg)
                raise exception.VirtualBMCError(message=msg)

            return POWEROFF
        else:
            LOG.info('power state option not supported for domain %(domain)s', {'domain': self.domain_name} )
            return IPMI_COMMAND_NODE_BUSY
예제 #30
0
    def set_boot_device(self, bootdevice):
        LOG.debug('Set boot device called for %(domain)s with boot '
                  'device "%(bootdev)s"', {'domain': self.domain_name,
                                           'bootdev': bootdevice})
        device = SET_BOOT_DEVICES_MAP.get(bootdevice)
        if device is None:
            # Invalid data field in request
            return IPMI_INVALID_DATA

        try:
            with utils.libvirt_open(**self._conn_args) as conn:
                domain = utils.get_libvirt_domain(conn, self.domain_name)
                tree = ET.fromstring(domain.XMLDesc())

                # Remove all "boot" element under "devices"
                # They are mutually exclusive with "os/boot"
                for device_element in tree.findall('devices/*'):
                    self._remove_boot_elements(device_element)

                for os_element in tree.findall('os'):
                    # Remove all "boot" elements under "os"
                    self._remove_boot_elements(os_element)

                    # Add a new boot element with the request boot device
                    boot_element = ET.SubElement(os_element, 'boot')
                    boot_element.set('dev', device)

                # conn.defineXML can't take bytes but
                # in py3 ET.tostring returns bytes unless "unicode"
                # in py2 "unicode" is unknown so specify "utf8" instead
                enc = sys.version_info[0] < 3 and "utf8" or "unicode"
                conn.defineXML(ET.tostring(tree, encoding=enc))
        except libvirt.libvirtError:
            LOG.error('Failed setting the boot device  %(bootdev)s for '
                      'domain %(domain)s', {'bootdev': device,
                                            'domain': self.domain_name})
            # Command failed, but let client to retry
            return IPMI_COMMAND_NODE_BUSY
예제 #31
0
    def _test_libvirt_open(self, mock_open, **kwargs):
        mock_open.return_value = self.fake_connection
        with utils.libvirt_open(self.uri, **kwargs) as conn:
            self.assertEqual(self.fake_connection, conn)

        self.fake_connection.close.assert_called_once_with()