Skip to content

Commit

Permalink
Merge pull request #163 from DLHub-Argonne/dev
Browse files Browse the repository at this point in the history
Dev -- funcx 1.0.x compatibility
  • Loading branch information
ascourtas committed Aug 26, 2022
2 parents 576acb6 + ff5e9d0 commit e35b097
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 10 deletions.
23 changes: 15 additions & 8 deletions dlhub_sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
from mdf_toolbox import login, logout
from mdf_toolbox.globus_search.search_helper import SEARCH_LIMIT
from funcx.sdk.client import FuncXClient
from globus_sdk.scopes import AuthScopes, SearchScopes

from dlhub_sdk.config import DLHUB_SERVICE_ADDRESS, CLIENT_ID
from dlhub_sdk.utils.futures import DLHubFuture
from dlhub_sdk.utils.schemas import validate_against_dlhub_schema
from dlhub_sdk.utils.search import DLHubSearchHelper, get_method_details, filter_latest
from dlhub_sdk.utils.validation import validate
from dlhub_sdk.utils.funcx_login_manager import FuncXLoginManager

# Directory for authentication tokens
_token_dir = os.path.expanduser("~/.dlhub/credentials")
Expand Down Expand Up @@ -92,9 +94,8 @@ def __init__(self, dlh_authorizer: Optional[GlobusAuthorizer] = None,
logger.warning('You have defined some of the authorizers but not all. DLHub is falling back to login. '
'You must provide authorizers for DLHub, Search, OpenID, FuncX.')

fx_scope = "https://auth.globus.org/scopes/facd7ccc-c5f4-42aa-916b-a0e270e2c2a9/all"
auth_res = login(services=["search", "dlhub",
fx_scope, "openid"],
FuncXClient.FUNCX_SCOPE, "openid"],
app_name="DLHub_Client",
make_clients=False,
client_id=CLIENT_ID,
Expand All @@ -105,16 +106,22 @@ def __init__(self, dlh_authorizer: Optional[GlobusAuthorizer] = None,

# Unpack the authorizers
dlh_authorizer = auth_res["dlhub"]
fx_authorizer = auth_res[fx_scope]
fx_authorizer = auth_res[FuncXClient.FUNCX_SCOPE]
openid_authorizer = auth_res['openid']
search_authorizer = auth_res['search']

# Define the subclients needed by the service
self._fx_client = FuncXClient(fx_authorizer=fx_authorizer,
search_authorizer=search_authorizer,
openid_authorizer=openid_authorizer,
no_local_server=kwargs.get("no_local_server", True),
no_browser=kwargs.get("no_browser", True))

auth_dict = {
FuncXClient.FUNCX_SCOPE: fx_authorizer,
AuthScopes.openid: openid_authorizer,
SearchScopes.all: search_authorizer,
'dlhub': dlh_authorizer,
}

login_manager = FuncXLoginManager(authorizers=auth_dict)
self._fx_client = FuncXClient(login_manager=login_manager)

self._search_client = globus_sdk.SearchClient(authorizer=search_authorizer,
transport_params={"http_timeout": http_timeout})

Expand Down
49 changes: 49 additions & 0 deletions dlhub_sdk/utils/funcx_login_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from __future__ import annotations

import logging
import globus_sdk
from globus_sdk.scopes import AuthScopes, SearchScopes
from funcx.sdk.web_client import FuncxWebClient
from funcx import FuncXClient

log = logging.getLogger(__name__)


class FuncXLoginManager:
"""
Implements the funcx.sdk.login_manager.protocol.LoginManagerProtocol class.
https://github.com/funcx-faas/funcX/blob/main/funcx_sdk/funcx/sdk/login_manager/protocol.py#L18
"""
SCOPES = [
FuncXClient.FUNCX_SCOPE,
AuthScopes.openid,
SearchScopes.all
]

def __init__(self, authorizers: dict[str, globus_sdk.RefreshTokenAuthorizer]):
self.authorizers = authorizers

def get_auth_client(self) -> globus_sdk.AuthClient:
return globus_sdk.AuthClient(
authorizer=self.authorizers[AuthScopes.openid]
)

def get_search_client(self) -> globus_sdk.SearchClient:
return globus_sdk.SearchClient(
authorizer=self.authorizers[SearchScopes.all]
)

def get_funcx_web_client(
self, *, base_url: str | None = None, app_name: str | None = None
) -> FuncxWebClient:
return FuncxWebClient(
base_url=base_url,
app_name=app_name,
authorizer=self.authorizers[FuncXClient.FUNCX_SCOPE],
)

def ensure_logged_in(self):
log.warning("ensure_logged_in cannot be invoked from here!")

def logout(self):
log.warning("logout cannot be invoked from here!")
2 changes: 1 addition & 1 deletion dlhub_sdk/utils/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def _type_name_to_type(name: str) -> Union[type, Tuple[type]]:
try:
return type_table.get(name) or __builtins__[name] # getattr(__builtins__, name) if __builtins__ is a module
except KeyError: # AttributeError if __builtins__ is a module
raise ValueError(f"found an unknown type name in servable metadata: {name}")
raise ValueError(f"found an unknown type name in servable metadata: {name}") from None


def _generate_err(err_type: Exception, path: List[Tuple[str, Hashable]], expected: type = None, given: type = None, *, msg: str = None) -> Exception:
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ globus-sdk>=3,<4
requests>=2.24.0
mdf_toolbox>=0.5.4
jsonschema>=3.2.0
funcx>=0.3.6
funcx>=1.0.0
pydantic

0 comments on commit e35b097

Please sign in to comment.