Buckets:
AnonymousSpottedToad/human_verified_bucket / codebase /python /bluesky_tiled /tiled /commandline /_admin.py
| from pathlib import Path | |
| from typing import Optional | |
| import typer | |
| from ._admin_api_key import admin_api_key_app | |
| from ._utils import get_context, get_profile # noqa E402 | |
| admin_app = typer.Typer(no_args_is_help=True) | |
| admin_app.add_typer( | |
| admin_api_key_app, name="api-key", help="Manage API keys for Principals." | |
| ) | |
| def initialize_database(database_uri: str): | |
| """ | |
| Initialize a SQL database for use by Tiled. | |
| """ | |
| import asyncio | |
| from sqlalchemy.ext.asyncio import create_async_engine | |
| from ..alembic_utils import UninitializedDatabase, check_database, stamp_head | |
| from ..authn_database.alembic_constants import ( | |
| ALEMBIC_DIR, | |
| ALEMBIC_INI_TEMPLATE_PATH, | |
| ) | |
| from ..authn_database.core import ( | |
| ALL_REVISIONS, | |
| REQUIRED_REVISION, | |
| initialize_database, | |
| ) | |
| from ..utils import ensure_specified_sql_driver | |
| database_uri = ensure_specified_sql_driver(database_uri) | |
| async def do_setup(): | |
| engine = create_async_engine(database_uri) | |
| redacted_url = engine.url._replace(password="[redacted]") | |
| try: | |
| await check_database(engine, REQUIRED_REVISION, ALL_REVISIONS) | |
| except UninitializedDatabase: | |
| # Create tables and stamp (alembic) revision. | |
| typer.echo( | |
| f"Database {redacted_url} is new. Creating tables and marking revision {REQUIRED_REVISION}.", | |
| err=True, | |
| ) | |
| await initialize_database(engine) | |
| typer.echo("Database initialized.", err=True) | |
| else: | |
| typer.echo(f"Database at {redacted_url} is already initialized.", err=True) | |
| raise typer.Abort() | |
| await engine.dispose() | |
| asyncio.run(do_setup()) | |
| stamp_head(ALEMBIC_INI_TEMPLATE_PATH, ALEMBIC_DIR, database_uri) | |
| def upgrade_database( | |
| database_uri: str, | |
| revision: Optional[str] = typer.Argument( | |
| None, | |
| help="The ID of a revision to upgrade to. By default, upgrade to the latest one.", | |
| ), | |
| ): | |
| """ | |
| Upgrade the database schema to the latest version. | |
| """ | |
| import asyncio | |
| from sqlalchemy.ext.asyncio import create_async_engine | |
| from ..alembic_utils import get_current_revision, upgrade | |
| from ..authn_database.alembic_constants import ( | |
| ALEMBIC_DIR, | |
| ALEMBIC_INI_TEMPLATE_PATH, | |
| ) | |
| from ..authn_database.core import ALL_REVISIONS | |
| from ..utils import ensure_specified_sql_driver | |
| database_uri = ensure_specified_sql_driver(database_uri) | |
| async def do_setup(): | |
| engine = create_async_engine(database_uri) | |
| redacted_url = engine.url._replace(password="[redacted]") | |
| current_revision = await get_current_revision(engine, ALL_REVISIONS) | |
| await engine.dispose() | |
| if current_revision is None: | |
| # Create tables and stamp (alembic) revision. | |
| typer.echo( | |
| f"Database {redacted_url} has not been initialized. Use `tiled admin initialize-database`.", | |
| err=True, | |
| ) | |
| raise typer.Abort() | |
| asyncio.run(do_setup()) | |
| upgrade(ALEMBIC_INI_TEMPLATE_PATH, ALEMBIC_DIR, database_uri, revision or "head") | |
| def downgrade_database( | |
| database_uri: str, | |
| revision: str = typer.Argument(..., help="The ID of a revision to downgrade to."), | |
| ): | |
| """ | |
| Downgrade the database schema to a prior version. | |
| """ | |
| import asyncio | |
| from sqlalchemy.ext.asyncio import create_async_engine | |
| from ..alembic_utils import downgrade, get_current_revision | |
| from ..authn_database.alembic_constants import ( | |
| ALEMBIC_DIR, | |
| ALEMBIC_INI_TEMPLATE_PATH, | |
| ) | |
| from ..authn_database.core import ALL_REVISIONS | |
| from ..utils import ensure_specified_sql_driver | |
| database_uri = ensure_specified_sql_driver(database_uri) | |
| async def do_setup(): | |
| engine = create_async_engine(database_uri) | |
| redacted_url = engine.url._replace(password="[redacted]") | |
| current_revision = await get_current_revision(engine, ALL_REVISIONS) | |
| if current_revision is None: | |
| # Create tables and stamp (alembic) revision. | |
| typer.echo( | |
| f"Database {redacted_url} has not been initialized. Use `tiled admin initialize-database`.", | |
| err=True, | |
| ) | |
| raise typer.Abort() | |
| asyncio.run(do_setup()) | |
| downgrade(ALEMBIC_INI_TEMPLATE_PATH, ALEMBIC_DIR, database_uri, revision) | |
| def check_config( | |
| config_path: Path = typer.Argument( | |
| None, | |
| help=( | |
| "Path to a config file or directory of config files. " | |
| "If None, check environment variable TILED_CONFIG. " | |
| "If that is unset, try default location ./config.yml." | |
| ), | |
| ), | |
| ): | |
| "Check configuration file for syntax and validation errors." | |
| import os | |
| from ..config import parse_configs | |
| config_path = config_path or os.getenv("TILED_CONFIG", "config.yml") | |
| try: | |
| parse_configs(config_path) | |
| except Exception as err: | |
| typer.echo(str(err), err=True) | |
| raise typer.Exit(1) | |
| typer.echo("No errors found in configuration.") | |
| def list_principals( | |
| profile: Optional[str] = typer.Option( | |
| None, help="If you use more than one Tiled server, use this to specify which." | |
| ), | |
| page_offset: int = typer.Argument(0), | |
| page_limit: int = typer.Argument(100, help="Max items to show"), | |
| ): | |
| """ | |
| List information about all Principals (users or services) that have ever logged in. | |
| """ | |
| import json | |
| context = get_context(profile) | |
| result = context.admin.list_principals(offset=page_offset, limit=page_limit) | |
| typer.echo(json.dumps(result, indent=2)) | |
| def show_principal( | |
| profile: Optional[str] = typer.Option( | |
| None, help="If you use more than one Tiled server, use this to specify which." | |
| ), | |
| uuid: str = typer.Argument(..., help="UUID identifying Principal of interest"), | |
| ): | |
| """ | |
| Show information about one Principal (user or service). | |
| """ | |
| import json | |
| context = get_context(profile) | |
| result = context.admin.show_principal(uuid) | |
| typer.echo(json.dumps(result, indent=2)) | |
| def create_service_principal( | |
| profile: Optional[str] = typer.Option( | |
| None, help="If you use more than one Tiled server, use this to specify which." | |
| ), | |
| role: Optional[str] = typer.Option( | |
| "user", help="Role of the new service principal (e.g. 'admin' or 'user')." | |
| ), | |
| ): | |
| """ | |
| Create a new service principal. The new principal will have a random UUID. | |
| """ | |
| import json | |
| context = get_context(profile) | |
| result = context.admin.create_service_principal(role or "user") | |
| typer.echo(json.dumps(result, indent=2)) | |
Xet Storage Details
- Size:
- 7.01 kB
- Xet hash:
- 1401d9827f7fce1bdec35ef875bd8dc3ffa7979869116f43f3ed30ff73e505fb
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.