Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete project command #581

Merged
merged 3 commits into from
Sep 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.D/581.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added command `neuro-flow delete-project <project-id>` that allows complete removal of project.
36 changes: 36 additions & 0 deletions neuro_flow/cli/click_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,39 @@ def extract_attempt_no(args: Sequence[str]) -> int:
args_to_bake_id=operator.itemgetter(-1),
args_to_attempt=extract_attempt_no,
)


class ProjectType(AsyncType[str]):
name = "project"

async def async_convert(
self,
root: Root,
value: str,
param: Optional[click.Parameter],
ctx: Optional[click.Context],
) -> str:
return value

async def async_complete(
self,
root: Root,
ctx: click.Context,
args: Sequence[str],
incomplete: str,
) -> List[Tuple[str, Optional[str]]]:
variants = []
async with AsyncExitStack() as stack:
client = await stack.enter_async_context(neuro_sdk.get())
storage: Storage = await stack.enter_async_context(ApiStorage(client))
try:
async for project in storage.list_projects():
variants.append(project.yaml_id)
except ValueError:
pass
return [
(yaml_id, None) for yaml_id in variants if yaml_id.startswith(incomplete)
]


PROJECT = ProjectType()
26 changes: 24 additions & 2 deletions neuro_flow/cli/live.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import click
import neuro_sdk
import sys
from typing import List, Optional, Tuple
from typing import List, Optional, Sequence, Tuple

from neuro_flow.cli.click_types import LIVE_JOB, LIVE_JOB_OR_ALL, SUFFIX_AFTER_LIVE_JOB
from neuro_flow.cli.click_types import (
LIVE_JOB,
LIVE_JOB_OR_ALL,
PROJECT,
SUFFIX_AFTER_LIVE_JOB,
)
from neuro_flow.cli.utils import argument, option, wrap_async
from neuro_flow.live_runner import LiveRunner

Expand Down Expand Up @@ -150,3 +155,20 @@ async def kill(
"Suffix is not supported when killing ALL jobs"
)
await runner.kill_all()


@click.command()
@argument("project_ids", type=PROJECT, nargs=-1, required=True)
@wrap_async()
async def delete_project(
root: Root,
project_ids: Sequence[str],
) -> None:
"""Completely remove project with all related entities"""
async with AsyncExitStack() as stack:
client = await stack.enter_async_context(neuro_sdk.get())
storage: Storage = await stack.enter_async_context(ApiStorage(client))
for project_id in project_ids:
await storage.project(yaml_id=project_id).delete()
if root.verbosity >= 0:
root.console.print(f"Project '{project_id}' was successfully removed.")
1 change: 1 addition & 0 deletions neuro_flow/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ def cli(
cli.add_command(live.logs)
cli.add_command(live.status)
cli.add_command(live.kill)
cli.add_command(live.delete_project)

# Batch commands
cli.add_command(batch.bake)
Expand Down
9 changes: 7 additions & 2 deletions neuro_flow/storage/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,9 +363,14 @@ async def create_project(
)

def list_projects(
self, name: str, cluster: Optional[str] = None
self, name: Optional[str] = None, cluster: Optional[str] = None
) -> AsyncIterator[Project]:
return self._raw_client.list("flow/projects", _parse_project_payload)
params = []
if name is not None:
params += [("name", name)]
if cluster is not None:
params += [("cluster", cluster)]
return self._raw_client.list("flow/projects", _parse_project_payload, params)

def bake(self, *, id: str) -> "BakeStorage":
return ApiBakeStorage(self._raw_client, id=id)
Expand Down
2 changes: 1 addition & 1 deletion neuro_flow/storage/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ async def create_project(

@abc.abstractmethod
def list_projects(
self, name: str, cluster: Optional[str] = None
self, name: Optional[str] = None, cluster: Optional[str] = None
) -> AsyncIterator[Project]:
pass

Expand Down
4 changes: 2 additions & 2 deletions neuro_flow/storage/in_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ async def create_project(
return project

async def list_projects(
self, name: str, cluster: Optional[str] = None
self, name: Optional[str] = None, cluster: Optional[str] = None
) -> AsyncIterator[Project]:
for project in self._db.projects.values():
if project.yaml_id != name:
if name and project.yaml_id != name:
continue
if cluster and project.cluster != cluster:
continue
Expand Down
17 changes: 17 additions & 0 deletions reference/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ neuro-flow [OPTIONS] COMMAND [ARGS]...
| [_neuro-flow cancel_](cli.md#neuro-flow-cancel) | Cancel a bake. |
| [_neuro-flow clean_](cli.md#neuro-flow-clean) | Clean volume. |
| [_neuro-flow clear-cache_](cli.md#neuro-flow-clear-cache) | Clear cache. |
| [_neuro-flow delete-project_](cli.md#neuro-flow-delete-project) | Completely remove project with all related entities |
| [_neuro-flow download_](cli.md#neuro-flow-download) | Download volume. |
| [_neuro-flow inspect_](cli.md#neuro-flow-inspect) | Inspect a bake. |
| [_neuro-flow kill_](cli.md#neuro-flow-kill) | Kill a job. |
Expand Down Expand Up @@ -221,6 +222,22 @@ neuro-flow clear-cache [OPTIONS] BATCH [TASK_ID]
| :--- | :--- |
| _--help_ | Show this message and exit. |

### neuro-flow delete-project

Completely remove project with all related entities

**Usage:**

```bash
neuro-flow delete-project [OPTIONS] PROJECT_IDS...
```

**Options:**

| Name | Description |
| :--- | :--- |
| _--help_ | Show this message and exit. |

### neuro-flow download

Download volume.
Expand Down