Skip to content

Commit

Permalink
Merge pull request #957 from QualiSystems/nahum/fix_vcenter_session_n…
Browse files Browse the repository at this point in the history
…ot_alive

add support for restart pyvmomi session if server disconnected us due…
  • Loading branch information
nahumtimerman authored Nov 5, 2018
2 parents 89ef999 + cf1cc5e commit e2bb5b0
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions package/cloudshell/cp/vcenter/common/wrappers/command_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@

from retrying import retry

from pyVmomi import vim
from cloudshell.cp.vcenter.common.model_factory import ResourceModelParser
from cloudshell.cp.vcenter.common.vcenter.vmomi_service import pyVmomiService, VCenterAuthError
from cloudshell.shell.core.session.cloudshell_session import CloudShellSessionContext
from cloudshell.cp.vcenter.common.cloud_shell.conn_details_retriever import ResourceConnectionDetailsRetriever


DISCONNCTING_VCENERT = 'disconnecting from vcenter: {0}'
COMMAND_ERROR = 'error has occurred while executing command: {0}'
DEBUG_COMMAND_RESULT = 'finished executing with the result: {0}'
Expand Down Expand Up @@ -134,9 +136,9 @@ def execute_command_with_connection(self, context, command, *args):

def get_py_service_connection(self, req_connection_details, logger):
logger.info("get_py_service_connection")
if self.si is None or self.has_connection_details_changed(req_connection_details):
if self.need_a_new_service_connection(req_connection_details, logger):
with self.lock:
if self.si is None or self.has_connection_details_changed(req_connection_details):
if self.need_a_new_service_connection(req_connection_details, logger):
logger.info("Creating a new connection.")
self.si = self.pv_service.connect(req_connection_details.host,
req_connection_details.username,
Expand All @@ -145,6 +147,27 @@ def get_py_service_connection(self, req_connection_details, logger):
self.connection_details = req_connection_details
return self.si

def need_a_new_service_connection(self, req_connection_details, logger):
# the idea is if we haven't instantiated the ServiceInstance session,
# or if the connect details were changed
# or if an SI no longer is authorized to work with host due to timing out,
# we try to get a new connection
return (

self.si is None or
self.has_connection_details_changed(req_connection_details) or
self.service_instance_disconnected_by_server(logger)

)

def service_instance_disconnected_by_server(self, logger):
try:
self.si.CurrentTime()
except vim.fault.NotAuthenticated:
logger.info("ServiceInstance was disconnected. Will try to retrieve a new serviceinstance")
return True
return False

def has_connection_details_changed(self, req_connection_details):
"""
:param cloudshell.cp.vcenter.models.VCenterConnectionDetails.VCenterConnectionDetails req_connection_details:
Expand Down

0 comments on commit e2bb5b0

Please sign in to comment.