63
63
return service_result
66
def service_pause(service_name, init_dir=None):
67
"""Pause a system service.
69
Stop it, and prevent it from starting again at boot."""
71
init_dir = "/etc/init"
72
stopped = service_stop(service_name)
73
# XXX: Support systemd too
74
override_path = os.path.join(
75
init_dir, '{}.override'.format(service_name))
76
with open(override_path, 'w') as fh:
81
def service_resume(service_name, init_dir=None):
82
"""Resume a system service.
84
Reenable starting again at boot. Start the service"""
85
# XXX: Support systemd too
87
init_dir = "/etc/init"
88
override_path = os.path.join(
89
init_dir, '{}.override'.format(service_name))
90
if os.path.exists(override_path):
91
os.unlink(override_path)
92
started = service_start(service_name)
66
96
def service(action, service_name):
67
97
"""Control a system service"""
68
98
cmd = ['service', service_name, action]
254
290
return system_mounts
293
def fstab_mount(mountpoint):
294
"""Mount filesystem using fstab"""
295
cmd_args = ['mount', mountpoint]
297
subprocess.check_output(cmd_args)
298
except subprocess.CalledProcessError as e:
299
log('Error unmounting {}\n{}'.format(mountpoint, e.output))
257
304
def file_hash(path, hash_type='md5'):
259
306
Generate a hash checksum of the contents of 'path' or None if not found.
370
417
return(''.join(random_chars))
373
def list_nics(nic_type):
420
def is_phy_iface(interface):
421
"""Returns True if interface is not virtual, otherwise False."""
423
sys_net = '/sys/class/net'
424
if os.path.isdir(sys_net):
425
for iface in glob.glob(os.path.join(sys_net, '*')):
426
if '/virtual/' in os.path.realpath(iface):
429
if interface == os.path.basename(iface):
435
def get_bond_master(interface):
436
"""Returns bond master if interface is bond slave otherwise None.
438
NOTE: the provided interface is expected to be physical
441
iface_path = '/sys/class/net/%s' % (interface)
442
if os.path.exists(iface_path):
443
if '/virtual/' in os.path.realpath(iface_path):
446
master = os.path.join(iface_path, 'master')
447
if os.path.exists(master):
448
master = os.path.realpath(master)
449
# make sure it is a bond master
450
if os.path.exists(os.path.join(master, 'bonding')):
451
return os.path.basename(master)
456
def list_nics(nic_type=None):
374
457
'''Return a list of nics of given type(s)'''
375
458
if isinstance(nic_type, six.string_types):
376
459
int_types = [nic_type]
378
461
int_types = nic_type
380
for int_type in int_types:
381
cmd = ['ip', 'addr', 'show', 'label', int_type + '*']
465
for int_type in int_types:
466
cmd = ['ip', 'addr', 'show', 'label', int_type + '*']
467
ip_output = subprocess.check_output(cmd).decode('UTF-8')
468
ip_output = ip_output.split('\n')
469
ip_output = (line for line in ip_output if line)
470
for line in ip_output:
471
if line.split()[1].startswith(int_type):
472
matched = re.search('.*: (' + int_type +
473
r'[0-9]+\.[0-9]+)@.*', line)
475
iface = matched.groups()[0]
477
iface = line.split()[1].replace(":", "")
479
if iface not in interfaces:
480
interfaces.append(iface)
382
483
ip_output = subprocess.check_output(cmd).decode('UTF-8').split('\n')
383
ip_output = (line for line in ip_output if line)
484
ip_output = (line.strip() for line in ip_output if line)
486
key = re.compile('^[0-9]+:\s+(.+):')
384
487
for line in ip_output:
385
if line.split()[1].startswith(int_type):
386
matched = re.search('.*: (' + int_type + r'[0-9]+\.[0-9]+)@.*', line)
388
interface = matched.groups()[0]
390
interface = line.split()[1].replace(":", "")
391
interfaces.append(interface)
488
matched = re.search(key, line)
490
iface = matched.group(1)
491
iface = iface.partition("@")[0]
492
if iface not in interfaces:
493
interfaces.append(iface)
393
495
return interfaces