/mandos/release

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/release

« back to all changes in this revision

Viewing changes to mandos-ctl

  • Committer: Teddy Hogeborn
  • Date: 2019-03-16 00:23:20 UTC
  • mto: This revision was merged to the branch mainline in revision 382.
  • Revision ID: teddy@recompile.se-20190316002320-ajpmbdl4jup156en
mandos-ctl: Refactor

* mandos-ctl (get_mandos_dbus_object, get_managed_objects): Factor out
                                             D-Bus exception catching.
                       
  (if_dbus_exception_log_with_exception_and_exit): New.

Show diffs side-by-side

added added

removed removed

Lines of Context:
425
425
 
426
426
 
427
427
def get_mandos_dbus_object(bus):
428
 
    try:
429
 
        log.debug("D-Bus: Connect to: (busname=%r, path=%r)",
430
 
                  dbus_busname, server_dbus_path)
 
428
    log.debug("D-Bus: Connect to: (busname=%r, path=%r)",
 
429
              dbus_busname, server_dbus_path)
 
430
    with if_dbus_exception_log_with_exception_and_exit(
 
431
            "Could not connect to Mandos server: %s"):
431
432
        mandos_dbus_object = bus.get_object(dbus_busname,
432
433
                                            server_dbus_path)
433
 
    except dbus.exceptions.DBusException:
434
 
        log.critical("Could not connect to Mandos server")
435
 
        sys.exit(1)
436
 
 
437
434
    return mandos_dbus_object
438
435
 
439
436
 
 
437
@contextlib.contextmanager
 
438
def if_dbus_exception_log_with_exception_and_exit(*args, **kwargs):
 
439
    try:
 
440
        yield
 
441
    except dbus.exceptions.DBusException as e:
 
442
        log.critical(*(args + (e,)), **kwargs)
 
443
        sys.exit(1)
 
444
 
 
445
 
440
446
def get_managed_objects(object_manager):
441
447
    log.debug("D-Bus: %s:%s:%s.GetManagedObjects()", dbus_busname,
442
448
              server_dbus_path, dbus.OBJECT_MANAGER_IFACE)
443
 
    try:
 
449
    with if_dbus_exception_log_with_exception_and_exit(
 
450
            "Failed to access Mandos server through D-Bus:\n%s"):
444
451
        with SilenceLogger("dbus.proxies"):
445
452
            managed_objects = object_manager.GetManagedObjects()
446
 
    except dbus.exceptions.DBusException as e:
447
 
        log.critical("Failed to access Mandos server through D-Bus:"
448
 
                     "\n%s", e)
449
 
        sys.exit(1)
450
453
    return managed_objects
451
454
 
452
455