Logo Search packages:      
Sourcecode: bcfg2 version File versions  Download package

def lib::Server::Plugins::Hostbase::buildZones (   self  ) 

Pre-build and stash zone files

Definition at line 109 of file Hostbase.py.

00109                         :
        '''Pre-build and stash zone files'''
        cursor = connection.cursor()

        cursor.execute("SELECT id, serial FROM hostbase_zone")
        zones = cursor.fetchall()

        for zone in zones:
        # update the serial number for all zone files
            todaydate = (strftime('%Y%m%d'))
            try:
                if todaydate == str(zone[1])[:8]:
                    serial = zone[1] + 1
                else:
                    serial = int(todaydate) * 100
            except (KeyError):
                serial = int(todaydate) * 100
            cursor.execute("""UPDATE hostbase_zone SET serial = \'%s\' WHERE id = \'%s\'""" % (str(serial), zone[0]))

        cursor.execute("SELECT * FROM hostbase_zone WHERE zone <> \'.rev\'")
        zones = cursor.fetchall()

        iplist = []
        hosts = {}

        cursor.execute("SELECT * FROM hostbase_zone WHERE zone = \'.rev\'")
        reversezone = cursor.fetchall()[0]
        cursor.execute("""SELECT n.name FROM hostbase_zone_nameservers z
        INNER JOIN hostbase_nameserver n ON z.nameserver_id = n.id
        WHERE z.zone_id = \'%s\'""" % reversezone[0])
        reverse_nameservers = cursor.fetchall()


        for zone in zones:
            cursor.execute("""SELECT n.name FROM hostbase_zone_nameservers z
            INNER JOIN hostbase_nameserver n ON z.nameserver_id = n.id
            WHERE z.zone_id = \'%s\'""" % zone[0])
            nameservers = cursor.fetchall()
            cursor.execute("""SELECT i.ip_addr FROM hostbase_zone_addresses z
            INNER JOIN hostbase_zoneaddress i ON z.zoneaddress_id = i.id
            WHERE z.zone_id = \'%s\'""" % zone[0])
            addresses = cursor.fetchall()
            cursor.execute("""SELECT m.priority, m.mx FROM hostbase_zone_mxs z
            INNER JOIN hostbase_mx m ON z.mx_id = m.id
            WHERE z.zone_id = \'%s\'""" % zone[0])
            mxs = cursor.fetchall()
            context = Context({
                'zone': zone,
                'nameservers': nameservers,
                'addresses': addresses,
                'mxs': mxs
                })
            self.filedata[zone[1]] = self.templates['zone'].render(context)

            querystring = """SELECT h.hostname, p.ip_addr,
            n.name, c.cname, m.priority, m.mx
            FROM (((((hostbase_host h INNER JOIN hostbase_interface i ON h.id = i.host_id)
            INNER JOIN hostbase_ip p ON i.id = p.interface_id)
            INNER JOIN hostbase_name n ON p.id = n.ip_id)
            INNER JOIN hostbase_name_mxs x ON n.id = x.name_id)
            INNER JOIN hostbase_mx m ON m.id = x.mx_id)
            LEFT JOIN hostbase_cname c ON n.id = c.name_id
            WHERE h.hostname LIKE '%%%%%s' AND h.status = 'active'
            ORDER BY h.hostname, n.name, p.ip_addr
            """ % zone[1]
            cursor.execute(querystring)
            zonehosts = cursor.fetchall()
            prevhost = (None, None, None, None)
            for host in zonehosts:
                if not host[0].split(".", 1)[1] == zone[1]:
                    continue
                if not prevhost[1] == host[1] or not prevhost[2] == host[2]:
                    self.filedata[zone[1]] += ("%-32s%-10s%-32s\n" %                   
                                           (host[2].split(".", 1)[0], 'A', host[1]))
                    self.filedata[zone[1]] += ("%-32s%-10s%-3s%s.\n" %                   
                                           ('', 'MX', host[4], host[5]))
                if host[3]:
                    if host[3].split(".", 1)[1] == zone[1]:
                        self.filedata[zone[1]] += ("%-32s%-10s%-32s\n" %                   
                                               (host[3].split(".", 1)[0],
                                                'CNAME',host[2].split(".", 1)[0]))
                    else:
                        self.filedata[zone[1]] += ("%-32s%-10s%-32s\n" %                   
                                               (host[3]+".",
                                                'CNAME',
                                                host[2].split(".", 1)[0]))
                prevhost = host
            self.filedata[zone[1]] += ("\n\n%s" % zone[9])

            self.Entries['ConfigFile']["%s/%s" % (self.filepath, zone[1])] = self.FetchFile


        filelist = []
        cursor.execute("""
        SELECT ip_addr FROM hostbase_ip ORDER BY ip_addr
        """)
        three_subnet = [ip[0].rstrip('0123456789').rstrip('.') \
                        for ip in cursor.fetchall()]
        three_subnet_set = Set(three_subnet)
        two_subnet = [subnet.rstrip('0123456789').rstrip('.') \
                      for subnet in three_subnet_set]
        two_subnet_set = Set(two_subnet)
        filelist = [each for each in two_subnet_set \
                    if two_subnet.count(each) > 1]
        for each in three_subnet_set:
            if each.rstrip('0123456789').rstrip('.') not in filelist:
                filelist.append(each)

        reversenames = []
        for filename in filelist:
            towrite = filename.split('.')
            towrite.reverse()
            reversename = '.'.join(towrite)
            context = Context({
                'inaddr': reversename,
                'zone': reversezone,
                'nameservers': reverse_nameservers,
                })

            self.filedata['%s.rev' % reversename] = self.templates['reversesoa'].render(context)
            reversenames.append((reversename, filename))

        ## here's where the named.conf file gets written
        context = Context({
            'zones': zones,
            'reverses': reversenames,
            })        
        self.filedata['named.conf'] = self.templates['named'].render(context)
        self.Entries['ConfigFile']['%s/named.conf' % self.filepath] = self.FetchFile

        reversenames.sort()
        for filename in reversenames:
            originlist = []
            cursor.execute("""
            SELECT h.hostname, p.ip_addr, p.num FROM ((hostbase_host h
            INNER JOIN hostbase_interface i ON h.id = i.host_id)
            INNER JOIN hostbase_ip p ON i.id = p.interface_id)
            WHERE p.ip_addr LIKE '%s%%%%' AND h.status = 'active' ORDER BY p.ip_addr
            """ % filename[1])
            reversehosts = cursor.fetchall()
            if len(filename[0].split(".")) == 2:
                [originlist.append((".".join([ip[1].split(".")[2], filename[0]]),
                                    ".".join([filename[1], ip[1].split(".")[2]])))
                 for ip in reversehosts
                 if (".".join([ip[1].split(".")[2], filename[0]]),
                     ".".join([filename[1], ip[1].split(".")[2]])) not in originlist]
                for origin in originlist:
                    hosts = [host.__add__((host[1].split("."), host[0].split(".", 1)))
                             for host in reversehosts
                             if host[1].rstrip('0123456789').rstrip('.') == origin[1]]
                    context = Context({
                        'hosts': hosts,
                        'inaddr': origin[0],
                        'fileorigin': filename[0],
                        })        
                    self.filedata['%s.rev' % filename[0]] += self.templates['reverseapp'].render(context)
            else:
                originlist = [filename[0]]
                hosts = [host.__add__((host[1].split("."), host[0].split(".", 1)))
                         for host in reversehosts]
                context = Context({
                    'hosts': hosts,
                    'inaddr': filename[0],
                    'fileorigin': None,
                    })        
                self.filedata['%s.rev' % filename[0]] += self.templates['reverseapp'].render(context)
            self.Entries['ConfigFile']['%s/%s.rev' % (self.filepath, filename[0])] = self.FetchFile

    def buildDHCP(self):


Generated by  Doxygen 1.6.0   Back to index