From 186ca9d1b8797ad47461b9924b30c9ad8fdf6ac2 Mon Sep 17 00:00:00 2001 From: Natti Katz Date: Wed, 25 Aug 2021 20:34:30 +0300 Subject: [PATCH 1/2] fixed connection method population order for network_cli definition, SSL Verification read from context attribute, single quoting vars for JSON vars --- package/cloudshell/cm/ansible/ansible_shell.py | 13 +++++++++---- .../cloudshell/cm/ansible/domain/host_vars_file.py | 3 ++- .../cm/ansible/domain/playbook_downloader.py | 9 +++++---- package/version.txt | 2 +- 4 files changed, 17 insertions(+), 10 deletions(-) diff --git a/package/cloudshell/cm/ansible/ansible_shell.py b/package/cloudshell/cm/ansible/ansible_shell.py index 5561a8d..21d2616 100644 --- a/package/cloudshell/cm/ansible/ansible_shell.py +++ b/package/cloudshell/cm/ansible/ansible_shell.py @@ -6,7 +6,7 @@ from cloudshell.cm.ansible.domain.exceptions import AnsibleException from cloudshell.cm.ansible.domain.ansible_command_executor import AnsibleCommandExecutor, ReservationOutputWriter from cloudshell.cm.ansible.domain.ansible_config_file import AnsibleConfigFile -from cloudshell.cm.ansible.domain.ansible_configuration import AnsibleConfigurationParser +from cloudshell.cm.ansible.domain.ansible_configuration import AnsibleConfigurationParser, AnsibleConfiguration from cloudshell.cm.ansible.domain.file_system_service import FileSystemService from cloudshell.cm.ansible.domain.filename_extractor import FilenameExtractor from cloudshell.cm.ansible.domain.host_vars_file import HostVarsFile @@ -19,6 +19,7 @@ from cloudshell.core.context.error_handling_context import ErrorHandlingContext from cloudshell.shell.core.session.cloudshell_session import CloudShellSessionContext from cloudshell.shell.core.session.logging_session import LoggingSessionContext +from cloudshell.shell.core.driver_context import ResourceCommandContext class AnsibleShell(object): @@ -52,10 +53,13 @@ def execute_playbook(self, command_context, ansi_conf_json, cancellation_context """ with LoggingSessionContext(command_context) as logger: logger.debug('\'execute_playbook\' is called with the configuration json: \n' + ansi_conf_json) - + attrs = command_context.resource.attributes + verify_certificate = attrs.get("Verify Certificate", "True") + is_verify_certificate = True if verify_certificate == "True" else False with ErrorHandlingContext(logger): with CloudShellSessionContext(command_context) as api: ansi_conf = AnsibleConfigurationParser(api).json_to_object(ansi_conf_json) + ansi_conf.verify_certificate = is_verify_certificate output_writer = ReservationOutputWriter(api, command_context) cancellation_sampler = CancellationSampler(cancellation_context) @@ -92,8 +96,8 @@ def _add_host_vars_files(self, ansi_conf, logger): """ for host_conf in ansi_conf.hosts_conf: with HostVarsFile(self.file_system, host_conf.ip, logger) as file: - file.add_vars(host_conf.parameters) file.add_connection_type(host_conf.connection_method) + file.add_vars(host_conf.parameters) ansible_port = self.ansible_connection_helper.get_ansible_port(host_conf) file.add_port(ansible_port) @@ -112,6 +116,7 @@ def _add_host_vars_files(self, ansi_conf, logger): def _download_playbook(self, ansi_conf, cancellation_sampler, logger): """ + :param AnsibleConfiguration ansi_conf :type ansi_conf: AnsibleConfiguration :type cancellation_sampler: CancellationSampler :type logger: Logger @@ -119,7 +124,7 @@ def _download_playbook(self, ansi_conf, cancellation_sampler, logger): """ repo = ansi_conf.playbook_repo auth = None - if ansi_conf.playbook_repo.username or ansi_conf.playbook_repo.token: + if ansi_conf.playbook_repo.username or ansi_conf.playbook_repo.token or ansi_conf.playbook_repo.password: auth = HttpAuth(repo.username, repo.password, repo.token) logger.info('Verify certificate: ' + str(ansi_conf.verify_certificate)) diff --git a/package/cloudshell/cm/ansible/domain/host_vars_file.py b/package/cloudshell/cm/ansible/domain/host_vars_file.py index f54bf54..b36162f 100644 --- a/package/cloudshell/cm/ansible/domain/host_vars_file.py +++ b/package/cloudshell/cm/ansible/domain/host_vars_file.py @@ -33,7 +33,8 @@ def __exit__(self, type, value, traceback): with self.file_system.create_file(self.file_path) as file_stream: lines = ['---'] for key, value in sorted(self.vars.items()): - lines.append(str(key) + ': "' + str(value) + '"') + # lines.append(str(key) + ": '" + str(value) + "'") + lines.append("{}: '{}'".format(str(key), str(value))) file_stream.write(bytes(os.linesep.join(lines), 'utf-8')) self.logger.debug(os.linesep.join(lines)) self.logger.info('Done.') diff --git a/package/cloudshell/cm/ansible/domain/playbook_downloader.py b/package/cloudshell/cm/ansible/domain/playbook_downloader.py index ea3ffcc..a5d4377 100644 --- a/package/cloudshell/cm/ansible/domain/playbook_downloader.py +++ b/package/cloudshell/cm/ansible/domain/playbook_downloader.py @@ -67,10 +67,11 @@ def _download(self, url, auth, logger, cancel_sampler, verify_certificate): if not response_valid and auth is None: raise Exception('Please make sure the URL is valid, and the credentials are correct and necessary.') + generic_auth = auth.token if auth.token else auth.password # repo is private and token provided - if not response_valid and auth.token is not None: + if not response_valid and generic_auth is not None: logger.info("Token provided. Starting download script with Token...") - headers = {"Authorization": "Bearer %s" % auth.token } + headers = {"Authorization": "Bearer %s" % generic_auth } response = self.http_request_service.get_response_with_headers(url, headers, verify_certificate) response_valid = self._is_response_valid(logger, response, "Token") @@ -79,9 +80,9 @@ def _download(self, url, auth, logger, cancel_sampler, verify_certificate): file_name = self.filename_extractor.get_filename(response) # try again with authorization {"Private-Token": "%s" % token}, since gitlab uses that pattern - if not response_valid and auth.token is not None: + if not response_valid and generic_auth is not None: logger.info("Token provided. Starting download script with Token (private-token pattern)...") - headers = {"Private-Token": "Bearer %s" % auth.token } + headers = {"Private-Token": "Bearer %s" % generic_auth } response = self.http_request_service.get_response_with_headers(url, headers, verify_certificate) response_valid = self._is_response_valid(logger, response, "Token") diff --git a/package/version.txt b/package/version.txt index 10bf840..4dd0c8d 100644 --- a/package/version.txt +++ b/package/version.txt @@ -1 +1 @@ -2.0.1 \ No newline at end of file +2.0.1.1 \ No newline at end of file From 9db783aecaa1ebbcf4c823748c3ddb6eabf994e3 Mon Sep 17 00:00:00 2001 From: Natti Katz Date: Thu, 26 Aug 2021 12:04:49 +0300 Subject: [PATCH 2/2] removed cached zips --- .gitignore | 4 ++-- drivers/ansible_shell.zip | Bin 5888 -> 0 bytes .../Ansible Shell Driver.zip | Bin 1547 -> 0 bytes 3 files changed, 2 insertions(+), 2 deletions(-) delete mode 100644 drivers/ansible_shell.zip delete mode 100644 drivers/ansible_shellPackage/Resource Drivers - Python/Ansible Shell Driver.zip diff --git a/.gitignore b/.gitignore index ee96af7..73c7f09 100644 --- a/.gitignore +++ b/.gitignore @@ -7,10 +7,10 @@ drivers/ansible_shellPackage/Resource Drivers - Python/*.zip .eggs/ cloudshell_cm_ansible.egg-info/ package/cloudshell_cm_ansible.egg-info/ -drivers/ansible_shell.zip *.zip .vscode/ venv .venv* dist/ -package/build/ \ No newline at end of file +package/build/ +driver/*.zip \ No newline at end of file diff --git a/drivers/ansible_shell.zip b/drivers/ansible_shell.zip deleted file mode 100644 index f5516b6fb8dbcc9d7cba427d38b1ea827aad40ec..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 5888 zcmd5=&5s<#6>npVhvy@Y$TyjJla^!?K`OnBL#|R;TA|?4Fc&|SCbG+;20=?>;nX30*z4w0aqpGI& zuRi-?fX{FK{I}yt^4}MVD?xzoFJLTVnkH1!=qyWzZ@@qDDU*ted=&0RJ0W3tEE1kC zMq#Pv+rJOr7~k9)9yulg^tswUQ^_b?YF+FP2CLO7TD=?zxfty2?CcKSKYDNyFIh&z zikM$ayv=i^X&y7jRdFaIOGTfQH05foG|N;Ji)=8Z@yB$*yk>UI2s8^pVM2xz$*2ai z?orLgJ9h`Wd!XO$U9$W7{;NCtuj~v5EegYP!X!B3J?oG0Ytdc=WbHvi=hl9>F4%aI ziZVG_vNUz0L$8{d2ImdDj^k;C%$FZ*o&4wa-~aXP%^<+{*MJOYN{>Xs(gEU=;kQeH zRPi6?b5S|}r$_3TDN)Lp9nl;B%UG@lm8w114z!m1wA4%ijMsDG-IRi%`+C4 z2raSDz(>krEo4a0<)iQfMC6N*++%ZErusdamN1IHRV;adtfTPZk}E>}YNQZ6*NSLC zC@Cb{PS~91EFlXi%7TbFVb!Y2M2U*Xy9$CbU6WD);ao_P@cEocFpww?77!c~8HL!6 zLUO2%8BN~F)AcBv(^N4G)a%1R%S02q5WTHIV(mW26+cZGIY<(YHqewD$VG_`RUP9R zB-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