Skip to main content

Reusable utilities for Pysae Python CLIs (k8s pod dispatch, …).

Project description

pysae-cli-tools

Reusable utilities for Pysae Python CLIs.

PyPI

Installation

pip install pysae-cli-tools
# or
poetry add pysae-cli-tools

What's included

pysae_cli_tools.k8s — run any Typer command in an ephemeral pod

The @k8s_support decorator injects three flags into a Typer command — --k8s, --k8s-environment {dev|prod}, --k8s-from-local-sources — and dispatches the call into a freshly-spawned Kubernetes pod when --k8s is set.

It is meant for CLIs that need to run inside the same network as their target infrastructure (private-link databases, VPC-only APIs, …) without rewriting the command for kubectl run.

Usage with build_k8s_support (recommended)

Most projects share the same K8sConfig across every decorated command — declare it once and reuse the bound decorator everywhere:

from pathlib import Path

from typer import Typer

from pysae_cli_tools.k8s import K8sConfig, build_k8s_support

K8S_CONFIG = K8sConfig(
    default_image="<registry>/<project>:latest",
    project_root=Path(__file__).resolve().parents[1],
    copy_paths=("my_pkg", "pyproject.toml", "poetry.lock"),
    install_script=(
        "apt-get update -qq && pip install poetry && "
        "poetry config virtualenvs.create false && "
        "poetry install --only main --no-interaction"
    ),
    forwarded_envvars=("MY_API_KEY", "MY_DB_URI"),
    redacted_options=("--api-key", "--password"),
)

k8s_support = build_k8s_support(K8S_CONFIG)
app = Typer()


@app.command()
@k8s_support()
def my_command() -> None:
    ...


@app.command()
@k8s_support(pod_name_prefix="my-second-command")  # override per-command
def my_second_command() -> None:
    ...

Usage with the explicit form

When you want to use a different config per command, pass it directly:

from pysae_cli_tools.k8s import K8sConfig, k8s_support

@app.command()
@k8s_support(config=K8S_CONFIG)
def my_command() -> None:
    ...

What happens at runtime

When --k8s is set on the command line, the decorator:

  1. Spawns an ephemeral pod using K8sConfig.default_image (or the Dockerfile base image when --k8s-from-local-sources is also set).
  2. Forwards every envvar listed in K8sConfig.forwarded_envvars from your local shell into the pod's env block.
  3. Runs python -m <your.cli.module> <subcommand> <filtered argv> inside the pod, with values matching K8sConfig.redacted_options masked in the [K8S] Running: log line.
  4. Streams stdout/stderr back to your terminal and deletes the pod on exit.

See pysae_cli_tools/k8s/config.py for the complete K8sConfig reference.

Development

poetry install
poetry run pre-commit install
poetry run pytest

CI publishes a new version to PyPI on every push to main — see .gitlab-ci.yml. The version is computed from git describe via pysae_cli_tools.compute_version.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pysae_cli_tools-0.1.2.tar.gz (12.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pysae_cli_tools-0.1.2-py3-none-any.whl (13.0 kB view details)

Uploaded Python 3

File details

Details for the file pysae_cli_tools-0.1.2.tar.gz.

File metadata

  • Download URL: pysae_cli_tools-0.1.2.tar.gz
  • Upload date:
  • Size: 12.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.4 CPython/3.12.13 Linux/6.12.80-106.156.amzn2023.x86_64

File hashes

Hashes for pysae_cli_tools-0.1.2.tar.gz
Algorithm Hash digest
SHA256 16724bff74ec7dcc9b0bab570a2f6b9dc966dc7e49032a325181d4684048fe31
MD5 0a43038bf8413fac3ffe8fb20a022d75
BLAKE2b-256 91160ff9bdb0072f6f348e3333b6f673ce486b16596cd6a651dde333ab6f1315

See more details on using hashes here.

File details

Details for the file pysae_cli_tools-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: pysae_cli_tools-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 13.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.4 CPython/3.12.13 Linux/6.12.80-106.156.amzn2023.x86_64

File hashes

Hashes for pysae_cli_tools-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 9b547d55b6c23d523be51632d4fb9558ed50a33359e721d9758c80e7ccb56bb0
MD5 0a272b7ef2fbd9769f9929a7773ae02b
BLAKE2b-256 700d990ac7e80ab04ee165a82358bd4282045dce816211316e79a573d0a98c58

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page