Rewrite to use ansible.
[mclu.git] / lib.py
1 #!/usr/bin/python
2 # mclu (mini cluster)
3 # Copyright (C) 2014 Red Hat Inc.
4 #
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.
9 #
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.
14 #
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.
18
19 import sys
20 import os
21 import re
22
23 import libvirt
24 import ansible.runner
25 import ansible.inventory
26
27 # Get separate list of running and inactive guests.
28 def get_all_guests (c):
29     running = {}
30     inactive = {}
31
32     # Find running guests.
33     runner = ansible.runner.Runner (
34         remote_user = 'root',
35         module_name = 'virt',
36         module_args = 'command=list_vms', # XXX ignore state=shutdown
37         pattern = c['nodes_group'],
38     )
39     data = runner.run ()
40
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.
45         for vm in vms:
46             if node in running:
47                 sys.exit ("error: virtual machine %s is running on two nodes!" % node)
48             running[vm] = { 'vm' : vm, 'node' : node }
49
50     # Find inactive guests (XML configuration files).
51     for name in get_guest_configs (c):
52         if name not in running:
53             inactive[name] = name
54
55     return running, inactive
56
57 # Get the names of guests from the XML configuration files in xmls_dir.
58 def get_guest_configs (c):
59     names = []
60     for filename in sorted (os.listdir (c['xmls_dir'])):
61         m = re.search (r'^(.*)\.xml$', filename)
62         if m:
63             names.append (m.group (1))
64     return names
65
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")
70     xml = fp.read ()
71     fp.close ()
72
73     conn = libvirt.open (uri_of_node (node_name))
74     if conn == None:
75         sys.exit ("error: could not open a libvirt connection to %s" %
76                   node_name)
77     conn.createXML (xml)
78
79 def pick_any_node_which_is_up (c):
80     inventory = ansible.inventory.Inventory ()
81     runner = ansible.runner.Runner (
82         remote_user = 'root',
83         module_name = 'ping',
84         inventory = inventory,
85         pattern = c['nodes_group'],
86     )
87     data = runner.run ()
88     if len (data['contacted']) == 0:
89         sys.exit ("error: no nodes are up, use mclu on")
90     return data['contacted'].keys()[0]
91
92 # XXX Make this configurable.
93 def uri_of_node (node_name):
94     return "qemu+ssh://root@%s/system" % node_name