Skip to content

Commit

Permalink
feat!: Adding the ability to copy a file from local to a remote host
Browse files Browse the repository at this point in the history
  • Loading branch information
MSAdministrator committed Feb 1, 2023
1 parent 84ca533 commit 3134779
Showing 1 changed file with 50 additions and 4 deletions.
54 changes: 50 additions & 4 deletions src/atomic_operator_runner/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__(
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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")

0 comments on commit 3134779

Please sign in to comment.