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.
26 def __init__ (self, name, host, mac, uri):
32 def ping (self, force = False):
33 """Test if node is up."""
35 if hasattr (self, 'up'):
38 devnull = open (os.devnull, "w")
39 r = subprocess.call (["ping", "-c", "1", self.host],
40 stdout = devnull, stderr = devnull)
45 """Test if we can open an SSH connection"""
47 devnull = open (os.devnull, "w")
48 r = subprocess.call ([config.SSH, "-l", "root", self.host, ":"],
49 stdout = devnull, stderr = devnull)
54 def libvirt_ping (self):
55 """Test if we can open a libvirt connection"""
57 conn = libvirt.openReadOnly (self.uri)
64 devnull = open (os.devnull, "w")
65 subprocess.check_call ([config.WOL, self.mac],
66 stdout = devnull, stderr = devnull)
70 devnull = open (os.devnull, "w")
71 r = subprocess.call ([config.SSH, "-l", "root", self.host,
73 stdout = devnull, stderr = devnull)
74 # returns a non-zero exit status, for unknown reasons
77 """Return the list of VMs running on this node"""
80 conn = self.get_connection ()
81 for id in conn.listDomainsID():
83 dom = conn.lookupByID (id)
89 def get_connection (self):
90 """Return cached libvirt connection, fail if not possible."""
92 sys.exit ("error: node %s is not awake" % self.name)
93 if hasattr (self, 'conn'):
95 conn = libvirt.open (self.uri)
97 sys.exit ("error: could not open a libvirt connection to %s (URI: %s)" %
98 (self.host, self.uri))