|
| 1 | +# Parsec Cloud (https://parsec.cloud) Copyright (c) BUSL-1.1 2016-present Scille SAS |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +import asyncio |
| 5 | +from typing import Any |
| 6 | + |
| 7 | +import click |
| 8 | + |
| 9 | +from parsec._parsec import ( |
| 10 | + OrganizationID, |
| 11 | +) |
| 12 | +from parsec.cli.options import ( |
| 13 | + db_server_options, |
| 14 | + debug_config_options, |
| 15 | + logging_config_options, |
| 16 | +) |
| 17 | +from parsec.cli.testbed import if_testbed_available |
| 18 | +from parsec.cli.utils import cli_exception_handler, spinner, start_backend |
| 19 | +from parsec.components.organization import OrganizationEraseBadOutcome |
| 20 | +from parsec.config import ( |
| 21 | + BaseDatabaseConfig, |
| 22 | + DisabledBlockStoreConfig, |
| 23 | + LogLevel, |
| 24 | + MockedBlockStoreConfig, |
| 25 | +) |
| 26 | + |
| 27 | + |
| 28 | +class DevOption(click.Option): |
| 29 | + def handle_parse_result( |
| 30 | + self, ctx: click.Context, opts: Any, args: list[str] |
| 31 | + ) -> tuple[Any, list[str]]: |
| 32 | + value, args = super().handle_parse_result(ctx, opts, args) |
| 33 | + if value: |
| 34 | + for key, value in ( |
| 35 | + ("debug", True), |
| 36 | + ("db", "MOCKED"), |
| 37 | + ("with_testbed", "coolorg"), |
| 38 | + ("organization", "CoolorgOrgTemplate"), |
| 39 | + ): |
| 40 | + if key not in opts: |
| 41 | + opts[key] = value |
| 42 | + |
| 43 | + return value, args |
| 44 | + |
| 45 | + |
| 46 | +@click.command(short_help="Erase an organization from the database") |
| 47 | +@click.option("--organization", type=OrganizationID, help="Organization ID", required=True) |
| 48 | +@click.option("--yes", is_flag=True, help="Don't ask for confirmation before proceeding") |
| 49 | +@db_server_options |
| 50 | +# Add --log-level/--log-format/--log-file |
| 51 | +@logging_config_options(default_log_level="INFO") |
| 52 | +# Add --debug & --version |
| 53 | +@debug_config_options |
| 54 | +@if_testbed_available( |
| 55 | + click.option("--with-testbed", help="Start by populating with a testbed template") |
| 56 | +) |
| 57 | +@if_testbed_available( |
| 58 | + click.option( |
| 59 | + "--dev", |
| 60 | + cls=DevOption, |
| 61 | + is_flag=True, |
| 62 | + is_eager=True, |
| 63 | + help=( |
| 64 | + "Equivalent to `--debug --db=MOCKED --with-testbed=coolorg --organization CoolorgOrgTemplate`" |
| 65 | + ), |
| 66 | + ) |
| 67 | +) |
| 68 | +def erase_organization( |
| 69 | + organization: OrganizationID, |
| 70 | + db: BaseDatabaseConfig, |
| 71 | + db_max_connections: int, |
| 72 | + db_min_connections: int, |
| 73 | + log_level: LogLevel, |
| 74 | + log_format: str, |
| 75 | + log_file: str | None, |
| 76 | + yes: bool, |
| 77 | + debug: bool, |
| 78 | + with_testbed: str | None = None, |
| 79 | + dev: bool = False, |
| 80 | +) -> None: |
| 81 | + with cli_exception_handler(debug): |
| 82 | + asyncio.run( |
| 83 | + _erase_organization( |
| 84 | + yes=yes, |
| 85 | + db_config=db, |
| 86 | + debug=debug, |
| 87 | + with_testbed=with_testbed, |
| 88 | + organization_id=organization, |
| 89 | + ) |
| 90 | + ) |
| 91 | + |
| 92 | + |
| 93 | +async def _erase_organization( |
| 94 | + db_config: BaseDatabaseConfig, |
| 95 | + yes: bool, |
| 96 | + debug: bool, |
| 97 | + with_testbed: str | None, |
| 98 | + organization_id: OrganizationID, |
| 99 | +) -> None: |
| 100 | + # Can use a dummy blockstore config since we are not going to query it |
| 101 | + if with_testbed is None: |
| 102 | + blockstore_config = DisabledBlockStoreConfig() |
| 103 | + else: |
| 104 | + # Testbed template might need to create some blocks |
| 105 | + blockstore_config = MockedBlockStoreConfig() |
| 106 | + |
| 107 | + display_org = click.style(organization_id.str, fg="yellow") |
| 108 | + click.echo( |
| 109 | + f"You are about to entirely erase the {display_org} organization from the database, this action cannot be undone." |
| 110 | + ) |
| 111 | + |
| 112 | + display_bucket_path = click.style(f"{organization_id.str}/", fg="yellow") |
| 113 | + click.echo("Notes:") |
| 114 | + click.echo( |
| 115 | + "- No trace of the organization will remain, so it will be possible to re-create another organization with the same name." |
| 116 | + ) |
| 117 | + click.echo( |
| 118 | + f"- The organization's blocks won't be erased from the blockstore, you should instead manually remove the {display_bucket_path} top level directory from it." |
| 119 | + ) |
| 120 | + click.echo("") |
| 121 | + |
| 122 | + if not yes: |
| 123 | + confirmation = click.prompt("To confirm, type the name of the organization") |
| 124 | + if confirmation != organization_id.str: |
| 125 | + raise RuntimeError("Organization name does not match, aborting") |
| 126 | + |
| 127 | + async with start_backend( |
| 128 | + db_config=db_config, |
| 129 | + blockstore_config=blockstore_config, |
| 130 | + debug=debug, |
| 131 | + populate_with_template=with_testbed, |
| 132 | + ) as backend: |
| 133 | + async with spinner("Removing from database..."): |
| 134 | + outcome = await backend.organization.erase(id=organization_id) |
| 135 | + match outcome: |
| 136 | + case None: |
| 137 | + pass |
| 138 | + case OrganizationEraseBadOutcome.ORGANIZATION_NOT_FOUND: |
| 139 | + raise RuntimeError("Organization doesn't exist") |
0 commit comments