Beispiel #1
0
def delete(ctx):
    """Delete project.

    Uses [Caching](/references/polyaxon-cli/#caching)
    """
    user, project_name = get_project_or_local(ctx.obj.get('project'))

    if not click.confirm("Are sure you want to delete project `{}/{}`".format(
            user, project_name)):
        click.echo('Existing without deleting project.')
        sys.exit(1)

    try:
        response = PolyaxonClient().project.delete_project(user, project_name)
        local_project = ProjectManager.get_config()
        if local_project and (user, project_name) == (local_project.user,
                                                      local_project.name):
            # Purge caching
            ProjectManager.purge()
    except (PolyaxonHTTPError, PolyaxonShouldExitError,
            PolyaxonClientException) as e:
        Printer.print_error('Could not delete project `{}/{}`.'.format(
            user, project_name))
        Printer.print_error('Error message `{}`.'.format(e))
        sys.exit(1)

    if response.status_code == 204:
        Printer.print_success("Project `{}/{}` was delete successfully".format(
            user, project_name))
Beispiel #2
0
def init(project, run, model):
    """Initialize a new polyaxonfile specification."""
    user, project_name = get_project_or_local(project)
    try:
        project_config = PolyaxonClients().project.get_project(user, project_name)
    except (PolyaxonHTTPError, PolyaxonShouldExitError) as e:
        Printer.print_error('Make sure you have a project with this name `{}`'.format(project))
        Printer.print_error('You can a new project with this command: '
                            'polyaxon project create --name={} --description=...'.format(project))
        Printer.print_error('Error message `{}`.'.format(e))
        sys.exit(1)

    if not any([model, run]) and not all([model, run]):
        Printer.print_error("You must specify which an init option, "
                            "possible values: `--model` or `--run`.")
        sys.exit(1)

    result = False
    if model:
        result = create_init_file(constants.INIT_FILE_MODEL)

    elif run:
        result = create_init_file(constants.INIT_FILE_RUN)

    if result:
        ProjectManager.set_config(project_config, init=True)
        IgnoreManager.init_config()
        Printer.print_success(
            "Project `{}` was initialized and Polyaxonfile was created successfully `{}`".format(
                project, constants.INIT_FILE))
        sys.exit(1)

    # if we are here the file was not created
    if not os.path.isfile(constants.INIT_FILE):
        Printer.print_error(
            "Something went wrong, init command did not create a file.\n"
            "Possible reasons: you don't have the write to create the file.")
        sys.exit(1)

    # file was already there, let's check if the project passed correspond to this file
    try:
        PolyaxonFile(constants.INIT_FILE).specification
    except (PolyaxonfileError, ValidationError) as e:
        Printer.print_error(
            "Something went wrong, init command did not create a file.\n"
            "Another file already exist with.")
        Printer.print_error('Error message: `{}`.'.format(e))
        sys.exit(1)

    # At this point we check if we need to re init configurations
    ProjectManager.set_config(project_config, init=True)
    IgnoreManager.init_config()
    Printer.print_success(
        "Project `{}` was initialized and Polyaxonfile was created successfully `{}`".format(
            project, constants.INIT_FILE))
Beispiel #3
0
def upload():  # pylint:disable=assign-to-new-keyword
    """Upload code of the current directory while respecting the .polyaxonignore file."""
    project = ProjectManager.get_config_or_raise()
    files = IgnoreManager.get_unignored_file_paths()
    with create_tarfile(files, project.name) as file_path:
        with get_files_in_current_directory('repo',
                                            [file_path]) as (files,
                                                             files_size):
            try:
                PolyaxonClient().project.upload_repo(project.user,
                                                     project.name, files,
                                                     files_size)
            except (PolyaxonHTTPError, PolyaxonShouldExitError,
                    PolyaxonClientException) as e:
                Printer.print_error(
                    'Could not upload code for project `{}`.'.format(
                        project.name))
                Printer.print_error('Error message `{}`.'.format(e))
                Printer.print_error(
                    'Check if you have access rights for this project and '
                    'that you are not uploading large files.'
                    'If you are running a notebook, '
                    'you should stop it before uploading.')
                sys.exit(1)
            Printer.print_success('Files uploaded.')
