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

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

Pre-build and stash /etc/hosts file

Definition at line 325 of file Hostbase.py.

00325                         :
        '''Pre-build and stash /etc/hosts file'''

        append_data = []

        cursor = connection.cursor()
        cursor.execute("""
        SELECT hostname FROM hostbase_host ORDER BY hostname
        """)
        hostbase = cursor.fetchall()
        domains = [host[0].split(".", 1)[1] for host in hostbase]
        domains_set = Set(domains)
        domain_data = [(domain, domains.count(domain)) for domain in domains_set]
        domain_data.sort()

        cursor.execute("""
        SELECT ip_addr FROM hostbase_ip ORDER BY ip_addr
        """)
        ips = cursor.fetchall()
        three_octets = [ip[0].rstrip('0123456789').rstrip('.') \
                        for ip in ips]
        three_octets_set = Set(three_octets)
        three_octets_data = [(octet, three_octets.count(octet)) \
                             for octet in three_octets_set]
        three_octets_data.sort()

        for three_octet in three_octets_data:
            querystring = """SELECT h.hostname, h.primary_user,
            p.ip_addr, n.name, c.cname
            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)
            LEFT JOIN hostbase_cname c ON n.id = c.name_id
            WHERE p.ip_addr LIKE \'%s%%%%\' AND h.status = 'active'
            ORDER BY p.ip_addr""" % three_octet[0]
            cursor.execute(querystring)
            append_data.append((three_octet, cursor.fetchall()))

        two_octets = [ip.rstrip('0123456789').rstrip('.') for ip in three_octets]
        two_octets_set = Set(two_octets)
        two_octets_data = [(octet, two_octets.count(octet))
                           for octet in two_octets_set]
        two_octets_data.sort()

        context = Context({
            'domain_data': domain_data,
            'three_octets_data': three_octets_data,
            'two_octets_data': two_octets_data,
            'three_octets': three_octets,
            'num_ips': len(three_octets),
            })

        self.filedata['hosts'] = self.templates['hosts'].render(context)

        for subnet in append_data:
            ips = []
            simple = True
            namelist = [name.split('.', 1)[0] for name in [subnet[1][0][3]]]
            cnamelist = []
            if subnet[1][0][4]:
                cnamelist.append(subnet[1][0][4].split('.', 1)[0])
                simple = False
            appenddata = subnet[1][0]
            for ip in subnet[1][1:]:
                if appenddata[2] == ip[2]:
                    namelist.append(ip[3].split('.', 1)[0])
                    if ip[4]:
                        cnamelist.append(ip[4].split('.', 1)[0])
                        simple = False
                    appenddata = ip
                else:
                    if appenddata[0] == ip[0]:
                        simple = False
                    ips.append((appenddata[2], appenddata[0], namelist,
                                cnamelist, simple, appenddata[1]))
                    appenddata = ip
                    simple = True
                    namelist = [ip[3].split('.', 1)[0]]
                    cnamelist = []
                    if ip[4]:
                        cnamelist.append(ip[4].split('.', 1)[0])
                        simple = False
            ips.append((appenddata[2], appenddata[0], namelist,
                        cnamelist, simple, appenddata[1]))
            context = Context({
                'subnet': subnet[0],
                'ips': ips,
                })
            self.filedata['hosts'] += self.templates['hostsapp'].render(context)
        self.Entries['ConfigFile']['/mcs/etc/hosts'] = self.FetchFile

    def buildPrinters(self):


Generated by  Doxygen 1.6.0   Back to index