Changeset 6464 for trunk


Ignore:
Timestamp:
Dec 16, 2008, 10:13:08 AM (16 years ago)
Author:
andrea
Message:

Implementation of the migration script is quite done :

  • Interface handling needs to be completed.
  • Output messages need review (perhaps they are too noisy)
File:
1 edited

Legend:

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

    r6459 r6464  
    1 from exodus.forms import NodeForm
     1from exodus.forms import NodeForm, LocationForm, InterfaceForm
     2from exodus.models import Node, Location, Interface
     3import ConfigParser
     4from sys import argv
     5
     6def import_location(location):
     7    locsearch = Location.objects.filter(description__exact=location['descr'])
     8    if len(locsearch):
     9        locmodel = locsearch[0]
     10        location = LocationForm(instance=locmodel)
     11    else:
     12        location = LocationForm({ 'description': location['descr'],
     13            'latitude': location['lat'], 'longitude': location['lon'] })
     14        print 'Location ' + location['descr'] + 'created'
     15        locmodel = location.save()
     16        if not location.is_valid():
     17            print location._errors
     18            return 0
     19    return locmodel.id
     20
     21def import_node(node):
     22    nodesearch = Node.objects.filter(name__exact=node['name'])
     23    if len(nodesearch):
     24        print 'Node '+node['name']+' already exists'
     25        nodemodel = nodesearch[0]
     26    else:
     27        form = NodeForm(node)
     28        if form.is_valid():
     29            print 'Node '+node['name']+' created'
     30            nodemodel = form.save()
     31        else:
     32            print form._errors
     33            return 0
     34    return nodemodel.id
     35
     36def import_interface(interface):
     37    isearch = Interface.objects.filter(node__exact=interface['node'], iface__exact=interface['iface'])
     38    if len(isearch):
     39        # XXX - should we update existing interfaces ??
     40        intfmodel = isearch[0]
     41        form = InterfaceForm(interface, instance=intfmodel)
     42    else:
     43        form = InterfaceForm(interface)
     44    if form.is_valid():
     45        intfmodel = form.save()
     46    else:
     47        print 'Can\'t import interface '+interface['iface']
     48        print form._errors
     49        return 0
     50    return intfmodel.id
     51
    252
    353def run():
    4     import pdb; pdb.set_trace() ;
    554    """Import script for genesis data."""
    655    print """Import script for genesis."""
     56
    757    # Please do mind to keep all the entries valid, altough some get values get updated
    858    dataform ={'name' : 'Foo8', 'location' : '1', 'status' : 'up',  'network' : '1', 'masterip' : '127.0.0.1' }
    9     form = NodeForm(dataform)
    10     if not form.is_valid():
    11         print form._errors
    12     else:
    13         form.save()
    1459
    15     # Alternative approch
    16     #from django.test.client import Client
    17     #c = Client()
    18     #c.login(username='admin', password='bert')
    19     #response = c.post('/admin/exodus/node/add', rick)
    20     #if response.content.find('errorlist') != -1:
    21     #    print "Import failure"
     60    config = ConfigParser.SafeConfigParser(dataform)
     61    config.read(argv[len(argv)-1]) # last cmdline argument is supposed to be the config file to use
     62    sections = config.sections()
     63    sections.sort()
     64    for s in sections:
     65        list = s.split('/')
     66        if s.find('/') == -1: # get only main node blocks (interface-specific blocks will be accessed later)
     67            node = { }
     68            for key in dataform:
     69                node[key] = config.get(s, key)
     70            if config.has_option(s, 'location'):
     71                location = { }
     72                location['descr'] = config.get(s, 'location')
     73                if location['descr']:
     74                    location['lat'] = config.get(s, 'latitude')
     75                    location['lon'] = config.get(s, 'longitude')
     76                    node['location'] = import_location(location)
     77                else:
     78                    node['location'] = 1 # unknown/unconfigured location has id 1
     79
     80            nodeid = import_node(node)
     81            if nodeid:
     82                interfaces = config.get(s, 'interfaces')
     83                for intf in interfaces.split(','):
     84                    cfgsection = node['name']+'/'+intf
     85                    if config.has_section(cfgsection): # check for an interface-specific block
     86                        # interface defaults
     87                        interface = { 'iface': intf, 'netmask': 24, 'ip': '127.0.0.1', 'mode': 'ms' }
     88
     89                        if config.has_option(cfgsection, 'ip'):
     90                            ip = config.get(cfgsection, 'ip')
     91                            if ip.find('/') >= 0:
     92                                interface['ip'] = ip.split('/')[0]
     93                                interface['netmask'] = ip.split('/')[1]
     94                            else:
     95                                interface['ip'] = ip
     96                        for key in [ 'type', 'desc', 'ssid', 'channel', 'mode']:
     97                            if (config.has_option(cfgsection, key)):
     98                                interface[key] = config.get(cfgsection, key)
     99                        if interface['mode'] == 'master':
     100                            interface['accesspoint'] = 1
     101                        interface['node'] = nodeid
     102                        import_interface(interface)
     103
    22104    pass
Note: See TracChangeset for help on using the changeset viewer.