From 3134779ead94d0ae7023c90e63f6dcd6e87c2943 Mon Sep 17 00:00:00 2001 From: MSAdministrator Date: Wed, 1 Feb 2023 13:29:20 -0600 Subject: [PATCH] feat!: Adding the ability to copy a file from local to a remote host --- src/atomic_operator_runner/runner.py | 54 +++++++++++++++++++++++++--- 1 file changed, 50 insertions(+), 4 deletions(-) diff --git a/src/atomic_operator_runner/runner.py b/src/atomic_operator_runner/runner.py index 6811f24..13294d5 100644 --- a/src/atomic_operator_runner/runner.py +++ b/src/atomic_operator_runner/runner.py @@ -20,6 +20,7 @@ class Runner(Base): """Runs the provided command string locally or remotely.""" + SUPPORTED_PLATFORMS = ["macos", "linux", "windows", "aws"] responses: List[RunnerResponse] = [] def __init__( @@ -52,7 +53,7 @@ def __init__( Raises: IncorrectPlatformError: Raised when the provided platform is not a correct option. """ - if platform.lower() not in ["macos", "linux", "windows", "aws"]: + if platform.lower() not in self.SUPPORTED_PLATFORMS: raise IncorrectPlatformError(provided_platform=platform) Base.config = Host( hostname=hostname, @@ -127,6 +128,51 @@ def run( self.responses.append(self.response) return [x.json() for x in self.responses] - def copy(self) -> None: - """Used to copy files from one system to another.""" - pass + def copy_file(self, source_file: str, destination_replacement_path: str, executor: str, elevation_required: bool = False) -> None: + """Copies the provided single file to the provided destination path. + + Args: + source_file (str): The source file path on the local systems disk. + destination_replacement_path (str): The folder path on the destination system to place the provided file to. + executor (str): The executor to use when running the provided command. + elevation_required (bool, optional): Whether or not elevation is required. Defaults to False. + """ + if not os.path.exists(source_file): + raise SourceFileNotFoundError(source_file=source_file) + if source_file.endswith(".yaml") or source_file.endswith(".yml") or source_file.endswith(".md"): + raise SourceFileNotSupportedError(source_file=source_file) + if Base.config.run_type == "remote": + from .remote import RemoteRunner + + self.log(val=f"Attempting to copy file '{source_file}' to remote host.") + Base.response = RunnerResponse( + start_timestamp=datetime.now(), + environment=TargetEnvironment( + platform=Base.config.platform, + hostname=Base.config.hostname if Base.config.hostname else platform.node(), + user=Base.config.username if Base.config.username else self._get_username(), + ), + ) + if self.config.platform == "windows": + response = RemoteRunner()._copy_file_to_windows( + source=source_file, + desintation=destination_replacement_path, + executor=executor, + elevation_required=elevation_required + ) + if response: + self.log(val=f"Successfully transferred file '{source_file}' to remote windows host.") + else: + self.log(val=f"Error occurred trying to transfer file '{source_file}' to remote host!", level="critical") + elif self.config.platform == "macos" or self.config.platform == "linux": + response = RemoteRunner()._copy_file_to_nix( + source=source_file, + destination=destination_replacement_path, + elevation_required=elevation_required + ) + if response: + self.log(val=f"Successfully transferred file '{source_file}' to remote nix host.") + else: + self.log(val=f"Error occurred trying to transfer file '{source_file}' to remote host!", level="critical") + else: + self.log(val="We only support copying of files on remote systems.", level="warning")