source: genesis/tools/node-editor@ 11508

Last change on this file since 11508 was 11506, checked in by rick, 12 years ago

Starting with a very simple CLI based menu configurator allowing to quickly
manipulate the gformat entries. Current IP and link configuration are cumbersome.

  • Property svn:executable set to *
File size: 4.2 KB
Line 
1#!/usr/bin/env python
2'''
3 CLI interface which configures and makes new nodes with the most commonly
4 used commands.
5
6 Rick van der Zwet <info@rickvanderzwet.nl>
7'''
8
9import getrange
10import gformat
11import os
12import readline
13import sys
14
15
16print "WARNING I AM NOT PRODUCTION CODE YET!" + __doc__
17
18DEBUG = "-g" in sys.argv
19
20def rlprompt(prompt, prefill=''):
21 ''' Get line with some boring default '''
22 if DEBUG:
23 return prefill
24 return raw_input("%s%s: " % (prompt, ' [' + str(prefill) + ']' if prefill else '')) or prefill
25
26def rlinput(prompt, prefill=''):
27 ''' Get line with some editable boring default '''
28 if DEBUG:
29 return prefill
30 readline.set_startup_hook(lambda: readline.insert_text(prefill))
31 try:
32 return raw_input(prompt + ": ")
33 finally:
34 readline.set_startup_hook()
35
36def rlinput_float(prompt, prefill=0):
37 while True:
38 try:
39 prefill = rlinput(prompt, prefill)
40 return float(prefill)
41 except ValueError:
42 print "Invalid Number!"
43 pass
44
45def rlinput_choices(prompt, choices, prefill=''):
46 if DEBUG:
47 return prefill
48 while True:
49 filtered_choices = [x for x in choices if (str(prefill).lower() in str(x).lower())]
50 if len(filtered_choices) == 1:
51 return filtered_choices[0]
52 elif len(filtered_choices) == 0:
53 filtered_choices = choices
54 prefill = rlinput(prompt + ' ' + str(filtered_choices), prefill)
55 if prefill in prompt:
56 return prefill
57
58
59
60
61rlinput_choices("Test", gformat.get_hostlist())
62
63
64def newNode():
65 datadump = {
66 'nodename' : "New",
67 'location' : "1234 AB, Leiden",
68 'nodetype' : "Hybrid",
69 'latitude' : "52.",
70 'longitude' : "4.",
71 }
72 datadump['nodename'] = rlprompt("Nodename", datadump['nodename'])
73 datadump['location'] = rlprompt("Location", datadump['location'])
74 datadump['nodetype'] = rlprompt("Nodetype", datadump['nodetype'])
75 datadump['latitude'] = rlinput_float("Latitude", datadump['latitude'])
76 datadump['longitude'] = rlinput_float("Longitude", datadump['longitude'])
77 datadump['autogen_item'] = datadump['nodetype'] + datadump['nodename']
78
79 print "Generating the Master IP range..."
80 master_network = getrange.get_ranges(False, 24, 1)[0]
81 master_ip = getrange.showaddr(master_network + 1)
82 datadump['master_ip'] = master_ip
83
84 if rlprompt("Add ath0 Accesspoint", "yes") == "yes":
85 ifname = rlinput("Interface name", "ath0")
86 datadump['iface_' + ifname] = {
87 'ip' : rlinput(" - IP", master_ip + "/26"),
88 'dhcp' : rlinput(" - DHCP range", "10-60"),
89 'ssid' : rlinput(" - SSID", "ap-WirelessLeiden-" + datadump['nodename']),
90 'mode' : 'ap',
91 'type' : '11b',
92 'comment' : rlinput(" - Comment", "Omni voor de Buurt"),
93 'sdesc': rlinput(" - Short Description", "2buurt"),
94 'channel' : rlprompt(" - Channel", "10"),
95 'status' : 'up',
96 }
97
98 if_nr = 0
99 while rlprompt("Add Interlink", "yes") == "yes":
100 ifname = rlinput("Interface name", "vr%i" % if_nr)
101 if_nr = if_nr + 1
102 remote = rlinput("Remote Node", "Unused")
103 netmask = 28
104 print "Generating the Interlink IP range..."
105 link_network = getrange.get_ranges(True, netmask, 1)[0]
106 datadump['iface_%s' % ifname] = {
107 'comment': rlinput(" - Comment", "Link naar " + remote),
108 'sdesc': rlinput(" - Short Description", "2" + remote),
109 'ssid': rlinput(" - SSID", 'il-X.wleiden.net'),
110 'dhcp': 'False',
111 'mode': "ap-wds",
112 'ip': rlinput(" - IP", getrange.showaddr(link_network + 1) + "/" + str(netmask)),
113 'type': "eth",
114 'ns_ip': rlinput(" - NS IP", getrange.showaddr(link_network + 2) + "/" + str(netmask)),
115 'ns_mac': rlinput(" - NS MAC", "00:27:22:"),
116 'bridge_type': rlinput(' - Bridge Type', 'NanoStation5M'),
117 'status': 'up',
118 }
119 break
120
121 print datadump
122 #os.mkdir(datadump['autogen_item'])
123 #gformat.store_yaml(datadump)
124
125
126
127if __name__ == '__main__':
128 def printMenu():
129 print '''Node File Editor by Rick van der Zwet
130
131 1) New Node
132 9) Quit
133 '''
134
135 printMenu()
136 choice = 1
137 while choice != 9:
138 choice = int(rlprompt("Choice", choice))
139 if choice == 9:
140 break
141 elif choice == 1:
142 newNode()
143 printMenu()
144 sys.exit(1)
145 else:
146 print "Invalid choice!"
Note: See TracBrowser for help on using the repository browser.