def _create_ipv4_intr_from_range(label, domain_name, system, mac, range_start, range_end): if range_start >= range_end - 1: errors['ip'] = ErrorList(["The start ip must be less than end ip."]) return None, errors ip = find_free_ip(range_start, range_end, ip_type='4') errors = ErrorDict() if not ip: errors['ip'] = ErrorList([ "Could not find free ip in range {0} - " "{1}".format(range_start, range_end) ]) return None, errors domain = ensure_domain(domain_name, inherit_soa=True) try: intr = StaticInterface(label=label, domain=domain, ip_str=str(ip), ip_type='4', system=system, mac=mac) intr.clean() except ValidationError, e: errors['interface'] = ErrorList(e.messages)
def do_add_intr(self, mac, label, domain, ip_str, ip_type='4'): 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 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_update_glue_record_intr(self): # Glue records can't change their name. glue = StaticInterface( label="ns788", domain=self.r, ip_str="128.193.1.10", ip_type="4", system=self.s, mac="11:22:33:44:55:66" ) glue.save() data = {"domain": self.r, "server": "ns788.ru"} ns = self.do_add(**data) self.assertTrue(ns.glue) glue.label = "asdfasdf" self.assertRaises(ValidationError, glue.clean)
def test_update_glue_to_no_intr(self): glue = StaticInterface( label="ns34", domain=self.r, ip_str="128.193.1.10", ip_type="4", system=self.s, mac="11:22:33:44:55:66" ) glue.save() data = {"domain": self.r, "server": "ns34.ru"} ns = self.do_add(**data) self.assertTrue(ns.glue) ns.server = "ns4.wee" ns.save() self.assertTrue(ns.glue is None)
def test_update_glue_to_no_intr(self): glue = StaticInterface(label='ns34', domain=self.r, ip_str='128.193.1.10', ip_type='4', system=self.s, mac="11:22:33:44:55:66") glue.save() data = {'domain': self.r, 'server': 'ns34.ru'} ns = self.do_add(**data) self.assertTrue(ns.glue) ns.server = "ns4.wee" ns.save() self.assertTrue(ns.glue is None)
def test_disallow_delete_of_glue_intr(self): # Interface glue records should not be allowed to be deleted. glue = StaticInterface( label="ns24", domain=self.f_r, ip_str="128.193.99.10", ip_type="4", system=self.s, mac="11:22:33:44:55:66" ) glue.clean() glue.save() data = {"domain": self.f_r, "server": "ns24.foo.ru"} ns = self.do_add(**data) self.assertTrue(ns.glue) self.assertEqual(ns.glue, glue) self.assertRaises(ValidationError, glue.delete)
def test_intr_exists(self): label = "tdfestyfoo" data = "waasdft" dom, _ = Domain.objects.get_or_create(name="cd") dom, _ = Domain.objects.get_or_create(name="what.cd") intr = StaticInterface(label=label, domain=dom, ip_str="10.0.0.1", ip_type='4', system=self.s, mac="11:22:33:44:55:66") intr.clean() intr.save() cn = CNAME(label=label, domain=dom, target=data) self.assertRaises(ValidationError, cn.full_clean)
def test_update_glue_record_intr(self): # Glue records can't change their name. glue = StaticInterface(label='ns788', domain=self.r, ip_str='128.193.1.10', ip_type='4', system=self.s, mac="11:22:33:44:55:66") glue.save() data = {'domain': self.r, 'server': 'ns788.ru'} ns = self.do_add(**data) self.assertTrue(ns.glue) glue.label = "asdfasdf" self.assertRaises(ValidationError, glue.clean)
def test_private_view_case_1_intr(self): intr = StaticInterface(label="asf", domain=self.f_o, ip_str="10.0.0.1", ip_type="4", mac="00:11:22:33:44:55", system=self.s) intr.clean() intr.save() # Object has to exist before views can be assigned. intr.views.add(self.private) intr.save()
def test_manual_assign_of_glue(self): # Test that assigning a different glue record doesn't get overriden by # the auto assinging during the Nameserver's clean function. glue = StaticInterface( label="ns25", domain=self.f_r, ip_str="128.193.99.10", ip_type="4", system=self.s, mac="11:22:33:44:55:66" ) glue.clean() glue.save() data = {"domain": self.f_r, "server": "ns25.foo.ru"} ns = self.do_add(**data) self.assertTrue(ns.glue) self.assertEqual(ns.glue, glue) glue2 = AddressRecord(label="ns25", domain=self.f_r, ip_str="128.193.1.78", ip_type="4") glue2.clean() glue2.save() ns.clean() # Make sure things didn't get overriden. self.assertEqual(ns.glue, glue) ns.glue = glue2 ns.save() # Refresh the object ns = Nameserver.objects.get(pk=ns.pk) # Again, Make sure things didn't get overriden. self.assertEqual(ns.glue, glue2) # Make sure we still can't delete. self.assertRaises(ValidationError, glue2.delete) self.assertRaises(ValidationError, ns.glue.delete) # We shuold be able to delelte the other one. glue.delete()
class Meta: always_return_data = True queryset = StaticInterface.objects.all() fields = StaticInterface.get_api_fields() + [ 'domain', 'views', 'system' ] authorization = Authorization() allowed_methods = allowed_methods resource_name = 'staticinterface'
def test1_no_system(self): mac = "15:22:33:44:55:66" label = "8888foo" domain = self.f_c ip_str = "188::" + mac ip_type = '6' r = StaticInterface(label=label, domain=domain, ip_str=ip_str, ip_type=ip_type, system=None) self.assertRaises(ValidationError, r.clean)
def _create_ipv4_intr_from_range(label, domain_name, system, mac, range_start, range_end): if range_start >= range_end - 1: errors['ip'] = ErrorList(["The start ip must be less than end ip."]) return None, errors ip = find_free_ip(range_start, range_end, ip_type='4') errors = ErrorDict() if not ip: errors['ip'] = ErrorList(["Could not find free ip in range {0} - " "{1}".format(range_start, range_end)]) return None, errors domain = ensure_domain(domain_name, inherit_soa=True) try: intr = StaticInterface(label=label, domain=domain, ip_str=str(ip), ip_type='4', system=system, mac=mac) intr.clean() except ValidationError, e: errors['interface'] = ErrorList(e.messages)
def test_intr_cname_exists(self): # Duplicate test? label = "tesafstyfoo" data = "wadfakt" dom, _ = Domain.objects.get_or_create(name="cd") dom, _ = Domain.objects.get_or_create(name="what.cd") cn, _ = CNAME.objects.get_or_create( label=label, domain=dom, target=data) cn.full_clean() cn.save() intr = StaticInterface(label=label, domain=dom, ip_str="10.0.0.2", ip_type='4', system=self.s, mac="00:11:22:33:44:55") self.assertRaises(ValidationError, intr.clean) cn.label = "differentlabel" cn.save() intr.clean() intr.save()
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_intr_cname_exists(self): # Duplicate test? label = "tesafstyfoo" data = "wadfakt" dom, _ = Domain.objects.get_or_create(name="cd") dom, _ = Domain.objects.get_or_create(name="what.cd") cn, _ = CNAME.objects.get_or_create(label=label, domain=dom, target=data) cn.full_clean() cn.save() intr = StaticInterface(label=label, domain=dom, ip_str="10.0.0.2", ip_type='4', system=self.s, mac="00:11:22:33:44:55") self.assertRaises(ValidationError, intr.clean) cn.label = "differentlabel" cn.save() intr.clean() intr.save()
def testtest_add_ns_in_domain_intr(self): # Use an Interface as a glue record. glue = StaticInterface( label="ns232", domain=self.r, ip_str="128.193.99.10", ip_type="4", system=self.s, mac="12:23:45:45:45:45" ) glue.clean() glue.save() data = {"domain": self.r, "server": "ns232.ru"} ns = self.do_add(**data) self.assertTrue(ns.glue) self.assertEqual(ns.server, ns.glue.fqdn) self.assertRaises(ValidationError, glue.delete) glue = StaticInterface( label="ns332", domain=self.f_r, ip_str="128.193.1.10", ip_type="4", system=self.s, mac="11:22:33:44:55:66" ) glue.clean() glue.save() data = {"domain": self.f_r, "server": "ns332.foo.ru"} ns = self.do_add(**data) self.assertTrue(ns.glue) self.assertEqual(ns.server, ns.glue.fqdn)
def add_intr_ipv4(self, ip): intr = StaticInterface( label=random_label(), domain=self.domain, ip_str=ip, ip_type='4', system=self.s, mac='11:22:33:44:55:66' ) intr.clean() intr.save() return intr
def test_disallow_name_update_of_glue_Intr(self): # Glue records should not be allowed to change their name. glue = StaticInterface(label='ns24', domain=self.f_r, ip_str='128.193.99.10', ip_type='4', system=self.s, mac="11:22:33:44:55:66") glue.clean() glue.save() data = {'domain': self.f_r, 'server': 'ns24.foo.ru'} ns = self.do_add(**data) self.assertTrue(ns.glue) self.assertEqual(ns.glue, glue) glue.label = "ns22" self.assertRaises(ValidationError, glue.clean)
def test_manual_assign_of_glue(self): # Test that assigning a different glue record doesn't get overriden by # the auto assinging during the Nameserver's clean function. glue = StaticInterface(label='ns25', domain=self.f_r, ip_str='128.193.99.10', ip_type='4', system=self.s, mac="11:22:33:44:55:66") glue.clean() glue.save() data = {'domain': self.f_r, 'server': 'ns25.foo.ru'} ns = self.do_add(**data) self.assertTrue(ns.glue) self.assertEqual(ns.glue, glue) glue2 = AddressRecord(label='ns25', domain=self.f_r, ip_str='128.193.1.78', ip_type='4') glue2.clean() glue2.save() ns.clean() # Make sure things didn't get overriden. self.assertEqual(ns.glue, glue) ns.glue = glue2 ns.save() # Refresh the object ns = Nameserver.objects.get(pk=ns.pk) # Again, Make sure things didn't get overriden. self.assertEqual(ns.glue, glue2) # Make sure we still can't delete. self.assertRaises(ValidationError, glue2.delete) self.assertRaises(ValidationError, ns.glue.delete) # We shuold be able to delelte the other one. glue.delete()
def do_combine_a_ptr_to_interface( addr, ptr, system, mac_address=None, interface=None, dhcp_hostname=None, dhcp_domain_name=None, dhcp_domain_name_servers=None, dhcp_filename=None): if mac_address == '00:00:00:00:00:00' or mac_address is None: next_adapter = system.get_next_key_value_adapter() if (addr.ip_str != ptr.ip_str or addr.fqdn != ptr.name or addr.ip_type != ptr.ip_type): raise ValidationError("This A and PTR have different data.") intr = StaticInterface( label=addr.label, mac=mac_address, domain=addr.domain, ip_str=addr.ip_str, ip_type=addr.ip_type, system=system) addr_deleted = False ptr_deleted = False addr.delete(check_cname=False) addr_deleted = True ptr.delete() ptr_deleted = True intr.full_clean() intr.save() if interface: from cyder.core.systems.system_api import SystemResource intr.update_attrs() adapter_type, primary, alias = SystemResource.extract_nic_attrs( interface) intr.attrs.primary = primary intr.attrs.alias = alias intr.attrs.interface_type = adapter_type if dhcp_hostname: intr.attrs.hostname = dhcp_hostname if dhcp_filename: intr.attrs.filename = dhcp_filename if dhcp_domain_name: intr.attrs.domain_name = dhcp_domain_name if dhcp_domain_name_servers: intr.attrs.domain_name_servers = dhcp_domain_name_servers return intr, addr_deleted, ptr_deleted
def test_bad_nameserver_soa_state_case_1_4(self): # This is Case 1 ... with StaticInterfaces's reverse_root_domain = create_fake_zone("14.in-addr.arpa", suffix="") root_domain = create_fake_zone("asdf14") for ns in root_domain.nameserver_set.all(): ns.delete() # At his point we should have a domain at the root of a zone with no # other records in it. # Let's create a child domain and try to add a record there. cdomain = Domain(name="10.14.in-addr.arpa") cdomain.soa = reverse_root_domain.soa cdomain.save() # Adding a record shouldn't be allowed because there is no NS record on # the zone's root domain. intr = StaticInterface(label="asdf", domain=root_domain, ip_str="14.10.1.1", ip_type="4", mac="11:22:33:44:55:66", system=self.s) self.assertRaises(ValidationError, intr.save)
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 quick_create(request, system_pk): # TODO, make sure the user has access to this system system = get_object_or_404(System, pk=system_pk) if request.method == 'POST': interface_form = StaticInterfaceQuickForm(request.POST) a, ptr, r = None, None, None if interface_form.is_valid(): try: #mac = interface_form.cleaned_data['mac'] if 'label' in interface_form.cleaned_data: label = interface_form.cleaned_data['label'] else: label = "" mrange_pk = interface_form.cleaned_data['range'] mrange = get_object_or_404(Range, pk=mrange_pk) network = mrange.network ip_type = network.ip_type vlan = network.vlan site = network.site networks = [] for network in vlan.network_set.all(): if not network.site: continue if network.site.get_site_path() == site.get_site_path(): networks.append(network) if not networks: raise ValidationError( "No appropriate networks found. " "Consider adding this interface manually.") ip = mrange.get_next_ip() if ip is None: raise ValidationError( "No appropriate IP found in {0} Vlan {1} " "Range {2} - {3}. Consider adding this interface " "manually.".format(site.name, vlan.name, mrange.start_str, mrange.end_str)) expected_name = "{0}.{1}.mozilla.com".format( vlan.name, site.get_site_path()) try: domain = Domain.objects.get(name=expected_name) except ObjectDoesNotExist, e: raise ValidationError( "The domain '{0}' doesn't seem to exist. " "Consider creating this interface " "manually.".format(expected_name)) intr = StaticInterface(label=label, domain=domain, ip_str=str(ip), ip_type=ip_type, system=system) intr.full_clean() intr.save() except ValidationError, e: interface_form._errors['__all__'] = ErrorList(e.messages) return render( request, 'static_intr/static_intr_form.html', { 'form': interface_form, 'form_title': "Quick Interface Create for System " "{0}".format(system) })
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 process_extra(self, bundle, request, **kwargs): patch_dict = json.loads(request.POST.items()[0][0]) if 'delete_interface' in patch_dict and 'interface' in patch_dict: patch_dict.pop('delete_interface') sys = bundle.obj interface = patch_dict.pop('interface', None) if sys and interface: sys.delete_adapter(interface) if 'update_interface' in patch_dict and 'interface' in patch_dict: patch_dict.pop('update_interface') sys = bundle.obj if 'interface' in patch_dict: sys.update_adapter(**patch_dict) ## Entry point for adding a new adapter by mac address via the rest API if 'mac' in patch_dict: enable_dns = True enable_private = True enable_public = False sys = bundle.obj range = patch_dict.pop('range', None) fqdn = patch_dict.pop('fqdn', None) ip_str = patch_dict.pop('ip_address', None) interface = patch_dict.pop('interface', None) domain = patch_dict.pop('domain', 'mozilla.com') from cyder.cydhcp.lib.utils import create_ipv4_intr_from_range mac = patch_dict['mac'] if interface: interface_type, primary, alias = SystemResource.extract_nic_attrs( interface) else: interface_type, primary, alias = sys.get_next_adapter() if not fqdn: domain_parsed = ".".join( sys.hostname.split('.')[1:]) + '.' + domain domain_name = domain_parsed.lower() label = sys.hostname.split('.')[0] else: domain_parsed = ".".join(fqdn.split('.')[1:]) domain_name = domain_parsed.lower() label = fqdn.split('.')[0] if range: range_start_str = range.split(',')[0] range_end_str = range.split(',')[1] s, errors = create_ipv4_intr_from_range( label, domain_name, sys, mac, range_start_str, range_end_str) if ip_str: domain = Domain.objects.filter(name=domain_parsed)[0] s = StaticInterface(label=label, mac=mac, domain=domain, ip_str=ip_str, ip_type='4', system=sys) try: if s: s.save() s.update_attrs() if enable_dns and enable_public: private = View.objects.get(name='private') s.views.add(private) s.save() elif enable_dns and enable_private and not enable_public: private = View.objects.get(name='private') s.views.add(private) s.save() if interface: interface_type, primary, alias = SystemResource.extract_nic_attrs( interface) else: interface_type, primary, alias = sys.get_next_adapter() s.attrs.primary = primary s.attrs.interface_type = interface_type s.attrs.alias = alias else: print 'We failed' bundle.errors[ 'error_message'] = "Unable to create adapter for unknown reason" raise ValidationError(join(e.messages)) except ValidationError, e: bundle.errors['error_message'] = " ".join(e.messages) raise ValidationError(join(e.messages)) except Exception, e: print e
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 do_add(self, mac, label, domain, ip_str, system, ip_type="4"): r = StaticInterface(mac=mac, label=label, domain=domain, ip_str=ip_str, ip_type=ip_type, system=system) r.clean() r.save() return r
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 quick_create(request, system_pk): # TODO, make sure the user has access to this system system = get_object_or_404(System, pk=system_pk) if request.method == 'POST': interface_form = StaticInterfaceQuickForm(request.POST) a, ptr, r = None, None, None if interface_form.is_valid(): try: #mac = interface_form.cleaned_data['mac'] if 'label' in interface_form.cleaned_data: label = interface_form.cleaned_data['label'] else: label = "" mrange_pk = interface_form.cleaned_data['range'] mrange = get_object_or_404(Range, pk=mrange_pk) network = mrange.network ip_type = network.ip_type vlan = network.vlan site = network.site networks = [] for network in vlan.network_set.all(): if not network.site: continue if network.site.get_site_path() == site.get_site_path(): networks.append(network) if not networks: raise ValidationError("No appropriate networks found. " "Consider adding this interface manually.") ip = mrange.get_next_ip() if ip is None: raise ValidationError("No appropriate IP found " "in {0} Vlan {1} Range {2} - {3}. Consider adding " "this interface manually.".format(site.name, vlan.name, mrange.start_str, mrange.end_str)) expected_name = "{0}.{1}.mozilla.com".format(vlan.name, site.get_site_path()) print "Expected name {0}".format(expected_name) try: domain = Domain.objects.get(name=expected_name) except ObjectDoesNotExist, e: raise ValidationError("The domain '{0}' doesn't seem to " "exist. Consider creating this interface " "manually.".format(expected_name)) intr = StaticInterface(label=label, domain=domain, ip_str=str(ip), #ip_type=ip_type, mac=mac, system=system) ip_type=ip_type, system=system) intr.full_clean() intr.save() except ValidationError, e: interface_form._errors['__all__'] = ErrorList(e.messages) return render(request, 'static_intr/static_intr_form.html', { 'form': interface_form, 'form_title': "Quick Interface Create for System " "{0}".format(system) })
def test_private_view_case_3_intr(self): intr = StaticInterface( label="asf3", domain=self.f_o, ip_str="10.0.0.1", ip_type="4", mac="01:11:22:33:44:55", system=self.s) intr.clean() intr.save() intr.views.add(self.private) intr.save() # Object has to exist before views can be assigned. intr.views.add(self.public) self.assertFalse(intr.views.filter(name="public")) intr = StaticInterface( label="asf3", domain=self.f_o, ip_str="172.31.255.254", ip_type="4", mac="01:11:22:33:44:55", system=self.s) intr.clean() intr.save() intr.views.add(self.private) intr.save() # Object has to exist before views can be assigned. intr.views.add(self.public) self.assertFalse(intr.views.filter(name="public")) intr = StaticInterface(label="asf3", domain=self.f_o, ip_str="192.168.255.254", ip_type="4", mac="01:11:22:33:44:55", system=self.s) intr.clean() intr.save() intr.views.add(self.private) intr.save() # Object has to exist before views can be assigned. intr.views.add(self.public) self.assertFalse(intr.views.filter(name="public"))
def process_extra(self, bundle, request, **kwargs): patch_dict = json.loads(request.POST.items()[0][0]) if 'delete_interface' in patch_dict and 'interface' in patch_dict: patch_dict.pop('delete_interface') sys = bundle.obj interface = patch_dict.pop('interface', None) if sys and interface: sys.delete_adapter(interface) if 'update_interface' in patch_dict and 'interface' in patch_dict: patch_dict.pop('update_interface') sys = bundle.obj if 'interface' in patch_dict: sys.update_adapter(**patch_dict) ## Entry point for adding a new adapter by mac address via the rest API if 'mac' in patch_dict: enable_dns = True enable_private = True enable_public = False sys = bundle.obj range = patch_dict.pop('range', None) fqdn = patch_dict.pop('fqdn', None) ip_str = patch_dict.pop('ip_address', None) interface = patch_dict.pop('interface', None) domain = patch_dict.pop('domain', 'mozilla.com') from cyder.cydhcp.lib.utils import create_ipv4_intr_from_range mac = patch_dict['mac'] if interface: interface_type, primary, alias = SystemResource.extract_nic_attrs(interface) else: interface_type, primary, alias = sys.get_next_adapter() if not fqdn: domain_parsed = ".".join( sys.hostname.split('.')[1:]) + '.' + domain domain_name = domain_parsed.lower() label = sys.hostname.split('.')[0] else: domain_parsed = ".".join(fqdn.split('.')[1:]) domain_name = domain_parsed.lower() label = fqdn.split('.')[0] if range: range_start_str = range.split(',')[0] range_end_str = range.split(',')[1] s, errors = create_ipv4_intr_from_range(label, domain_name, sys, mac, range_start_str, range_end_str) if ip_str: domain = Domain.objects.filter(name=domain_parsed)[0] s = StaticInterface(label=label, mac=mac, domain=domain, ip_str=ip_str, ip_type='4', system=sys) try: if s: s.save() s.update_attrs() if enable_dns and enable_public: private = View.objects.get(name='private') s.views.add(private) s.save() elif enable_dns and enable_private and not enable_public: private = View.objects.get(name='private') s.views.add(private) s.save() if interface: interface_type, primary, alias = SystemResource.extract_nic_attrs(interface) else: interface_type, primary, alias = sys.get_next_adapter() s.attrs.primary = primary s.attrs.interface_type = interface_type s.attrs.alias = alias else: print 'We failed' bundle.errors['error_message'] = "Unable to create adapter for unknown reason" raise ValidationError(join(e.messages)) except ValidationError, e: bundle.errors['error_message'] = " ".join(e.messages) raise ValidationError(join(e.messages)) except Exception, e: print e
def do_add(self, mac, label, domain, ip_str, ip_type='4'): r = StaticInterface(mac=mac, label=label, domain=domain, ip_str=ip_str, ip_type=ip_type, system=self.n) r.clean() r.save() r.details() r.get_update_url() r.get_delete_url() r.get_detail_url() repr(r) return r
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_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 testtest_add_ns_in_domain_intr(self): # Use an Interface as a glue record. glue = StaticInterface(label='ns232', domain=self.r, ip_str='128.193.99.10', ip_type='4', system=self.s, mac="12:23:45:45:45:45") glue.clean() glue.save() data = {'domain': self.r, 'server': 'ns232.ru'} ns = self.do_add(**data) self.assertTrue(ns.glue) self.assertEqual(ns.server, ns.glue.fqdn) self.assertRaises(ValidationError, glue.delete) glue = StaticInterface(label='ns332', domain=self.f_r, ip_str='128.193.1.10', ip_type='4', system=self.s, mac="11:22:33:44:55:66") glue.clean() glue.save() data = {'domain': self.f_r, 'server': 'ns332.foo.ru'} ns = self.do_add(**data) self.assertTrue(ns.glue) self.assertEqual(ns.server, ns.glue.fqdn)
def test6_create_ipv4_interface(self): v, _ = Vlan.objects.get_or_create(name="6db", number=3) s, _ = Site.objects.get_or_create(name="6scl3") d, _ = Domain.objects.get_or_create(name="6scl3.mozilla.com") d, _ = Domain.objects.get_or_create(name="6db.6scl3.mozilla.com") 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="11.in-addr.arpa") n = Network(network_str="11.0.0.0/8", ip_type="4") n.clean() n.site = s n.vlan = v n.save() r = Range(start_str="11.0.0.0", end_str="11.0.0.2", network=n, ip_type='4') r.clean() r.save() s = StaticInterface(label="fab", domain=d, ip_type="4", ip_str="11.0.0.0", system=self.system, mac="00:00:00:00:00:00") s.clean() s.save() s = StaticInterface(label="fab", domain=d, ip_type="4", ip_str="11.0.0.1", system=self.system, mac="00:00:00:00:00:00") s.clean() s.save() s = StaticInterface(label="fab", domain=d, ip_type="4", ip_str="11.0.0.2", system=self.system, mac="00:00:00:00:00:00") s.clean() s.save() intr, errors = create_ipv4_interface("foo", "6db", "6scl3", self.system, "11:22:33:44:55:66", "mozilla.com") self.assertEqual(intr, None) self.assertTrue("vlan" not in errors) self.assertTrue("site" not in errors) self.assertTrue("domain" not in errors) self.assertTrue("network" not in errors) self.assertTrue("range" not in errors) self.assertTrue("ip" in errors)
def test6_create_ipv4_interface(self): v, _ = Vlan.objects.get_or_create(name="6db", number=3) s, _ = Site.objects.get_or_create(name="6scl3") d, _ = Domain.objects.get_or_create(name="6scl3.mozilla.com") d, _ = Domain.objects.get_or_create(name="6db.6scl3.mozilla.com") 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="11.in-addr.arpa") n = Network(network_str="11.0.0.0/8", ip_type="4") n.clean() n.site = s n.vlan = v n.save() r = Range(start_str="11.0.0.0", end_str="11.0.0.2", network=n) r.clean() r.save() s = StaticInterface(label="fab", domain=d, ip_type="4", ip_str="11.0.0.0", system=self.system, mac="00:00:00:00:00:00") s.clean() s.save() s = StaticInterface(label="fab", domain=d, ip_type="4", ip_str="11.0.0.1", system=self.system, mac="00:00:00:00:00:00") s.clean() s.save() s = StaticInterface(label="fab", domain=d, ip_type="4", ip_str="11.0.0.2", system=self.system, mac="00:00:00:00:00:00") s.clean() s.save() intr, errors = create_ipv4_interface( "foo", "6db", "6scl3", self.system, "11:22:33:44:55:66", "mozilla.com") self.assertEqual(intr, None) self.assertTrue("vlan" not in errors) self.assertTrue("site" not in errors) self.assertTrue("domain" not in errors) self.assertTrue("network" not in errors) self.assertTrue("range" not in errors) self.assertTrue("ip" in errors)