def get(target=None): """Return current connection to libvirt or open a new one. Use target to get/create the connection object linked to that object. target must have a callable attribute named 'dispatchLibvirtEvents' which will be registered as a callback on libvirt events. Wrap methods of connection object so that they catch disconnection, and take the current process down. """ if not is_supported(): raise NotAvailable() with _lock: conn = _connections.get(id(target)) if conn: _log.debug('reusing cached container connection') return conn _log.debug('trying to connect to container manager') conn = open_connection() _connections[id(target)] = conn if target is not None: for event in (libvirt.VIR_DOMAIN_EVENT_ID_LIFECYCLE, libvirt.VIR_DOMAIN_EVENT_ID_REBOOT, libvirt.VIR_DOMAIN_EVENT_ID_RTC_CHANGE, libvirt.VIR_DOMAIN_EVENT_ID_IO_ERROR_REASON, libvirt.VIR_DOMAIN_EVENT_ID_GRAPHICS, libvirt.VIR_DOMAIN_EVENT_ID_BLOCK_JOB, libvirt.VIR_DOMAIN_EVENT_ID_WATCHDOG): conn.domainEventRegisterAny(None, event, target.dispatchLibvirtEvents, event) return conn
def open_connection(uri=None, username=None, passwd=None): """ by calling this method you are getting a new and unwrapped connection if you want to use wrapped and cached connection use the get() method """ # no argument is needed, they are present only to mimic libvirtconnection if not is_supported(): raise NotAvailable() return vdsm.virt.containers.connection.Connection()