Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rename ssh to sftp and graylog optional in cli #5

Merged
merged 4 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,18 @@ def main():
to_share = os.environ.get("TOSMBSHARE")
to_method = os.environ.get("TOMETHOD")
to_delete = os.environ.get("TODELETE")
# Environment variable added for filename change in single file mode
new_filename = os.environ.get("NEW_FILENAME")

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)
# Check if GRAYLOG SERVER configurations are set and add handler accordingly
if os.environ.get("GRAYLOG_SERVER") and os.environ.get("GRAYLOG_PORT") is not None:
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)
Expand All @@ -62,6 +66,7 @@ def main():
"to_dir": to_dir,
"integration": "filepass",
"filepass_name": os.environ.get("INTEGRATION_NAME"),
"new_filename": new_filename,
},
)
try:
Expand All @@ -84,6 +89,7 @@ def main():
to_share,
to_method,
to_delete,
new_filename,
)
except:
logger.exception("Critical error found", stack_info=True)
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.0"
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
38 changes: 24 additions & 14 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 All @@ -24,8 +24,10 @@ def smb_connection(logger, user, pw, svr, port, smbshare, dir):
return fs_conn


def transfer_file(from_fs, to_fs, filename):
to_fs.writefile(filename, from_fs.open(filename, "rb"))
# Boolean parameter to add ability to rename target file in single file mode
def transfer_file(from_fs, to_fs, filename, should_rename=False, new_filename=None):
target_filename = new_filename if new_filename and should_rename else filename
to_fs.writefile(target_filename, from_fs.open(filename, "rb"))


def file_pass(
Expand All @@ -47,11 +49,12 @@ def file_pass(
to_share,
to_method,
to_delete,
new_filename=None,
):
# 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 +65,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 All @@ -73,7 +76,8 @@ def file_pass(

# Do the move
walker = Walker(filter=[from_filter], ignore_errors=True, max_depth=1)

# Create a list of files to be transferred based on the filter.
total_files = list(walker.files(from_fs))
for path in walker.files(from_fs):
logger.debug("File to move: {}".format(path))

Expand All @@ -89,8 +93,13 @@ def file_pass(
else:
logger.debug("No delete (to): {}".format(path))

# todo: document, paths with files should be at the lowest level (no sub dirs)
transfer_file(from_fs, to_fs, path)
# Confirm if single file mode condition is satisfied
if len(total_files) == 1 and new_filename:
should_rename = True
else:
should_rename = False

transfer_file(from_fs, to_fs, path, should_rename, new_filename=new_filename)

if from_delete == "yes" and from_fs.exists(path):
logger.debug("delete (from): {}".format(path))
Expand All @@ -106,4 +115,5 @@ def file_pass(
logger.debug("No delete (from): {}".format(path))

from_fs.close()

to_fs.close()
Loading