Ejemplo n.º 1
0
def delete(ctx, _project):
    """Delete project.

    Uses /docs/core/cli/#caching
    """
    owner, project_name = get_project_or_local(_project
                                               or ctx.obj.get("project"),
                                               is_cli=True)

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

    try:
        polyaxon_client = ProjectClient(owner=owner, project=project_name)
        polyaxon_client.delete()
        local_project = ProjectConfigManager.get_config()
        if local_project and (owner, project_name) == (
                local_project.user,
                local_project.name,
        ):
            # Purge caching
            ProjectConfigManager.purge()
    except (ApiException, HTTPError) as e:
        handle_cli_error(e,
                         message="Could not delete project `{}/{}`.".format(
                             owner, project_name))
        sys.exit(1)

    Printer.print_success("Project `{}/{}` was delete successfully".format(
        owner, project_name))
Ejemplo n.º 2
0
def get_project_or_local(project=None, is_cli: bool = False):
    from polyaxon import settings

    if not project and not ProjectConfigManager.is_initialized():
        if is_cli:
            Printer.print_error("Please provide a valid project.")
            sys.exit(1)
        else:
            raise PolyaxonClientException("Please provide a valid project.")

    if project:
        owner, project_name = get_project_info(project)
    else:
        project = ProjectConfigManager.get_config()
        owner, project_name = project.owner, project.name

    if not owner and (not settings.CLI_CONFIG or settings.CLI_CONFIG.is_ce):
        owner = DEFAULT

    if not all([owner, project_name]):
        if is_cli:
            Printer.print_error("Please provide a valid project.")
            sys.exit(1)
        else:
            raise PolyaxonClientException("Please provide a valid project.")
    return owner, project_name
Ejemplo n.º 3
0
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.")
Ejemplo n.º 4
0
def cache(config_manager, config, owner=None, project=None):
    if config_manager == ProjectConfigManager:
        _cache_project(config=config, project=project, owner=owner)

    # Set caching only if we have an initialized project
    if not ProjectConfigManager.is_initialized():
        return

    if not _is_same_project(owner, project):
        return

    visibility = (ProjectConfigManager.VISIBILITY_LOCAL
                  if ProjectConfigManager.is_locally_initialized() else
                  ProjectConfigManager.VISIBILITY_GLOBAL)
    config_manager.set_config(config, visibility=visibility)
Ejemplo n.º 5
0
def get_project_or_local(project=None, is_cli: bool = False):
    from polyaxon import settings

    if not project and not ProjectConfigManager.is_initialized():
        error_message = "Please provide a valid project or initialize a project in the current path."
        if is_cli:
            Printer.print_error(error_message)
            sys.exit(1)
        else:
            raise PolyaxonClientException(error_message)

    if project:
        owner, project_name = get_entity_info(project)
    else:
        project = get_local_project()

        owner, project_name = project.owner, project.name

    if not owner:
        owner = get_local_owner(is_cli=is_cli)

    if not owner and (not settings.CLI_CONFIG or settings.CLI_CONFIG.is_ce):
        owner = DEFAULT

    if not all([owner, project_name]):
        error_message = get_project_error_message(owner, project_name)
        if is_cli:
            Printer.print_error(error_message)
            sys.exit(1)
        else:
            raise PolyaxonClientException(error_message)
    return owner, project_name
Ejemplo n.º 6
0
def get_local_project(is_cli: bool = False):
    try:
        return ProjectConfigManager.get_config()
    except Exception:  # noqa
        if is_cli:
            Printer.print_error(CACHE_ERROR, sys_exit=True)
        else:
            raise PolyaxonSchemaError(CACHE_ERROR)
Ejemplo n.º 7
0
def _cache_project(config, owner=None, project=None):
    if (ProjectConfigManager.is_initialized()
            and ProjectConfigManager.is_locally_initialized()):
        if _is_same_project(owner, project):
            ProjectConfigManager.set_config(config)
            return

    ProjectConfigManager.set_config(
        config, visibility=ProjectConfigManager.VISIBILITY_GLOBAL)
