Changeset 6507


Ignore:
Timestamp:
Dec 29, 2008, 10:09:18 PM (16 years ago)
Author:
roland
Message:

Mayor work on dhcpstatic.
Need to update static ipaddresses when changing ipaddresses on an interlink.

Location:
trunk/exodus
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified trunk/exodus/admin.py

    r6496 r6507  
    44from exodus.models import Location, Node, Network, Interface, Antenna, \
    55        DnsServer, DhcpStatic       
    6 from exodus.forms import NodeForm, InterfaceForm
     6from exodus.forms import NodeForm, InterfaceForm, DhcpStaticForm
    77from exodus.contrib import ReadOnlyAdminFields
    88
     
    4242        js = ("/exodus/media/js/admin/CollapsedFieldsets.js",
    4343                "/static/showandhide.js", )       
     44
     45class DhcpStaticAdmin(ReadOnlyAdminFields, admin.ModelAdmin):
     46    readonly = ('ipaddress',)
     47    form = DhcpStaticForm
     48    ordering = ('accesspoint__node', 'accesspoint')
     49    search_fields = ('hostname', 'macaddress')
    4450       
    4551
     
    5056advanced_admin.register(Node, NodeAdmin)
    5157advanced_admin.register(Interface, InterfaceAdmin)
    52 advanced_admin.register(DhcpStatic)
     58advanced_admin.register(DhcpStatic, DhcpStaticAdmin)
    5359
    5460plain_admin.register(Antenna)
  • TabularUnified trunk/exodus/forms.py

    r6496 r6507  
    11from django import forms
    2 from exodus.models import Location, Node, Interface
     2from exodus.models import Location, Node, Interface, DhcpStatic
    33from exodus.wllogic import free_master_ip, link_has_compat_type, \
    44        link_is_wireless, new_ssid, new_ssid_for_existing_interface
    5 from exodus.wllogic import MASTER, MANAGED, calc_subnet
     5from exodus.wllogic import MASTER, MANAGED, calc_subnet, network, show_addr
    66from exodus.settings import AP_NETMASK
    77from exodus.wlipcalc import IPCalc
     8import re
    89
    910class LocationForm(forms.ModelForm):
     
    1415    class Meta:
    1516       model = Node
     17
     18    def clean_name(self):
     19        name = self.cleaned_data.get('name')
     20        re_name = re.compile('[0-9a-zA-Z-]+')
     21        if not name == re_name.match(name).group():
     22            raise forms.ValidationError(
     23                    'Not a valid name. Use letters, digits and -.')
     24        return name
    1625
    1726    def clean_masterip(self):
     
    2534                masterip = self.cleaned_data.get('masterip')
    2635            else:
    27                 #XXX: Need to update ipconfigurations for depending interfaces
    28                 #XXX: Need to set netmask for interfaces
    2936                masterip = free_master_ip(new_network)
    3037                masterlinks = ( i for i in self.instance.interface_set.all()
     
    5562                        j.save()
    5663                    i.save()
     64                    # update the static hosts on this interface
     65                    for j in i.dhcpstatic_set.all():
     66                        ip_size = int(pow(2,32-netmask))
     67                        networkaddrL = network(i.ip, i.netmask)
     68                        # 1, -1 compensate for network and broadcast addr.
     69                        iplist = [show_addr(networkaddrL + k) for k in
     70                                xrange(1, ip_size-1)]
     71                        import pdb; pdb.set_trace() ;
     72                        for k in i.interface_set.all():
     73                            iplist.remove(k.ip)
     74                        #remove used dhcpstatic ips, beware that interfaces
     75                        #that haven't been updated yet have ipaddress that
     76                        #are not in iplist.
     77                        for k in i.dhcpstatic_set.all():
     78                            try:
     79                                iplist.remove(k.ipaddress)
     80                            except ValueError:
     81                                pass
     82                        iplist.reverse()
     83                        j.ipaddress = iplist.pop()
     84                        j.save()
    5785        return masterip       
    5886
     
    6088    class Meta:
    6189        model = Interface
     90
     91    def clean_iface(self):
     92        iface = self.cleaned_data.get('iface')
     93        re_iface = re.compile('[a-z]+[0-9]+')
     94        if not re_iface.match(iface) or \
     95                re_iface.match(iface).group() != iface:
     96            raise forms.ValidationError("Please enter a valid interface name")
     97        return iface
    6298
    6399    def clean_polar(self):
     
    116152            elif self.cleaned_data.get('accesspoint') and \
    117153                    self.instance.pk != link.pk:
    118                 raise forms.ValidationError( "A link can't be made to another interface when this interface has an accesspoint.")
     154                raise forms.ValidationError( "A link can't be made to another"
     155                        " interface when this interface has an accesspoint.")
    119156
    120157            # if link is referenced to itself, link is master and linkable
     
    170207            elif len(link.link.interface_set.all()) > 2 or \
    171208                    link.link.accesspoint:
    172                 raise forms.ValidationError(
    173                     "The other interface is in managed mode, and can't be changed to master.")
     209                raise forms.ValidationError( "The other interface is in"
     210                        " managed mode, and can't be changed to master.")
    174211            # We shouldn't come here, because all link possibilities are
    175212            # accounted for.
     
    206243        if not link:
    207244            if not ap:
    208                 self.data['netmask'] = 32
    209245                try:
    210246                    new_ip = IPCalc(node, 32)
     
    215251
    216252            if ap:
    217                 self.data['netmask'] = AP_NETMASK       
    218253                try:
    219254                    new_ip = IPCalc(node, AP_NETMASK)
     
    248283                link.save()
    249284                ip = new_ip.ips.pop()
    250                 self.data['netmask'] = netmask
    251285                # filter out own and master interface
    252286                for i in (i for i in all_links if not i.id in (pk, link.id)):
     
    275309                new_ip.ips.reverse()
    276310                ip = new_ip.ips.pop()
    277                 self.data['netmask'] = netmask
    278311                for i in (i for i in all_links if not i.id == pk):
    279312                    i.ip  = new_ip.ips.pop()
     
    324357        # iface is saved for the first time
    325358        if not edit and not link:
    326             return new_ssid(node, iface, accesspoint, direction)
     359            return new_ssid(node.network, node, iface, accesspoint, direction)
    327360
    328361        # iface is in managed mode
     
    336369        if self.instance.link_id != link.id or \
    337370                self.instance.accesspoint != accesspoint:
    338             return new_ssid(node, iface, accesspoint, direction)
     371            return new_ssid(node.network, node, iface, accesspoint, direction)
    339372       
    340373        # else, don't change ssid and return original
     
    355388       
    356389        return MANAGED
     390
     391class DhcpStaticForm(forms.ModelForm):
     392    class Meta:
     393        model = DhcpStatic
     394
     395    def clean_hostname(self):
     396        hostname = self.cleaned_data.get('hostname')
     397        re_hostname = re.compile('[0-9a-zA-Z-]+')
     398        if not hostname == re_hostname.match(hostname).group():
     399            raise forms.ValidationError(
     400                    'Not a valid hostname. Use letters, digits and -.')
     401        return hostname
     402
     403    def clean_macaddress(self):
     404        macaddress = self.cleaned_data.get('macaddress')
     405        re_mac = re.compile('([0-9a-fA-F]{2}[:]){5}[0-9a-fA-F]{2}')
     406        if not re_mac.match(macaddress):
     407            raise forms.ValidationError( "Please enter a valid macaddress in"
     408                    " the form of : XX:XX:XX:XX:XX:XX")
     409        return macaddress
     410
     411    def clean_accesspoint(self):
     412        accesspoint = self.cleaned_data.get('accesspoint')
     413        if not accesspoint.accesspoint:
     414            raise forms.ValidationError("Please choose an accesspoint that is"
     415                    " enabled as an accesspoint")
     416        return accesspoint
     417
     418    def clean_ipaddress(self):
     419        # XXX: don't forget to update this address when changing ips.
     420        accesspoint = self.cleaned_data.get('accesspoint')
     421        ip = accesspoint.ip
     422        netmask = accesspoint.netmask
     423        ip_size = int(pow(2,32-netmask))
     424        networkaddrL = network(ip, netmask)
     425        # 1, -1 compensate for network and broadcast addr.
     426        iplist = [show_addr(networkaddrL + i) for i in xrange(1, ip_size-1)]
     427        for i in accesspoint.interface_set.all():
     428            iplist.remove(i.ip)
     429        [iplist.remove(i.ipaddress) for i in
     430                accesspoint.dhcpstatic_set.all() if not
     431                i.pk == self.instance.pk]
     432        iplist.reverse()
     433        return iplist.pop()
  • TabularUnified trunk/exodus/models.py

    r6491 r6507  
    136136    hostname = models.CharField(max_length=10,unique=True)
    137137    macaddress = models.CharField(max_length=17)
    138     address = models.IPAddressField()
    139     public_accesspoint = models.ForeignKey(Interface)
     138    accesspoint = models.ForeignKey(Interface)
     139    ipaddress = models.IPAddressField(unique=True, blank=True)
    140140
    141141    class Meta:
     
    143143
    144144    def __unicode__(self):
    145         return self.hostname
     145        return "%s.%s.%s %s" % (self.hostname, self.accesspoint.node.name,
     146                self.accesspoint.node.network.name, self.macaddress)
Note: See TracChangeset for help on using the changeset viewer.