def get_console_output(self, context, instance):
    virt_dom = self._lookup_by_name(instance.name)
    xml = virt_dom.XMLDesc(0)
    tree = etree.fromstring(xml)
    console_types = {}
    console_nodes = tree.findall('./devices/console')
    for console_node in console_nodes:
        console_type = console_node.get('type')
        console_types.setdefault(console_type, [])
        console_types[console_type].append(console_node)
    if console_types.get('file'):
        for file_console in console_types.get('file'):
            source_node = file_console.find('./source')
            if (source_node is None):
                continue
            path = source_node.get('path')
            if (not path):
                continue
            libvirt_utils.chown(path, os.getuid())
            with libvirt_utils.file_open(path, 'rb') as fp:
                (log_data, remaining) = utils.last_bytes(fp, MAX_CONSOLE_BYTES)
                if (remaining > 0):
                    LOG.info(_LI('Truncated console log returned, %d bytes ignored'), remaining, instance=instance)
                return log_data
    if console_types.get('pty'):
        for pty_console in console_types.get('pty'):
            source_node = pty_console.find('./source')
            if (source_node is None):
                continue
            pty = source_node.get('path')
            if (not pty):
                continue
            break
    else:
        msg = _('Guest does not have a console available')
        raise exception.NovaException(msg)
    self._chown_console_log_for_instance(instance)
    data = self._flush_libvirt_console(pty)
    console_log = self._get_console_log_path(instance)
    fpath = self._append_to_file(data, console_log)
    with libvirt_utils.file_open(fpath, 'rb') as fp:
        (log_data, remaining) = utils.last_bytes(fp, MAX_CONSOLE_BYTES)
        if (remaining > 0):
            LOG.info(_LI('Truncated console log returned, %d bytes ignored'), remaining, instance=instance)
        return log_data
def get_console_output(self, context, instance):
    virt_dom = self._lookup_by_name(instance.name)
    xml = virt_dom.XMLDesc(0)
    tree = etree.fromstring(xml)
    console_types = {}
    console_nodes = tree.findall('./devices/console')
    for console_node in console_nodes:
        console_type = console_node.get('type')
        console_types.setdefault(console_type, [])
        console_types[console_type].append(console_node)
    if console_types.get('file'):
        for file_console in console_types.get('file'):
            source_node = file_console.find('./source')
            if (source_node is None):
                continue
            path = source_node.get('path')
            if (not path):
                continue
            libvirt_utils.chown(path, os.getuid())
            with libvirt_utils.file_open(path, 'rb') as fp:
                (log_data, remaining) = utils.last_bytes(fp, MAX_CONSOLE_BYTES)
                if (remaining > 0):
                    LOG.info(_LI('Truncated console log returned, %d bytes ignored'), remaining, instance=instance)
                return log_data
    if console_types.get('pty'):
        for pty_console in console_types.get('pty'):
            source_node = pty_console.find('./source')
            if (source_node is None):
                continue
            pty = source_node.get('path')
            if (not pty):
                continue
            break
    else:
        msg = _('Guest does not have a console available')
        raise exception.NovaException(msg)
    self._chown_console_log_for_instance(instance)
    data = self._flush_libvirt_console(pty)
    console_log = self._get_console_log_path(instance)
    fpath = self._append_to_file(data, console_log)
    with libvirt_utils.file_open(fpath, 'rb') as fp:
        (log_data, remaining) = utils.last_bytes(fp, MAX_CONSOLE_BYTES)
        if (remaining > 0):
            LOG.info(_LI('Truncated console log returned, %d bytes ignored'), remaining, instance=instance)
        return log_data