def deployments_update(deployment_name, new_name, default_version, yaml_file, quiet): """ Update a deployment. If you only want to update the name of the deployment or the default deployment version, use the options `<new_name>` and `<default_version>`. If you want to update the deployment input/output fields, description or labels, please use a yaml file to define the new deployment. \b For example: ``` deployment_description: Deployment created via command line. deployment_labels: my-key-1: my-label-1 my-key-2: my-label-2 input_fields: - name: param1 data_type: int - name: param2 data_type: string output_fields: - name: param1 data_type: int - name: param2 data_type: string ``` """ project_name = get_current_project(error=True) yaml_content = read_yaml(yaml_file) deployment = api.DeploymentUpdate(name=new_name, default_version=default_version) if 'deployment_description' in yaml_content: deployment.description = yaml_content['deployment_description'] if 'deployment_labels' in yaml_content: deployment.labels = yaml_content['deployment_labels'] if 'input_fields' in yaml_content and isinstance(yaml_content['input_fields'], list): deployment.input_fields = [ api.DeploymentInputFieldCreate(name=item['name'], data_type=item['data_type']) for item in yaml_content['input_fields'] ] if 'output_fields' in yaml_content and isinstance(yaml_content['output_fields'], list): deployment.output_fields = [ api.DeploymentInputFieldCreate(name=item['name'], data_type=item['data_type']) for item in yaml_content['output_fields'] ] client = init_client() client.deployments_update(project_name=project_name, deployment_name=deployment_name, data=deployment) client.api_client.close() if not quiet: click.echo("Deployment was successfully updated")
def current_project_set(project_name, format_): """Set your current CLI project.""" client = init_client() response = client.projects_get(project_name=project_name) client.api_client.close() user_config = Config() user_config.set('default.project', response.name) user_config.write() print_projects_list([response], response.name, LIST_ITEMS, fmt=format_)
def revisions_list(deployment_name, version_name, format_): """ List the revisions of a deployment version. """ project_name = get_current_project(error=True) client = init_client() response = client.revisions_list(project_name=project_name, deployment_name=deployment_name, version=version_name) client.api_client.close() print_list(response, LIST_ITEMS, sorting_col=0, fmt=format_)
def pipelines_get(pipeline_name, output_path, quiet, format_): """ Get the pipeline settings, like, input_type and input_fields. If you specify the <output_path> option, this location will be used to store the pipeline structure in a yaml file. You can either specify the <output_path> as file or directory. If the specified <output_path> is a directory, the settings will be stored in `pipeline.yaml`. """ project_name = get_current_project(error=True) client = init_client() pipeline = client.pipelines_get(project_name=project_name, pipeline_name=pipeline_name) client.api_client.close() if output_path is not None: dictionary = format_yaml( pipeline, required_front=['name', 'description', 'input_type'], optional=[ 'input_fields name', 'input_fields data_type', 'output_type', 'output_fields name', 'output_fields data_type' ], rename={ 'name': 'pipeline_name', 'description': 'pipeline_description' }, as_str=False) yaml_file = write_yaml(output_path, dictionary, default_file_name="pipeline.yaml") if not quiet: click.echo('Pipeline file is stored in: %s' % yaml_file) else: print_item(pipeline, row_attrs=LIST_ITEMS, required_front=['name', 'description', 'input_type'], optional=[ 'input_fields name', 'input_fields data_type', 'output_type', 'output_fields name', 'output_fields data_type', 'creation_date', 'last_updated', 'default_version' ], rename={ 'name': 'pipeline_name', 'description': 'pipeline_description' }, fmt=format_)
def builds_get(deployment_name, version_name, build_id, format_): """Get the build of a deployment version.""" project_name = get_current_project(error=True) client = init_client() build = client.builds_get(project_name=project_name, deployment_name=deployment_name, version=version_name, build_id=build_id) client.api_client.close() print_item(build, row_attrs=LIST_ITEMS, fmt=format_)
def exports_list(status, format_): """ List all your exports in your project. The `<status>` option can be used to filter on specific statuses. """ project_name = get_current_project() if project_name: client = init_client() exports = client.exports_list(project_name=project_name, status=status) client.api_client.close() print_list(items=exports, attrs=LIST_ITEMS, sorting_col=1, sorting_reverse=True, fmt=format_)
def blobs_list(format_): """List blobs in project.""" project_name = get_current_project(error=True) client = init_client() response = client.blobs_list(project_name=project_name) client.api_client.close() print_list(response, LIST_ITEMS, rename_cols={'ttl': 'time_to_live'}, sorting_col=2, fmt=format_)
def blobs_get(blob_id, output_path, quiet): """Download an existing blob.""" project_name = get_current_project(error=True) client = init_client() with client.blobs_get(project_name=project_name, blob_id=blob_id) as response: filename = response.getfilename() output_path = write_blob(response.read(), output_path, filename) client.api_client.close() if not quiet: click.echo("Blob stored in: %s" % output_path)
def revisions_upload(deployment_name, version_name, zip_path, format_): """Create a revision of a deployment version by uploading a ZIP. Please, specify the deployment package `<zip_path>` that should be uploaded. """ project_name = get_current_project(error=True) client = init_client() revision = client.revisions_file_upload(project_name=project_name, deployment_name=deployment_name, version=version_name, file=zip_path) client.api_client.close() print_item(revision, row_attrs=['revision', 'build'], fmt=format_)
def schedules_get(schedule_name, format_): """Get a request schedule.""" project_name = get_current_project(error=True) client = init_client() response = client.request_schedules_get(project_name=project_name, schedule_name=schedule_name) client.api_client.close() print_item(response, row_attrs=LIST_ITEMS, rename=RENAME_COLUMNS, fmt=format_)
def schedules_list(format_): """List request schedules in project.""" project_name = get_current_project(error=True) client = init_client() response = client.request_schedules_list(project_name=project_name) client.api_client.close() print_list(response, LIST_ITEMS, rename_cols=RENAME_COLUMNS, sorting_col=1, fmt=format_)
def blobs_delete(blob_id, assume_yes, quiet): """Delete a blob.""" project_name = get_current_project(error=True) if assume_yes or click.confirm("Are you sure you want to delete blob <%s> " "of project <%s>?" % (blob_id, project_name)): client = init_client() client.blobs_delete(project_name=project_name, blob_id=blob_id) client.api_client.close() if not quiet: click.echo("Blob was successfully deleted")
def requests_list(pipeline_name, version_name, limit, format_, **kwargs): """ List pipeline requests. Pipeline requests are only stored for pipeline versions with `request_retention_mode` 'full' or 'metadata'. Use the version option to list the requests for a specific pipeline version. If not specified, the requests are listed for the default version. """ project_name = get_current_project(error=True) if 'start_date' in kwargs and kwargs['start_date']: try: kwargs['start_date'] = format_datetime(parse_datetime( kwargs['start_date']), fmt='%Y-%m-%dT%H:%M:%SZ') except ValueError: raise Exception( "Failed to parse start_date. Please use iso-format, " "for example, '2020-01-01T00:00:00.000000Z'") if 'end_date' in kwargs and kwargs['end_date']: try: kwargs['end_date'] = format_datetime(parse_datetime( kwargs['end_date']), fmt='%Y-%m-%dT%H:%M:%SZ') except ValueError: raise Exception("Failed to parse end_date. Please use iso-format, " "for example, '2020-01-01T00:00:00.000000Z'") client = init_client() if version_name is not None: response = client.pipeline_version_requests_list( project_name=project_name, pipeline_name=pipeline_name, version=version_name, limit=limit, **kwargs) else: response = client.pipeline_requests_list(project_name=project_name, pipeline_name=pipeline_name, limit=limit, **kwargs) client.api_client.close() print_list(response, REQUEST_LIST_ITEMS, fmt=format_) if len(response) == limit: click.echo("\n(Use the <offset> and <limit> options to load more)")
def deprecated_batch_requests_get(pipeline_name, version_name, request_id, format_): """ [DEPRECATED] Get the results of one or more pipeline batch requests. Pipeline requests are only stored for pipeline versions with `request_retention_mode` 'full' or 'metadata'. Use the version option to get a batch request for a specific pipeline version. If not specified, the batch request is retrieved for the default version. Multiple request ids can be specified at ones by using the '-id' options multiple times: `ubiops pipelines batch_requests get <my-pipeline> -v <my-version> -id <id-1> -id <id-2> -id <id-3>` """ if format_ != 'json': click.secho( "Deprecation warning: 'batch_requests get' is deprecated, use 'requests get' instead", fg='red') request_ids = list(request_id) project_name = get_current_project(error=True) client = init_client() if version_name is not None: response = client.pipeline_version_requests_batch_get( project_name=project_name, pipeline_name=pipeline_name, version=version_name, data=request_ids) else: response = client.pipeline_requests_batch_get( project_name=project_name, pipeline_name=pipeline_name, data=request_ids) client.api_client.close() if format_ == 'reference': click.echo(format_pipeline_requests_reference(response)) elif format_ == 'oneline': click.echo(format_pipeline_requests_oneline(response)) elif format_ == 'json': click.echo(format_json(response)) else: click.echo(format_pipeline_requests_reference(response))
def blobs_update(blob_id, input_path, ttl, quiet): """Update an existing blob by uploading a new file.""" project_name = get_current_project(error=True) input_path = abs_path(input_path) client = init_client() client.blobs_update(project_name=project_name, blob_id=blob_id, file=input_path, blob_ttl=ttl) client.api_client.close() if not quiet: click.echo("Blob was successfully updated")
def schedules_delete(schedule_name, assume_yes, quiet): """Delete a request schedule.""" project_name = get_current_project(error=True) if assume_yes or click.confirm( "Are you sure you want to delete request schedule <%s> " "of project <%s>?" % (schedule_name, project_name)): client = init_client() client.request_schedules_delete(project_name=project_name, schedule_name=schedule_name) client.api_client.close() if not quiet: click.echo("Request schedule was successfully deleted")
def revisions_get(deployment_name, version_name, revision_id, format_): """Get a revision of a deployment version.""" project_name = get_current_project(error=True) client = init_client() revision = client.revisions_get( project_name=project_name, deployment_name=deployment_name, version=version_name, revision_id=revision_id ) client.api_client.close() print_item(revision, row_attrs=LIST_ITEMS, fmt=format_)
def deployments_delete(deployment_name, assume_yes, quiet): """ Delete a deployment. """ project_name = get_current_project(error=True) if assume_yes or click.confirm("Are you sure you want to delete deployment <%s> " "of project <%s>?" % (deployment_name, project_name)): client = init_client() client.deployments_delete(project_name=project_name, deployment_name=deployment_name) client.api_client.close() if not quiet: click.echo("Deployment was successfully deleted")
def deployments_list(labels, format_): """ List all your deployments in your project. The `<labels>` option can be used to filter on specific labels. """ label_filter = get_label_filter(labels) project_name = get_current_project() if project_name: client = init_client() deployments = client.deployments_list(project_name=project_name, labels=label_filter) client.api_client.close() print_list(items=deployments, attrs=LIST_ITEMS, sorting_col=1, fmt=format_)
def projects_list(organization_name, format_): """List all your projects. To select a project, use: `ubiops current_project set <project_name>` """ client = init_client() projects = client.projects_list() if organization_name: projects = [ i for i in projects if i.organization_name == organization_name ] current = get_current_project() client.api_client.close() print_projects_list(projects, current, LIST_ITEMS, fmt=format_)
def pipelines_list(labels, format_): """ List pipelines in project. The <labels> option can be used to filter on specific labels. """ label_filter = get_label_filter(labels) project_name = get_current_project(error=True) if project_name: client = init_client() pipelines = client.pipelines_list(project_name=project_name, labels=label_filter) print_list(pipelines, LIST_ITEMS, sorting_col=1, fmt=format_) client.api_client.close()
def blobs_create(input_path, ttl, format_): """Upload a new blob.""" project_name = get_current_project(error=True) input_path = abs_path(input_path) client = init_client() response = client.blobs_create(project_name=project_name, file=input_path, blob_ttl=ttl) client.api_client.close() print_item(response, LIST_ITEMS, rename={'ttl': 'time_to_live'}, fmt=format_)
def exports_delete(export_id, assume_yes, quiet): """ Delete an export. """ project_name = get_current_project(error=True) if assume_yes or click.confirm( "Are you sure you want to delete export <%s> of project <%s>?" % (export_id, project_name) ): client = init_client() client.exports_delete(project_name=project_name, export_id=export_id) client.api_client.close() if not quiet: click.echo("Export was successfully deleted")
def imports_create(zip_path, skip_confirmation, format_): """ Create a new import by uploading a ZIP. Please, specify the import file `<zip_path>` that should be uploaded. """ project_name = get_current_project(error=True) client = init_client() _import = client.imports_create(project_name=project_name, file=zip_path, skip_confirmation=skip_confirmation) client.api_client.close() print_item(_import, row_attrs=LIST_ITEMS, fmt=format_)
def deprecated_deployments_request(deployment_name, version_name, data, timeout, format_): """ [DEPRECATED] Create a deployment request and retrieve the result. Use the version option to make a request to a specific deployment version: `ubiops deployments request <my-deployment> -v <my-version> --data <input>` If not specified, a request is made to the default version: `ubiops deployments request <my-deployment> --data <input>` For structured input, specify the data as JSON formatted string. For example: `ubiops deployments request <my-deployment> --data "{\\"param1\\": 1, \\"param2\\": \\"two\\"}"` """ if format_ != 'json': click.secho( "Deprecation warning: 'request' is deprecated, use 'requests create' instead", fg='red' ) project_name = get_current_project(error=True) client = init_client() deployment = client.deployments_get(project_name=project_name, deployment_name=deployment_name) if deployment.input_type == STRUCTURED_TYPE: data = parse_json(data) if version_name is not None: response = client.deployment_version_requests_create( project_name=project_name, deployment_name=deployment_name, version=version_name, data=data, timeout=timeout ) else: response = client.deployment_requests_create( project_name=project_name, deployment_name=deployment_name, data=data, timeout=timeout ) client.api_client.close() if format_ == 'reference': click.echo(format_requests_reference([response])) elif format_ == 'oneline': click.echo(format_requests_oneline([response])) elif format_ == 'json': click.echo(format_json(response)) else: click.echo(format_requests_reference([response]))
def env_vars_copy(from_deployment, from_version, to_deployment, to_version, assume_yes): """ Copy environment variables from one deployment (version) to another deployment (version). """ project_name = get_current_project(error=True) client = init_client() if from_version is None: data = api.EnvironmentVariableCopy(source_deployment=from_deployment) env_vars = client.deployment_environment_variables_list( project_name=project_name, deployment_name=from_deployment) else: data = api.EnvironmentVariableCopy(source_deployment=from_deployment, source_version=from_version) env_vars = client.deployment_version_environment_variables_list( project_name=project_name, deployment_name=from_deployment, version=from_version) if not assume_yes: env_vars = [env for env in env_vars if env.inheritance_type is None] print_list(env_vars, ['id', 'name', 'value', 'secret'], sorting_col=1, fmt='table') click.echo("\n%s" % click.style( "All destination variables with the same name " "will be overwritten by this action", fg='yellow')) confirm_message = "Are you sure you want to copy %s these environment variables?" % click.style( "ALL", fg='red') if assume_yes or click.confirm(confirm_message): if to_version is None: client.deployment_environment_variables_copy( project_name=project_name, deployment_name=to_deployment, data=data) else: client.deployment_version_environment_variables_copy( project_name=project_name, deployment_name=to_deployment, version=to_version, data=data) client.api_client.close()
def pipelines_delete(pipeline_name, assume_yes, quiet): """ Delete a pipeline. """ project_name = get_current_project(error=True) if assume_yes or click.confirm( f"Are you sure you want to delete pipeline <{pipeline_name}> of project <{project_name}>?" ): client = init_client() client.pipelines_delete(project_name=project_name, pipeline_name=pipeline_name) client.api_client.close() if not quiet: click.echo("Pipeline was successfully deleted")
def revisions_download(deployment_name, version_name, revision_id, output_path, quiet): """Download a revision of a deployment version. The `<output_path>` option will be used as output location of the zip file. If not specified, the current directory will be used. If the `<output_path>` is a directory, the zip will be saved in `[deployment_name]_[deployment_version]_[datetime.now()].zip`. """ project_name = get_current_project(error=True) client = init_client() with client.revisions_file_download(project_name=project_name, deployment_name=deployment_name, version=version_name, revision_id=revision_id) as response: filename = default_version_zip_name(deployment_name, version_name) output_path = write_blob(response.read(), output_path, filename) client.api_client.close() if not quiet: click.echo("Zip stored in: %s" % output_path)
def exports_download(export_id, output_path, quiet): """ Download an export. The `<output_path>` option will be used as output location of the zip file. If not specified, the current directory will be used. If the `<output_path>` is a directory, the zip will be saved in `export_[export_id]_[datetime.now()].zip`. """ project_name = get_current_project(error=True) client = init_client() with client.exports_download(project_name=project_name, export_id=export_id) as response: filename = import_export_zip_name(object_id=export_id, object_type='export') output_path = write_blob(response.read(), output_path, filename) client.api_client.close() if not quiet: click.echo("Export file stored in: %s" % output_path)
def env_vars_get(env_var_id, deployment_name, version_name, format_): """ Get an environment variable. \b - When deployment_name and version_name are provided: the environment variable will be collected on deployment version level. - When a deployment name is provided, but not a version name: the environment variable will be collected on deployment level. - When no deployment_name nor a version name is provided: the environment variable will be collected on project level. """ project_name = get_current_project(error=True) if version_name and not deployment_name: raise Exception("Missing option <deployment_name>") client = init_client() try: if version_name: item = client.deployment_version_environment_variables_get( project_name=project_name, deployment_name=deployment_name, version=version_name, id=env_var_id) elif deployment_name: item = client.deployment_environment_variables_get( project_name=project_name, deployment_name=deployment_name, id=env_var_id) else: item = client.project_environment_variables_get( project_name=project_name, id=env_var_id) except api.exceptions.ApiException as e: if hasattr(e, "status") and e.status == 404: click.echo("%s %s" % (click.style('Warning:', fg='yellow'), WARNING_MSG)) raise e client.api_client.close() print_item(item, LIST_ITEMS, fmt=format_)