-
Notifications
You must be signed in to change notification settings - Fork 403
connection/aws_ssm - create TerminalManager class and move related methods #2270
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
softwarefactory-project-zuul
merged 12 commits into
ansible-collections:main
from
mandar242:terminal_manager_class
Apr 7, 2025
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
fa365aa
move prepare_terminal and wrap_command methods to TerminalManager class"
mandar242 fd2a668
move disable_echo_command to TerminalManager
mandar242 13a72f5
move disable_prompt_command to TerminalManager
mandar242 c93f98b
move ensure_ssm_session_has_started to TerminalManager
mandar242 982c841
move TerminalManager class to plugin_utils
mandar242 9aaebe3
sanity fixes
mandar242 0dab02c
add changelog fragment
mandar242 e882fd2
minor fix
mandar242 aa5d89e
modified based on feedback
mandar242 a9d5a55
linter fix
mandar242 2778c9f
Revert aa5d89e47c9eb775b98cf0b3b532d97088c46477 and a9d5a557e6097a9b3…
mandar242 3bc4f0a
use f string
mandar242 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
changelogs/fragments/2270-aws_ssm-refactor-create-terminalmanager-class.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
--- | ||
minor_changes: | ||
- aws_ssm - Refactor connection/aws_ssm to add new TerminalManager class and move relevant methods to the new class (https://github.com/ansible-collections/community.aws/pull/2270). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
# Copyright: Contributors to the Ansible project | ||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) | ||
|
||
import random | ||
import re | ||
import string | ||
|
||
from ansible.module_utils._text import to_bytes | ||
from ansible.module_utils._text import to_text | ||
from ansible.plugins.shell.powershell import _common_args | ||
|
||
|
||
class TerminalManager: | ||
def __init__(self, connection): | ||
self.connection = connection | ||
mandar242 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def prepare_terminal(self) -> None: | ||
"""perform any one-time terminal settings""" | ||
# No Windows setup for now | ||
if self.connection.is_windows: | ||
return | ||
mandar242 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# Ensure SSM Session has started | ||
self.ensure_ssm_session_has_started() | ||
|
||
# Disable echo command | ||
self.disable_echo_command() # pylint: disable=unreachable | ||
|
||
# Disable prompt command | ||
self.disable_prompt_command() # pylint: disable=unreachable | ||
|
||
self.connection.verbosity_display(4, "PRE Terminal configured") # pylint: disable=unreachable | ||
mandar242 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def wrap_command(self, cmd: str, mark_start: str, mark_end: str) -> str: | ||
"""Wrap command so stdout and status can be extracted""" | ||
|
||
if self.connection.is_windows: | ||
if not cmd.startswith(" ".join(_common_args) + " -EncodedCommand"): | ||
cmd = self.connection._shell._encode_script(cmd, preserve_rc=True) | ||
cmd = f"{cmd}; echo {mark_start}\necho {mark_end}\n" | ||
else: | ||
cmd = ( | ||
f"printf '%s\\n' '{mark_start}';\n" | ||
f"echo | {cmd};\n" | ||
f"printf '\\n%s\\n%s\\n' \"$?\" '{mark_end}';\n" | ||
) # fmt: skip | ||
|
||
self.connection.verbosity_display(4, f"wrap_command: \n'{to_text(cmd)}'") | ||
mandar242 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return cmd | ||
|
||
def disable_echo_command(self) -> None: | ||
"""Disable echo command from the host""" | ||
disable_echo_cmd = to_bytes("stty -echo\n", errors="surrogate_or_strict") | ||
mandar242 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# Send command | ||
self.connection.verbosity_display(4, f"DISABLE ECHO Disabling Prompt: \n{disable_echo_cmd}") | ||
self.connection._session.stdin.write(disable_echo_cmd) | ||
|
||
stdout = "" | ||
for poll_result in self.connection.poll("DISABLE ECHO", disable_echo_cmd): | ||
mandar242 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if poll_result: | ||
stdout += to_text(self.connection._stdout.read(1024)) | ||
self.connection.verbosity_display(4, f"DISABLE ECHO stdout line: \n{to_bytes(stdout)}") | ||
mandar242 marked this conversation as resolved.
AE96
Show resolved
Hide resolved
|
||
match = str(stdout).find("stty -echo") | ||
if match != -1: | ||
break | ||
|
||
def disable_prompt_command(self) -> None: | ||
"""Disable prompt command from the host""" | ||
end_mark = "".join([random.choice(string.ascii_letters) for i in range(self.connection.MARK_LENGTH)]) | ||
disable_prompt_cmd = to_bytes( | ||
"PS1='' ; bind 'set enable-bracketed-paste off'; printf '\\n%s\\n' '" + end_mark + "'\n", | ||
errors="surrogate_or_strict", | ||
) | ||
disable_prompt_reply = re.compile(r"\r\r\n" + re.escape(end_mark) + r"\r\r\n", re.MULTILINE) | ||
|
||
# Send command | ||
self.connection.verbosity_display(4, f"DISABLE PROMPT Disabling Prompt: \n{disable_prompt_cmd}") | ||
self.connection._session.stdin.write(disable_prompt_cmd) | ||
|
||
stdout = "" | ||
for poll_result in self.connection.poll("DISABLE PROMPT", disable_prompt_cmd): | ||
if poll_result: | ||
stdout += to_text(self.connection._stdout.read(1024)) | ||
self.connection.verbosity_display(4, f"DISABLE PROMPT stdout line: \n{to_bytes(stdout)}") | ||
mandar242 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if disable_prompt_reply.search(stdout): | ||
break | ||
|
||
def ensure_ssm_session_has_started(self) -> None: | ||
"""Ensure the SSM session has started on the host. We poll stdout | ||
until we match the following string 'Starting session with SessionId' | ||
""" | ||
stdout = "" | ||
for poll_result in self.connection.poll("START SSM SESSION", "start_session"): | ||
if poll_result: | ||
stdout += to_text(self.connection._stdout.read(1024)) | ||
self.connection.verbosity_display(4, f"START SSM SESSION stdout line: \n{to_bytes(stdout)}") | ||
match = str(stdout).find("Starting session with SessionId") | ||
if match != -1: | ||
self.connection.verbosity_display(4, "START SSM SESSION startup output received") | ||
mandar242 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
break |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.