3 # Copyright (C) 2014 Red Hat Inc.
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 import ansible.inventory
27 # Get separate list of running and inactive guests.
28 def get_all_guests (c):
32 # Find running guests.
33 runner = ansible.runner.Runner (
36 module_args = 'command=list_vms', # XXX ignore state=shutdown
37 pattern = c['nodes_group'],
41 for node in data['contacted'].keys():
42 # Get the flat list of guests from this node:
43 vms = data['contacted'][node]['list_vms']
44 # Store which node the guest is running on, and turn list into a map.
47 sys.exit ("error: virtual machine %s is running on two nodes!" % node)
48 running[vm] = { 'vm' : vm, 'node' : node }
50 # Find inactive guests (XML configuration files).
51 for name in get_guest_configs (c):
52 if name not in running:
55 return running, inactive
57 # Get the names of guests from the XML configuration files in xmls_dir.
58 def get_guest_configs (c):
60 for filename in sorted (os.listdir (c['xmls_dir'])):
61 m = re.search (r'^(.*)\.xml$', filename)
63 names.append (m.group (1))
66 # Start a guest running on a given node. The node must not be
67 # running anywhere already.
68 def start_guest (c, node_name, guest_name):
69 fp = open ("%s/%s.xml" % (c['xmls_dir'], guest_name), "r")
73 conn = libvirt.open (uri_of_node (node_name))
75 sys.exit ("error: could not open a libvirt connection to %s" %
79 def pick_any_node_which_is_up (c):
80 inventory = ansible.inventory.Inventory ()
81 runner = ansible.runner.Runner (
84 inventory = inventory,
85 pattern = c['nodes_group'],
88 if len (data['contacted']) == 0:
89 sys.exit ("error: no nodes are up, use mclu on")
90 return data['contacted'].keys()[0]
92 # XXX Make this configurable.
93 def uri_of_node (node_name):
94 return "qemu+ssh://root@%s/system" % node_name