Пример #1
0
    def gen_static(self):
        """
        Generates the Static Interface objects related to this zone's domain.

        .. note::
            Every static interface needs a system.

        :System uniqueness: hostname, mac, ip_str

        :StaticInterface uniqueness: hostname, mac, ip_str
        """
        from dhcp_migrate import migrate_zones

        if Ctnr.objects.count() <= 2:
            print "WARNING: Zones not migrated. Attempting to migrate now."
            migrate_zones()

        sys_value_keys = {"type": "Hardware Type",
                          "os": "Operating System",
                          "location": "Location",
                          "department": "Department",
                          "serial": "Serial Number",
                          "other_id": "Other ID",
                          "purchase_date": "Purchase Date",
                          "po_number": "PO Number",
                          "warranty_date": "Warranty Date",
                          "owning_unit": "Owning Unit",
                          "user_id": "User ID"}

        keys = ("host.id", "ip", "host.name", "zone.name", "workgroup",
                "enabled", "ha", "zone", "type", "os", "location",
                "department", "serial", "other_id", "purchase_date",
                "po_number", "warranty_date", "owning_unit", "user_id",
                "last_seen", "expire", "ttl", "last_update")

        sql = ("SELECT %s FROM host JOIN zone ON host.zone = zone.id "
               "WHERE ip != 0 AND domain = '%s';" %
               (", ".join(keys), self.domain_id))

        cursor.execute(sql)
        for values in cursor.fetchall():
            items = dict(zip(keys, values))
            ctnr = self.ctnr_from_zone_name(items['zone.name'])
            if ctnr is None:
                continue

            name = items['host.name']
            enabled = bool(items['enabled'])
            dns_enabled, dhcp_enabled = enabled, enabled
            ip = items['ip']
            ha = items['ha']
            if ip == 0:
                continue

            if len(ha) != 12 or ha == '0' * 12:
                ha = ""

            if ha == "":
                dhcp_enabled = False

            # check for duplicate
            static = StaticInterface.objects.filter(
                label=name, mac=(clean_mac(ha) or None), ip_str=long2ip(ip))
            if static:
                stderr.write("Ignoring host %s: already exists.\n"
                             % items['host.id'])
                continue

            # create system
            system = System(name=name)
            system.save()
            for key in sys_value_keys.keys():
                value = items[key].strip()
                if not value or value == '0':
                    continue
                attr = Attribute.objects.get(
                    name=fix_attr_name(sys_value_keys[key]))
                eav = SystemAV(entity=system, attribute=attr, value=value)
                eav.full_clean()
                eav.save()

            # check for workgroup
            if items['workgroup'] is not None:
                cursor.execute("SELECT name "
                               "FROM workgroup "
                               "WHERE id = {0}".format(items['workgroup']))
                wname = cursor.fetchone()[0]
                w, _ = Workgroup.objects.get_or_create(name=wname)
            else:
                w = None

            last_seen = items['last_seen'] or None
            if last_seen:
                last_seen = datetime.fromtimestamp(last_seen)

            static = StaticInterface(
                label=name, domain=self.domain, mac=(clean_mac(ha) or None),
                system=system, ip_str=long2ip(ip), ip_type='4',
                workgroup=w, ctnr=ctnr, ttl=items['ttl'],
                dns_enabled=dns_enabled, dhcp_enabled=dhcp_enabled,
                last_seen=last_seen)

            # create static interface
            try:
                static.save(update_range_usage=False)
            except ValidationError as e:
                try:
                    static.dhcp_enabled = False
                    static.dns_enabled = dns_enabled
                    static.save(update_range_usage=False)
                    stderr.write('WARNING: Static interface with IP {} has '
                                 'been disabled\n'.format(static.ip_str))
                    stderr.write('    {}\n'.format(e))
                except ValidationError as e:
                    stderr.write('WARNING: Could not create static interface '
                                 'with IP {}\n'.format(static.ip_str))
                    stderr.write('    {}\n'.format(e))
                    static = None
                    system.delete()

            if static:
                static.views.add(public)
                static.views.add(private)
