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 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 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 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 """ 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)