~openstack-charmers-next/charms/precise/neutron-openvswitch/trunk

« back to all changes in this revision

Viewing changes to hooks/charmhelpers/core/host.py

  • Committer: Liam Young
  • Date: 2016-04-12 14:08:57 UTC
  • Revision ID: liam.young@canonical.com-20160412140857-v1crmayssmaum4at
Charmhelper sync before 1604 testing

Change-Id: I74e737bd374966018421725321acbdee22526d0d

Show diffs side-by-side

added added

removed removed

Lines of Context:
423
423
    pass
424
424
 
425
425
 
426
 
def restart_on_change(restart_map, stopstart=False):
 
426
def restart_on_change(restart_map, stopstart=False, restart_functions=None):
427
427
    """Restart services based on configuration files changing
428
428
 
429
429
    This function is used a decorator, for example::
444
444
 
445
445
    @param restart_map: {path_file_name: [service_name, ...]
446
446
    @param stopstart: DEFAULT false; whether to stop, start OR restart
 
447
    @param restart_functions: nonstandard functions to use to restart services
 
448
                              {svc: func, ...}
447
449
    @returns result from decorated function
448
450
    """
449
451
    def wrap(f):
450
452
        @functools.wraps(f)
451
453
        def wrapped_f(*args, **kwargs):
452
454
            return restart_on_change_helper(
453
 
                (lambda: f(*args, **kwargs)), restart_map, stopstart)
 
455
                (lambda: f(*args, **kwargs)), restart_map, stopstart,
 
456
                restart_functions)
454
457
        return wrapped_f
455
458
    return wrap
456
459
 
457
460
 
458
 
def restart_on_change_helper(lambda_f, restart_map, stopstart=False):
 
461
def restart_on_change_helper(lambda_f, restart_map, stopstart=False,
 
462
                             restart_functions=None):
459
463
    """Helper function to perform the restart_on_change function.
460
464
 
461
465
    This is provided for decorators to restart services if files described
464
468
    @param lambda_f: function to call.
465
469
    @param restart_map: {file: [service, ...]}
466
470
    @param stopstart: whether to stop, start or restart a service
 
471
    @param restart_functions: nonstandard functions to use to restart services
 
472
                              {svc: func, ...}
467
473
    @returns result of lambda_f()
468
474
    """
 
475
    if restart_functions is None:
 
476
        restart_functions = {}
469
477
    checksums = {path: path_hash(path) for path in restart_map}
470
478
    r = lambda_f()
471
479
    # create a list of lists of the services to restart
476
484
    services_list = list(OrderedDict.fromkeys(itertools.chain(*restarts)))
477
485
    if services_list:
478
486
        actions = ('stop', 'start') if stopstart else ('restart',)
479
 
        for action in actions:
480
 
            for service_name in services_list:
481
 
                service(action, service_name)
 
487
        for service_name in services_list:
 
488
            if service_name in restart_functions:
 
489
                restart_functions[service_name](service_name)
 
490
            else:
 
491
                for action in actions:
 
492
                    service(action, service_name)
482
493
    return r
483
494
 
484
495