Beispiel #4
0
def get(ctx):
    """Get experiment group by uuid.

    Uses [Caching](/polyaxon_cli/introduction#Caching)

    Examples:

    \b
    ```bash
    $ polyaxon group -g 13 get
    ```
    """
    user, project_name, _group = get_group_or_local(ctx.obj['project'], ctx.obj['group'])
    try:
        response = PolyaxonClients().experiment_group.get_experiment_group(
            user, project_name, _group)
        # Set caching only if we have an initialized project
        if ProjectManager.is_initialized():
            GroupManager.set_config(response)
    except (PolyaxonHTTPError, PolyaxonShouldExitError) as e:
        Printer.print_error('Could not get experiment group `{}`.'.format(_group))
        Printer.print_error('Error message `{}`.'.format(e))
        sys.exit(1)

    get_group_details(response)
Beispiel #5
0
def upload(sync=True):  # pylint:disable=assign-to-new-keyword
    """Upload code of the current directory while respecting the .polyaxonignore file."""
    project = ProjectManager.get_config_or_raise()
    files = IgnoreManager.get_unignored_file_paths()
    try:
        with create_tarfile(files, project.name) as file_path:
            with get_files_in_current_directory('repo', [file_path]) as (files, files_size):
                try:
                    PolyaxonClient().project.upload_repo(project.user,
                                                         project.name,
                                                         files,
                                                         files_size,
                                                         sync=sync)
                except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:
                    Printer.print_error(
                        'Could not upload code for project `{}`.'.format(project.name))
                    Printer.print_error('Error message `{}`.'.format(e))
                    Printer.print_error(
                        'Check the project exists, '
                        'and that you have access rights, '
                        'this could happen as well when uploading large files. '
                        'Please also make sure that you have enough space to upload the data.')
                    sys.exit(1)
                Printer.print_success('Files uploaded.')
    except Exception as e:
        Printer.print_error("Could not upload the file.")
        Printer.print_error('Error message `{}`.'.format(e))
        sys.exit(1)
