-
Notifications
You must be signed in to change notification settings - Fork 0
/
selexorhelper.py
372 lines (293 loc) · 9.84 KB
/
selexorhelper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
"""
<Program Name>
selexorruleparserhelper.py
<Started>
July 24, 2012
<Author>
Leonard Law
<Purpose>
Contains helper functions that are needed by selexor.
"""
import math
import selexorexceptions
import os
import seattleclearinghouse_xmlrpc
import MySQLdb
import logging
import settings
import socket
helpercontext = {}
initialized_loggers = {}
logger = None
testbed_ip_list = []
NODE_TESTBED = "testbed"
NODE_UNIVERSITY = "university"
NODE_HOME = "home"
NODE_UNKNOWN = "unknown"
VALID_NODETYPES = [NODE_TESTBED, NODE_UNIVERSITY, NODE_HOME, NODE_UNKNOWN]
def is_ipv4_address(ipstring):
''' Checks if the given string is a valid IPv4 address. '''
tokens = ipstring.split(".") # ipv4 octets are '.' separated
if len(tokens) != 4: # there must be 4 octets
return False
for token in tokens:
# each octet must be an integer in [0, 256)
if not (token.isdigit() and int(token) in range(256)):
return False
return True
def get_ports_from_resource_string(resource_string):
'''
<Purpose>
Returns the set of all ports accessible on a resource with the given
resource string.
<Parameters>
resource_string: A string representing the resources file on a vessel.
<Exceptions>
None
<Side Effects>
None
<Return>
The set of all accessible ports on TCP/UDP.
'''
available_ports = set()
for resource_description in resource_string.split('\n'):
if 'messport' in resource_description or\
'connport' in resource_description:
# ports are sometimes specified as floats
port_number = int(float(resource_description.split()[2]))
available_ports.add(port_number)
return available_ports
def get_node_ip_port_from_nodelocation(nodelocation):
'''
<Purpose>
Obtains the nodeid and nodeport from a given nodelocation.
<Arguments>
nodelocation: string
A string representing a nodelocation.
<Exceptions>
None
<Side Effects>
None
<Return>
A dictionary containing:
'nodeid': the node ID
'port': the port
'''
node_info = nodelocation.split(':')
return {'id': node_info[0], 'port': int(node_info[1])}
def initialize():
global acquirable_vessel_resources
global logger
logger = setup_logging(__name__)
helpercontext['COUNTRY_TO_ID'] = load_ids('country')
# This takes up too much memory...
# helpercontext['CITY_TO_ID'] = load_ids('city')
testbed_ip_lines = open('lookup/testbed_iplist.txt').readlines()
testbed_ip_list.extend(ip_addr.strip() for ip_addr in testbed_ip_lines)
resources_file = settings.path_to_seattle_trunk + '/seattlegeni/node_state_transitions/resource_files/twopercent.resources'
restrictions_file = settings.path_to_seattle_trunk + '/resource/vessel.restrictions'
resources = open(resources_file).readlines()
resources.extend(open(restrictions_file).readlines())
acquirable_vessel_resources = set()
for line in resources:
line = line.strip()
if line and 'port' not in line:
acquirable_vessel_resources.add(line)
def is_resource_acquirable(resource_string):
'''
<Purpose>
Given a resource string, determine if the resource can be acquired
by a user.
<Arguments>
resource_string:
A string describing a file's restrictions and resources.
Obtainable via GetVesselResources via the nodemanager.
<Side Effects>
None
<Exceptions>
None
<Returns>
True if the resource is acquirable, False otherwise.
'''
resources_without_ports = []
for line in resource_string.split('\n'):
line = line.strip()
if line and 'port' not in line:
resources_without_ports.append(line)
return set(resources_without_ports) == acquirable_vessel_resources
def get_node_type(ip_addr):
'''
<Purpose>
Returns the type of the node specified.
<Arguments>
ip_addr:
The IP address of the node in question.
<Exceptions>
None
<Side Effects>
None
<Return>
A string representing the type of the node.
'''
if ip_addr in testbed_ip_list:
return NODE_TESTBED
domain_name = socket.getfqdn(ip_addr)
# Use URL components like .edu, planetlab, etc. to identify where a
# node is running from.
node_type_urlparts = {
NODE_TESTBED: ['planet', 'lab', 'silicon-valley.ru'],
NODE_UNIVERSITY: ['edu', 'uni', '.ca', '.hk', 'ac.jp', 'ac.kr', 'ac.be',
'epfl', 'ece', 'tuwien.ac.at', 'unavarra.es', 'uam.es', 'unl.pt',
'opole.pl', 'tu.koszalin.pl', 'p.lodz.pl', 'tu-harburg.de'],
NODE_HOME: ['dsl', 'dial', 'dyn', 'cable', 'comcast', 'qwest', 'pool',
'cust', 'broad', 'cox.net', 'netvigator', 'telering', 'rr.com', 'triband',
'surfer', 'wayport', 'highway.a1']
}
for node_type, urlparts in node_type_urlparts.iteritems():
matches = [part for part in urlparts if part in domain_name]
if matches:
return node_type
return NODE_UNKNOWN
def get_city_id(cityname):
# City table takes up several hundred MB of space... We treat all city names
# as valid for now
return cityname
def get_country_id(countryname):
countryname = countryname.lower()
try:
return helpercontext['COUNTRY_TO_ID'][countryname]
except KeyError, e:
raise selexorexceptions.UnknownLocation(countryname)
def load_ids(idtype):
id_file = open('./lookup/' + idtype + '.txt', 'r')
id_map = {}
line = id_file.readline().lower()
while line:
ids = line.split('\t')
good_id = ids[0].strip()
for id in ids:
id_map[id.strip()] = good_id
line = id_file.readline().lower()
id_file.close()
return id_map
def connect_to_clearinghouse(authdata):
'''
<Purpose>
Wrapper for a SeattleClearinghouseClient constructor.
<Arguments>
authdata:
An authdict. See module documentation for more information.
<Exceptions>
SelexorAuthenticationFailed
<Side Effects>
Opens an outgoing connection to the specified clearinghouse.
<Returns>
A client object that can be used to communicate with the Clearinghouse as the
specified user.
'''
username = authdata.keys()[0]
apikey = None
private_key_string = None
if 'apikey' in authdata[username]:
apikey = authdata[username]['apikey']
if 'privatekey' in authdata[username]:
private_key_string = rsa_repy.rsa_privatekey_to_string(authdata[username]['privatekey'])
if not (apikey or private_key_string):
raise selexorexceptions.SelexorAuthenticationFailed("Either apikey or privatekey must be given!")
logger.info("Connecting to the clearinghouse on behalf of "+username)
client = seattleclearinghouse_xmlrpc.SeattleClearinghouseClient(
username = username,
api_key = apikey,
private_key_string = private_key_string,
xmlrpc_url = settings.clearinghouse_xmlrpc_url,
allow_ssl_insecure = settings.allow_ssl_insecure)
return client
def haversine_distance(long1, lat1, long2, lat2):
'''
Given two coordinates, calculate the great circle distance between them.
These coordinates are specified in decimal degrees.
'''
# convert decimal degrees to radians
long1 = math.radians(long1)
long2 = math.radians(long2)
lat1 = math.radians(lat1)
lat2 = math.radians(lat2)
# haversine formula
dlong = long2 - long1
dlat = lat2 - lat1
a = math.sin(dlat / 2) ** 2
b = math.cos(lat1) * math.cos(lat2) * math.sin(dlong / 2) ** 2
c = 2 * math.asin(math.sqrt(a + b))
dist = 6367 * c
return dist
def haversine_distance_between_handles(handledict1, handledict2):
return haversine_distance(handledict1['geographic']['longitude'], handledict1['geographic']['latitude'],
handledict2['geographic']['longitude'], handledict2['geographic']['latitude'])
def get_handle_location(handle, loctype, database):
if loctype == 'cities':
return (database.handle_table[handle]['geographic']['city'], database.handle_table[handle]['geographic']['country_code'])
if loctype == 'countries':
return (database.handle_table[handle]['geographic']['country_code'],)
raise UnknownLocationType(loctype)
def connect_to_db():
"""
<Purpose>
Connect to the MySQL database using the user/pass/db specified in the
configuration file.
<Arguments>
configuration - Configuration dictionary from load_config_with_file().
<Exceptions>
None
<Side Effects>
Connects to the specified db.
<Return>
A db and cursor object representing the connection.
"""
db = MySQLdb.connect(
host='localhost', port=3306,
user=settings.dbusername, passwd=settings.dbpassword,
db=settings.dbname)
cursor = db.cursor()
return db, cursor
def autoretry_mysql_command(cursor, command):
"""
<Purpose>
Inserts the specified command into the command queue, and returns its
result when the command is executed. This is used to prevent deadlocks
that would occur due to simultaneous db accesses.
<Arguments>
command: The command to send to the database.
<Side Effects>
Executes the specified MySQL statement.
<Exceptions>
None
<Returns>
None
"""
while True:
try:
result = cursor.execute(command)
return result
except MySQLdb.OperationalError, e:
if (e.args == (1213, 'Deadlock found when trying to get lock; try restarting transaction') or
e.args == (1205, 'Lock wait timeout exceeded; try restarting transaction')):
continue
raise
def setup_logging(loggername):
global initialized_loggers
# We shouldn't try to re-initialize a logger that already exists...
if loggername in initialized_loggers:
return initialized_loggers[loggername]
logger = logging.getLogger(loggername)
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter("%(asctime)s %(message)s")
filehandler = logging.FileHandler(loggername+'.log', 'a')
streamhandler = logging.StreamHandler()
filehandler.setFormatter(formatter)
streamhandler.setFormatter(formatter)
logger.addHandler(streamhandler)
logger.addHandler(filehandler)
initialized_loggers[loggername] = logger
return logger
initialize()