import sys
import os
import re
-import libvirt
-
-# Helper function to ping all the nodes.
-def ping_nodes (nodes):
- for node in nodes.values():
- node.ping ()
-# Helper function to get node objects from a list of node names.
-def get_nodes_by_name (all_nodes, names, all):
- if not all:
- return map (lambda name : get_node_by_name (all_nodes, name), names)
- else:
- return all_nodes.values()
-
-def get_node_by_name (nodes, name):
- if name in nodes:
- return nodes[name]
- else:
- sys.exit ("error: node does not exist: %s" % name)
+import libvirt
+import ansible.runner
+import ansible.inventory
# Get separate list of running and inactive guests.
-def get_all_guests (c, nodes):
+def get_all_guests (c):
running = {}
inactive = {}
# Find running guests.
- for node in nodes:
+ runner = ansible.runner.Runner (
+ remote_user = 'root',
+ module_name = 'virt',
+ module_args = 'command=list_vms', # XXX ignore state=shutdown
+ pattern = c['nodes_group'],
+ )
+ data = runner.run ()
+
+ for node in data['contacted'].keys():
# Get the flat list of guests from this node:
- doms = node.guests ()
+ vms = data['contacted'][node]['list_vms']
# Store which node the guest is running on, and turn list into a map.
- for dom in doms:
- name = dom.name()
- if name in running:
- sys.exit ("error: virtual machine %s is running on two nodes!" % name)
- running[name] = { 'dom' : dom, 'node' : node }
+ for vm in vms:
+ if node in running:
+ sys.exit ("error: virtual machine %s is running on two nodes!" % node)
+ running[vm] = { 'vm' : vm, 'node' : node }
# Find inactive guests (XML configuration files).
- for name in get_guest_configs (c, nodes):
+ for name in get_guest_configs (c):
if name not in running:
inactive[name] = name
return running, inactive
# Get the names of guests from the XML configuration files in xmls_dir.
-def get_guest_configs (c, nodes):
+def get_guest_configs (c):
names = []
for filename in sorted (os.listdir (c['xmls_dir'])):
m = re.search (r'^(.*)\.xml$', filename)
names.append (m.group (1))
return names
-# Convert virDomainState to string.
-# Copied from virt-manager.
-def pretty_run_status (status):
- if status == libvirt.VIR_DOMAIN_RUNNING:
- return "running"
- elif status == libvirt.VIR_DOMAIN_PAUSED:
- return "paused"
- elif status == libvirt.VIR_DOMAIN_SHUTDOWN:
- return "shutting down"
- elif status == libvirt.VIR_DOMAIN_SHUTOFF:
- return "shutoff"
- elif status == libvirt.VIR_DOMAIN_CRASHED:
- return "crashed"
- elif status == libvirt.VIR_DOMAIN_PMSUSPENDED:
- return "suspended"
-
# Start a guest running on a given node. The node must not be
# running anywhere already.
-def start_guest (c, node, guest_name):
+def start_guest (c, node_name, guest_name):
fp = open ("%s/%s.xml" % (c['xmls_dir'], guest_name), "r")
xml = fp.read ()
fp.close ()
- conn = node.get_connection()
+ conn = libvirt.open (uri_of_node (node_name))
+ if conn == None:
+ sys.exit ("error: could not open a libvirt connection to %s" %
+ node_name)
conn.createXML (xml)
-def pick_any_node_which_is_up (nodes):
- node = None
- for n in nodes.values():
- if n.ping ():
- node = n
- break
- if not node:
- sys.exit ("error: no nodes are up, use mclu on [node|--all]")
- return node
+def pick_any_node_which_is_up (c):
+ inventory = ansible.inventory.Inventory ()
+ runner = ansible.runner.Runner (
+ remote_user = 'root',
+ module_name = 'ping',
+ inventory = inventory,
+ pattern = c['nodes_group'],
+ )
+ data = runner.run ()
+ if len (data['contacted']) == 0:
+ sys.exit ("error: no nodes are up, use mclu on")
+ return data['contacted'].keys()[0]
+
+# XXX Make this configurable.
+def uri_of_node (node_name):
+ return "qemu+ssh://root@%s/system" % node_name