def set(**kwargs): # pylint:disable=redefined-builtin """Set the global config values. Example: \b $ polyaxon config set --host=localhost """ try: _config = ClientConfigManager.get_config_or_default() except Exception as e: handle_cli_error(e, message="Polyaxon load configuration.") Printer.print_header( "You can reset your config by running: polyaxon config purge") sys.exit(1) should_purge = False for key, value in kwargs.items(): if key == "host": should_purge = True if value is not None: setattr(_config, key, value) ClientConfigManager.set_config(_config) Printer.print_success("Config was updated.") # Reset cli config CliConfigManager.purge() if should_purge: AuthConfigManager.purge() UserConfigManager.purge()
def purge(): """Purge the global config values.""" ClientConfigManager.purge() CliConfigManager.purge() AuthConfigManager.purge() ProjectManager.purge() RunManager.purge() Printer.print_success("Config was removed.")
def set_auth_config(): from polyaxon.managers.auth import AuthConfigManager global AUTH_CONFIG try: AUTH_CONFIG = AuthConfigManager.get_config_from_env() except (TypeError, ValidationError): AuthConfigManager.purge() Printer.print_warning("Your auth Configuration was purged!")
def purge(cache_only): """Purge the global config values.""" if not cache_only: ClientConfigManager.purge() CliConfigManager.purge() AuthConfigManager.purge() UserConfigManager.purge() ProjectConfigManager.purge() RunConfigManager.purge() Printer.print_success("Configs was removed.")
def create(ctx, name, owner, description, private, init): """Create a new project. Uses /docs/core/cli/#caching Example: \b $ polyaxon project create --name=cats-vs-dogs --description="Image Classification with DL" """ owner = owner or AuthConfigManager.get_value("username") if not owner: Printer.print_error( "Please login first or provide a valid owner --owner. " "`polyaxon login --help`") sys.exit(1) try: project_config = V1Project(name=name, description=description, is_public=not private) _project = ProjectClient(owner=owner).create(project_config) except (ApiException, HTTPError) as e: handle_cli_error(e, message="Could not create project `{}`.".format(name)) sys.exit(1) Printer.print_success("Project `{}` was created successfully.".format( _project.name)) if init: ctx.obj = {} ctx.invoke(init_project, project=name)
def ls(owner, limit, offset): """List projects. Uses /docs/core/cli/#caching """ owner = owner or AuthConfigManager.get_value("username") if not owner: Printer.print_error( "Please login first or provide a valid owner --owner. " "`polyaxon login --help`") sys.exit(1) try: polyaxon_client = ProjectClient(owner=owner) response = polyaxon_client.list(limit=limit, offset=offset) except (ApiException, HTTPError) as e: handle_cli_error(e, message="Could not get list of projects.") sys.exit(1) meta = get_meta_response(response) if meta: Printer.print_header("Projects for current user") Printer.print_header("Navigation:") dict_tabulate(meta) else: Printer.print_header("No projects found for current user") objects = list_dicts_to_tabulate( [o.to_dict() for o in response.results], humanize_values=True, exclude_attrs=["uuid", "description"], ) if objects: Printer.print_header("Projects:") dict_tabulate(objects, is_list_dict=True)
def get_project_info(project): parts = project.replace(".", "/").split("/") if len(parts) == 2: owner, project_name = parts else: owner = AuthConfigManager.get_value("username") project_name = project return owner, project_name
def port_forward(port, namespace, deployment_type, release_name): """If you deploy Polyaxon using ClusterIP, you can use this command to access the gateway through `localhost:port`. """ from polyaxon.deploy.operators.kubectl import KubectlOperator if not port and deployment_type in [ DeploymentTypes.MICRO_K8S, DeploymentTypes.MINIKUBE, ]: port = 31833 port = port or 8000 namespace = namespace or "polyaxon" release_name = release_name or "polyaxon" kubectl = KubectlOperator() args = [ "port-forward", "-n", namespace, "svc/{}-polyaxon-gateway".format(release_name), "{}:80".format(port), ] try: _config = ClientConfigManager.get_config_or_default() except Exception as e: handle_cli_error(e, message="Polyaxon load configuration.") Printer.print_header( "You can reset your config by running: polyaxon config purge") sys.exit(1) _config.host = "http://localhost:{}".format(port) ClientConfigManager.set_config(_config) CliConfigManager.purge() AuthConfigManager.purge() UserConfigManager.purge() Printer.print_header("Client configuration is updated!") Printer.print_success("Polyaxon will be available at: {}".format( _config.host)) stdout = kubectl.execute(args=args, is_json=False, stream=settings.CLIENT_CONFIG.debug) click.echo(stdout)
def login(token, username, password): """Login to Polyaxon.""" polyaxon_client = PolyaxonClient() if username and not token: # Use user or email / password login if not password: password = click.prompt( "Please enter your password", type=str, hide_input=True ) password = password.strip() if not password: logger.info( "You entered an empty string. " "Please make sure you enter your password correctly." ) sys.exit(1) try: body = V1Credentials(username=username, password=password) access_auth = polyaxon_client.auth_v1.login(body=body) except (ApiException, HTTPError) as e: AuthConfigManager.purge() CliConfigManager.purge() handle_cli_error(e, message="Could not login.") sys.exit(1) if not access_auth.token: Printer.print_error("Failed to login") return else: if not token: token_url = "{}/profile/token".format( clean_host(polyaxon_client.config.host) ) click.confirm( "Authentication token page will now open in your browser. Continue?", abort=True, default=True, ) click.launch(token_url) logger.info("Please copy and paste the authentication token.") token = click.prompt( "This is an invisible field. Paste token and press ENTER", type=str, hide_input=True, ) if not token: logger.info( "Empty token received. " "Make sure your shell is handling the token appropriately." ) logger.info( "See docs for help: http://polyaxon.com/docs/polyaxon_cli/commands/auth" ) return access_auth = polyaxon_sdk.models.V1Auth(token=token.strip(" ")) # Set user try: AuthConfigManager.purge() polyaxon_client = PolyaxonClient(token=access_auth.token) user = polyaxon_client.users_v1.get_user() except (ApiException, HTTPError) as e: handle_cli_error(e, message="Could not load user info.") sys.exit(1) access_token = AccessTokenConfig(username=user.username, token=access_auth.token) AuthConfigManager.set_config(access_token) polyaxon_client.config.token = access_auth.token Printer.print_success("Login successful") # Reset current cli server_versions = get_server_versions(polyaxon_client=polyaxon_client) current_version = get_current_version() log_handler = get_log_handler(polyaxon_client=polyaxon_client) CliConfigManager.reset( check_count=0, current_version=current_version, server_versions=server_versions.to_dict(), log_handler=log_handler, )
def logout(): """Logout of Polyaxon.""" AuthConfigManager.purge() CliConfigManager.purge() Printer.print_success("You are logged out")
def test_default_props(self): assert AuthConfigManager.is_global() is True assert AuthConfigManager.is_local() is False assert AuthConfigManager.IS_POLYAXON_DIR is False assert AuthConfigManager.CONFIG_FILE_NAME == ".auth" assert AuthConfigManager.CONFIG == AccessTokenConfig
def logout(): """Logout from Polyaxon Cloud or Polyaxon EE.""" AuthConfigManager.purge() UserConfigManager.purge() CliConfigManager.purge() Printer.print_success("You are logged out")
def session_expired(): AuthConfigManager.purge() CliConfigManager.purge() click.echo("Session has expired, please try again.") sys.exit(1)
def get_config_from_manager(): from polyaxon.managers.client import ClientConfigManager config = ClientConfigManager.get_config_or_default() config.token = AuthConfigManager.get_value("token") return config
from polyaxon.managers.auth import AuthConfigManager from polyaxon.managers.client import ClientConfigManager from polyaxon.utils.bool_utils import to_bool MIN_TIMEOUT = 1 LONG_REQUEST_TIMEOUT = 3600 HEALTH_CHECK_INTERVAL = 60 AUTH_CONFIG = None CLIENT_CONFIG = None PROXIES_CONFIG = None AGENT_CONFIG = None if not to_bool(os.environ.get("POLYAXON_NO_CONFIG", False)): AUTH_CONFIG = AuthConfigManager.get_config_from_env() CLIENT_CONFIG = ClientConfigManager.get_config_from_env() if CLIENT_CONFIG.set_agent: from polyaxon.managers.agent import AgentManager AGENT_CONFIG = AgentManager.get_config_from_env( agent_path=CLIENT_CONFIG.agent_path ) else: CLIENT_CONFIG = ClientConfigManager.CONFIG() def set_proxies_config(): from polyaxon.managers.proxies import ProxiesManager
def get_username_or_local(username): return username or AuthConfigManager.get_value("username")