Skip to content

Commit d0ea47b

Browse files
committed
added retry in connect when vCenter is under stress
2 parents 184ed8e + 8896449 commit d0ea47b

4 files changed

Lines changed: 27 additions & 9 deletions

File tree

package/cloudshell/cp/vcenter/commands/connect_orchestrator.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import traceback
12
from multiprocessing.pool import ThreadPool
23

34
import jsonpickle
@@ -345,7 +346,8 @@ def _remove_vlan(self, action_mappings, si, vm_uuid, logger):
345346
results.append(action_result)
346347
final_res = self._consolidate_duplicate_results(results)
347348
except Exception as e:
348-
self.logger.error('Exception raised while disconnecting vm({0}) with exception: {1}'.format(vm_uuid, e))
349+
self.logger.error('Exception raised while disconnecting vm({0}) with exception: {1}'
350+
.format(vm_uuid, traceback.format_exc()))
349351
for mode, actions in mode_to_actions.items():
350352
for action in actions:
351353
error_result = self._create_error_action_res(action, e)

package/cloudshell/cp/vcenter/common/vcenter/vmomi_service.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66
from cloudshell.cp.vcenter.common.utilites.common_utils import str2bool
77
from cloudshell.cp.vcenter.common.vcenter.task_waiter import SynchronousTaskWaiter
88

9+
10+
class VCenterAuthError (Exception):
11+
def __init__(self, original_exception):
12+
super(VCenterAuthError, self).__init__(original_exception.message)
13+
self.original_exception = original_exception
14+
15+
916
class pyVmomiService:
1017
# region consts
1118
ChildEntity = 'childEntity'
@@ -61,7 +68,7 @@ def connect(self, address, user, password, port=443):
6168
si = self.pyvmomi_connect(host=address, user=user, pwd=password, port=port)
6269
return si
6370
except vim.fault.InvalidLogin as e:
64-
raise ValueError(e.msg)
71+
raise VCenterAuthError(e.msg)
6572
except IOError as e:
6673
# logger.info("I/O error({0}): {1}".format(e.errno, e.strerror))
6774
raise ValueError('Cannot connect to vCenter, please check that the address is valid')

package/cloudshell/cp/vcenter/common/wrappers/command_wrapper.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import inspect
2+
3+
from retrying import retry
4+
25
from cloudshell.cp.vcenter.common.model_factory import ResourceModelParser
36
from cloudshell.cp.vcenter.common.cloud_shell.driver_helper import CloudshellDriverHelper
4-
from cloudshell.cp.vcenter.common.vcenter.vmomi_service import pyVmomiService
7+
from cloudshell.cp.vcenter.common.vcenter.vmomi_service import pyVmomiService, VCenterAuthError
58
from cloudshell.cp.vcenter.models.VMwarevCenterResourceModel import VMwarevCenterResourceModel
69

710
DISCONNCTING_VCENERT = 'disconnecting from vcenter: {0}'
@@ -21,6 +24,10 @@
2124
LOG_FORMAT = 'action:{0} command_name:{1}'
2225

2326

27+
def retry_if_auth_error(ex):
28+
return isinstance(ex, VCenterAuthError)
29+
30+
2431
class CommandWrapper:
2532
def __init__(self, pv_service, cloud_shell_helper, resource_model_parser, context_based_logger_factory):
2633
"""
@@ -37,6 +44,7 @@ def __init__(self, pv_service, cloud_shell_helper, resource_model_parser, contex
3744
self.resource_model_parser = resource_model_parser # type: ResourceModelParser
3845
self.context_based_logger_factory = context_based_logger_factory # type ContextBasedLoggerFactory
3946

47+
@retry(stop_max_attempt_number=3, wait_fixed=2000, retry_on_exception=retry_if_auth_error)
4048
def execute_command_with_connection(self, context, command, *args):
4149
"""
4250
Note: session & vcenter_data_model objects will be injected dynamically to the command
@@ -47,8 +55,8 @@ def execute_command_with_connection(self, context, command, *args):
4755
"""
4856

4957
logger = self.context_based_logger_factory.create_logger_for_context(
50-
logger_name='vCenterShell',
51-
context=context)
58+
logger_name='vCenterShell',
59+
context=context)
5260

5361
if not command:
5462
logger.error(COMMAND_CANNOT_BE_NONE)
@@ -76,9 +84,9 @@ def execute_command_with_connection(self, context, command, *args):
7684
if connection_details:
7785
logger.info(INFO_CONNECTING_TO_VCENTER.format(connection_details.host))
7886
logger.debug(
79-
DEBUG_CONNECTION_INFO.format(connection_details.host,
80-
connection_details.username,
81-
connection_details.port))
87+
DEBUG_CONNECTION_INFO.format(connection_details.host,
88+
connection_details.username,
89+
connection_details.port))
8290

8391
si = self.pv_service.connect(connection_details.host,
8492
connection_details.username,

package/requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ cloudshell-automation-api>=7.1.0.0,<7.2.0.0
22
cloudshell-core>=2.0.0,<2.1.0
33
pyvmomi==6.0.0
44
jsonpickle==0.9.3
5-
enum==0.4.6
5+
enum==0.4.6
6+
retrying==1.3.3

0 commit comments

Comments
 (0)