-
Notifications
You must be signed in to change notification settings - Fork 240
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
Add FTP support to jobstores #5142
Merged
Merged
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
195fdc8
Add ftp support
stxue1 176381c
FTP support
stxue1 af46642
strtobool for hardcoded strings for AWS env vars
stxue1 1ea4e3d
typo
stxue1 65e5564
Merge master into issues/5134-ftp-get-size
github-actions[bot] 5ba00b2
Merge master into issues/5134-ftp-get-size
github-actions[bot] 35fbe08
Merge master into issues/5134-ftp-get-size
github-actions[bot] 754b43c
Change envvar to USE_SSL and move to separate file to put original co…
stxue1 5ed71cf
Forgot to add file
stxue1 17536ba
missing space in rst
stxue1 13f4ac5
Merge master into issues/5134-ftp-get-size
github-actions[bot] f234825
Merge master into issues/5134-ftp-get-size
github-actions[bot] 0f38d58
Don't keep FTP objects around forever and disable FTP SSL by default
stxue1 bc5da85
Merge master into issues/5134-ftp-get-size
github-actions[bot] 665e3bd
Merge master into issues/5134-ftp-get-size
github-actions[bot] a7455a0
Merge master into issues/5134-ftp-get-size
github-actions[bot] 7d31e54
Merge master into issues/5134-ftp-get-size
github-actions[bot] d2da46c
Merge master into issues/5134-ftp-get-size
github-actions[bot] a2105af
Fix HTTP errors + add prot_p in secure mode
stxue1 66d3587
Merge remote-tracking branch 'origin/issues/5134-ftp-get-size' into i…
stxue1 9f86277
Always try to use SSL before falling back, raising if user enforces SSL
stxue1 91f1cbd
Change environment_vars.rst to reflect enforcement
stxue1 5884417
add proper port suport
stxue1 f490ed3
Merge master into issues/5134-ftp-get-size
github-actions[bot] bbd321a
Merge master into issues/5134-ftp-get-size
github-actions[bot] 59e3fea
Merge master into issues/5134-ftp-get-size
github-actions[bot] 8fe81cc
Remove FTP SSL support and add comments
stxue1 f499caf
Remove envvar
stxue1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,217 @@ | ||
# Copyright 2017 Oregon Health and Science University | ||
# | ||
# Copyright (C) 2015-2021 Regents of the University of California | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
import ftplib | ||
import logging | ||
import netrc | ||
import os | ||
from contextlib import closing | ||
from typing import Optional, Any, cast, IO | ||
from urllib.parse import urlparse | ||
from urllib.request import urlopen | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class FtpFsAccess: | ||
""" | ||
FTP access with upload. | ||
|
||
Taken and modified from https://github.com/ohsu-comp-bio/cwl-tes/blob/03f0096f9fae8acd527687d3460a726e09190c3a/cwl_tes/ftp.py#L37-L251 | ||
""" | ||
# TODO: Properly support FTP over SSL | ||
|
||
def __init__( | ||
self, cache: Optional[dict[Any, ftplib.FTP]] = None | ||
): | ||
""" | ||
FTP object to handle FTP connections. By default, connect over FTP with TLS. | ||
|
||
:param cache: cache of generated FTP objects | ||
""" | ||
self.cache = cache or {} | ||
self.netrc = None | ||
try: | ||
if "HOME" in os.environ: | ||
if os.path.exists(os.path.join(os.environ["HOME"], ".netrc")): | ||
self.netrc = netrc.netrc(os.path.join(os.environ["HOME"], ".netrc")) | ||
elif os.path.exists(os.path.join(os.curdir, ".netrc")): | ||
self.netrc = netrc.netrc(os.path.join(os.curdir, ".netrc")) | ||
except netrc.NetrcParseError as err: | ||
logger.debug(err) | ||
|
||
def exists(self, fn: str) -> bool: | ||
""" | ||
Check if a file/directory exists over an FTP server | ||
:param fn: FTP url | ||
:return: True or false depending on whether the object exists on the server | ||
""" | ||
return self.isfile(fn) or self.isdir(fn) | ||
|
||
def isfile(self, fn: str) -> bool: | ||
""" | ||
Check if the FTP url points to a file | ||
:param fn: FTP url | ||
:return: True if url is file, else false | ||
""" | ||
ftp = self._connect(fn) | ||
if ftp: | ||
try: | ||
if not self.size(fn) is None: | ||
return True | ||
else: | ||
return False | ||
except ftplib.all_errors: | ||
return False | ||
return False | ||
|
||
def isdir(self, fn: str) -> bool: | ||
""" | ||
Check if the FTP url points to a directory | ||
:param fn: FTP url | ||
:return: True if url is directory, else false | ||
""" | ||
ftp = self._connect(fn) | ||
if ftp: | ||
try: | ||
cwd = ftp.pwd() | ||
ftp.cwd(urlparse(fn).path) | ||
ftp.cwd(cwd) | ||
return True | ||
except ftplib.all_errors: | ||
return False | ||
return False | ||
|
||
def open(self, fn: str, mode: str) -> IO[bytes]: | ||
""" | ||
Open an FTP url. | ||
|
||
Only supports reading, no write support. | ||
:param fn: FTP url | ||
:param mode: Mode to open FTP url in | ||
:return: | ||
""" | ||
if "r" in mode: | ||
host, port, user, passwd, path = self._parse_url(fn) | ||
handle = urlopen("ftp://{}:{}@{}:{}/{}".format(user, passwd, host, port, path)) | ||
return cast(IO[bytes], closing(handle)) | ||
# TODO: support write mode | ||
raise Exception("Write mode FTP not implemented") | ||
|
||
def _parse_url( | ||
self, url: str | ||
) -> tuple[str, int, Optional[str], Optional[str], str]: | ||
""" | ||
Parse an FTP url into hostname, username, password, and path | ||
:param url: | ||
:return: hostname, username, password, path | ||
""" | ||
parse = urlparse(url) | ||
user = parse.username | ||
passwd = parse.password | ||
host = parse.hostname | ||
port = parse.port | ||
path = parse.path | ||
if host is None: | ||
# The URL we connect to must have a host | ||
raise RuntimeError(f"FTP URL does not contain a host: {url}") | ||
# default port is 21 | ||
if port is None: | ||
port = 21 | ||
if parse.scheme == "ftp": | ||
if not user and self.netrc: | ||
if host is not None: | ||
creds = self.netrc.authenticators(host) | ||
if creds: | ||
user, _, passwd = creds | ||
if not user: | ||
if host is not None: | ||
user, passwd = self._recall_credentials(host) | ||
if passwd is None: | ||
passwd = "anonymous@" | ||
if user is None: | ||
user = "anonymous" | ||
return host, port, user, passwd, path | ||
|
||
def _connect(self, url: str) -> Optional[ftplib.FTP]: | ||
""" | ||
Connect to an FTP server. Handles authentication. | ||
:param url: FTP url | ||
:return: FTP object | ||
""" | ||
parse = urlparse(url) | ||
if parse.scheme == "ftp": | ||
host, port, user, passwd, _ = self._parse_url(url) | ||
if host is None: | ||
# there has to be a host | ||
return None | ||
if (host, user, passwd) in self.cache: | ||
if self.cache[(host, user, passwd)].pwd(): | ||
return self.cache[(host, user, passwd)] | ||
ftp = ftplib.FTP_TLS() | ||
# Note: the FTP lib logger handles logging itself and doesn't go through our logging implementation | ||
ftp.set_debuglevel(1 if logger.isEnabledFor(logging.DEBUG) else 0) | ||
ftp.connect(host, port) | ||
env_user = os.getenv("TOIL_FTP_USER") | ||
env_passwd = os.getenv("TOIL_FTP_PASSWORD") | ||
if env_user: | ||
user = env_user | ||
if env_passwd: | ||
passwd = env_passwd | ||
ftp.login(user or "", passwd or "", secure=False) | ||
self.cache[(host, user, passwd)] = ftp | ||
return ftp | ||
return None | ||
|
||
def _recall_credentials( | ||
self, desired_host: str | ||
) -> tuple[Optional[str], Optional[str]]: | ||
""" | ||
Grab the cached credentials | ||
:param desired_host: FTP hostname | ||
:return: username, password | ||
""" | ||
for host, user, passwd in self.cache: | ||
if desired_host == host: | ||
return user, passwd | ||
return None, None | ||
|
||
def size(self, fn: str) -> Optional[int]: | ||
""" | ||
Get the size of an FTP object | ||
:param fn: FTP url | ||
:return: Size of object | ||
""" | ||
ftp = self._connect(fn) | ||
if ftp: | ||
host, port, user, passwd, path = self._parse_url(fn) | ||
try: | ||
return ftp.size(path) | ||
except ftplib.all_errors as e: | ||
if str(e) == "550 SIZE not allowed in ASCII mode": | ||
# some servers don't allow grabbing size in ascii mode | ||
# https://stackoverflow.com/questions/22090001/get-folder-size-using-ftplib/22093848#22093848 | ||
ftp.voidcmd("TYPE I") | ||
return ftp.size(path) | ||
handle = urlopen("ftp://{}:{}@{}:{}/{}".format(user, passwd, host, port, path)) | ||
info = handle.info() | ||
handle.close() | ||
if "Content-length" in info: | ||
return int(info["Content-length"]) | ||
return None | ||
|
||
return None |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't respect
TOIL_FTP_USE_SSL
.If we implemented
open()
on top of https://docs.python.org/3/library/ftplib.html#ftplib.FTP.retrbinary and supporting files larger than memory, we'd need some kind of pipe file object and a thread to feed the data into it.We have some pipe implementations over in
toil/src/toil/jobStores/utils.py
Line 153 in a5657cf
open()
needs to produce a file object that Just Works and have all the cleanup happen automatically when it goes out of scope and is closed. We might need to useclosing()
to help with that?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed to put into another issue as it appears to be too large of an issue