Exemple #1
0
    def download_artifact(self,
                          path: str,
                          force: bool = False,
                          path_to: str = None):
        """Downloads a single run artifact.

        Args:
            path: str, the relative path of the artifact to return.
            path_to: str, optional, path to download to.
            force: bool, force reload the artifact.

        Returns:
            str
        """
        url = PolyaxonStore.URL.format(
            namespace=self.namespace,
            owner=self.owner,
            project=self.project,
            uuid=self.run_uuid,
            subpath="artifact",
        )
        url = "{host}/{url}".format(host=clean_host(self.client.config.host),
                                    url=url)
        if force:
            url = "{}?force=true".format(url)

        return PolyaxonStore(client=self).download_file(url=url,
                                                        path=path,
                                                        path_to=path_to)
Exemple #2
0
    def download_artifacts(
        self,
        path: str = "",
        path_to: str = None,
        untar: bool = True,
        delete_tar: bool = True,
        extract_path: str = None,
    ):
        """Downloads a list of run artifacts.

        Args:
            path: str, the relative path of the artifact to return.
            path_to: str, optional, path to download to.
            untar: bool, optional, default: true.
            delete_tar: bool, optional, default: true.
            extract_path: str, optional.

        Returns:
            str.
        """
        url = "{host}/streams/v1/{namespace}/{owner}/{project}/runs/{uuid}/artifacts".format(
            host=clean_host(self.client.config.host),
            namespace=self.namespace,
            owner=self.owner,
            project=self.project,
            uuid=self.run_uuid,
        )

        return PolyaxonStore(client=self).download_file(
            url=url,
            path=path,
            untar=untar,
            delete_tar=delete_tar,
            extract_path=extract_path,
        )
Exemple #3
0
def get_dashboard_url(base: str = "ui",
                      subpath: str = "",
                      use_cloud: bool = False) -> str:
    host = POLYAXON_CLOUD_HOST if use_cloud else clean_host(
        settings.CLIENT_CONFIG.host)
    dashboard_url = "{}/{}/".format(host, base)
    if subpath:
        return "{}{}/".format(dashboard_url, subpath.rstrip("/"))
    return dashboard_url
Exemple #4
0
 def get_url(notification: NotificationSpec):
     run_path = notification.get_url_path()
     if not run_path:
         return
     url = "{}/{}".format(clean_host(settings.CLIENT_CONFIG.host), run_path)
     return add_notification_referrer_param(
         url,
         provider="polyaxon",
         is_absolute=False,
     )
Exemple #5
0
def dashboard(ctx, yes, url):
    """Open this operation's dashboard details in browser."""
    owner, project_name = get_project_or_local(ctx.obj.get("project"), is_cli=True)
    dashboard_url = clean_host(settings.CLIENT_CONFIG.host)
    project_url = "{}/{}/{}/".format(dashboard_url, owner, project_name)
    if url:
        Printer.print_header("The dashboard is available at: {}".format(project_url))
        sys.exit(0)
    if not yes:
        click.confirm(
            "Dashboard page will now open in your browser. Continue?",
            abort=True,
            default=True,
        )
    click.launch(project_url)
Exemple #6
0
    def sdk_config(self):
        if not self.host and not self.in_cluster:
            raise PolyaxonClientException(
                "Api config requires at least a host if not running in-cluster."
            )

        config = polyaxon_sdk.Configuration()
        config.debug = self.debug
        config.host = clean_host(self.host)
        config.verify_ssl = self.verify_ssl
        config.ssl_ca_cert = self.ssl_ca_cert
        config.cert_file = self.cert_file
        config.key_file = self.key_file
        config.assert_hostname = self.assert_hostname
        if self.connection_pool_maxsize:
            config.connection_pool_maxsize = self.connection_pool_maxsize
        if self.token:
            config.api_key["Authorization"] = self.token
            config.api_key_prefix["Authorization"] = self.authentication_type
        return config
Exemple #7
0
    def download_artifact(self, path: str, force: bool = False):
        """Downloads run artifact.

        Args:
            path: str, the relative path of the artifact to return.
            force: bool, force reload the artifact.

        Returns:
            str
        """
        url = "{host}/streams/v1/{namespace}/{owner}/{project}/runs/{uuid}/artifact".format(
            host=clean_host(self.client.config.host),
            namespace=self.namespace,
            owner=self.owner,
            project=self.project,
            uuid=self.run_uuid,
        )
        if force:
            url = "{}?force=true".format(url)

        return PolyaxonStore(client=self).download_file(url=url, path=path, force=force)
Exemple #8
0
 def get_service_env_vars(
     self,
     service_header: str,
     header: str = None,
     include_secret_key: bool = False,
     include_internal_token: bool = False,
     include_agent_token: bool = False,
     authentication_type: str = None,
 ) -> List[k8s_schemas.V1EnvVar]:
     header = header or PolyaxonServiceHeaders.SERVICE
     return get_service_env_vars(
         header=header,
         service_header=service_header,
         authentication_type=authentication_type,
         include_secret_key=include_secret_key,
         include_internal_token=include_internal_token,
         include_agent_token=include_agent_token,
         polyaxon_default_secret_ref=settings.AGENT_CONFIG.app_secret_name,
         polyaxon_agent_secret_ref=settings.AGENT_CONFIG.agent_secret_name,
         api_host=clean_host(settings.CLIENT_CONFIG.host),
         api_version=VERSION_V1,
     )
