mclu initial commit.
[mclu.git] / node.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 os
20 import subprocess
21 import libvirt
22
23 import config
24
25 class Node:
26     def __init__ (self, name, host, mac, uri):
27         self.name = name
28         self.host = host
29         self.mac = mac
30         self.uri = uri
31
32     def ping (self, force = False):
33         """Test if node is up."""
34         if not force:
35             if hasattr (self, 'up'):
36                 return self.up
37
38         devnull = open (os.devnull, "w")
39         r = subprocess.call (["ping", "-c", "1", self.host],
40                              stdout = devnull, stderr = devnull)
41         self.up = r == 0
42         return self.up
43
44     def ssh_ping (self):
45         """Test if we can open an SSH connection"""
46         if self.ping ():
47             devnull = open (os.devnull, "w")
48             r = subprocess.call ([config.SSH, "-l", "root", self.host, ":"],
49                                  stdout = devnull, stderr = devnull)
50             return r == 0
51         else:
52             return False
53
54     def libvirt_ping (self):
55         """Test if we can open a libvirt connection"""
56         if self.ping():
57             conn = libvirt.openReadOnly (self.uri)
58             return conn != None
59         else:
60             return False
61
62     def wake (self):
63         if not self.ping ():
64             devnull = open (os.devnull, "w")
65             subprocess.check_call ([config.WOL, self.mac],
66                                    stdout = devnull, stderr = devnull)
67
68     def shutdown (self):
69         if self.ping ():
70             devnull = open (os.devnull, "w")
71             r = subprocess.call ([config.SSH, "-l", "root", self.host,
72                                   "/sbin/poweroff"],
73                                  stdout = devnull, stderr = devnull)
74             # returns a non-zero exit status, for unknown reasons
75
76     def guests (self):
77         """Return the list of VMs running on this node"""
78         guests = []
79         if self.ping ():
80             conn = self.get_connection ()
81             for id in conn.listDomainsID():
82                 try:
83                     dom = conn.lookupByID (id)
84                 except:
85                     continue
86                 guests.append (dom)
87         return guests
88
89     def get_connection (self):
90         """Return cached libvirt connection, fail if not possible."""
91         if not self.ping ():
92             sys.exit ("error: node %s is not awake" % self.name)
93         if hasattr (self, 'conn'):
94             return self.conn
95         conn = libvirt.open (self.uri)
96         if conn == None:
97             sys.exit ("error: could not open a libvirt connection to %s (URI: %s)" %
98                       (self.host, self.uri))
99         self.conn = conn
100         return self.conn