diff --git a/abm/__init__.py b/abm/__init__.py index fb8a5b7..adc875e 100644 --- a/abm/__init__.py +++ b/abm/__init__.py @@ -1,6 +1,8 @@ import os import sys +import yaml + sys.path.append(os.path.dirname(os.path.realpath(__file__))) diff --git a/abm/lib/__init__.py b/abm/lib/__init__.py index 4efc7e1..a17780b 100644 --- a/abm/lib/__init__.py +++ b/abm/lib/__init__.py @@ -24,3 +24,20 @@ class Keys: COLLECTION = 'collection' HISTORY_BASE_NAME = 'output_history_base_name' HISTORY_NAME = 'history_name' + + +# def get_master_api_key(): +# ''' +# Get the master API key from the environment or configuration file. +# ''' +# if 'GALAXY_MASTER_API_KEY' in os.environ: +# return os.environ['GALAXY_MASTER_API_KEY'] +# config_path = os.path.expanduser("~/.abm/config.yml") +# if not os.path.exists(config_path): +# raise RuntimeError(f"ERROR: Configuration file not found: {config_path}") +# with open(config_path, 'r') as f: +# config = yaml.safe_load(f) +# key = config.get('GALAXY_MASTER_API_KEY', None) +# if key == None: +# raise RuntimeError("ERROR: GALAXY_MASTER_API_KEY not found in config.yml") +# return key diff --git a/abm/lib/common.py b/abm/lib/common.py index cbc035f..0fe6b3f 100644 --- a/abm/lib/common.py +++ b/abm/lib/common.py @@ -61,20 +61,26 @@ def __init__(self, *args): if len(args) == 1: arg = args[0] if type(arg) == str: - self.GALAXY_SERVER, self.API_KEY, self.KUBECONFIG = parse_profile(arg) + self.GALAXY_SERVER, self.API_KEY, self.KUBECONFIG, self.MASTER_KEY = parse_profile(arg) elif type(arg) == dict: self.GALAXY_SERVER = arg['GALAXY_SERVER'] self.API_KEY = arg['API_KEY'] self.KUBECONFIG = arg['KUBECONFIG'] + if 'MASTER_KEY' in arg: + self.MASTER_KEY = arg['MASTER_KEY'] + else: + self.MASTER_KEY = None else: raise Exception(f'Invalid arg for Context: {type(arg)}') - elif len(args) == 3: + elif len(args) == 3 or len(args) == 4: self.GALAXY_SERVER = args[0] self.API_KEY = args[1] self.KUBECONFIG = args[2] + if len(args) == 4: + self.MASTER_KEY = args[3] else: raise Exception( - f'Invalid args for Context. Expected one or three, found {len(args)}' + f'Invalid args for Context. Expected one or four, found {len(args)}' ) @@ -86,7 +92,7 @@ def print_yaml(obj): get_yaml_parser().dump(obj, sys.stdout) -def connect(context: Context): +def connect(context: Context, use_master_key=False): """ Create a connection to the Galaxy instance @@ -100,7 +106,14 @@ def connect(context: Context): print('ERROR: The Galaxy API key has not been set. Please check your') print(' configuration in ~/.abm/profile.yml and try again.') sys.exit(1) - gi = bioblend.galaxy.GalaxyInstance(url=context.GALAXY_SERVER, key=context.API_KEY) + key = context.API_KEY + if use_master_key: + if context.MASTER_KEY is None: + print('ERROR: The Galaxy master key has not been set. Please check your') + print(' configuration in ~/.abm/profile.yml and try again.') + sys.exit(1) + key = context.MASTER_KEY + gi = bioblend.galaxy.GalaxyInstance(url=context.GALAXY_SERVER, key=key) gi.max_get_attempts = 3 gi.get_retry_delay = 1 return gi @@ -113,7 +126,7 @@ def _set_active_profile(profile_name: str): :param profile_name: :return: """ - lib.GALAXY_SERVER, lib.API_KEY, lib.KUBECONFIG = parse_profile(profile_name) + lib.GALAXY_SERVER, lib.API_KEY, lib.KUBECONFIG, lib.MASTER_KEY = parse_profile(profile_name) return lib.GALAXY_SERVER != None @@ -174,10 +187,11 @@ def parse_profile(profile_name: str): :param profile_name: path to the profile to parse :return: a tuple containing the Galaxy URL, API key, and path to the kubeconfig ''' + nones = (None, None, None, None) profiles = load_profiles() if profiles is None: print(f'ERROR: Could not locate an abm profile file in {PROFILE_SEARCH_PATH}') - return None, None, None + return nones if profile_name not in profiles: print(f'ERROR: {profile_name} is not the name of a valid profile.') keys = list(profiles.keys()) @@ -186,11 +200,15 @@ def parse_profile(profile_name: str): ', '.join([f"'{k}'" for k in keys[0:-2]]) + f", and '{keys[-1]}'" ) print(f'The defined profile names are: {quoted_keys}') - return None, None, None + return nones profile = profiles[profile_name] + kube = None + master = 'galaxypassword' if 'kube' in profile: - return (profile['url'], profile['key'], os.path.expanduser(profile['kube'])) - return (profile['url'], profile['key'], None) + kube = os.path.expanduser(profile['kube']) + if 'master' in profile: + master = profile['master'] + return (profile['url'], profile['key'], kube, master) def run(command, env: dict = None): diff --git a/abm/lib/history.py b/abm/lib/history.py index e3fb4b8..b19233b 100644 --- a/abm/lib/history.py +++ b/abm/lib/history.py @@ -235,7 +235,7 @@ def error_message(msg='Invalid command'): return url = datasets[args[0]] elif len(args) == 3: - server, key = parse_profile(args[0]) + server, key, kube, master = parse_profile(args[0]) if server is None: error_message(f"Invalid server profile name: {args[0]}") return diff --git a/abm/lib/users.py b/abm/lib/users.py index cb98a46..ceacd82 100644 --- a/abm/lib/users.py +++ b/abm/lib/users.py @@ -6,9 +6,7 @@ def do_list(context: Context, args: list): - # TODO the master API key needs to be parameterized or specified in a config file. - context.API_KEY = "galaxypassword" - gi = connect(context) + gi = connect(context, use_master_key=True) user_list = gi.users.get_users() pprint(user_list) @@ -22,9 +20,7 @@ def get_api_key(context: Context, args: list): print("ERROR: no user email given") return - # TODO the master API key needs to be parameterized or specified in a config file. - context.API_KEY = "galaxypassword" - gi = connect(context) + gi = connect(context, use_master_key=True) user_list = gi.users.get_users(f_email=args[0]) if user_list is None or len(user_list) == 0: print("WARNING: no such user") @@ -52,9 +48,7 @@ def create(context: Context, args: list): print(f"ERROR: {email} does not look like a valid email address") return - # TODO the master API key needs to be parameterized or specified in a config file. - context.API_KEY = "galaxypassword" - gi = connect(context) + gi = connect(context, use_master_key=True) user_record = gi.users.create_local_user(name, email, password) id = user_record['id'] key = gi.users.create_user_apikey(id) @@ -67,9 +61,7 @@ def show(context: Context, args: list): print("ERROR: no user email given") return - # TODO the master API key needs to be parameterized or specified in a config file. - context.API_KEY = "galaxypassword" - gi = connect(context) + gi = connect(context, use_master_key=True) id = _get_user_id(gi, args[0]) if id is None: return