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.
24 # Helper function to ping all the nodes.
25 def ping_nodes (nodes):
26 for node in nodes.values():
29 # Helper function to get node objects from a list of node names.
30 def get_nodes_by_name (all_nodes, names, all):
32 return map (lambda name : get_node_by_name (all_nodes, name), names)
34 return all_nodes.values()
36 def get_node_by_name (nodes, name):
40 sys.exit ("error: node does not exist: %s" % name)
42 # Get separate list of running and inactive guests.
43 def get_all_guests (c, nodes):
47 # Find running guests.
49 # Get the flat list of guests from this node:
51 # Store which node the guest is running on, and turn list into a map.
55 sys.exit ("error: virtual machine %s is running on two nodes!" % name)
56 running[name] = { 'dom' : dom, 'node' : node }
58 # Find inactive guests (XML configuration files).
59 for name in get_guest_configs (c, nodes):
60 if name not in running:
63 return running, inactive
65 # Get the names of guests from the XML configuration files in xmls_dir.
66 def get_guest_configs (c, nodes):
68 for filename in sorted (os.listdir (c['xmls_dir'])):
69 m = re.search (r'^(.*)\.xml$', filename)
71 names.append (m.group (1))
74 # Convert virDomainState to string.
75 # Copied from virt-manager.
76 def pretty_run_status (status):
77 if status == libvirt.VIR_DOMAIN_RUNNING:
79 elif status == libvirt.VIR_DOMAIN_PAUSED:
81 elif status == libvirt.VIR_DOMAIN_SHUTDOWN:
82 return "shutting down"
83 elif status == libvirt.VIR_DOMAIN_SHUTOFF:
85 elif status == libvirt.VIR_DOMAIN_CRASHED:
87 elif status == libvirt.VIR_DOMAIN_PMSUSPENDED:
90 # Start a guest running on a given node. The node must not be
91 # running anywhere already.
92 def start_guest (c, node, guest_name):
93 fp = open ("%s/%s.xml" % (c['xmls_dir'], guest_name), "r")
97 conn = node.get_connection()
100 def pick_any_node_which_is_up (nodes):
102 for n in nodes.values():
107 sys.exit ("error: no nodes are up, use mclu wake [node|--all]")