From e41a955230da632ca78a84de79993ccd2ac353b9 Mon Sep 17 00:00:00 2001 From: MSAdministrator Date: Wed, 14 Sep 2022 11:19:19 -0500 Subject: [PATCH 1/7] bump: Bumped patch version --- .cz.toml | 2 +- pyproject.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.cz.toml b/.cz.toml index 2ec7bc1..da517b1 100644 --- a/.cz.toml +++ b/.cz.toml @@ -1,5 +1,5 @@ [tool.commitizen] -version = "0.1.5" +version = "0.1.6" version_files = [ "pyproject.toml:version" ] diff --git a/pyproject.toml b/pyproject.toml index e117a58..7f85c5c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "atomic-operator-runner" -version = "0.1.6" +version = "0.1.7" description = "atomic-operator-runner" authors = ["Josh Rickard "] license = "MIT" From 7e05db928b36dbbfcb635ea23bf07796537fef7b Mon Sep 17 00:00:00 2001 From: MSAdministrator Date: Wed, 14 Sep 2022 11:19:37 -0500 Subject: [PATCH 2/7] feat: Added log method to base class --- src/atomic_operator_runner/base.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/atomic_operator_runner/base.py b/src/atomic_operator_runner/base.py index 5e97825..91bc47d 100644 --- a/src/atomic_operator_runner/base.py +++ b/src/atomic_operator_runner/base.py @@ -1,6 +1,7 @@ """Base class for all classes in this project.""" # Copyright: (c) 2022, Swimlane # MIT License (see LICENSE or https://opensource.org/licenses/MIT) +import inspect import platform from typing import Dict @@ -45,3 +46,20 @@ def get_local_system_platform(self) -> str: if os_name == "darwin": return "macos" return os_name + + def log(self, val, level="info") -> None: + """Used to centralize logging across components. + + We identify the source of the logging class by inspecting the calling stack. + + Args: + val (str): The log value string to output. + level (str, optional): The log level. Defaults to "info". + """ + component = None + parent = inspect.stack()[1][0].f_locals.get("self", None) + component = parent.__class__.__name__ + try: + getattr(getattr(parent, f"_{component}__logger"), level)(val) + except AttributeError as ae: + getattr(self.__logger, level)(val) From bfdad4ea077f6d8d6c1c2f86c1e01af6d4039fa7 Mon Sep 17 00:00:00 2001 From: MSAdministrator Date: Wed, 14 Sep 2022 11:19:55 -0500 Subject: [PATCH 3/7] feat: Added better handling/converting of executors to actual executor --- src/atomic_operator_runner/local.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/atomic_operator_runner/local.py b/src/atomic_operator_runner/local.py index eee7947..f4a4e52 100644 --- a/src/atomic_operator_runner/local.py +++ b/src/atomic_operator_runner/local.py @@ -7,6 +7,7 @@ from .base import Base from .processor import Processor +from .utils.exceptions import IncorrectExecutorError, IncorrectPlatformError class LocalRunner(Base): @@ -33,9 +34,14 @@ def run( env (dict, optional): Environment to use including environmental variables.. Defaults to os.environ. cwd (str, optional): The current working directory. Defaults to None. """ + if not self.COMMAND_MAP.get(executor): + raise IncorrectExecutorError(provided_executor=executor) + if not self.COMMAND_MAP.get(executor).get(Base.config.platform): + raise IncorrectPlatformError(provided_platform=Base.config.platform) + _executor = self.COMMAND_MAP[executor][Base.config.platform] self.__logger.debug("Starting a subprocess on the local system.") process = subprocess.Popen( - executor, + _executor, shell=shell, stdin=subprocess.PIPE, stdout=subprocess.PIPE, @@ -48,7 +54,7 @@ def run( outs, errs = process.communicate(bytes(command, "utf-8") + b"\n", timeout=timeout) # Adding details to our object response object Processor( - command=command, executor=executor, return_code=process.returncode, output=str(outs), errors=str(errs) + command=command, executor=_executor, return_code=process.returncode, output=str(outs), errors=str(errs) ) except subprocess.TimeoutExpired as e: if e.output: From 1083eb1b6dbc5d4768865345f8b8ba58081b29df Mon Sep 17 00:00:00 2001 From: MSAdministrator Date: Wed, 14 Sep 2022 11:20:18 -0500 Subject: [PATCH 4/7] fix: Updated cpature_base_records to ensure default value of none on variable --- src/atomic_operator_runner/processor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/atomic_operator_runner/processor.py b/src/atomic_operator_runner/processor.py index eb3270c..b4daa26 100644 --- a/src/atomic_operator_runner/processor.py +++ b/src/atomic_operator_runner/processor.py @@ -42,7 +42,7 @@ def _capture_base_records(self, data: Any) -> None: Args: data (Any): Data to build a BaseRecord from. """ - record: Union[BaseRecord, List[BaseRecord]] + record: Union[BaseRecord, List[BaseRecord]] = None if isinstance(data, dict): try: record = BaseRecord(**data) From 59fe8903481ec1aee09812efb6ba247dedcd76c1 Mon Sep 17 00:00:00 2001 From: MSAdministrator Date: Wed, 14 Sep 2022 11:20:28 -0500 Subject: [PATCH 5/7] feat: Added and updated exceptions --- .../utils/exceptions.py | 51 +++++++++++-------- 1 file changed, 30 insertions(+), 21 deletions(-) diff --git a/src/atomic_operator_runner/utils/exceptions.py b/src/atomic_operator_runner/utils/exceptions.py index 09ee7c7..c56f9fb 100644 --- a/src/atomic_operator_runner/utils/exceptions.py +++ b/src/atomic_operator_runner/utils/exceptions.py @@ -17,7 +17,15 @@ class IncorrectExecutorError(Exception): """Raised when the incorrect executor is used.""" - pass + def __init__(self, provided_executor: str) -> None: + """Raises when the provided executor is not correct or unknown.""" + from ..base import Base + + Base().log( + val=f"The provided executor of '{provided_executor}' is not one of " + f"{','.join([k for k in Base.COMMAND_MAP.keys()])}", + level="critical" + ) class IncorrectPlatformError(Exception): @@ -27,8 +35,9 @@ def __init__(self, provided_platform: str) -> None: """Raises when the provided platforms is not correct.""" from ..base import Base - Base.__logger.critical( - f"The provided platform of '{provided_platform}' is not one of macos, linux, windows or aws" + Base().log( + f"The provided platform of '{provided_platform}' is not one of macos, linux, windows or aws", + level="critical" ) @@ -43,45 +52,45 @@ def __init__(self, exception: Any) -> None: error_string = ( f"SSH Error - Unable to connect to {Base.config.hostname} - Received {type(exception).__name__}" ) - Base.__logger.debug(f"Full stack trace: {exception}") - Base.__logger.warning(error_string) + Base().log(f"Full stack trace: {exception}", level="debug") + Base().log(error_string, level="warning") elif exception is AuthenticationException: error_string = f"SSH Error - Unable to authenticate to host - {Base.config.hostname} " error_string += f"- Received {type(exception).__name__}" - Base.__logger.debug(f"Full stack trace: {exception}") - Base.__logger.warning(error_string) + Base().log(f"Full stack trace: {exception}", level="debug") + Base().log(error_string, level="warning") elif exception is BadAuthenticationType: error_string = f"SSH Error - Unable to use provided authentication type to host - {Base.config.hostname} " error_string += f"- Received {type(exception).__name__}" - Base.__logger.debug(f"Full stack trace: {exception}") - Base.__logger.warning(error_string) + Base().log(f"Full stack trace: {exception}", level="debug") + Base().log(error_string, level="warning") elif exception is PasswordRequiredException: error_string = f"SSH Error - Must provide a password to authenticate to host - {Base.config.hostname} " error_string += f"- Received {type(exception).__name__}" - Base.__logger.debug(f"Full stack trace: {exception}") - Base.__logger.warning(error_string) + Base().log(f"Full stack trace: {exception}", level="debug") + Base().log(error_string, level="warning") elif exception is AuthenticationError: error_string = f"Windows Error - Unable to authenticate to host - {Base.config.hostname} " error_string += f"- Received {type(exception).__name__}" - Base.__logger.debug(f"Full stack trace: {exception}") - Base.__logger.warning(error_string) + Base().log(f"Full stack trace: {exception}", level="debug") + Base().log(error_string, level="warning") elif exception is WinRMTransportError: error_string = f"Windows Error - Error occurred during transport on host - {Base.config.hostname} " error_string += f"- Received {type(exception).__name__}" - Base.__logger.debug(f"Full stack trace: {exception}") - Base.__logger.warning(error_string) + Base().log(f"Full stack trace: {exception}", level="debug") + Base().log(error_string, level="warning") elif exception is WSManFaultError: error_string = f"Windows Error - Received WSManFault information from host - {Base.config.hostname} " error_string += f"- Received {type(exception).__name__}" - Base.__logger.debug(f"Full stack trace: {exception}") - Base.__logger.warning(error_string) + Base().log(f"Full stack trace: {exception}", level="debug") + Base().log(error_string, level="warning") elif exception is RequestException: error_string = f"Request Exception - Connection Error to the configured host - {Base.config.hostname} " error_string += f"- Received {type(exception).__name__}" - Base.__logger.debug(f"Full stack trace: {exception}") - Base.__logger.warning(error_string) + Base().log(f"Full stack trace: {exception}", level="debug") + Base().log(error_string, level="warning") else: error_string = f"Unknown Error - Received an unknown error from host - {Base.config.hostname} " error_string += f"- Received {type(exception).__name__}" - Base.__logger.debug(f"Full stack trace: {exception}") - Base.__logger.warning(error_string) + Base().log(f"Full stack trace: {exception}", level="debug") + Base().log(error_string, level="warning") From 18686036ce5f4830f60f7362cc1f6495ec3d688b Mon Sep 17 00:00:00 2001 From: MSAdministrator Date: Wed, 14 Sep 2022 11:22:18 -0500 Subject: [PATCH 6/7] dev: Updated formatting of exceptions based on black --- src/atomic_operator_runner/utils/exceptions.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/atomic_operator_runner/utils/exceptions.py b/src/atomic_operator_runner/utils/exceptions.py index c56f9fb..00b7640 100644 --- a/src/atomic_operator_runner/utils/exceptions.py +++ b/src/atomic_operator_runner/utils/exceptions.py @@ -24,7 +24,7 @@ def __init__(self, provided_executor: str) -> None: Base().log( val=f"The provided executor of '{provided_executor}' is not one of " f"{','.join([k for k in Base.COMMAND_MAP.keys()])}", - level="critical" + level="critical", ) @@ -37,7 +37,7 @@ def __init__(self, provided_platform: str) -> None: Base().log( f"The provided platform of '{provided_platform}' is not one of macos, linux, windows or aws", - level="critical" + level="critical", ) From 49e37587541aa2b617d683ed6bad65b3f6fa0328 Mon Sep 17 00:00:00 2001 From: MSAdministrator Date: Wed, 14 Sep 2022 11:28:44 -0500 Subject: [PATCH 7/7] dev: Updated based on flake8 standards --- src/atomic_operator_runner/base.py | 2 +- src/atomic_operator_runner/local.py | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/atomic_operator_runner/base.py b/src/atomic_operator_runner/base.py index 91bc47d..2761c06 100644 --- a/src/atomic_operator_runner/base.py +++ b/src/atomic_operator_runner/base.py @@ -62,4 +62,4 @@ def log(self, val, level="info") -> None: try: getattr(getattr(parent, f"_{component}__logger"), level)(val) except AttributeError as ae: - getattr(self.__logger, level)(val) + getattr(self.__logger, level)(val + ae) diff --git a/src/atomic_operator_runner/local.py b/src/atomic_operator_runner/local.py index f4a4e52..17cd566 100644 --- a/src/atomic_operator_runner/local.py +++ b/src/atomic_operator_runner/local.py @@ -7,7 +7,8 @@ from .base import Base from .processor import Processor -from .utils.exceptions import IncorrectExecutorError, IncorrectPlatformError +from .utils.exceptions import IncorrectExecutorError +from .utils.exceptions import IncorrectPlatformError class LocalRunner(Base): @@ -33,6 +34,10 @@ def run( shell (bool, optional): Whether to spawn a new shell or not. Defaults to False. env (dict, optional): Environment to use including environmental variables.. Defaults to os.environ. cwd (str, optional): The current working directory. Defaults to None. + + Raises: + IncorrectExecutorError: Raises when an incorrect executor is provided + IncorrectPlatformError: Raised when an incorrect platform is provided """ if not self.COMMAND_MAP.get(executor): raise IncorrectExecutorError(provided_executor=executor)