Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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/
package/build/
driver/*.zip
Binary file removed drivers/ansible_shell.zip
Binary file not shown.
Binary file not shown.
13 changes: 9 additions & 4 deletions package/cloudshell/cm/ansible/ansible_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand All @@ -112,14 +116,15 @@ 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
:rtype str
"""
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:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is purpose here?
can there be a password if no username?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there can be a token with no username. My use case here was backwards support of driver for users on system without the token attribute field available yet. If the token field is empty fallback to the password. Customers can use the driver and won't have to migrate attributes from password to token.

auth = HttpAuth(repo.username, repo.password, repo.token)

logger.info('Verify certificate: ' + str(ansi_conf.verify_certificate))
Expand Down
3 changes: 2 additions & 1 deletion package/cloudshell/cm/ansible/domain/host_vars_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -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) + "'")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prefer to delete than to comment

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure

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.')
Expand Down
9 changes: 5 additions & 4 deletions package/cloudshell/cm/ansible/domain/playbook_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is a different section intended to handle auth password further down, if the normal token path does not succeed.
What is the intention of using this path for auth password as well?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for supporting older customers that can't upgrade yet to also use tokens

# 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")
Expand All @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion package/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.0.1
2.0.1.1