From c820a09ca7b7068477b00891123857ea6b4ac229 Mon Sep 17 00:00:00 2001 From: Parul Date: Tue, 13 Feb 2024 11:56:29 -0800 Subject: [PATCH] rename ssh to sftp and graylog optional in cli --- cli.py | 91 +++++++++++++++++++++++++++------------- pyproject.toml | 2 +- src/filepass/__init__.py | 2 +- src/filepass/filepass.py | 19 +++++---- 4 files changed, 75 insertions(+), 39 deletions(-) diff --git a/cli.py b/cli.py index 680f11e..9d039f2 100644 --- a/cli.py +++ b/cli.py @@ -1,6 +1,7 @@ import logging import os import sys +from sys import stdout import graypy @@ -32,38 +33,72 @@ def main(): to_share = os.environ.get("TOSMBSHARE") to_method = os.environ.get("TOMETHOD") to_delete = os.environ.get("TODELETE") + # Environment variable added for using local or Graylog logging + use_graylog = os.environ.get("USE_GRAYLOG") - filepass_logger = logging.getLogger("filepass_logger") - filepass_logger.setLevel(logging.DEBUG) + if use_graylog == "yes": + filepass_logger = logging.getLogger("filepass_logger") + filepass_logger.setLevel(logging.DEBUG) - handler = graypy.GELFTCPHandler( - os.environ.get("GRAYLOG_SERVER"), int(os.environ.get("GRAYLOG_PORT")) - ) - filepass_logger.addHandler(handler) + handler = graypy.GELFTCPHandler( + os.environ.get("GRAYLOG_SERVER"), int(os.environ.get("GRAYLOG_PORT")) + ) + filepass_logger.addHandler(handler) + + handler_std = logging.StreamHandler(sys.stdout) + filepass_logger.addHandler(handler_std) - handler_std = logging.StreamHandler(sys.stdout) - filepass_logger.addHandler(handler_std) + logger = logging.LoggerAdapter( + filepass_logger, + { + "from_method": from_method, + "from_user": from_user, + "from_svr": from_svr, + "from_port": from_port, + "from_share": from_share, + "from_dir": from_dir, + "from_filter": from_filter, + "to_method": to_method, + "to_user": to_user, + "to_svr": to_svr, + "to_port": to_port, + "to_share": to_share, + "to_dir": to_dir, + "integration": "filepass", + "filepass_name": os.environ.get("INTEGRATION_NAME"), + }, + ) - logger = logging.LoggerAdapter( - filepass_logger, - { - "from_method": from_method, - "from_user": from_user, - "from_svr": from_svr, - "from_port": from_port, - "from_share": from_share, - "from_dir": from_dir, - "from_filter": from_filter, - "to_method": to_method, - "to_user": to_user, - "to_svr": to_svr, - "to_port": to_port, - "to_share": to_share, - "to_dir": to_dir, - "integration": "filepass", - "filepass_name": os.environ.get("INTEGRATION_NAME"), - }, - ) + else: + # Docker logger output to stdout + logFormatter = logging.Formatter( + "%(asctime)s [%(threadName)-12.12s] [%(levelname)-5.5s] %(message)s" + ) + local_logger = logging.getLogger("local_logger") + local_logger.setLevel(logging.DEBUG) + consoleHandler = logging.StreamHandler(stdout) + consoleHandler.setFormatter(logFormatter) + local_logger.addHandler(consoleHandler) + logger = logging.LoggerAdapter( + local_logger, + { + "from_method": from_method, + "from_user": from_user, + "from_svr": from_svr, + "from_port": from_port, + "from_share": from_share, + "from_dir": from_dir, + "from_filter": from_filter, + "to_method": to_method, + "to_user": to_user, + "to_svr": to_svr, + "to_port": to_port, + "to_share": to_share, + "to_dir": to_dir, + "integration": "filepass", + "filepass_name": os.environ.get("INTEGRATION_NAME"), + }, + ) try: file_pass( logger, diff --git a/pyproject.toml b/pyproject.toml index 87aec4d..ed29138 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "hatchling.build" [project] name = "filepass" -version = "0.2.0" +version = "0.3.1" authors = [ { name="Orlund Norstrom", email="onorstrom@kamloops.ca" }, { name="Marco Lussetti", email="mlussetti@kamloops.ca" }, diff --git a/src/filepass/__init__.py b/src/filepass/__init__.py index d169ea3..69d6714 100644 --- a/src/filepass/__init__.py +++ b/src/filepass/__init__.py @@ -1 +1 @@ -from .filepass import file_pass, smb_connection, ssh_connection +from .filepass import file_pass, sftp_connection, smb_connection diff --git a/src/filepass/filepass.py b/src/filepass/filepass.py index 27a6f37..d3b2ebc 100644 --- a/src/filepass/filepass.py +++ b/src/filepass/filepass.py @@ -5,9 +5,9 @@ # File Transfer Types -def ssh_connection(logger, user, pw, svr, port, dir): - logger.debug("ssh://{}:{}@{}:{}{}".format(user, "passwordhere", svr, port, dir)) - fs_conn = fs.open_fs("ssh://{}:{}@{}:{}{}".format(user, pw, svr, port, dir)) +def sftp_connection(logger, user, pw, svr, port, dir): + logger.debug("sftp://{}:{}@{}:{}{}".format(user, "passwordhere", svr, port, dir)) + fs_conn = fs.open_fs("sftp://{}:{}@{}:{}{}".format(user, pw, svr, port, dir)) return fs_conn @@ -49,9 +49,9 @@ def file_pass( to_delete, ): # From File System - if from_method == "ssh": - logger.debug("Create from SSH connection") - from_fs = ssh_connection( + if from_method == "sftp": + logger.debug("Create from SFTP connection") + from_fs = sftp_connection( logger, from_user, from_pw, from_svr, from_port, from_dir ) @@ -62,9 +62,9 @@ def file_pass( ) # To File System - if to_method == "ssh": - logger.debug("Create to SSH connection") - to_fs = ssh_connection(logger, to_user, to_pw, to_svr, to_port, to_dir) + if to_method == "sftp": + logger.debug("Create to SFTP connection") + to_fs = sftp_connection(logger, to_user, to_pw, to_svr, to_port, to_dir) if to_method == "smb": logger.debug("Create to SMB connection") to_fs = smb_connection( @@ -106,4 +106,5 @@ def file_pass( logger.debug("No delete (from): {}".format(path)) from_fs.close() + to_fs.close()