|
Lines 6-12
Link Here
|
| 6 |
The GLIUtility module contians all utility functions used throughout GLI. |
6 |
The GLIUtility module contians all utility functions used throughout GLI. |
| 7 |
""" |
7 |
""" |
| 8 |
|
8 |
|
| 9 |
import string, os, re, signal, time, shutil, sys, random, commands, crypt |
9 |
import string, os, re, shutil, random, commands, crypt |
| 10 |
from GLIException import * |
10 |
from GLIException import * |
| 11 |
|
11 |
|
| 12 |
## |
12 |
## |
|
Lines 209-215
Link Here
|
| 209 |
# @param input Parameter description |
209 |
# @param input Parameter description |
| 210 |
def strtobool(input): |
210 |
def strtobool(input): |
| 211 |
if type(input) != str: |
211 |
if type(input) != str: |
| 212 |
raise GLIException("InputError", 'fatal','strtobool',"The input must be a string!") |
212 |
raise GLIException("GLIUtilityError", 'fatal','strtobool',"The input must be a string!") |
| 213 |
|
213 |
|
| 214 |
if string.lower(input) == 'true': |
214 |
if string.lower(input) == 'true': |
| 215 |
return True |
215 |
return True |
|
Lines 227-239
Link Here
|
| 227 |
return False |
227 |
return False |
| 228 |
|
228 |
|
| 229 |
# Create a regular expression to test the specified device. |
229 |
# Create a regular expression to test the specified device. |
| 230 |
expr = re.compile('^(eth|wlan|ppp)([0-9]{1,2})(:[0-9]{1,2})?$') |
230 |
# expr = re.compile('^(eth|wlan|ppp)([0-9]{1,2})(:[0-9]{1,2})?$') |
| 231 |
|
231 |
|
| 232 |
# Run the match |
232 |
# Run the match |
| 233 |
res = expr.match(device) |
233 |
# res = expr.match(device) |
| 234 |
|
234 |
|
|
|
235 |
# if res != None: |
| 236 |
status, output = spawn("/sbin/ifconfig -a | grep -e '^[A-Za-z]'| cut -d ' ' -f 1 | grep '"+ device + "'", return_output=True) |
| 237 |
if output: |
| 238 |
return True |
| 235 |
# Return True only if there are results |
239 |
# Return True only if there are results |
| 236 |
return(res != None) |
240 |
return False |
| 237 |
|
241 |
|
| 238 |
## |
242 |
## |
| 239 |
# Brief description of function |
243 |
# Brief description of function |
|
Lines 262-278
Link Here
|
| 262 |
# @param netmask Parameter description |
266 |
# @param netmask Parameter description |
| 263 |
def set_ip(dev, ip, broadcast, netmask): |
267 |
def set_ip(dev, ip, broadcast, netmask): |
| 264 |
if not is_ip(ip) or not is_ip(netmask) or not is_ip(broadcast): |
268 |
if not is_ip(ip) or not is_ip(netmask) or not is_ip(broadcast): |
| 265 |
raise GLIException("IPAddressError", 'fatal','set_ip', "ip, netmask and broadcast must be a valid IP's!") |
269 |
raise GLIException("GLIUtilityError", 'fatal','set_ip', ip + ", " + netmask + "and, " + broadcast + "must be a valid IP's!") |
| 266 |
|
270 |
|
| 267 |
if not is_eth_device(dev): |
271 |
if not is_eth_device(dev): |
| 268 |
raise GLIException("EthDeviceError", 'fatal','set_ip',"dev must be a valid ethernet device!") |
272 |
raise GLIException("GLIUtilityError", 'fatal','set_ip', dev + "is not a valid ethernet device!") |
| 269 |
|
273 |
|
| 270 |
options = "%s inet %s broadcast %s netmask %s" % (dev, ip, broadcast, netmask) |
274 |
options = "%s inet %s broadcast %s netmask %s" % (dev, ip, broadcast, netmask) |
| 271 |
|
275 |
|
| 272 |
status = spawn("ifconfig " + options, quiet=True) |
276 |
spawn("ifconfig " + options, quiet=True) |
| 273 |
|
|
|
| 274 |
if not exitsuccess(status): |
| 275 |
return False |
| 276 |
|
277 |
|
| 277 |
return True |
278 |
return True |
| 278 |
|
279 |
|
|
Lines 281-292
Link Here
|
| 281 |
# @param route Parameter description |
282 |
# @param route Parameter description |
| 282 |
def set_default_route(route): |
283 |
def set_default_route(route): |
| 283 |
if not is_ip(route): |
284 |
if not is_ip(route): |
| 284 |
raise GLIException("IPAddressError", 'fatal', 'set_default_route', "The default route must be an IP address!") |
285 |
raise GLIException("GLIUtilityError", 'fatal', 'set_default_route', route + " is not an ip address!") |
| 285 |
|
286 |
|
| 286 |
status = spawn("route add default gw " + route, quiet=True) |
287 |
spawn("route add default gw " + route, quiet=True) |
| 287 |
|
|
|
| 288 |
if not exitsuccess(status): |
| 289 |
return False |
| 290 |
|
288 |
|
| 291 |
return True |
289 |
return True |
| 292 |
|
290 |
|
|
Lines 320-325
Link Here
|
| 320 |
|
318 |
|
| 321 |
# print "Running command: " + cmd |
319 |
# print "Running command: " + cmd |
| 322 |
ret, output = commands.getstatusoutput(cmd) |
320 |
ret, output = commands.getstatusoutput(cmd) |
|
|
321 |
|
| 322 |
if not os.WIFEXITED(ret) and os.WEXITSTATUS(ret) == 0: |
| 323 |
#raise GLIException('GLIExceptionError', 'fatal', 'spawn', "WIFEXITED = " + str(os.WIFEXITED(ret)) + ", WEXITSTATUS = " + str(os.WEXITSTATUS(ret))) |
| 324 |
raise GLIException('GLIUtilityError', 'fatal', 'spawn', "Process exited abnormally while trying to run " + cmd + " with status of " + ret + " and output of " + output) |
| 323 |
if return_output: |
325 |
if return_output: |
| 324 |
return ret, output |
326 |
return ret, output |
| 325 |
else: |
327 |
else: |
|
Lines 327-343
Link Here
|
| 327 |
|
329 |
|
| 328 |
## |
330 |
## |
| 329 |
# Brief description of function |
331 |
# Brief description of function |
| 330 |
# @param status Parameter description |
|
|
| 331 |
def exitsuccess(status): |
| 332 |
if os.WIFEXITED(status) and os.WEXITSTATUS(status) == 0: |
| 333 |
return True |
| 334 |
print "WIFEXITED = " + str(os.WIFEXITED(status)) + ", WEXITSTATUS = " + str(os.WEXITSTATUS(status)) |
| 335 |
|
| 336 |
return False |
| 337 |
|
| 338 |
def spawn_bash(): |
332 |
def spawn_bash(): |
| 339 |
os.putenv("PROMPT_COMMAND","echo \"Type 'exit' to return to the installer.\"") |
333 |
os.putenv("PROMPT_COMMAND","echo \"Type 'exit' to return to the installer.\"") |
| 340 |
return spawn("bash") |
334 |
status = spawn("bash") |
|
|
335 |
return status |
| 341 |
|
336 |
|
| 342 |
## |
337 |
## |
| 343 |
# Brief description of function |
338 |
# Brief description of function |
|
Lines 345-390
Link Here
|
| 345 |
# @param path Parameter description |
340 |
# @param path Parameter description |
| 346 |
def get_uri(uri, path): |
341 |
def get_uri(uri, path): |
| 347 |
uri = uri.strip() |
342 |
uri = uri.strip() |
| 348 |
status = 1 |
|
|
| 349 |
|
343 |
|
| 350 |
if re.match('^(ftp|http(s)?)://',uri): |
344 |
if re.match('^(ftp|http(s)?)://',uri): |
| 351 |
status = spawn("wget --quiet " + uri + " -O " + path) |
345 |
spawn("wget --quiet " + uri + " -O " + path) |
| 352 |
|
346 |
|
| 353 |
elif re.match('^rsync://', uri): |
347 |
elif re.match('^rsync://', uri): |
| 354 |
status = spawn("rsync --quiet " + uri + " " + path) |
348 |
spawn("rsync --quiet " + uri + " " + path) |
|
|
349 |
|
| 355 |
|
350 |
|
| 356 |
elif re.match('^file://', uri): |
351 |
elif re.match('^file://', uri): |
| 357 |
file = uri[7:] |
352 |
r_file = uri[7:] |
| 358 |
if os.path.isfile(file): |
353 |
if os.path.isfile(r_file): |
| 359 |
shutil.copy(file, path) |
354 |
shutil.copy(r_file, path) |
| 360 |
if os.path.isfile(path): |
355 |
if not os.path.isfile(path): |
| 361 |
status = 0 |
356 |
raise GLIException("GLIUtilityError", 'fatal', 'get_uri', "Cannot copy " + r_file + " to " + path) |
| 362 |
else: |
|
|
| 363 |
status = 1 |
| 364 |
else: |
357 |
else: |
| 365 |
# Just in case a person forgets file:// |
358 |
# Just in case a person forgets file:// |
| 366 |
if os.path.isfile(uri): |
359 |
if os.path.isfile(uri): |
| 367 |
shutil.copy(uri, path) |
360 |
shutil.copy(uri, path) |
| 368 |
if os.path.isfile(path): |
361 |
if not os.path.isfile(path): |
| 369 |
status = 0 |
362 |
raise GLIException("GLIUtilityError", 'fatal', 'get_uri', "Cannot copy " + uri + " to " + path) |
| 370 |
else: |
|
|
| 371 |
status = 1 |
| 372 |
else: |
363 |
else: |
| 373 |
print "I don't know how to download/copy that profile!" |
364 |
raise GLIException("GLIUtilityError", 'fatal', 'get_uri', "I don't know how to handle " + uri + " or " + path + " that!") |
| 374 |
|
|
|
| 375 |
if exitsuccess(status): |
| 376 |
return True |
| 377 |
|
| 378 |
return False |
| 379 |
|
365 |
|
| 380 |
## |
366 |
## |
| 381 |
# Brief description of function |
367 |
# Brief description of function |
| 382 |
# @param host Parameter description |
368 |
# @param host Parameter description |
| 383 |
def ping(host): |
369 |
def ping(host): |
| 384 |
host = str(host) |
370 |
if not (is_hostname(host) or is_ip(host)): |
| 385 |
status = spawn("ping -n -c 3 " + host,quiet=True) |
371 |
raise GLIException("GLIUtilityError", 'fatal', 'ping', host + " is not a valid hostname or ip!") |
| 386 |
if not exitsuccess(status): |
372 |
try: |
|
|
373 |
spawn("ping -n -c 3 " + host,quiet=True) |
| 374 |
except: |
| 387 |
return False |
375 |
return False |
|
|
376 |
|
| 388 |
return True |
377 |
return True |
| 389 |
|
378 |
|
| 390 |
## |
379 |
## |
|
Lines 396-432
Link Here
|
| 396 |
Order is hw_addr, ip_addr, mask, bcast, route, and |
385 |
Order is hw_addr, ip_addr, mask, bcast, route, and |
| 397 |
whether it's up (True or False). |
386 |
whether it's up (True or False). |
| 398 |
""" |
387 |
""" |
| 399 |
device = str(device) |
388 |
hw_addr = 'None' |
| 400 |
if device: |
389 |
ip_addr = 'None' |
| 401 |
hw_addr = 'None' |
390 |
mask = 'None' |
| 402 |
ip_addr = 'None' |
391 |
bcast = 'None' |
| 403 |
mask = 'None' |
392 |
gw = 'None' |
| 404 |
bcast = 'None' |
393 |
up = False |
| 405 |
gw = 'None' |
394 |
|
| 406 |
up = False |
395 |
if not is_eth_device("eth" + str(device)): |
| 407 |
device_info = commands.getstatusoutput("ifconfig eth" + device) |
396 |
raise GLIException("GLIUtilityError", 'fatal', "get_eth_info", "eth" + str(device) +" is not a valid ethernet device!") |
| 408 |
if device_info[0] == 0: |
397 |
|
| 409 |
for line in device_info[1].splitlines(): |
398 |
status, device_info = spawn("/sbin/ifconfig eth" + str(device), return_output=True) |
| 410 |
line = line.strip() |
399 |
if status==0: |
| 411 |
if 'HWaddr' in line: |
400 |
for line in device_info.splitlines(): |
| 412 |
hw_addr = line.split('HWaddr',1)[1].strip() |
401 |
line = line.strip() |
| 413 |
if 'inet addr' in line: |
402 |
if 'HWaddr' in line: |
| 414 |
ip_addr = line.split(' ')[0].split(':')[1] |
403 |
hw_addr = line.split('HWaddr',1)[1].strip() |
| 415 |
if 'Bcast' in line: |
404 |
if 'inet addr' in line: |
| 416 |
bcast = line.split(' ')[1].split(':')[1] |
405 |
ip_addr = line.split(' ')[0].split(':')[1] |
| 417 |
if 'Mask' in line: |
406 |
if 'Bcast' in line: |
| 418 |
mask = line.split(' ')[2].split(':')[1] |
407 |
bcast = line.split(' ')[1].split(':')[1] |
| 419 |
if line.startswith('UP'): |
408 |
if 'Mask' in line: |
| 420 |
up = True |
409 |
mask = line.split(' ')[2].split(':')[1] |
| 421 |
else: return (device_info[0], device_info[1]) |
410 |
if line.startswith('UP'): |
| 422 |
route_info = commands.getstatusoutput("netstat -nr") |
411 |
up = True |
| 423 |
if route_info[0] == 0: |
412 |
else: |
| 424 |
for line in route_info[1].splitlines(): |
413 |
raise GLIException("GLIUtilityError", 'fatal', "get_eth_info", device_info) |
| 425 |
if line.startswith('0.0.0.0'): |
414 |
status, route_info = spawn("netstat -nr", return_output=True) |
| 426 |
gw = line.split('0.0.0.0')[1].strip() |
415 |
for line in route_info[1].splitlines(): |
| 427 |
else: return (route_info[0], route_info[1]) |
416 |
if line.startswith('0.0.0.0'): |
| 428 |
return (hw_addr, ip_addr, mask, bcast, gw, up) |
417 |
gw = line.split('0.0.0.0')[1].strip() |
| 429 |
else: return False |
418 |
|
|
|
419 |
return (hw_addr, ip_addr, mask, bcast, gw, up) |
| 430 |
|
420 |
|
| 431 |
## |
421 |
## |
| 432 |
# Brief description of function |
422 |
# Brief description of function |
|
Lines 459-470
Link Here
|
| 459 |
tar_options = tar_options + "p" |
449 |
tar_options = tar_options + "p" |
| 460 |
|
450 |
|
| 461 |
# Unpack the tarball |
451 |
# Unpack the tarball |
| 462 |
exitstatus = spawn("tar -" + tar_options + " -f " + temp_directory + "/" + tarball_filename + " -C " + target_directory, display_on_tty8=True, logfile="/tmp/compile_output.log", append_log=True) # change this to the logfile variable |
452 |
try: |
| 463 |
|
453 |
spawn("tar -" + tar_options + " -f " + temp_directory + "/" + tarball_filename + " -C " + target_directory, display_on_tty8=True, logfile="/tmp/compile_output.log", append_log=True) # change this to the logfile variable |
| 464 |
if not exitsuccess(exitstatus): |
454 |
except: |
| 465 |
raise GLIException("UnpackTarballError", 'fatal', 'fetch_and_unpack_tarball',"Could not unpack tarball!") |
455 |
raise GLIException("GLIUtilityError", 'fatal', 'fetch_and_unpack_tarball',"Could not unpack " + tarball_uri + " to " + target_directory) |
| 466 |
|
|
|
| 467 |
|
456 |
|
|
|
457 |
## |
| 458 |
# Brief description of function |
| 468 |
def generate_random_password(): |
459 |
def generate_random_password(): |
| 469 |
s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890$%^&*[]{}-=+_,|'\"<>:/" |
460 |
s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890$%^&*[]{}-=+_,|'\"<>:/" |
| 470 |
s = list(s) |
461 |
s = list(s) |
|
Lines 487-493
Link Here
|
| 487 |
# @param filename Parameter description |
478 |
# @param filename Parameter description |
| 488 |
# @param value Parameter description |
479 |
# @param value Parameter description |
| 489 |
def get_value_from_config(filename, value): |
480 |
def get_value_from_config(filename, value): |
| 490 |
return string.strip(commands.getoutput("source " + filename + " && echo $" + value)) |
481 |
status, output = spawn("source " + filename + " && echo $" + value, return_output=True) |
|
|
482 |
return string.strip(output) |
| 491 |
|
483 |
|
| 492 |
## |
484 |
## |
| 493 |
# Brief description of function |
485 |
# Brief description of function |