#!/usr/bin/env python # # $Id: lavahosts_1_0.py 147 2009-02-24 09:37:05Z mkchew $ # # Copyright 2007 Platform Computing Inc # # This program is free software; you can redistribute it and/or modify # it under the terms of version 2 of the GNU General Public License as # published by the Free Software Foundation. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA # from kusu.genconfig import Report # # This will generate the contents of the Lava hosts file. # It reads the existing file and adds any new hosts. # import os import string class thisReport(Report): def toolHelp(self): print self.gettext("genconfig_Hosts_Help") def haveLsf(self): path = "/etc/lava/conf" return os.path.exists(path) def getHost(self): filename = "/etc/lava/conf/hosts" if not os.path.exists(filename): return fp = file(filename, 'r') data = [] while True: line = fp.readline().strip() if len(line) == 0: break if line[0] == '#': continue data.append(line.split(' ')[0]) #print "Found %s\n" % line.split(' ')[0] fp.close() return data def listHosts(self): # List the contents of the lava hosts file filename = "/etc/lava/conf/hosts" if not os.path.exists(filename): return fp = file(filename, 'r') data = [] while True: line = fp.readline() if len(line) == 0: break print line, fp.close() return data def printLine(self, ip, name, suffix, domain): if suffix: print "%s %s %s%s.%s" % (ip, name, name, suffix, domain) else: print "%s %s %s.%s" % (ip, name, name, domain) def newLines(self, currenthosts): domain = self.db.getAppglobals('DNSZone') query = ('select nics.ip, nodes.name, networks.suffix ' 'from nics,nodes,networks where nics.nid = nodes.nid ' 'and nics.netid = networks.netid ' 'and networks.usingdhcp=False ' 'and not networks.device="bmc" order by nics.ip') try: self.db.execute(query) except: sys.stderr.write(self.gettext("DB_Query_Error\n")) sys.exit(-1) for row in self.db.fetchall(): ip, name, suffix = row if currenthosts: if ip in currenthosts: continue self.printLine(ip, name, suffix, domain) def runPlugin(self, pluginargs): if not self.haveLsf(): print "# Error: Not a Lava machine" return chosts = [] chosts = self.getHost() self.listHosts() self.newLines(chosts)