def setup_from_config(config_file=None, fallback=None): """ setup from a cloud configuration file If the configuration files does not contain OMEGA_USERID and OMEGA_APIKEY, will use given fallback. """ from omegaml import _base_config config_file = config_file or _base_config.OMEGA_CONFIG_FILE AuthenticationEnv.secure() if isinstance(config_file, str) and os.path.exists(config_file): with open(config_file, 'r') as fin: userconfig = yaml.safe_load(fin) if isinstance(userconfig, dict) and 'OMEGA_USERID' in userconfig: try: omega = setup(userid=userconfig['OMEGA_USERID'], apikey=userconfig['OMEGA_APIKEY'], qualifier=userconfig.get('OMEGA_QUALIFIER'), api_url=userconfig.get('OMEGA_RESTAPI_URL')) except Exception as e: # TODO make this a SystemError so that OmegaDeferredIstance.setup reverts to proper defaults raise SystemError( f'Could not login using config file {config_file}, error={e}' ) elif fallback: # if alternative loading was provided, use that omega = fallback() else: raise SystemError( 'No cloud cloud userid/apikey found in config file {}.'. format(config_file)) return omega raise SystemError('Config file {} does not exist'.format(config_file))
def setup(userid=None, apikey=None, api_url=None, qualifier=None, bucket=None): import omegaml as om_mod api_url = ensure_api_url(api_url, om_mod._base_config) view = getattr(om_mod._base_config, 'OMEGA_SERVICES_INCLUSTER', False) auth_env = AuthenticationEnv.secure() om = auth_env.get_omega_from_apikey(userid=userid, apikey=apikey, api_url=api_url, qualifier=qualifier, view=view) # prepare om to be linked to default om.Omega = OmegaCloud om.setup = lambda *args, **kwargs: setup( **{ **dict(userid=userid, apikey=apikey, qualifier=qualifier, bucket=bucket), **kwargs }) om._om = om # ensure link to deferred instance om_mod.Omega = OmegaCloud om_mod.link(om) return om[bucket]
def _save_userconfig_from_apikey(configfile, userid, apikey, api_url=None, requested_userid=None, view=False, keys=None, qualifier=None): from omegaml import settings defaults = settings() api_url = ensure_api_url(api_url, defaults) required_keys = [ 'OMEGA_USERID', 'OMEGA_APIKEY', 'OMEGA_RESTAPI_URL', 'OMEGA_QUALIFIER' ] keys = keys or [] auth_env = AuthenticationEnv.secure() with open(configfile, 'w') as fconfig: configs = auth_env.get_userconfig_from_api( api_url=api_url, userid=userid, apikey=apikey, requested_userid=requested_userid, qualifier=qualifier, view=view) config = configs['objects'][0]['data'] config['OMEGA_RESTAPI_URL'] = api_url config['OMEGA_QUALIFIER'] = qualifier or 'default' config['OMEGA_USERID'] = userid config['OMEGA_APIKEY'] = apikey config = { k: v for k, v in config.items() if k in (required_keys + keys) } yaml.safe_dump(config, fconfig, default_flow_style=False) print("Config is in {configfile}".format(**locals()))
def _get_omega_from_apikey(userid, apikey, api_url=None, requested_userid=None, qualifier=None, view=False): """ setup an Omega instance from userid and apikey :param userid: the userid :param apikey: the apikey :param api_url: the api URL :param requested_userid: the userid to request config for. in this case userid and apikey must for a staff user for the request to succeed :param qualifier: the database qualifier requested. defaults to 'default' :returns: OmegaCloud instance configured for the given user Returns: OmegaCloud """ from omegaml.client.cloud import OmegaCloud from omegaml import settings, _base_config defaults = settings(reload=True) qualifier = qualifier or 'default' defaults.OMEGA_USERID = userid defaults.OMEGA_APIKEY = apikey defaults.OMEGA_QUALIFIER = qualifier api_url = ensure_api_url(api_url, defaults) auth_env = AuthenticationEnv.secure() if api_url.startswith('http') or any('test' in v for v in sys.argv): configs = auth_env.get_userconfig_from_api( requested_userid=requested_userid, view=view, defaults=defaults) configs = configs['objects'][0]['data'] elif api_url == 'local': configs = { k: getattr(defaults, k) for k in dir(defaults) if k.startswith('OMEGA') } else: raise ValueError('invalid api_url {}'.format(api_url)) config = configs.get(qualifier, configs) # update _base_config.update_from_dict(config, attrs=defaults) _base_config.update_from_config(defaults) _base_config.load_framework_support(defaults) _base_config.load_user_extensions(defaults) auth = auth_env.get_runtime_auth(defaults) om = OmegaCloud(defaults=defaults, auth=auth) # update config to reflect request om.defaults.OMEGA_RESTAPI_URL = api_url om.defaults.OMEGA_USERID = userid om.defaults.OMEGA_APIKEY = apikey om.defaults.OMEGA_QUALIFIER = qualifier return om