X-Git-Url: http://git.annexia.org/?a=blobdiff_plain;f=lib.py;h=1430f147c8d53c904575a4d0c2fb2588fbef9c6d;hb=1c09f2768075322fcd4d71d71fc8cda536a99223;hp=a4981da939ffaf4c5d70be7b02f68777b9b5fac1;hpb=8f509fea52826d18df504f18ad0e702f54320f48;p=mclu.git diff --git a/lib.py b/lib.py index a4981da..1430f14 100644 --- a/lib.py +++ b/lib.py @@ -19,51 +19,43 @@ 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) @@ -71,38 +63,32 @@ def get_guest_configs (c, nodes): 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 wake [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