# $Id: hosts.py 3572 2008-10-16 09:11:19Z hsaliak $ # # 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 # # # THIS IS TH REAL ONE from kusu.genconfig import Report import sys import IPy class thisReport(Report): def toolHelp(self): print self.gettext("genconfig_Hosts_Help") def runPlugin(self, pluginargs): print "# " print "# Dynamically generated by: genconfig (Do not edit!)" print "#" print "%-15s localhost.localdomain\tlocalhost" % '127.0.0.1' _ = self.gettext # Get the DNS Zone served by the Installer dnszone = self.db.getAppglobals('DNSZone') if not dnszone: sys.stderr.write(_("genconfig_cannot_determine_DNS_zone\n")) sys.exit(0) # publicdnszone is not required to generate the hosts file; if # not defined, the entry is omitted from the hosts file publicdnszone = self.db.getAppglobals('PublicDNSZone') # ordered by hostname, network type, boot flag and network device # ngid == 5 means those unmanaged nodes, don't consider them query = ('SELECT nics.ip,nodes.name,networks.suffix,nics.boot,networks.type,nodes.ngid,networks.device ' 'FROM nics, nodes, networks WHERE nics.nid = nodes.nid ' 'AND nics.netid = networks.netid AND networks.usingdhcp=False ' 'AND nodes.ngid!=5 ORDER BY nodes.name, networks.type, nics.boot desc, networks.device') try: self.db.execute(query) except: sys.stderr.write(self.gettext("DB_Query_Error\n")) sys.exit(-1) else: bFirstPublicInterface = False bFirstProvisionInterface = False data = self.db.fetchall() prevNodeName = '' for row in data: ip, name, suffix, boot, nettype, nngid, netdevice= row if suffix: str = "%-15s" % (ip) bNeedShortName = False # boot, nettype, nngid to confirm the first boot provision interafce (compute nodes) # nettype, nngid to confirm the first provision interface (master node) if ( boot == 1 and nettype == 'provision' and nngid != 1 ) or ( nettype == 'provision' and nngid == 1 ): # to guarantee the 'first' if prevNodeName and name == prevNodeName: bNeedShortName = False else: bNeedShortName = True # Display the short name for the interfaces that were booted if nngid != 1 and boot == 1: str += '\t%s.%s' % (name, dnszone) # Include short host name for all nodes other than # installer nodes. else: # provisioning interface if nettype == 'provision': if not bFirstProvisionInterface: if suffix == '-bmc': str += "\t%s%s.%s" % (name, suffix, dnszone) else: str += "\t%s.%s" % (name, dnszone) bFirstProvisionInterface = True elif nettype == 'public': if publicdnszone: str += "\t%s.%s" % (name, publicdnszone) # Display host name only on first public interface else: # Unknown 'nettype' pass if nettype == 'provision': str += '\t%s%s.%s\t%s%s' % (name, suffix, dnszone, name, suffix) elif nettype == 'public': if publicdnszone: str += '\t%s%s.%s\t%s%s' % (name, suffix, publicdnszone, name, suffix) else: # Unknown 'nettype' pass if bNeedShortName: str += "\t%s" % (name) print str else: if nettype == 'provision': print "%-15s\t%s.%s \t%s" % (ip, name, dnszone, name) elif nettype == 'public': if publicdnszone: print "%-15s\t%s.%s \t%s" % (ip, name, publicdnszone, name) else: print "%-15s \t%s" % (ip, name) prevNodeName = name # Create the unmanaged hosts entries query = ('SELECT nics.ip,nodes.name ' 'FROM nics, nodes, networks WHERE nics.nid = nodes.nid ' 'AND nics.netid = networks.netid AND networks.usingdhcp=False ' 'AND nodes.ngid=5 ORDER BY nics.ip') try: self.db.execute(query) except: sys.stderr.write(self.gettext("DB_Query_Error\n")) sys.exit(-1) else: data = self.db.fetchall() if data: print "\n# Unmanaged Nodes" for row in data: ip, name = row # When HP-ICE discovers a new node, that node is added to # the unmanaged nodegroup with it's IP as the node name. # Ignore such nodes here. try: IPy.parseAddress(name) # Node name can be parsed to an IP address. # Hence skip this node. continue except ValueError: pass print "%-15s\t%s.%s \t%s" % (ip, name, dnszone, name)