Changeset 6473 for trunk


Ignore:
Timestamp:
Dec 21, 2008, 2:20:33 AM (16 years ago)
Author:
andrea
Message:
  • interfaces and links handling should now be correct. Checks are still needed to determine correctness of imported data
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/exodus/scripts/genesis_to_exodus.py

    r6466 r6473  
     1#
     2# Usage: django runscript genesis_to_exodus /path/to/nodes/py.conf
     3#
     4
    15from exodus.forms import NodeForm, LocationForm, InterfaceForm
    2 from exodus.models import Node, Location, Interface
     6from exodus.models import Node, Location, Interface, Antenna
    37import ConfigParser
    48from sys import argv
     
    812    if len(locsearch):
    913        locmodel = locsearch[0]
    10         location = LocationForm(instance=locmodel)
     14        # XXX - should we update an existing location with new lat/lon data ?
    1115    else:
    1216        location = LocationForm({ 'description': location['descr'],
    1317            'latitude': location['lat'], 'longitude': location['lon'] })
     18        locmodel = location.save()
    1419        print 'Location ' + location['descr'] + 'created'
    15         locmodel = location.save()
    1620        if not location.is_valid():
    1721            print location._errors
     
    2731        form = NodeForm(node)
    2832        if form.is_valid():
     33            nodemodel = form.save()
    2934            print 'Node '+node['name']+' created'
    30             nodemodel = form.save()
    3135        else:
    3236            print form._errors
     
    4650    else:
    4751        print 'Can\'t import interface '+interface['iface']
    48         print form._errors
     52        print form._errors # XXX - outputs html but this is supposed to be a console script
    4953        return None
    5054    return intfmodel.id
    5155
     56def import_antenna(antenna):
     57    antsearch = Antenna.objects.filter(type__exact=antenna['type'], gain__exact=antenna['gain'])
     58    if len(antsearch):
     59        return antsearch[0].id
     60    else:
     61        newantenna = Antenna(type=antenna['type'], gain=antenna['gain'])
     62        newantenna.save()
     63        return newantenna.id
    5264
    5365def run():
     
    5870    dataform ={'name' : 'Foo8', 'location' : '1', 'status' : 'up',  'network' : '1', 'masterip' : '127.0.0.1' }
    5971
     72    networks = { }
    6073    config = ConfigParser.SafeConfigParser(dataform)
    6174    config.read(argv[len(argv)-1]) # last cmdline argument is supposed to be the config file to use
     
    8194                interfaces = config.get(s, 'interfaces')
    8295                for intf in interfaces.split(','):
    83                     cfgsection = node['name']+'/'+intf
    84                     if config.has_section(cfgsection): # check for an interface-specific block
    85                         # interface defaults
    86                         interface = { 'iface': intf, 'netmask': 24, 'ip': '127.0.0.1', 'mode': 'ms' }
     96                    if intf.find(':') == -1: # alias interface
     97                        cfgsection = node['name']+'/'+intf
     98                        if config.has_section(cfgsection): # check for an interface-specific block
     99                            # interface defaults
     100                            interface = { 'iface': intf, 'netmask': 24, 'ip': '127.0.0.1', 'mode': 'ms', 'polar': '', 'type': 'eth', 'accesspoint': False }
    87101
    88                         if config.has_option(cfgsection, 'ip'):
    89                             ip = config.get(cfgsection, 'ip')
    90                             if ip.find('/') >= 0:
    91                                 interface['ip'] = ip.split('/')[0]
    92                                 interface['netmask'] = ip.split('/')[1]
    93                             else:
    94                                 interface['ip'] = ip
    95                         for key in [ 'type', 'desc', 'ssid', 'channel', 'mode']:
    96                             if (config.has_option(cfgsection, key)):
    97                                 interface[key] = config.get(cfgsection, key)
    98                         if interface['mode'] == 'master':
    99                             interface['accesspoint'] = 1
    100                         interface['node'] = nodeid
    101                         import_interface(interface)
     102                            if config.has_option(cfgsection, 'ip'):
     103                                ip = config.get(cfgsection, 'ip')
     104                                if ip.find('/') >= 0:
     105                                    interface['ip'] = ip.split('/')[0]
     106                                    interface['netmask'] = ip.split('/')[1]
     107                                else:
     108                                    interface['ip'] = ip
     109                            for key in [ 'type', 'desc', 'ssid', 'channel', 'mode', 'polar' ]:
     110                                if (config.has_option(cfgsection, key)):
     111                                    interface[key] = config.get(cfgsection, key)
     112                            interface['node'] = nodeid
    102113
     114                            # handle Antenna
     115                            # XXX - disabled because it seems to me that genesis doesn't have complete info on equipped antennas
     116                            #if config.has_option(cfgsection, 'antenna') and config.has_option(cfgsection, 'gain'):
     117                            #    antenna = config.get(cfgsection, 'antenna')
     118                            #    gain = config.get(cfgsection, 'gain')
     119                            #    if antenna and gain: # ensure we have both antenna type and gain otherwise model will complain
     120                            #        interface['antenna'] = import_antenna({ 'type': antenna, 'gain': gain })
     121
     122                            # handle wifi links
     123                            if config.has_option(cfgsection, 'ssid'):
     124                                ssid = interface['ssid']
     125                                if ssid not in networks:
     126                                    networks[ssid] = { 'links': [] }
     127                                if interface['mode'] == 'ms':
     128                                    interface['accesspoint'] = 1
     129                                    networks[ssid]['master'] = import_interface(interface)
     130                                else:
     131                                    networks[ssid]['links'].append(interface);
     132                            else: # XXX - should we handle links on not-wifi interfaces?
     133                                import_interface(interface)
     134
     135    # now we have already created all nodes and master interfaces...
     136    # let's continue configuring all wifi links!
     137    # XXX - Note we need to add links later to ensure having all 'master nodes'
     138    #       already configured and their interfaces (id) can be used to define the link.
     139    #       if done while processing the input file , it could happens to process managed
     140    #       interfaces sooner than their master ones and it's more convenient to just add
     141    #       links later (once we have already configured all master interfaces) instead of
     142    #       trying to update interfaces progressively
     143    for ssid in networks:
     144        for interface in networks[ssid]['links']:
     145            if 'master' in networks[ssid]:
     146                interface['link'] = networks[ssid]['master']
     147                import_interface(interface)
     148            else:
     149                print "No master defined for network " + ssid
    103150    pass
Note: See TracChangeset for help on using the changeset viewer.