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