def test_cleanup_intr(self): self.assertFalse(Domain.objects.filter(name="x.y.z.foo.poo")) self.assertFalse(Domain.objects.filter(name="y.z.foo.poo")) self.assertFalse(Domain.objects.filter(name="z.foo.poo")) self.assertTrue(Domain.objects.filter(name="foo.poo")) Domain.objects.get_or_create(name="arpa") Domain.objects.get_or_create(name="in-addr.arpa") Domain.objects.get_or_create(name="10.in-addr.arpa") fqdn = "bar.x.y.z.foo.poo" label, the_domain = ensure_label_domain(fqdn) system = System() addr = StaticInterface(label=label, domain=the_domain, ip_type='4', ip_str="10.2.3.4", mac="00:11:22:33:44:55", system=system) addr.save() self.assertFalse(prune_tree(the_domain)) addr.delete() self.assertFalse(Domain.objects.filter(name="x.y.z.foo.poo")) self.assertFalse(Domain.objects.filter(name="y.z.foo.poo")) self.assertFalse(Domain.objects.filter(name="z.foo.poo")) self.assertTrue(Domain.objects.filter(name="foo.poo"))
def test_freeip(self): system = System(name='foobar', ctnr=self.ctnr) system.save() r = Range.objects.create( start_str="10.0.33.1", end_str="10.0.33.3", network=self.s, ip_type='4', ) self.ctnr.ranges.add(r) self.assertEqual(str(r.get_next_ip()), "10.0.33.1") self.assertEqual(str(r.get_next_ip()), "10.0.33.1") s = StaticInterface.objects.create(label="foo1", domain=self.d, ip_type='4', ip_str=str(r.get_next_ip()), system=system, mac="00:00:00:00:00:01") self.assertEqual(str(r.get_next_ip()), "10.0.33.2") s = StaticInterface.objects.create(label="foo2", domain=self.d, ip_type='4', ip_str=str(r.get_next_ip()), system=system, mac="00:00:00:00:00:01") self.assertEqual(str(r.get_next_ip()), "10.0.33.3") s = StaticInterface.objects.create(label="foo3", domain=self.d, ip_type='4', ip_str=str(r.get_next_ip()), system=system, mac="00:00:00:00:00:01") self.assertEqual(r.get_next_ip(), None)
def setUp(self): self.system = System() self.system.save() d1 = create_fake_zone("oregonstate.com", suffix="") soa = d1.soa self.soa = soa v, _ = Vlan.objects.get_or_create(name="private", number=3) s, _ = Site.objects.get_or_create(name="phx1") s1, _ = Site.objects.get_or_create(name="corp", parent=s) d, _ = Domain.objects.get_or_create(name="phx1.oregonstate.com") d.soa = soa d.save() d1, _ = Domain.objects.get_or_create(name="corp.phx1.oregonstate.com") d1.soa = soa d1.save() d2, _ = Domain.objects.get_or_create( name="private.corp.phx1.oregonstate.com") d2.soa = soa d2.save() d, _ = Domain.objects.get_or_create(name="arpa") d, _ = Domain.objects.get_or_create(name="in-addr.arpa") d, _ = Domain.objects.get_or_create(name="15.in-addr.arpa") n = Network(network_str="15.0.0.0/8", ip_type="4") n.clean() n.site = s1 n.vlan = v n.save() r = Range(start_str="15.0.0.0", end_str="15.0.0.10", network=n, ip_type='4') r.clean() r.save()
def setUp(self): self.g = create_fake_zone("gz", suffix="") self.c_g = create_fake_zone("coo.gz", suffix="") self.d = create_fake_zone("dz", suffix="") self.r1 = create_fake_zone("10.in-addr.arpa", suffix="") self.r1.save() self.s = System() self.s.save()
def do_add(self, mac, label, domain, ip_str, ip_type='4'): self.n = System() r = StaticInterface(mac=mac, label=label, domain=domain, ip_str=ip_str, ip_type=ip_type, system=self.n) r.clean() r.save() repr(r) return r
def setUp(self): self.arpa = self.create_domain(name='arpa') self.arpa.save() self.i_arpa = self.create_domain(name='in-addr.arpa') self.i_arpa.save() self.c = Domain(name="ccc") self.c.save() self.f_c = Domain(name="foo.ccc") self.f_c.save() self.r1 = self.create_domain(name="10") self.r1.save() self.n = System() self.n.clean() self.n.save()
def setUp(self): self.o = Domain(name="org") self.o.save() self.f_o = Domain(name="foo.org") self.f_o.save() self.s = System() Domain.objects.get_or_create(name="arpa") Domain.objects.get_or_create(name="in-addr.arpa") Domain.objects.get_or_create(name="10.in-addr.arpa") Domain.objects.get_or_create(name="172.in-addr.arpa") Domain.objects.get_or_create(name="192.in-addr.arpa") self.public, _ = View.objects.get_or_create(name="public") self.private, _ = View.objects.get_or_create(name="private")
def setUp(self): self.arpa = self.create_domain(name='arpa') self.arpa.save() self.i_arpa = self.create_domain(name='in-addr.arpa') self.i_arpa.save() self.c = Domain(name="ccc") self.c.save() self.f_c = Domain(name="foo.ccc") self.f_c.save() self.r1 = self.create_domain(name="10") self.r1.save() self.n = System() self.n.clean() self.n.save() View.objects.get_or_create(name="private")
def test1_freeip(self): start_str = "10.0.33.1" end_str = "10.0.33.3" default_domain = self.d network = self.s rtype = 's' ip_type = '4' system = System() kwargs = { 'start_str': start_str, 'end_str': end_str, 'default_domain': default_domain, 'network': network, 'rtype': rtype, 'ip_type': ip_type } r = self.do_add(**kwargs) self.assertEqual(str(r.get_next_ip()), "10.0.33.1") self.assertEqual(str(r.get_next_ip()), "10.0.33.1") s = StaticInterface(label="foo", domain=self.d, ip_type='4', ip_str=str(r.get_next_ip()), system=system, mac="00:00:00:00:00:00") s.clean() s.save() self.assertEqual(str(r.get_next_ip()), "10.0.33.2") s = StaticInterface(label="foo", domain=self.d, ip_type='4', ip_str=str(r.get_next_ip()), system=system, mac="00:00:00:00:00:00") s.clean() s.save() self.assertEqual(str(r.get_next_ip()), "10.0.33.3") s = StaticInterface(label="foo", domain=self.d, ip_type='4', ip_str=str(r.get_next_ip()), system=system, mac="00:00:00:00:00:00") s.clean() s.save() self.assertEqual(r.get_next_ip(), None)
def setUp(self): self.arpa = self.create_domain(name='arpa') self.arpa.save() self.i_arpa = self.create_domain(name='ip6.arpa', ip_type='6') self.i_arpa.save() self.c = Domain(name="ccc") self.c.save() self.f_c = Domain(name="foo.ccc") self.f_c.save() self.r1 = self.create_domain(name="0", ip_type='6') self.r1.save() self.r2 = self.create_domain(name="1", ip_type='6') self.r2.save() self.n = System() self.n.clean() self.n.save()
def setUp(self): self.r1 = create_fake_zone("10.in-addr.arpa", suffix="") self.sr = self.r1.soa self.sr.dirty = False self.sr.save() self.dom = create_fake_zone("bgaz", suffix="") self.soa = self.dom.soa self.soa.dirty = False self.soa.save() self.rdom = create_fake_zone("123.in-addr.arpa", suffix="") self.rsoa = self.r1.soa self.rsoa.dirty = False self.rsoa.save() self.s = System() self.s.save()
def test1_delete_basic(self): # Does deleting a system delete it's interfaces? mac = "11:22:33:44:55:66" label = "foo" domain = self.f_c ip_str = "10.0.0.2" system = System() system.save() kwargs = { 'mac': mac, 'label': label, 'domain': domain, 'ip_str': ip_str, 'system': system } self.do_add(**kwargs) self.assertTrue(StaticInterface.objects.filter(**kwargs)) system.delete() self.assertFalse(StaticInterface.objects.filter(**kwargs))
def setUp(self): self.arpa = self.create_domain(name='arpa') self.arpa.save() self.i_arpa = self.create_domain(name='in-addr.arpa') self.i_arpa.save() self.i6_arpa = self.create_domain(name='ip6.arpa') self.i6_arpa.save() self.r = Domain(name="ru") self.r.save() self.f_r = Domain(name="foo.ru") self.f_r.save() self.b_f_r = Domain(name="bar.foo.ru") self.b_f_r.save() self.f = Domain(name="fam") self.f.save() self._128 = self.create_domain(name='128', ip_type='4') self._128.save() self.s = System() self.s.save()
def setUp(self): self.system = System() Domain.objects.get_or_create(name="com") Domain.objects.get_or_create(name="mozilla.com")
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)
def migrate_dynamic_hosts(): print "Migrating dynamic hosts." default, _ = Workgroup.objects.get_or_create(name='default') 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 = ("id", "dynamic_range", "name", "workgroup", "enabled", "ha", "type", "os", "location", "department", "serial", "other_id", "purchase_date", "po_number", "warranty_date", "owning_unit", "user_id", "last_seen", "expire", "ttl", "last_update", "domain", "zone") sql = "SELECT %s FROM host WHERE ip = 0" % ", ".join(keys) count = 0 cursor.execute(sql) for values in cursor.fetchall(): items = dict(zip(keys, values)) enabled = items['enabled'] if len(items['ha']) == 0: mac = None elif len(items['ha']) == 12: if items['ha'] == '0' * 12: mac = None enabled = False else: mac = items['ha'] else: stderr.write( 'Host with id {} has invalid hardware address "{}"'.format( items['id'], items['ha'])) continue # TODO: Verify that there is no valid range/zone/workgroup with id 0 r, c, w = None, None, default if items['dynamic_range']: try: r = maintain_find_range(items['dynamic_range']) except ObjectDoesNotExist: stderr.write( 'Could not create dynamic interface %s: Range %s ' 'is in Maintain, but was not created in Cyder.' % (items['ha'], items['dynamic_range'])) if items['zone']: c = maintain_find_zone(items['zone']) if items['workgroup']: w = maintain_find_workgroup(items['workgroup']) if not all([r, c]): stderr.write('Trouble migrating host with mac {0}\n' .format(items['ha'])) continue s = System(name=items['name']) s.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=s, attribute=attr, value=value) eav.full_clean() eav.save() last_seen = items['last_seen'] or None if last_seen: last_seen = datetime.fromtimestamp(last_seen) intr = DynamicInterface( range=r, workgroup=w, ctnr=c, mac=mac, system=s, dhcp_enabled=enabled, last_seen=last_seen) try: intr.save(update_range_usage=False, commit=False) except ValidationError as e: try: intr.dhcp_enabled = False intr.save(update_range_usage=False) stderr.write( 'WARNING: Dynamic interface with MAC address {} has been ' 'disabled\n'.format(intr.mac)) stderr.write(' {}\n'.format(e)) except ValidationError as e: stderr.write( 'WARNING: Could not create dynamic interface with MAC ' 'address {}\n'.format(intr.mac)) stderr.write(' {}\n'.format(e)) intr = None if intr: count += 1 if not count % 1000: print "%s valid hosts found so far." % count print "%s valid hosts found." % count