Skip to content

Commit

Permalink
rename ssh to sftp and graylog optional in cli
Browse files Browse the repository at this point in the history
  • Loading branch information
sparul93 committed Feb 13, 2024
1 parent 7d5382f commit c820a09
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 39 deletions.
91 changes: 63 additions & 28 deletions cli.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import os
import sys
from sys import stdout

import graypy

Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "hatchling.build"

[project]
name = "filepass"
version = "0.2.0"
version = "0.3.1"
authors = [
{ name="Orlund Norstrom", email="[email protected]" },
{ name="Marco Lussetti", email="[email protected]" },
Expand Down
2 changes: 1 addition & 1 deletion src/filepass/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .filepass import file_pass, smb_connection, ssh_connection
from .filepass import file_pass, sftp_connection, smb_connection
19 changes: 10 additions & 9 deletions src/filepass/filepass.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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
)

Expand All @@ -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(
Expand Down Expand Up @@ -106,4 +106,5 @@ def file_pass(
logger.debug("No delete (from): {}".format(path))

from_fs.close()

to_fs.close()

0 comments on commit c820a09

Please sign in to comment.