def _open_connection_to_quali_server(self, cloudshell_config, pbar, retry): if retry == 0: raise FatalError( "Connection to CloudShell Server failed. Please make sure it is up and running properly." ) try: client = PackagingRestApiClient( ip=cloudshell_config.host, username=cloudshell_config.username, port=cloudshell_config.port, domain=cloudshell_config.domain, password=cloudshell_config.password) return client except HTTPError as e: if e.code == 401: raise FatalError( "Login to CloudShell failed. Please verify the credentials in the config" ) raise FatalError( "Connection to CloudShell Server failed. Please make sure it is up and running properly." ) except: self._increase_pbar(pbar, time_wait=CLOUDSHELL_RETRY_INTERVAL_SEC) return self._open_connection_to_quali_server( cloudshell_config, pbar, retry - 1)
def _create_client(self): try: client = PackagingRestApiClient(ip=self._cs_config.host, username=self._cs_config.username, port=self._cs_config.port, domain=self._cs_config.domain, password=self._cs_config.password) return client except (HTTPError, Exception) as e: if hasattr(e, 'code') and e.code == 401: raise FatalError( u'Login to CloudShell failed. Please verify the credentials in the config' ) raise FatalError(self.ConnectionFailureMessage)
def _install_old_school_shell(self): error = None try: cloudshell_config = self.cloudshell_config_reader.read() shell_config = self.shell_config_reader.read() self.installer.install(shell_config.name, cloudshell_config) except HTTPError as e: if e.code == 401: raise FatalError('Login to CloudShell failed. Please verify the credentials in the config') error = str(e) except URLError: raise FatalError('Connection to CloudShell Server failed. Please make sure it is up and running properly.') except Exception as e: error = str(e) if error: raise FatalError("Failed to install shell. CloudShell responded with: '{}'".format(error))
def test_client_wrapper_raises_an_error_when_create_client_fails(self): # Act with patch.object(CloudShellClient, 'create_client', side_effect=FatalError('failure')): with self.assertRaises(FatalError) as context: create_cloudshell_client() # Assert self.assertEqual(context.exception.message, 'failure')
def test_client_wrapper_raises_an_error_when_create_client_fails_after_retries(self, api_mock): # Arrange api_mock.side_effect = [Exception(), api_mock] # Act with patch.object(CloudShellClient, 'create_client', side_effect=FatalError('failure')): with self.assertRaises(FatalError) as context: create_cloudshell_client(retries=2) # Assert self.assertEqual(context.exception.message, 'failure')
def create_client(self, **kwargs): retries = kwargs.get('retries', 1) if retries == 0: raise FatalError(self.ConnectionFailureMessage) try: return self._create_client() except FatalError as e: retry = retries - 1 if retry == 0: raise e return self.create_client(retries=retry)
def install(self, path): shell_package = ShellPackage(path) shell_filename = shell_package.get_shell_name() + '.zip' package_full_path = os.path.join(path, 'dist', shell_filename) cloudshell_config = self.cloudshell_config_reader.read() cs_connection_label = 'Connecting to CloudShell at {}:{}'.format( cloudshell_config.host, cloudshell_config.port) with click.progressbar(length=CloudShell_Max_Retries, show_eta=False, label=cs_connection_label) as pbar: try: client = self._open_connection_to_quali_server( cloudshell_config, pbar, retry=CloudShell_Max_Retries) finally: self._render_pbar_finish(pbar) pbar_install_shell_len = 2 # amount of possible actions (update and add) installation_label = 'Installing shell into CloudShell'.ljust( len(cs_connection_label)) with click.progressbar(length=pbar_install_shell_len, show_eta=False, label=installation_label) as pbar: try: client.update_shell(package_full_path) except ShellNotFoundException: self._increase_pbar(pbar, Default_Time_Wait) self._add_new_shell(client, package_full_path) except Exception as e: self._increase_pbar(pbar, Default_Time_Wait) raise FatalError( self._parse_installation_error('Failed to update shell', e)) finally: self._render_pbar_finish(pbar)
def install(self, path): """ Install new or Update existed Shell """ shell_package = ShellPackage(path) # shell_name = shell_package.get_shell_name() shell_name = shell_package.get_name_from_definition() shell_filename = shell_name + ".zip" package_full_path = os.path.join(path, "dist", shell_filename) cloudshell_config = self.cloudshell_config_reader.read() if cloudshell_config.domain != self.GLOBAL_DOMAIN: raise click.UsageError( "Gen2 shells could not be installed into non Global domain.") cs_connection_label = "Connecting to CloudShell at {}:{}".format( cloudshell_config.host, cloudshell_config.port) with click.progressbar(length=CLOUDSHELL_MAX_RETRIES, show_eta=False, label=cs_connection_label) as pbar: try: client = self._open_connection_to_quali_server( cloudshell_config, pbar, retry=CLOUDSHELL_MAX_RETRIES) finally: self._render_pbar_finish(pbar) try: is_official = client.get_shell(shell_name=shell_name).get( SHELL_IS_OFFICIAL_FLAG, False) if is_official: click.confirm( text= "Upgrading to a custom version of the shell will limit you " "only to customized versions of this shell from now on. " "You won't be able to upgrade it to an official version of the shell in the future." "\nDo you wish to continue?", abort=True) except FeatureUnavailable: # try to update shell first pass except ShellNotFoundException: # try to install shell pass except click.Abort: raise except Exception as e: raise FatalError( self._parse_installation_error( "Failed to get information about installed shell", e)) pbar_install_shell_len = 2 # amount of possible actions (update and add) installation_label = "Installing shell into CloudShell".ljust( len(cs_connection_label)) with click.progressbar(length=pbar_install_shell_len, show_eta=False, label=installation_label) as pbar: try: client.update_shell(package_full_path) except ShellNotFoundException: self._increase_pbar(pbar, DEFAULT_TIME_WAIT) self._add_new_shell(client, package_full_path) except Exception as e: self._increase_pbar(pbar, DEFAULT_TIME_WAIT) raise FatalError( self._parse_installation_error("Failed to update shell", e)) finally: self._render_pbar_finish(pbar)
def _add_new_shell(self, client, package_full_path): try: client.add_shell(package_full_path) except Exception as e: raise FatalError( self._parse_installation_error("Failed to add new shell", e))