Changeset 11513 in genesis


Ignore:
Timestamp:
Sep 30, 2012, 7:12:43 PM (12 years ago)
Author:
rick
Message:

Try to standardize interface creating and editing via CLI.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • tools/node-editor

    r11510 r11513  
    2020def rlprompt(prompt, prefill=''):
    2121   ''' Get line with some boring default '''
     22   prompt = "%s [%s]: " % (prompt, prefill)
    2223   if DEBUG:
     24     print prompt
    2325     return prefill
    24    return raw_input("%s%s: " % (prompt, ' [' + str(prefill) + ']' if prefill else '')) or prefill
     26   return raw_input(prompt) or prefill
    2527
    2628def rlinput(prompt, prefill=''):
    2729   ''' Get line with some editable boring default '''
    2830   if DEBUG:
     31     print "%s [%s]:" % (prompt, prefill)
    2932     return prefill
    3033   readline.set_startup_hook(lambda: readline.insert_text(prefill))
     
    4548def rlinput_choices(prompt, choices, prefill=''):
    4649  if DEBUG:
     50    print '%s [%s]:' % (prompt, prefill)
    4751    return prefill
    4852  while True:
    49     filtered_choices = [x for x in choices if (str(prefill).lower() in str(x).lower())]
     53    filtered_choices = sorted(list(set([x for x in choices if (str(prefill).lower() in str(x).lower())])))
    5054    if len(filtered_choices) == 1:
    5155      return filtered_choices[0]
     
    8185  datadump['master_ip'] = master_ip
    8286
    83   if rlprompt("Add ath0 Accesspoint", "yes") == "yes":
     87  def new_access_point():
    8488    ifname = rlinput("Interface name", "ath0")
    8589    datadump['iface_' + ifname] = {
     
    9599    }
    96100
    97   if_nr = 0
    98   while rlprompt("Add Interlink", "yes") == "yes":
    99     ifname = rlinput("Interface name", "vr%i" % if_nr)
    100     if_nr = if_nr + 1
    101     remote = rlinput_choices("Create interlink", gformat.get_hostlist(),'HybridCeTIM2')
    102     remote_datadump = gformat.get_yaml(remote)
    103     print "Available remote interfaces:"
    104     for ifkey in remote_datadump['autogen_iface_keys']:
    105       ifdump = remote_datadump[ifkey]
    106       print " - %4s" % ifdump['autogen_ifbase'], '- %30s' % ifdump['comment'],
    107       if ifdump.has_key('ssid'):
    108         print '-', ifdump['ssid']
    109       else:
    110         print ''
    111     iface = rlinput_choices("Interface Used", remote_datadump['autogen_iface_keys'], "vr1")
     101  newNode.if_nr = 0
     102  def new_interlink(link_type='wireless', link_mode='station'):
     103    ifname = rlinput("Interface name", "vr%i" % newNode.if_nr)
     104    newNode.if_nr = newNode.if_nr + 1
    112105    netmask = 28
    113     print "Generating the Interlink IP range..."
    114     link_network = getrange.get_ranges(True, netmask, 1)[0]
    115 
     106    if link_mode == 'station':
     107      remote = rlinput_choices("Create interlink with (remote host)", gformat.get_hostlist(),'Hybrid')
     108      remote_datadump = gformat.get_yaml(remote)
     109      print "Available remote interfaces:"
     110      if_choices = []
     111      for ifkey in remote_datadump['autogen_iface_keys']:
     112        ifdump = remote_datadump[ifkey]
     113        # Could only do this if all meta information is correct
     114        #if link_type == 'wireless' and link_mode == 'station':
     115        #  basedump = remote_datadump['iface_' + ifdump['autogen_ifbase']]
     116        #  if not basedump.has_key('mode') or basedump['mode'] != 'ap-wds':
     117        #    continue
     118        if_choices.append(ifdump['autogen_ifbase'])
     119        print " - %4s" % ifdump['autogen_ifbase'], '- %20s' % ifdump['ip'], '- %30s' % ifdump['comment'],
     120        if ifdump.has_key('ssid'):
     121          print '-', ifdump['ssid']
     122        else:
     123          print ''
     124      iface = rlinput_choices("Remote interface", if_choices)
     125      ifdump = remote_datadump['iface_%s' % iface]
     126
     127      #make_alias = rlprompt("Create Alias", "no") == "yes"
     128      link_network = gformat.network(ifdump['ip'])
     129    elif link_mode == 'master':
     130      remote = rlinput("Interlink is planned for (remote host)", 'Unused%s' % newNode.if_nr)
     131      print "Generating the Interlink IP range..."
     132      link_network = getrange.get_ranges(True, netmask, 1)[0]
     133
     134    print "Using Network %s/%s" % (gformat.showaddr(link_network), netmask)
     135
     136    # XXX: Create remote linking
     137    # XXX: This does not handle IP pools
     138
     139    # Common part
    116140    datadump['iface_%s' % ifname] = {
    117141      'comment': rlinput(" - Comment", "Link naar " + remote),
    118142      'sdesc': rlinput(" - Short Description", "2" + remote),
    119       'ssid': rlinput(" - SSID", 'il-X.wleiden.net'),
     143      'type': "eth",
     144      'status': 'up',
    120145      'dhcp': 'False',
    121       'mode': "ap-wds",
    122       'ip': rlinput(" - IP", getrange.showaddr(link_network + 1) + "/" + str(netmask)),
    123       'type': "eth",
    124       'ns_ip': rlinput(" - NS IP", getrange.showaddr(link_network + 2) + "/" + str(netmask)),
    125       'ns_mac': rlinput(" - NS MAC", "00:27:22:"),
    126       'bridge_type': rlinput(' - Bridge Type', 'NanoStation5M'),
    127       'status': 'up',
    128146    }
    129     break
    130 
     147    if link_type == 'wireless':
     148      compass = rlprompt("Compass direction", 'n')
     149      datadump['iface_%s' % ifname].update({
     150        'ns_mac': rlinput(" - NS MAC", "00:27:22:"),
     151        'bridge_type': rlinput(' - Bridge Type', 'NanoStation5M'),
     152        'compass': compass,
     153      })
     154      if link_mode == 'master':
     155        datadump['iface_%s' % ifname].update({
     156          'ssid': rlinput(" - SSID", 'il-%s.%s.wleiden.net' % (compass, datadump['nodename'])),
     157          'ip': rlinput(" - IP", getrange.showaddr(link_network + 1) + "/" + str(netmask)),
     158          'ns_ip': rlinput(" - NS IP", getrange.showaddr(link_network + 2) + "/" + str(netmask)),
     159          'mode': "ap-wds",
     160        })
     161      elif link_mode == 'station':
     162        datadump['iface_%s' % ifname].update({
     163          'ssid': rlinput(" - SSID", ifdump['ssid']),
     164          'ip': rlinput(" - IP", getrange.showaddr(link_network + 3) + "/" + str(netmask)),
     165          'ns_ip': rlinput(" - NS IP", getrange.showaddr(link_network + 4) + "/" + str(netmask)),
     166          'mode': "station-wds",
     167        })
     168    elif link_type == 'wired':
     169      if link_mode == 'master':
     170        datadump['iface_%s' % ifname].update({
     171          'ip': rlinput(" - IP", getrange.showaddr(link_network + 1) + "/" + str(netmask)),
     172        })
     173      elif link_mode == 'station':
     174        datadump['iface_%s' % ifname].update({
     175          'ip': rlinput(" - IP", getrange.showaddr(link_network + 2) + "/" + str(netmask)),
     176        })
     177
     178  def printMenu():
     179    print '''\
     180==========================================
     181 Create Interfaces
     182 
     183  1) New Accesspoint (ath0)
     184  2) New Wired Interlink (station)
     185  3) New Wired Interlink (master)
     186  4) New Wireless Interlink (station)
     187  5) New Wireless Interlink (master)
     188  9) Finish and Save
     189==========================================
     190  '''
     191
     192  choice = 5
     193  while choice != 9:
     194    printMenu()
     195    choice = int(rlprompt("Choice", choice))
     196    if choice == 1:
     197      new_access_point()
     198    elif choice == 2:
     199      new_interlink('wired', 'station')
     200    elif choice == 3:
     201      new_interlink('wired', 'master')
     202    elif choice == 4:
     203      new_interlink('wireless', 'station')
     204    elif choice == 5:
     205      new_interlink('wireless', 'master')
     206    else:
     207      print "Invalid choice!"
     208
     209  # Output final result
    131210  print datadump
     211
    132212  #os.mkdir(datadump['autogen_item'])
    133213  #gformat.store_yaml(datadump)
     
    137217if __name__ == '__main__':
    138218  def printMenu():
    139     print '''Node File Editor by Rick van der Zwet
     219    print '''\
     220==========================================
     221Node File Editor by Rick van der Zwet
    140222 
    141223  1) New Node
    142224  9) Quit
     225==========================================
    143226  '''
    144227
Note: See TracChangeset for help on using the changeset viewer.