connection: Cleanup some initial connection code

This commit is contained in:
Cole Robinson 2014-09-11 18:48:04 -04:00
parent bd5e57dbdc
commit adbe68fc7f
1 changed files with 72 additions and 80 deletions

View File

@ -575,6 +575,8 @@ class vmmConnection(vmmGObject):
def _change_state(self, newstate): def _change_state(self, newstate):
if self._state != newstate: if self._state != newstate:
self._state = newstate self._state = newstate
logging.debug("conn=%s changed to state=%s",
self.get_uri(), self.get_state_text())
self.emit("state-changed") self.emit("state-changed")
def is_active(self): def is_active(self):
@ -861,6 +863,10 @@ class vmmConnection(vmmGObject):
def set_autoconnect(self, val): def set_autoconnect(self, val):
self.config.set_conn_autoconnect(self.get_uri(), val) self.config.set_conn_autoconnect(self.get_uri(), val)
def _schedule_close(self):
self._closing = True
self.idle_add(self.close)
def close(self): def close(self):
if not self.is_disconnected(): if not self.is_disconnected():
logging.debug("conn.close() uri=%s", self.get_uri()) logging.debug("conn.close() uri=%s", self.get_uri())
@ -940,97 +946,84 @@ class vmmConnection(vmmGObject):
logging.debug("Launching creds dialog failed", exc_info=True) logging.debug("Launching creds dialog failed", exc_info=True)
return -1 return -1
def _open_thread(self): def _do_open(self, retry_for_tgt=True):
logging.debug("Background 'open connection' thread is running") warnconsole = False
libvirt_error_code = None
while True: libvirt_error_message = None
libexc = None
exc = None
tb = None
warnconsole = False
try:
self._backend.open(self._do_creds_password)
except libvirt.libvirtError, libexc:
tb = "".join(traceback.format_exc())
except Exception, exc:
tb = "".join(traceback.format_exc())
if libexc:
exc = libexc
if not exc:
self._state = self._STATE_ACTIVE
break
self._state = self._STATE_DISCONNECTED
if (libexc and
(libexc.get_error_code() ==
getattr(libvirt, "VIR_ERR_AUTH_CANCELLED", None))):
logging.debug("User cancelled auth, not raising any error.")
break
if (libexc and
libexc.get_error_code() == libvirt.VIR_ERR_AUTH_FAILED and
"not authorized" in libexc.get_error_message().lower()):
logging.debug("Looks like we might have failed policykit "
"auth. Checking to see if we have a valid "
"console session")
if (not self.is_remote() and
not connectauth.do_we_have_session()):
warnconsole = True
if (libexc and
libexc.get_error_code() == libvirt.VIR_ERR_AUTH_FAILED and
"GSSAPI Error" in libexc.get_error_message() and
"No credentials cache found" in libexc.get_error_message()):
if connectauth.acquire_tgt():
continue
self._connectError = (str(exc), tb, warnconsole)
break
# We want to kill off this thread asap, so schedule an
# idle event to inform the UI of result
logging.debug("Background open thread complete, scheduling notify")
self.idle_add(self._open_notify)
self._connectThread = None
def _open_notify(self):
logging.debug("Notifying open result")
try: try:
self.idle_emit("state-changed") self._backend.open(self._do_creds_password)
if self.is_active(): return True
logging.debug("libvirt version=%s", except Exception, exc:
self._backend.local_libvirt_version()) tb = "".join(traceback.format_exc())
logging.debug("daemon version=%s", if type(exc) is libvirt.libvirtError:
self._backend.daemon_version()) libvirt_error_code = exc.get_error_code()
logging.debug("conn version=%s", self._backend.conn_version()) libvirt_error_message = exc.get_error_message()
logging.debug("%s capabilities:\n%s",
self.get_uri(), self.caps.xml)
self._add_conn_events()
try: if (libvirt_error_code ==
self._backend.setKeepAlive(20, 1) getattr(libvirt, "VIR_ERR_AUTH_CANCELLED", None)):
except Exception, e: logging.debug("User cancelled auth, not raising any error.")
if (type(e) is not AttributeError and return
not util.is_error_nosupport(e)):
raise
logging.debug("Connection doesn't support KeepAlive, "
"skipping")
if (libvirt_error_code == libvirt.VIR_ERR_AUTH_FAILED and
"not authorized" in libvirt_error_message.lower()):
logging.debug("Looks like we might have failed policykit "
"auth. Checking to see if we have a valid "
"console session")
if (not self.is_remote() and
not connectauth.do_we_have_session()):
warnconsole = True
if (libvirt_error_code == libvirt.VIR_ERR_AUTH_FAILED and
"GSSAPI Error" in libvirt_error_message and
"No credentials cache found" in libvirt_error_message):
if retry_for_tgt and connectauth.acquire_tgt():
self._do_open(retry_for_tgt=False)
self._connectError = (str(exc), tb, warnconsole)
def _populate_initial_state(self):
logging.debug("libvirt version=%s",
self._backend.local_libvirt_version())
logging.debug("daemon version=%s",
self._backend.daemon_version())
logging.debug("conn version=%s", self._backend.conn_version())
logging.debug("%s capabilities:\n%s",
self.get_uri(), self.caps.xml)
self._add_conn_events()
try:
self._backend.setKeepAlive(20, 1)
except Exception, e:
if (type(e) is not AttributeError and
not util.is_error_nosupport(e)):
raise
logging.debug("Connection doesn't support KeepAlive, "
"skipping")
def _open_thread(self):
is_active = False
try:
is_active = self._do_open()
if is_active:
self._populate_initial_state()
self.idle_add(self._change_state,
is_active and self._STATE_ACTIVE or self._STATE_DISCONNECTED)
if is_active:
self.schedule_priority_tick(stats_update=True, self.schedule_priority_tick(stats_update=True,
pollvm=True, pollnet=True, pollvm=True, pollnet=True,
pollpool=True, polliface=True, pollpool=True, polliface=True,
pollnodedev=True, pollmedia=True, pollnodedev=True, pollmedia=True,
force=True) force=True)
except Exception, e: except Exception, e:
self.close() is_active = False
self._schedule_close()
self._connectError = (str(e), self._connectError = (str(e),
"".join(traceback.format_exc()), False) "".join(traceback.format_exc()), False)
if self.is_disconnected(): if not is_active:
if self._connectError: if self._connectError:
self.idle_emit("connect-error", *self._connectError) self.idle_emit("connect-error", *self._connectError)
self._connectError = None self._connectError = None
@ -1113,8 +1106,7 @@ class vmmConnection(vmmGObject):
logging.debug("Not showing user error since libvirtd " logging.debug("Not showing user error since libvirtd "
"appears to have stopped.") "appears to have stopped.")
self._closing = True self._schedule_close()
self.idle_add(self.close)
if e: if e:
raise e # pylint: disable=raising-bad-type raise e # pylint: disable=raising-bad-type