62
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)
65
96
def service(action, service_name):
66
97
"""Control a system service"""
67
98
cmd = ['service', service_name, action]
297
360
@restart_on_change({
298
361
'/etc/ceph/ceph.conf': [ 'cinder-api', 'cinder-volume' ]
362
'/etc/apache/sites-enabled/*': [ 'apache2' ]
300
def ceph_client_changed():
364
def config_changed():
301
365
pass # your code here
303
367
In this example, the cinder-api and cinder-volume services
304
368
would be restarted if /etc/ceph/ceph.conf is changed by the
305
ceph_client_changed function.
369
ceph_client_changed function. The apache2 service would be
370
restarted if any file matching the pattern got changed, created
371
or removed. Standard wildcards are supported, see documentation
372
for the 'glob' module for more information.
308
375
def wrapped_f(*args, **kwargs):
310
for path in restart_map:
311
checksums[path] = file_hash(path)
376
checksums = {path: path_hash(path) for path in restart_map}
312
377
f(*args, **kwargs)
314
379
for path in restart_map:
315
if checksums[path] != file_hash(path):
380
if path_hash(path) != checksums[path]:
316
381
restarts += restart_map[path]
317
382
services_list = list(OrderedDict.fromkeys(restarts))
318
383
if not stopstart:
339
404
def pwgen(length=None):
340
405
"""Generate a random pasword."""
341
406
if length is None:
407
# A random length is ok to use a weak PRNG
342
408
length = random.choice(range(35, 45))
343
409
alphanumeric_chars = [
344
410
l for l in (string.ascii_letters + string.digits)
345
411
if l not in 'l0QD1vAEIOUaeiou']
412
# Use a crypto-friendly PRNG (e.g. /dev/urandom) for making the
414
random_generator = random.SystemRandom()
347
random.choice(alphanumeric_chars) for _ in range(length)]
416
random_generator.choice(alphanumeric_chars) for _ in range(length)]
348
417
return(''.join(random_chars))
351
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):
352
457
'''Return a list of nics of given type(s)'''
353
458
if isinstance(nic_type, six.string_types):
354
459
int_types = [nic_type]
356
461
int_types = nic_type
358
for int_type in int_types:
359
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)
360
483
ip_output = subprocess.check_output(cmd).decode('UTF-8').split('\n')
361
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+(.+):')
362
487
for line in ip_output:
363
if line.split()[1].startswith(int_type):
364
matched = re.search('.*: (' + int_type + r'[0-9]+\.[0-9]+)@.*', line)
366
interface = matched.groups()[0]
368
interface = line.split()[1].replace(":", "")
369
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)
371
495
return interfaces