Пример #2
0
    def gen_static(self):
        """
        Generates the Static Interface objects related to this zone's domain.

        .. note::
            Every static interface needs a system.

        :System uniqueness: hostname, mac, ip_str

        :StaticInterface uniqueness: hostname, mac, ip_str
        """
        from dhcp_migrate import migrate_zones

        if Ctnr.objects.count() <= 2:
            print "WARNING: Zones not migrated. Attempting to migrate now."
            migrate_zones()

        sys_value_keys = {
            "type": "Hardware Type",
            "os": "Operating System",
            "location": "Location",
            "department": "Department",
            "serial": "Serial Number",
            "other_id": "Other ID",
            "purchase_date": "Purchase Date",
            "po_number": "PO Number",
            "warranty_date": "Warranty Date",
            "owning_unit": "Owning Unit",
            "user_id": "User ID"
        }

        keys = ("host.id", "ip", "host.name", "zone.name", "workgroup",
                "enabled", "ha", "zone", "type", "os", "location",
                "department", "serial", "other_id", "purchase_date",
                "po_number", "warranty_date", "owning_unit", "user_id",
                "last_seen", "expire", "ttl", "last_update")

        sql = ("SELECT %s FROM host JOIN zone ON host.zone = zone.id "
               "WHERE ip != 0 AND domain = '%s';" %
               (", ".join(keys), self.domain_id))

        cursor.execute(sql)
        for values in cursor.fetchall():
            items = dict(zip(keys, values))
            ctnr = self.ctnr_from_zone_name(items['zone.name'])
            if ctnr is None:
                continue

            name = items['host.name']
            enabled = bool(items['enabled'])
            dns_enabled, dhcp_enabled = enabled, enabled
            ip = items['ip']
            ha = items['ha']
            if ip == 0:
                continue

            if len(ha) != 12 or ha == '0' * 12:
                ha = ""

            if ha == "":
                dhcp_enabled = False

            # check for duplicate
            static = StaticInterface.objects.filter(label=name,
                                                    mac=(clean_mac(ha)
                                                         or None),
                                                    ip_str=long2ip(ip))
            if static:
                stderr.write("Ignoring host %s: already exists.\n" %
                             items['host.id'])
                continue

            # create system
            system = System(name=name)
            system.save()
            for key in sys_value_keys.keys():
                value = items[key].strip()
                if not value or value == '0':
                    continue
                attr = Attribute.objects.get(
                    name=fix_attr_name(sys_value_keys[key]))
                eav = SystemAV(entity=system, attribute=attr, value=value)
                eav.full_clean()
                eav.save()

            # check for workgroup
            if items['workgroup'] is not None:
                cursor.execute("SELECT name "
                               "FROM workgroup "
                               "WHERE id = {0}".format(items['workgroup']))
                wname = cursor.fetchone()[0]
                w, _ = Workgroup.objects.get_or_create(name=wname)
            else:
                w = None

            last_seen = items['last_seen'] or None
            if last_seen:
                last_seen = datetime.fromtimestamp(last_seen)

            static = StaticInterface(label=name,
                                     domain=self.domain,
                                     mac=(clean_mac(ha) or None),
                                     system=system,
                                     ip_str=long2ip(ip),
                                     ip_type='4',
                                     workgroup=w,
                                     ctnr=ctnr,
                                     ttl=items['ttl'],
                                     dns_enabled=dns_enabled,
                                     dhcp_enabled=dhcp_enabled,
                                     last_seen=last_seen)

            # create static interface
            try:
                static.save(update_range_usage=False)
            except ValidationError as e:
                try:
                    static.dhcp_enabled = False
                    static.dns_enabled = dns_enabled
                    static.save(update_range_usage=False)
                    stderr.write('WARNING: Static interface with IP {} has '
                                 'been disabled\n'.format(static.ip_str))
                    stderr.write('    {}\n'.format(e))
                except ValidationError as e:
                    stderr.write('WARNING: Could not create static interface '
                                 'with IP {}\n'.format(static.ip_str))
                    stderr.write('    {}\n'.format(e))
                    static = None
                    system.delete()

            if static:
                static.views.add(public)
                static.views.add(private)