Exemple #9
0
    def download_artifacts(
        self,
        path: str = "",
        path_to: str = None,
        untar: bool = True,
        delete_tar: bool = True,
        extract_path: str = None,
    ):
        """Downloads a subpath containing multiple run artifacts.

        Args:
            path: str, the relative path of the artifact to return.
            path_to: str, optional, path to download to.
            untar: bool, optional, default: true.
            delete_tar: bool, optional, default: true.
            extract_path: str, optional.

        Returns:
            str.
        """
        url = PolyaxonStore.URL.format(
            namespace=self.namespace,
            owner=self.owner,
            project=self.project,
            uuid=self.run_uuid,
            subpath="artifacts",
        )
        url = "{host}/{url}".format(host=clean_host(self.client.config.host),
                                    url=url)

        return PolyaxonStore(client=self).download_file(
            url=url,
            path=path,
            untar=untar,
            path_to=path_to,
            delete_tar=delete_tar and untar,
            extract_path=extract_path,
        )
Exemple #10
0
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,
    )
Exemple #11
0
 def get_api_host(self, external_host: bool = False):
     if external_host:
         return get_api_host(default=settings.CLIENT_CONFIG.host)
     else:
         return clean_host(settings.CLIENT_CONFIG.host)
Exemple #12
0
 def get_url():
     return add_notification_referrer_param(
         clean_host(settings.CLIENT_CONFIG.host),
         provider="polyaxon",
         is_absolute=False,
     )
Exemple #13
0
def get_dashboard_url(base: str = "ui", subpath: str = "") -> str:
    dashboard_url = "{}/{}/".format(clean_host(settings.CLIENT_CONFIG.host), base)
    if subpath:
        return "{}{}/".format(dashboard_url, subpath.rstrip("/"))
    return dashboard_url
Exemple #14
0
def get_api_host(default: str = LOCALHOST):
    return clean_host(os.environ.get(POLYAXON_KEYS_PLATFORM_HOST, default))
Exemple #15
0
 def base_url(self):
     return self.BASE_URL.format(clean_host(self.host), self.version)
Exemple #16
0
    def __init__(self,
                 service=None,
                 host=None,
                 token=None,
                 debug=None,
                 log_level=None,
                 version=None,
                 authentication_type=None,
                 is_managed=None,
                 is_offline=None,
                 is_ops=None,
                 in_cluster=None,
                 no_op=None,
                 timeout=None,
                 tracking_timeout=None,
                 timezone=None,
                 watch_interval=None,
                 interval=None,
                 verify_ssl=None,
                 ssl_ca_cert=None,
                 cert_file=None,
                 key_file=None,
                 assert_hostname=None,
                 connection_pool_maxsize=None,
                 archive_root=None,
                 header=None,
                 header_service=None,
                 namespace=None,
                 no_api=None,
                 disable_errors_reporting=None,
                 compatibility_check_interval=None,
                 **kwargs):
        self.service = service
        self.host = clean_host(get_default_host(host, service))
        self.token = token
        self.debug = self._get_bool(debug, False)
        self.log_level = log_level
        self.version = version or "v1"
        self.is_managed = self._get_bool(is_managed, False)
        self.is_offline = self._get_bool(is_offline, False)
        self.is_ops = self._get_bool(is_ops, False)
        self.in_cluster = self._get_bool(in_cluster, False)
        self.no_op = self._get_bool(no_op, False)
        self.verify_ssl = clean_verify_ssl(host=self.host,
                                           verify_ssl=self._get_bool(
                                               verify_ssl, None))
        self.ssl_ca_cert = ssl_ca_cert
        self.cert_file = cert_file
        self.key_file = key_file
        self.assert_hostname = self._get_bool(assert_hostname, None)
        self.connection_pool_maxsize = connection_pool_maxsize
        self.archive_root = archive_root or CONTEXT_ARCHIVE_ROOT
        self.header = header
        self.header_service = header_service
        self.timeout = timeout or 20
        self.tracking_timeout = tracking_timeout or 1
        self.timezone = timezone
        self.interval = interval or 5
        self.watch_interval = watch_interval or 5
        self.namespace = namespace
        self.no_api = self._get_bool(no_api, False)
        self.authentication_type = authentication_type or AuthenticationTypes.TOKEN
        self.disable_errors_reporting = self._get_bool(
            disable_errors_reporting, False)
        self.compatibility_check_interval = compatibility_check_interval

        self.client_header = {}

        if all([self.header, self.header_service]):
            self.client_header["header_name"] = self.header
            self.client_header["header_value"] = self.header_service