Beispiel #6
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:
        user, project_name = get_project_info(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
Beispiel #7
0
def run(ctx, file, description, u):  # pylint:disable=redefined-builtin
    """Run polyaxonfile specification.

    Example:

    \b
    ```bash
    $ polyaxon run -f file -f file_override ...
    ```

    Example: upload before running

    \b
    ```bash
    $ polyaxon run -f file -u
    ```

    """
    file = file or 'polyaxonfile.yml'
    specification = check_polyaxonfile(file, log=False).specification

    # Check if we need to upload
    if u:
        ctx.invoke(upload, async=False)

    project = ProjectManager.get_config_or_raise()
    project_client = PolyaxonClients().project

    if specification.is_experiment:
        click.echo('Creating an independent experiment.')
        experiment = ExperimentConfig(
            description=description,
            config=specification.parsed_data)
        try:
            project_client.create_experiment(project.user,
                                             project.name,
                                             experiment)
        except (PolyaxonHTTPError, PolyaxonShouldExitError) as e:
            Printer.print_error('Could not create experiment.')
            Printer.print_error('Error message `{}`.'.format(e))
            sys.exit(1)
        Printer.print_success('Experiment was created')
    else:
        click.echo('Creating an experiment group with the following definition:')
        experiments_def = specification.experiments_def
        get_group_experiments_info(**experiments_def)
        experiment_group = ExperimentGroupConfig(
            description=description,
            content=specification._data)  # pylint:disable=protected-access
        try:
            project_client.create_experiment_group(project.user,
                                                   project.name,
                                                   experiment_group)
            Printer.print_success('Experiment group was created')
        except (PolyaxonHTTPError, PolyaxonShouldExitError) as e:
            Printer.print_error('Could not create experiment group.')
            Printer.print_error('Error message `{}`.'.format(e))
            sys.exit(1)
Beispiel #8
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
Beispiel #9
0
def init(project, polyaxonfile):
    """Initialize a new polyaxonfile specification."""
    user, project_name = get_project_or_local(project)
    try:
        project_config = PolyaxonClient().project.get_project(
            user, project_name)
    except (PolyaxonHTTPError, PolyaxonShouldExitError,
            PolyaxonClientException) as e:
        Printer.print_error(
            'Make sure you have a project with this name `{}`'.format(project))
        Printer.print_error(
            'You can a create new project with this command: '
            'polyaxon project create '
            '--name={} [--description=...] [--tags=...]'.format(project_name))
        Printer.print_error('Error message `{}`.'.format(e))
        sys.exit(1)

    init_project = False
    if ProjectManager.is_initialized():
        local_project = ProjectManager.get_config()
        click.echo(
            'Warning! This project is already initialized with the following project:'
        )
        with clint.textui.indent(4):
            clint.textui.puts('User: {}'.format(local_project.user))
            clint.textui.puts('Project: {}'.format(local_project.name))
        if click.confirm('Would you like to override this current config?',
                         default=False):
            init_project = True
    else:
        init_project = True

    if init_project:
        ProjectManager.purge()
        ProjectManager.set_config(project_config, init=True)
        Printer.print_success('Project was initialized')
    else:
        Printer.print_header('Project config was not changed.')

    init_ignore = False
    if IgnoreManager.is_initialized():
        click.echo('Warning! Found a .polyaxonignore file.')
        if click.confirm('Would you like to override it?', default=False):
            init_ignore = True
    else:
        init_ignore = True

    if init_ignore:
        IgnoreManager.init_config()
        Printer.print_success('New .polyaxonignore file was created.')
    else:
        Printer.print_header('.polyaxonignore file was not changed.')

    if polyaxonfile:
        create_polyaxonfile()
Beispiel #10
0
def upload(async):  # pylint:disable=assign-to-new-keyword
    """Upload code of the current directory while respecting the .polyaxonignore file."""
    project = ProjectManager.get_config_or_raise()
    files = IgnoreManager.get_unignored_file_paths()
    with create_tarfile(files, project.name) as file_path:
        with get_files_in_current_directory('repo',
                                            [file_path]) as (files,
                                                             files_size):
            try:
                PolyaxonClients().project.upload_repo(project.user,
                                                      project.name, files,
                                                      files_size, async)
            except (PolyaxonHTTPError, PolyaxonShouldExitError) as e:
                Printer.print_error(
                    'Could not upload code for project `{}`.'.format(
                        project.name))
                Printer.print_error('Error message `{}`.'.format(e))
                sys.exit(1)
            Printer.print_success('Files uploaded.')
Beispiel #11
0
def get(ctx):
    """Get job.

    Uses [Caching](/polyaxon_cli/introduction#Caching)

    Examples:

    \b
    ```bash
    $ polyaxon job --job=1 --experiment=1 get
    ```

    \b
    ```bash
    $ polyaxon job --job=1 --project=project_name get
    ```
    """
    user, project_name, experiment, _job = get_job_or_local(
        ctx.obj['project'], ctx.obj['experiment'], ctx.obj['job'])
    try:
        response = PolyaxonClients().job.get_job(user, project_name,
                                                 experiment, _job)
        # Set caching only if we have an initialized project
        if ProjectManager.is_initialized():
            JobManager.set_config(response)
    except (PolyaxonHTTPError, PolyaxonShouldExitError) as e:
        Printer.print_error('Could not get job `{}`.'.format(_job))
        Printer.print_error('Error message `{}`.'.format(e))
        sys.exit(1)

    if response.resources:
        get_resources(response.resources.to_dict(), header="Job resources:")

    response = Printer.add_status_color(
        response.to_light_dict(humanize_values=True,
                               exclude_attrs=[
                                   'uuid', 'definition', 'experiment',
                                   'unique_name', 'resources'
                               ]))
    Printer.print_header("Job info:")
    dict_tabulate(response)
Beispiel #12
0
def get(ctx):
    """Get experiment.

    Uses [Caching](/polyaxon_cli/introduction#Caching)

    Example:

    \b
    ```bash
    $ polyaxon experiment get  # if experiment is cached
    ```

    \b
    ```bash
    $ polyaxon experiment --experiment=1 get
    ```

    \b
    ```bash
    $ polyaxon experiment -xp 1 --project=cats-vs-dogs get
    ```

    \b
    ```bash
    $ polyaxon experiment -xp 1 -p alain/cats-vs-dogs get
    ```
    """
    user, project_name, _experiment = get_experiment_or_local(ctx.obj['project'],
                                                              ctx.obj['experiment'])
    try:
        response = PolyaxonClients().experiment.get_experiment(user, project_name, _experiment)
        # Set caching only if we have an initialized project
        if ProjectManager.is_initialized():
            ExperimentManager.set_config(response)
    except (PolyaxonHTTPError, PolyaxonShouldExitError) as e:
        Printer.print_error('Could not load experiment `{}` info.'.format(_experiment))
        Printer.print_error('Error message `{}`.'.format(e))
        sys.exit(1)

    get_experiment_details(response)
Beispiel #13
0
def run(ctx, file, name, tags, description, ttl, u, l):  # pylint:disable=redefined-builtin
    """Run polyaxonfile specification.

    Examples:

    \b
    ```bash
    $ polyaxon run -f file -f file_override ...
    ```

    Upload before running

    \b
    ```bash
    $ polyaxon run -f file -u
    ```

    Run and set description and tags for this run

    \b
    ```bash
    $ polyaxon run -f file -u --description="Description of the current run" --tags="foo, bar, moo"
    ```
    Run and set a unique name for this run

    \b
    ```bash
    polyaxon run --name=foo
    ```
    """
    file = file or 'polyaxonfile.yml'
    specification = check_polyaxonfile(file, log=False).specification

    spec_cond = (specification.is_experiment or specification.is_group
                 or specification.is_job or specification.is_build)
    if not spec_cond:
        Printer.print_error(
            'This command expects an experiment, a group, a job, or a build specification,'
            'received instead a `{}` specification'.format(specification.kind))
        if specification.is_notebook:
            click.echo(
                'Please check "polyaxon notebook --help" to start a notebook.')
        elif specification.is_tensorboard:
            click.echo(
                'Please check: "polyaxon tensorboard --help" to start a tensorboard.'
            )
        sys.exit(1)

    # Check if we need to upload
    if u:
        ctx.invoke(upload)

    project = ProjectManager.get_config_or_raise()
    project_client = PolyaxonClient().project

    tags = validate_tags(tags)

    def run_experiment():
        click.echo('Creating an independent experiment.')
        experiment = ExperimentConfig(name=name,
                                      description=description,
                                      tags=tags,
                                      config=specification.parsed_data,
                                      ttl=ttl)
        try:
            response = PolyaxonClient().project.create_experiment(
                project.user, project.name, experiment)
            cache.cache(config_manager=ExperimentManager, response=response)
            Printer.print_success('Experiment `{}` was created'.format(
                response.id))
        except (PolyaxonHTTPError, PolyaxonShouldExitError,
                PolyaxonClientException) as e:
            Printer.print_error('Could not create experiment.')
            Printer.print_error('Error message `{}`.'.format(e))
            sys.exit(1)

    def run_group():
        click.echo(
            'Creating an experiment group with the following definition:')
        experiments_def = specification.experiments_def
        get_group_experiments_info(**experiments_def)
        experiment_group = ExperimentGroupConfig(name=name,
                                                 description=description,
                                                 tags=tags,
                                                 content=specification._data)  # pylint:disable=protected-access
        try:
            response = project_client.create_experiment_group(
                project.user, project.name, experiment_group)
            cache.cache(config_manager=GroupManager, response=response)
            Printer.print_success('Experiment group {} was created'.format(
                response.id))
        except (PolyaxonHTTPError, PolyaxonShouldExitError,
                PolyaxonClientException) as e:
            Printer.print_error('Could not create experiment group.')
            Printer.print_error('Error message `{}`.'.format(e))
            sys.exit(1)

    def run_job():
        click.echo('Creating a job.')
        job = JobConfig(name=name,
                        description=description,
                        tags=tags,
                        config=specification.parsed_data,
                        ttl=ttl)
        try:
            response = project_client.create_job(project.user, project.name,
                                                 job)
            cache.cache(config_manager=JobManager, response=response)
            Printer.print_success('Job {} was created'.format(response.id))
        except (PolyaxonHTTPError, PolyaxonShouldExitError,
                PolyaxonClientException) as e:
            Printer.print_error('Could not create job.')
            Printer.print_error('Error message `{}`.'.format(e))
            sys.exit(1)

    def run_build():
        click.echo('Creating a build.')
        job = JobConfig(name=name,
                        description=description,
                        tags=tags,
                        config=specification.parsed_data,
                        ttl=ttl)
        try:
            response = project_client.create_build(project.user, project.name,
                                                   job)
            cache.cache(config_manager=BuildJobManager, response=response)
            Printer.print_success('Build {} was created'.format(response.id))
        except (PolyaxonHTTPError, PolyaxonShouldExitError,
                PolyaxonClientException) as e:
            Printer.print_error('Could not create build.')
            Printer.print_error('Error message `{}`.'.format(e))
            sys.exit(1)

    logs = None
    if specification.is_experiment:
        run_experiment()
        logs = experiment_logs
    elif specification.is_group:
        run_group()
    elif specification.is_job:
        run_job()
        logs = job_logs
    elif specification.is_build:
        run_build()
        logs = build_logs

    # Check if we need to invoke logs
    if l and logs:
        ctx.obj = {}
        ctx.invoke(logs)
Beispiel #14
0
def get_current_project():
    if not ProjectManager.is_initialized():
        return None
    return ProjectManager.get_config()
Beispiel #15
0
def cache(config_manager, response):
    # Set caching only if we have an initialized project
    if ProjectManager.is_initialized():
        config_manager.set_config(response)