diff --git a/backend/capellacollab/cli/__init__.py b/backend/capellacollab/cli/__init__.py new file mode 100644 index 0000000000..677cdfe33a --- /dev/null +++ b/backend/capellacollab/cli/__init__.py @@ -0,0 +1,2 @@ +# SPDX-FileCopyrightText: Copyright DB Netz AG and the capella-collab-manager contributors +# SPDX-License-Identifier: Apache-2.0 diff --git a/backend/capellacollab/cli/__main__.py b/backend/capellacollab/cli/__main__.py new file mode 100644 index 0000000000..4b6f443650 --- /dev/null +++ b/backend/capellacollab/cli/__main__.py @@ -0,0 +1,12 @@ +# SPDX-FileCopyrightText: Copyright DB Netz AG and the capella-collab-manager contributors +# SPDX-License-Identifier: Apache-2.0 + +import typer + +import capellacollab.cli.ws + +app = typer.Typer() +app.add_typer(capellacollab.cli.ws.app, name="ws") + +if __name__ == "__main__": + app() diff --git a/backend/capellacollab/cli/ws.py b/backend/capellacollab/cli/ws.py new file mode 100644 index 0000000000..ca90243371 --- /dev/null +++ b/backend/capellacollab/cli/ws.py @@ -0,0 +1,76 @@ +# SPDX-FileCopyrightText: Copyright DB Netz AG and the capella-collab-manager contributors +# SPDX-License-Identifier: Apache-2.0 + +import typer +from kubernetes import client, config + +app = typer.Typer() + + +@app.command() +def list(namespace: str = None): + config.load_kube_config() + core_api = client.CoreV1Api() + + for item in core_api.list_namespaced_persistent_volume_claim( + namespace=namespace or get_current_namespace(), watch=False + ).items: + print(item.metadata.name) + + +@app.command() +def download(volume_name: str, namespace: str = None): + config.load_kube_config() + pod = create_temporary_pod(volume_name, namespace) + print(pod) + + +def create_temporary_pod(volume_name: str, namespace: str): + name = f"download-{volume_name}" + + containers = [ + client.V1Container( + name=name, + image="alpine:latest", + command=["sleep", "infinity"], + volume_mounts=[ + client.V1VolumeMount( + name=volume_name, + mount_path="/mnt", + read_only=True, + ) + ], + image_pull_policy="Always", + ) + ] + + volumes = [ + client.V1Volume( + name=volume_name, + persistent_volume_claim=client.V1PersistentVolumeClaimVolumeSource( + claim_name=volume_name + ), + ) + ] + + pod = client.V1Pod( + kind="Pod", + api_version="v1", + metadata=client.V1ObjectMeta(name=name), + spec=client.V1PodSpec( + # security_context=pod_security_context, + containers=containers, + volumes=volumes, + restart_policy="Never", + ), + ) + + return client.CoreV1Api().create_namespaced_pod(namespace, pod) + + +def get_current_namespace(): + try: + _, active_context = config.list_kube_config_contexts() + return active_context["context"]["namespace"] + except KeyError: + return "default" diff --git a/backend/pyproject.toml b/backend/pyproject.toml index f165432c89..e62c7e2e52 100644 --- a/backend/pyproject.toml +++ b/backend/pyproject.toml @@ -44,6 +44,7 @@ dependencies = [ "starlette-prometheus", "fastapi-pagination>=0.12.5", "aiohttp", + "typer", ] [project.urls]