Ejemplo n.º 8
0
def upload():
    """N.B. This is not available in all distributions.

    Upload code of the current directory while respecting the .polyaxonignore file.
    """
    import sys

    from polyaxon.cli.errors import handle_cli_error
    from polyaxon.client import PolyaxonClient
    from polyaxon.exceptions import (
        PolyaxonClientException,
        PolyaxonHTTPError,
        PolyaxonShouldExitError,
    )
    from polyaxon.managers.ignore import IgnoreConfigManager
    from polyaxon.managers.project import ProjectConfigManager
    from polyaxon.utils.formatting import Printer
    from polyaxon.utils.path_utils import create_project_tarfile, get_files_by_paths

    project = ProjectConfigManager.get_config_or_raise()
    files = IgnoreConfigManager.get_unignored_filepaths()
    try:
        with create_project_tarfile(files, project.name) as filepath:
            with get_files_by_paths("repo", [filepath]) as (files, files_size):
                try:
                    PolyaxonClient().project.upload_repo(
                        project.user, project.name, files, files_size)
                except (
                        PolyaxonHTTPError,
                        PolyaxonShouldExitError,
                        PolyaxonClientException,
                ) as e:
                    handle_cli_error(
                        e,
                        message="Could not upload code for project `{}`.".
                        format(project.name),
                    )
                    Printer.print_error(
                        "Check that 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:
        handle_cli_error(e, message="Could not upload the file.")
        sys.exit(1)
Ejemplo n.º 9
0
def _is_same_project(owner=None, project=None):
    local_project = ProjectConfigManager.get_config()
    if project and project == local_project.name:
        return not all([owner, local_project.owner]) or owner == local_project.owner
Ejemplo n.º 10
0
def init(project, git_connection, git_url, polyaxonfile, polyaxonignore):
    """Initialize a new local project and cache directory.

    Note: Make sure to add the local cache `.polyaxon`
    to your `.gitignore` and `.dockerignore` files.
    """
    if not any(
        [project, git_connection, git_url, polyaxonfile, polyaxonignore]):
        Printer.print_warning(
            "`polyaxon init` did not receive any valid option.",
            command_help="polyaxon init",
        )
    if project:
        owner, project_name = get_project_or_local(project, is_cli=True)
        try:
            polyaxon_client = ProjectClient(owner=owner, project=project_name)
            polyaxon_client.refresh_data()
        except (ApiException, HTTPError) as e:
            Printer.print_error(
                "Make sure you have a project with this name `{}`".format(
                    project))
            handle_cli_error(
                e,
                message="You can a create new project with this command: "
                "polyaxon project create "
                "--name={} [--description=...] [--tags=...]".format(
                    project_name),
            )
            sys.exit(1)
        init_project = False
        if ProjectConfigManager.is_initialized():
            local_project = get_local_project()
            click.echo(
                "Warning! This project is already initialized with the following project:"
            )
            with indentation.indent(4):
                indentation.puts("Owner: {}".format(local_project.owner))
                indentation.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:
            ProjectConfigManager.purge(
                visibility=ProjectConfigManager.VISIBILITY_LOCAL)
            config = polyaxon_client.client.sanitize_for_serialization(
                polyaxon_client.project_data)
            ProjectConfigManager.set_config(
                config,
                init=True,
                visibility=ProjectConfigManager.VISIBILITY_LOCAL)
            Printer.print_success("Project was initialized")
            Printer.print_header(
                "Make sure to add the local cache `.polyaxon` "
                "to your `.gitignore` and `.dockerignore` files.")
        else:
            Printer.print_header("Project config was not changed.")

    if git_connection or git_url:
        init_git = False
        if GitConfigManager.is_initialized():
            click.echo("Warning! A {} file was found.".format(
                GitConfigManager.CONFIG_FILE_NAME))
            if click.confirm("Would you like to override it?", default=False):
                init_git = True
        else:
            init_git = True

        if init_git:
            GitConfigManager.purge(
                visibility=GitConfigManager.VISIBILITY_LOCAL)
            config = GitConfigManager.CONFIG(
                connection=git_connection,
                git=V1GitType(url=git_url) if git_url else None,
            )
            GitConfigManager.set_config(config=config, init=True)
            Printer.print_success("New {} file was created.".format(
                GitConfigManager.CONFIG_FILE_NAME))
        else:
            Printer.print_header("{} file was not changed.".format(
                GitConfigManager.CONFIG_FILE_NAME))

    if polyaxonfile:
        create_polyaxonfile()

    if polyaxonignore:
        init_ignore = False
        if IgnoreConfigManager.is_initialized():
            click.echo("Warning! A {} file was found.".format(
                IgnoreConfigManager.CONFIG_FILE_NAME))
            if click.confirm("Would you like to override it?", default=False):
                init_ignore = True
        else:
            init_ignore = True

        if init_ignore:
            IgnoreConfigManager.init_config()
            Printer.print_success("New {} file was created.".format(
                IgnoreConfigManager.CONFIG_FILE_NAME))
        else:
            Printer.print_header("{} file was not changed.".format(
                IgnoreConfigManager.CONFIG_FILE_NAME))
Ejemplo n.º 11
0
 def test_default_props(self):
     assert ProjectConfigManager.is_all_visibility() is True
     assert ProjectConfigManager.IS_POLYAXON_DIR is True
     assert ProjectConfigManager.CONFIG_FILE_NAME == ".project"
     assert ProjectConfigManager.CONFIG == V1Project
Ejemplo n.º 12
0
def get_local_project():
    try:
        return ProjectConfigManager.get_config()
    except:
        Printer.print_error(CACHE_ERROR, sys_exit=True)