Exemple #17
0
def service(ctx, yes, external, url):
    """Open the operation service in browser.

    N.B. The operation must have a run kind service, otherwise it will raise an error.

    You can open the service embedded in Polyaxon UI or using the real service URL,
    please use the `--external` flag.
    """
    owner, project_name, run_uuid = get_project_run_or_local(
        ctx.obj.get("project"), ctx.obj.get("run_uuid"), is_cli=True,
    )
    client = RunClient(owner=owner, project=project_name, run_uuid=run_uuid)
    client.refresh_data()
    if client.run_data.kind != V1RunKind.SERVICE:
        Printer.print_warning(
            "Command expected a operations of "
            "kind `service` received kind: {}!".format(client.run_data.kind)
        )
        sys.exit(1)
    dashboard_url = clean_host(settings.CLIENT_CONFIG.host)

    Printer.print_header("Waiting for running condition ...")
    client.wait_for_condition(
        statuses={V1Statuses.RUNNING} | LifeCycle.DONE_VALUES, print_status=True
    )

    client.refresh_data()
    if LifeCycle.is_done(client.run_data.status):
        Printer.print_header("The operations reached a done statuses.")
        latest_status = Printer.add_status_color(
            {"status": client.run_data.status}, status_key="status"
        )
        click.echo("{}\n".format(latest_status["status"]))

    namespace = client.run_data.settings.namespace

    run_url = "{}/{}/{}/runs/{}/service".format(
        dashboard_url, owner, project_name, run_uuid
    )
    service_endpoint = SERVICES_V1
    if client.run_data.meta_info.get("rewrite_path", False):
        service_endpoint = REWRITE_SERVICES_V1
    external_run_url = "{}/{}/{}/{}/{}/runs/{}/".format(
        dashboard_url, service_endpoint, namespace, owner, project_name, run_uuid
    )
    if url:
        Printer.print_header("The service will be available at: {}".format(run_url))
        Printer.print_header(
            "You can also view it in an external link at: {}".format(external_run_url)
        )
        sys.exit(0)
    if not yes:
        click.confirm(
            "Dashboard page will now open in your browser. Continue?",
            abort=True,
            default=True,
        )
    if external:
        click.launch(external_run_url)
        sys.exit(0)
    click.launch(run_url)
Exemple #18
0
    def __init__(self,
                 host=None,
                 token=None,
                 debug=None,
                 log_level=None,
                 version=None,
                 authentication_type=None,
                 is_managed=None,
                 is_service=None,
                 is_local=None,
                 is_offline=None,
                 is_ops=None,
                 in_cluster=None,
                 no_op=None,
                 timeout=None,
                 tracking_timeout=None,
                 timezone=None,
                 watch_interval=None,
                 interval=None,
                 verify_ssl=None,
                 ssl_ca_cert=None,
                 cert_file=None,
                 key_file=None,
                 assert_hostname=None,
                 connection_pool_maxsize=None,
                 upload_size_warn=None,
                 upload_size_max=None,
                 archive_root=None,
                 header=None,
                 header_service=None,
                 pod_id=None,
                 namespace=None,
                 no_api=None,
                 agent_path=None,
                 set_agent=None,
                 **kwargs):

        self.host = clean_host(host or POLYAXON_CLOUD_HOST)
        self.token = token
        self.debug = self._get_bool(debug, False)
        self.log_level = log_level
        self.version = version or "v1"
        self.is_managed = self._get_bool(is_managed, False)
        self.is_service = self._get_bool(is_service, False)
        self.is_local = self._get_bool(is_local, False)
        self.is_offline = self._get_bool(is_offline, False)
        self.is_ops = self._get_bool(is_ops, False)
        self.in_cluster = self._get_bool(in_cluster, False)
        self.no_op = self._get_bool(no_op, False)
        self.verify_ssl = self._get_bool(verify_ssl, None)
        self.ssl_ca_cert = ssl_ca_cert
        self.cert_file = cert_file
        self.key_file = key_file
        self.assert_hostname = self._get_bool(assert_hostname, None)
        self.connection_pool_maxsize = connection_pool_maxsize
        self.upload_size_warn = upload_size_warn or 1024 * 1024 * 10
        self.upload_size_max = upload_size_max or 1024 * 1024 * 150
        self.archive_root = archive_root or CONTEXT_ARCHIVE_ROOT
        self.header = header
        self.header_service = header_service
        self.timeout = timeout or 20
        self.tracking_timeout = tracking_timeout or 1
        self.timezone = timezone
        self.interval = interval or 5
        self.watch_interval = watch_interval or 5
        self.pod_id = pod_id
        self.namespace = namespace
        self.no_api = self._get_bool(no_api, False)
        self.agent_path = agent_path
        self.set_agent = set_agent
        self.authentication_type = authentication_type or AuthenticationTypes.TOKEN

        self.client_header = {}

        if all([self.header, self.header_service]):
            self.client_header["header_name"] = self.header
            self.client_header["header_value"] = self.header_service