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.
27 def __init__ (self, name, host, mac, uri):
35 def ping (self, force = False):
36 """Test if node is up."""
38 if self.up is not None:
41 devnull = open (os.devnull, "w")
42 r = subprocess.call (["ping", "-c", "1", self.host],
43 stdout = devnull, stderr = devnull)
48 """Test if we can open an SSH connection"""
50 devnull = open (os.devnull, "w")
51 r = subprocess.call ([config.SSH, "-l", "root", self.host, ":"],
52 stdout = devnull, stderr = devnull)
57 def libvirt_ping (self):
58 """Test if we can open a libvirt connection"""
60 conn = libvirt.openReadOnly (self.uri)
67 devnull = open (os.devnull, "w")
68 subprocess.check_call ([config.WOL, self.mac],
69 stdout = devnull, stderr = devnull)
73 devnull = open (os.devnull, "w")
74 r = subprocess.call ([config.SSH, "-l", "root", self.host,
76 stdout = devnull, stderr = devnull)
77 # returns a non-zero exit status, for unknown reasons
80 """Return the list of VMs running on this node"""
83 conn = self.get_connection ()
84 for id in conn.listDomainsID():
86 dom = conn.lookupByID (id)
92 def get_connection (self):
93 """Return cached libvirt connection, fail if not possible."""
95 sys.exit ("error: node %s is not awake" % self.name)
96 if self.conn is not None:
98 conn = libvirt.open (self.uri)
100 sys.exit ("error: could not open a libvirt connection to %s (URI: %s)" %
101 (self.host, self.uri))