def gen_AR(self): """ Generates the Address Record and PTR objects related to this zone's domain. .. note:: Some AddressRecords may need to be added to the pointer table in MAINTAIN for successful migration, for example, cob-dc81 and cob-dc82.bus.oregonstate.edu .. note:: AddressRecords/PTRs with the same ip as a StaticInterface can't coexist, so if a StaticInterface with the same ip exists, it has priority. :AddressRecord uniqueness: label, domain, ip_str, ip_type :PTR uniqueness: name, ip_str, ip_type """ name = self.domain.name cursor.execute("SELECT ip, hostname, type, enabled " "FROM pointer " "WHERE hostname LIKE '%%.%s';" % name) for ip, hostname, ptr_type, enabled, in cursor.fetchall(): hostname = hostname.lower() label, dname = hostname.split('.', 1) if dname != name: continue if StaticInterface.objects.filter(ip_str=long2ip(ip)).exists(): continue if ptr_type == 'forward': arec, _ = AddressRecord.objects.get_or_create( label=label, domain=self.domain, ip_str=long2ip(ip), ip_type='4') if enabled: arec.views.add(public) elif ptr_type == 'reverse': if not PTR.objects.filter(name=name, ip_str=long2ip(ip)).exists(): ptr = PTR(name=name, ip_str=long2ip(ip), ip_type='4') # PTRs need to be cleaned independently of saving # (no get_or_create) ptr.full_clean() ptr.save() if enabled: ptr.views.add(public)
def gen_AR(self): """ Generates the Address Record and PTR objects related to this zone's domain. .. note:: Some AddressRecords may need to be added to the pointer table in MAINTAIN for successful migration, for example, cob-dc81 and cob-dc82.bus.oregonstate.edu .. note:: AddressRecords/PTRs with the same ip as a StaticInterface can't coexist, so if a StaticInterface with the same ip exists, it has priority. :AddressRecord uniqueness: label, domain, ip_str, ip_type :PTR uniqueness: name, ip_str, ip_type """ name = self.domain.name cursor.execute("SELECT ip, hostname, type, enabled " "FROM pointer " "WHERE hostname LIKE '%%.%s';" % name) for ip, hostname, ptr_type, enabled in cursor.fetchall(): hostname = hostname.lower() label, dname = hostname.split(".", 1) if dname != name: continue if StaticInterface.objects.filter(ip_str=long2ip(ip)).exists(): continue if ptr_type == "forward": arec, _ = AddressRecord.objects.get_or_create( label=label, domain=self.domain, ip_str=long2ip(ip), ip_type="4" ) if enabled: arec.views.add(public) elif ptr_type == "reverse": if not PTR.objects.filter(name=name, ip_str=long2ip(ip)).exists(): ptr = PTR(name=name, ip_str=long2ip(ip), ip_type="4") # PTRs need to be cleaned independently of saving # (no get_or_create) ptr.full_clean() ptr.save() if enabled: ptr.views.add(public)
def migrate_zone_reverse(): print "Migrating container-reverse_domain relationship." cursor.execute("SELECT ip,zone FROM pointer WHERE type='reverse'") results = cursor.fetchall() for ip, zone_id in results: ctnr = maintain_find_zone(zone_id) if not ctnr: continue doctets = [] octets = long2ip(ip).split(".") for octet in octets: doctets = [octet] + doctets dname = ".".join(doctets) + ".in-addr.arpa" domain, _ = Domain.objects.get_or_create(name=dname, is_reverse=True) try: ctnr.domains.add(domain) ctnr.save() except Exception, e: print e raise
def gen_AR(self): """ Generates the Address Record and PTR objects related to this zone's domain. .. note:: Some AddressRecords may need to be added to the pointer table in MAINTAIN for successful migration, for example, cob-dc81 and cob-dc82.bus.oregonstate.edu .. note:: AddressRecords/PTRs with the same ip as a StaticInterface can't coexist, so if a StaticInterface with the same ip exists, it has priority. :AddressRecord uniqueness: label, domain, ip_str, ip_type :PTR uniqueness: name, ip_str, ip_type """ name = self.domain.name cursor.execute("SELECT ip, hostname, type, zone.name, enabled " "FROM pointer JOIN zone ON pointer.zone = zone.id " "WHERE hostname LIKE '%%.%s';" % name) for ip, hostname, ptr_type, zone, enabled, in cursor.fetchall(): hostname = hostname.lower() label, dname = hostname.split('.', 1) if dname != name: continue dup_stats = StaticInterface.objects.filter(ip_str=long2ip(ip)) if dup_stats.exists(): if ptr_type == 'reverse': print "Ignoring PTR %s; Static intr exists." % long2ip(ip) continue elif dup_stats.filter(fqdn=hostname).exists(): print "Ignoring AR %s; Static intr exists." % hostname continue else: pass ctnr = self.ctnr_from_zone_name(zone, 'AR/PTR') if ctnr is None: continue if ptr_type == 'forward': if AddressRecord.objects.filter( fqdn=hostname, ip_str=long2ip(ip)).exists(): continue try: arec, _ = range_usage_get_create( AddressRecord, label=label, domain=self.domain, ip_str=long2ip(ip), ip_type='4', ctnr=ctnr) except ValidationError, e: print "Could not migrate AR %s: %s" % (hostname, e) continue if enabled: arec.views.add(public) arec.views.add(private) elif ptr_type == 'reverse': if not PTR.objects.filter(ip_str=long2ip(ip)).exists(): ptr = PTR(fqdn=hostname, ip_str=long2ip(ip), ip_type='4', ctnr=ctnr) # PTRs need to be cleaned independently of saving # (no get_or_create) try: ptr.full_clean() except ValidationError, e: print "Could not migrate PTR %s: %s" % (ptr.ip_str, e) continue ptr.save(update_range_usage=False) if enabled: ptr.views.add(public) ptr.views.add(private)
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)
ip_type='4', ctnr=ctnr) # PTRs need to be cleaned independently of saving # (no get_or_create) try: ptr.full_clean() except ValidationError, e: print "Could not migrate PTR %s: %s" % (ptr.ip_str, e) continue ptr.save(update_range_usage=False) if enabled: ptr.views.add(public) ptr.views.add(private) else: print "Ignoring PTR %s; already exists." % long2ip(ip) def gen_NS(self): """ Generates the Nameserver objects related to this zone's domain. :uniqueness: domain, server name """ cursor.execute("SELECT * " "FROM nameserver " "WHERE domain='%s';" % self.domain_id) for pk, name, _, _ in cursor.fetchall(): name = name.lower() try: ns, _ = Nameserver.objects.get_or_create(domain=self.domain, server=name)
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 """ cursor.execute( "SELECT id, ip, name, workgroup, enabled, ha, " "type, os, location, department , serial, other_id, " "purchase_date, po_number, warranty_date, owning_unit, " "user_id " "FROM host " "WHERE ip != 0 AND domain = '%s';" % self.domain_id ) for ( id, ip, name, workgroup, enabled, ha, type, os, location, dept, serial, other_id, purchase_date, po_number, warranty_date, owning_unit, user_id, ) in cursor.fetchall(): name = name.lower() enabled = bool(enabled) if ip == 0: continue if len(ha) != 12: ha = "0" * 12 # TODO: Make systems unique by hostname, ip, mac tuple # TODO: Add key-value attributes to system objects. system, _ = System.objects.get_or_create(name=name, location=location, department=dept) try: cursor.execute("SELECT name " "FROM workgroup" "WHERE id = {0}".format(workgroup)) name = cursor.fetchone()[0] w, _ = Workgroup.objects.get_or_create(name=name) v, _ = Vrf.objects.get_or_create(name="{0}-".format(name)) except: v = None w = None if not (StaticInterface.objects.filter(label=name, mac=clean_mac(ha), ip_str=long2ip(ip)).exists()): try: static = StaticInterface( label=name, domain=self.domain, mac=clean_mac(ha), system=system, ip_str=long2ip(ip), ip_type="4", vrf=v, workgroup=w, ) # Static Interfaces need to be cleaned independently. # (no get_or_create) static.full_clean() static.save() if enabled: static.views.add(public) cursor.execute( "SELECT dhcp_option, value " "FROM object_option " "WHERE object_id = {0} " "AND type = 'host'".format(id) ) results = cursor.fetchall() for dhcp_option, value in results: cursor.execute("SELECT name, type " "FROM dhcp_options " "WHERE id = {0}".format(dhcp_option)) name, type = cursor.fetchone() kv = StaticIntrKeyValue(intr=static, key=name, value=value) kv.clean() kv.save() except ValidationError, e: print "Error generating static interface. %s" % e exit(1)
def gen_AR(self): """ Generates the Address Record and PTR objects related to this zone's domain. .. note:: Some AddressRecords may need to be added to the pointer table in MAINTAIN for successful migration, for example, cob-dc81 and cob-dc82.bus.oregonstate.edu .. note:: AddressRecords/PTRs with the same ip as a StaticInterface can't coexist, so if a StaticInterface with the same ip exists, it has priority. :AddressRecord uniqueness: label, domain, ip_str, ip_type :PTR uniqueness: name, ip_str, ip_type """ name = self.domain.name cursor.execute("SELECT ip, hostname, type, zone.name, enabled " "FROM pointer JOIN zone ON pointer.zone = zone.id " "WHERE hostname LIKE '%%.%s';" % name) for ip, hostname, ptr_type, zone, enabled, in cursor.fetchall(): hostname = hostname.lower() label, dname = hostname.split('.', 1) if dname != name: continue dup_stats = StaticInterface.objects.filter(ip_str=long2ip(ip)) if dup_stats.exists(): if ptr_type == 'reverse': print "Ignoring PTR %s; Static intr exists." % long2ip(ip) continue elif dup_stats.filter(fqdn=hostname).exists(): print "Ignoring AR %s; Static intr exists." % hostname continue else: pass ctnr = self.ctnr_from_zone_name(zone, 'AR/PTR') if ctnr is None: continue if ptr_type == 'forward': if AddressRecord.objects.filter(fqdn=hostname, ip_str=long2ip(ip)).exists(): continue try: arec, _ = range_usage_get_create(AddressRecord, label=label, domain=self.domain, ip_str=long2ip(ip), ip_type='4', ctnr=ctnr) except ValidationError, e: print "Could not migrate AR %s: %s" % (hostname, e) continue if enabled: arec.views.add(public) arec.views.add(private) elif ptr_type == 'reverse': if not PTR.objects.filter(ip_str=long2ip(ip)).exists(): ptr = PTR(fqdn=hostname, ip_str=long2ip(ip), ip_type='4', ctnr=ctnr) # PTRs need to be cleaned independently of saving # (no get_or_create) try: ptr.full_clean() except ValidationError, e: print "Could not migrate PTR %s: %s" % (ptr.ip_str, e) continue ptr.save(update_range_usage=False) if enabled: ptr.views.add(public) ptr.views.add(private)
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)
ctnr=ctnr) # PTRs need to be cleaned independently of saving # (no get_or_create) try: ptr.full_clean() except ValidationError, e: print "Could not migrate PTR %s: %s" % (ptr.ip_str, e) continue ptr.save(update_range_usage=False) if enabled: ptr.views.add(public) ptr.views.add(private) else: print "Ignoring PTR %s; already exists." % long2ip(ip) def gen_NS(self): """ Generates the Nameserver objects related to this zone's domain. :uniqueness: domain, server name """ cursor.execute("SELECT * " "FROM nameserver " "WHERE domain='%s';" % self.domain_id) for pk, name, _, _ in cursor.fetchall(): name = name.lower() try: ns, _ = Nameserver.objects.get_or_create(domain=self.domain, server=name)
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 """ cursor.execute("SELECT id, ip, name, workgroup, enabled, ha, " "type, os, location, department , serial, other_id, " "purchase_date, po_number, warranty_date, owning_unit, " "user_id " "FROM host " "WHERE ip != 0 AND domain = '%s';" % self.domain_id) for id, ip, name, workgroup, enabled, ha, type, os, location, dept, \ serial, other_id, purchase_date, po_number, warranty_date, \ owning_unit, user_id in cursor.fetchall(): name = name.lower() enabled = bool(enabled) if ip == 0: continue if len(ha) != 12: ha = "0" * 12 # TODO: Make systems unique by hostname, ip, mac tuple # TODO: Add key-value attributes to system objects. system, _ = System.objects.get_or_create(name=name, location=location, department=dept) try: cursor.execute("SELECT name " "FROM workgroup" "WHERE id = {0}".format(workgroup)) name = cursor.fetchone()[0] w, _ = Workgroup.objects.get_or_create(name=name) v, _ = Vrf.objects.get_or_create(name="{0}-".format(name)) except: v = None w = None if not (StaticInterface.objects.filter( label=name, mac=clean_mac(ha), ip_str=long2ip(ip)).exists()): try: static = StaticInterface(label=name, domain=self.domain, mac=clean_mac(ha), system=system, ip_str=long2ip(ip), ip_type='4', vrf=v, workgroup=w) # Static Interfaces need to be cleaned independently. # (no get_or_create) static.full_clean() static.save() if enabled: static.views.add(public) cursor.execute("SELECT dhcp_option, value " "FROM object_option " "WHERE object_id = {0} " "AND type = 'host'".format(id)) results = cursor.fetchall() for dhcp_option, value in results: cursor.execute("SELECT name, type " "FROM dhcp_options " "WHERE id = {0}".format(dhcp_option)) name, type = cursor.fetchone() kv = StaticIntrKeyValue(intr=static, key=name, value=value) kv.clean() kv.save() except ValidationError, e: print "Error generating static interface. %s" % e exit(1)