Esempio n. 1
0
def login(token):
    """Log into Polyaxon via Auth0."""
    if not token:
        cli_info_url = "{}/settings/security".format(
            GlobalConfigManager.get_value('host'))
        click.confirm(
            'Authentication token page will now open in your browser. Continue?',
            abort=True,
            default=True)

        webbrowser.open(cli_info_url)

    logger.info("Please copy and paste the authentication token.")
    access_code = click.prompt(
        'This is an invisible field. Paste token and press ENTER',
        type=str,
        hide_input=True)

    if not access_code:
        logger.info(
            "Empty token received. "
            "Make sure your shell is handling the token appropriately.")
        logger.info(
            "See docs for help: http://docs.polyaxon.com/faqs/authentication/")
        return

    access_code = access_code.strip(" ")
    user = AuthClient().get_user(access_code)
    access_token = AccessTokenConfig(username=user.username, token=access_code)
    AuthConfigManager.set_config(access_token)
    logger.info("Login Successful")
Esempio n. 2
0
def list(page):
    """List projects."""
    user = AuthConfigManager.get_value('username')
    if not user:
        Printer.print_error('Please login first. `polyaxon login --help`')

    page = page or 1
    try:
        response = PolyaxonClients().project.list_projects(user, page=page)
    except (PolyaxonHTTPError, PolyaxonShouldExitError) as e:
        Printer.print_error('Could not get list of projects.')
        Printer.print_error('Error message `{}`.'.format(e))
        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']])
    if objects:
        Printer.print_header("Projects:")
        dict_tabulate(objects, is_list_dict=True)
Esempio n. 3
0
def list(page):  # pylint:disable=redefined-builtin
    """List projects.

    Uses [Caching](/references/polyaxon-cli/#caching)
    """
    user = AuthConfigManager.get_value('username')
    if not user:
        Printer.print_error('Please login first. `polyaxon login --help`')

    page = page or 1
    try:
        response = PolyaxonClient().project.list_projects(user, page=page)
    except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:
        Printer.print_error('Could not get list of projects.')
        Printer.print_error('Error message `{}`.'.format(e))
        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_light_dict(
            humanize_values=True,
            exclude_attrs=['uuid', 'experiment_groups', 'experiments', 'description',
                           'num_experiments', 'num_independent_experiments',
                           'num_experiment_groups', 'num_jobs', 'num_builds', 'unique_name'])
            for o in response['results']])
    if objects:
        Printer.print_header("Projects:")
        dict_tabulate(objects, is_list_dict=True)
Esempio n. 4
0
def get_project_info(project):
    parts = project.replace('.', '/').split('/')
    if len(parts) == 2:
        user, project_name = parts
    else:
        user = AuthConfigManager.get_value('username')
        project_name = project

    return user, project_name
Esempio n. 5
0
 def __init__(self):
     self.host = GlobalConfigManager.get_value('host')
     self.http_port = GlobalConfigManager.get_value('http_port')
     self.ws_port = GlobalConfigManager.get_value('ws_port')
     self.use_https = GlobalConfigManager.get_value('use_https')
     self.token = AuthConfigManager.get_value('token')
     self.params = dict(host=self.host,
                        http_port=self.http_port,
                        ws_port=self.ws_port,
                        token=self.token,
                        use_https=self.use_https,
                        reraise=True)
Esempio n. 6
0
 def __init__(self):
     host = GlobalConfigManager.get_value('host')
     if not host:
         Printer.print_error(
             'Received an invalid config, you need to provide a valid host.'
         )
         sys.exit(1)
     port = GlobalConfigManager.get_value('port')
     use_https = GlobalConfigManager.get_value('use_https')
     verify_ssl = GlobalConfigManager.get_value('verify_ssl')
     token = AuthConfigManager.get_value('token')
     super(PolyaxonClient, self).__init__(
         host=host,
         http_port=port,
         ws_port=port,
         use_https=use_https,
         verify_ssl=verify_ssl,
         token=token,
         schema_response=True,
         reraise=True,
     )
Esempio n. 7
0
def get_project_or_local(project=None):
    if not project and not ProjectManager.is_initialized():
        Printer.print_error(
            'Please provide a valid project, or init a new project.'
            '{}'.format(constants.INIT_COMMAND))
        sys.exit(1)

    if project:
        parts = project.split('/')
        if len(parts) == 2:
            user, project_name = parts
        else:
            user = AuthConfigManager.get_value('username')
            project_name = project
    else:
        project = ProjectManager.get_config()
        user, project_name = project.user, project.name

    if not all([user, project_name]):
        Printer.print_error(
            'Please provide a valid project, or init a new project.'
            '{}'.format(constants.INIT_COMMAND))
        sys.exit(1)
    return user, project_name
Esempio n. 8
0
def logout():
    """Logout of Polyaxon."""
    AuthConfigManager.purge()
    logger.info("You are logged out")
Esempio n. 9
0
def session_expired():
    AuthConfigManager.purge()
    CliConfigManager.purge()
    click.echo('Session has expired, please try again.')
    sys.exit(1)
Esempio n. 10
0
 def __init__(self):
     self.base_url = "{}/api/v1/".format(
         GlobalConfigManager.get_value('host'))
     self.auth_config = AuthConfigManager.get_config()
Esempio n. 11
0
def login(token, username, password):
    """Login to Polyaxon."""
    auth_client = PolyaxonClient().auth
    if username:
        # Use username / 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)

        credentials = CredentialsConfig(username=username, password=password)
        try:
            access_code = auth_client.login(credentials=credentials)
        except (PolyaxonHTTPError, PolyaxonShouldExitError,
                PolyaxonClientException) as e:
            Printer.print_error('Could not login.')
            Printer.print_error(e)
            sys.exit(1)

        if not access_code:
            Printer.print_error("Failed to login")
            return
    else:
        if not token:
            token_url = "{}/app/token".format(auth_client.config.http_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://docs.polyaxon.com/polyaxon_cli/commands/auth"
            )
            return

        access_code = token.strip(" ")

    # Set user
    try:
        AuthConfigManager.purge()
        user = PolyaxonClient().auth.get_user(token=access_code)
    except (PolyaxonHTTPError, PolyaxonShouldExitError,
            PolyaxonClientException) as e:
        Printer.print_error('Could not load user info.')
        Printer.print_error('Error message `{}`.'.format(e))
        sys.exit(1)
    access_token = AccessTokenConfig(username=user.username, token=access_code)
    AuthConfigManager.set_config(access_token)
    Printer.print_success("Login successful")

    # Reset current cli
    server_version = get_server_version()
    current_version = get_current_version()
    log_handler = get_log_handler()
    CliConfigManager.reset(check_count=0,
                           current_version=current_version,
                           min_version=server_version.min_version,
                           log_handler=log_handler)
Esempio n. 12
0
def logout():
    """Logout of Polyaxon."""
    AuthConfigManager.purge()
    CliConfigManager.purge()
    Printer.print_success("You are logged out")