Skip to content

Commit

Permalink
Fix libvirt get_console_output for Python < 2.7
Browse files Browse the repository at this point in the history
Fixes bug 953410

Unit tests fail when using Python 2.6.  This fixes it.

Change-Id: Ic53d16a75f5e4e134a92f27597a77404dd78d0f6
  • Loading branch information
comstud committed Mar 12, 2012
1 parent e189962 commit 625c5e4
Showing 1 changed file with 31 additions and 10 deletions.
41 changes: 31 additions & 10 deletions nova/virt/libvirt/connection.py
Expand Up @@ -908,18 +908,39 @@ def get_console_output(self, instance):
xml = virt_dom.XMLDesc(0)
tree = ElementTree.fromstring(xml)

console_types = {}

# NOTE(comstud): We want to try 'file' types first, then try 'pty'
# types. We can't use Python 2.7 syntax of:
# tree.find("./devices/console[@type='file']/source")
# because we need to support 2.6.
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 the guest has a console logging to a file prefer to use that
node = tree.find("./devices/console[@type='file']/source")
if node is not None:
fpath = node.get("path")
return libvirt_utils.load_file(fpath)

# else if there is a PTY, then try to read latest data from that
node = tree.find("./devices/console[@type='pty']/source")
if node is None:
raise exception.Error(_("Guest does not have a console available"))
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
return libvirt_utils.load_file(path)

pty = node.get("path")
# Try 'pty' types
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:
raise exception.Error(_("Guest does not have a console available"))

console_log = os.path.join(FLAGS.instances_path, instance['name'],
'console.log')
Expand Down

0 comments on commit 625c5e4

Please sign in to comment.