Skip to content

Commit

Permalink
Refactor for argocd (#160)
Browse files Browse the repository at this point in the history
* Refactor for argocd

Touch up front end:
- Use typer to handle env variables (Show in --help and do type casting/sanitising for things like export ENV_VARIABLE=false to read as boolean false)
- Dynamic command hiding based on chosen backend and commands implemented
- Consistant flow of user inputs for commands through backend rather than a mix of typer ctx, env reads, globals - to support validation, sanitising
- Separate frontend (typer) from backend (particular: error handling)
- Lazy import of monitor to reduce load time for everytime ec is called

Backend work:
- Remove docker ("local" backend) and related
- Implement an argocd backend
- Remove handling of symlink charts for helm commands and replace with shared helm values
- Allow changes to shared files to show as a new revision of a service
- Only interact with "ec-services" - These are statefulsets which are tagged "ec_service=true"
- Use custom errors over "typer error" so that targetted error handling can be done
- Add schema validation and type hinting for polars schemas to interface more clear
- Add tempfile context manager for file retension at debug and handle case of multiple temp files

* Remove some redundancy

* Use more portable polars package

* Remove system test action

* Remove reference to sys action
  • Loading branch information
marcelldls authored Aug 30, 2024
1 parent 302c4fd commit 748e9a7
Show file tree
Hide file tree
Showing 64 changed files with 1,677 additions and 2,726 deletions.
36 changes: 0 additions & 36 deletions .github/workflows/_sys_test.yml

This file was deleted.

13 changes: 0 additions & 13 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,6 @@ jobs:
secrets:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

system:
needs: check
if: needs.check.outputs.branch-pr == ''
strategy:
matrix:
runs-on: ["ubuntu-latest"]
python-version: ["3.12"]
fail-fast: false
uses: ./.github/workflows/_sys_test.yml
with:
runs-on: ${{ matrix.runs-on }}
python-version: ${{ matrix.python-version }}

dist:
needs: check
if: needs.check.outputs.branch-pr == ''
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ dependencies = [
"requests",
"ruamel.yaml",
"jinja2",
"polars",
"polars-lts-cpu",
"textual",
]

Expand Down
10 changes: 10 additions & 0 deletions src/edge_containers_cli/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
import polars

from ._version import __version__

__all__ = ["__version__"]

# Set formatting of polars tables
polars.Config.set_tbl_hide_column_data_types(True)
polars.Config.set_tbl_hide_dataframe_shape(True)
polars.Config.set_tbl_rows(-1)
polars.Config.set_tbl_cols(-1)
polars.Config.set_fmt_str_lengths(82)
polars.Config.set_tbl_formatting("ASCII_MARKDOWN")
95 changes: 74 additions & 21 deletions src/edge_containers_cli/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

import typer

import edge_containers_cli.globals as globals
from edge_containers_cli.cmds.cli import cli
from edge_containers_cli.cli import cli
from edge_containers_cli.definitions import ENV, ECBackends, ECContext, ECLogLevels

from . import __version__
from .backend import backend as ec_backend
from .backend import init_backend
from .logging import init_logging
from .shell import init_shell
from .utils import init_cleanup

__all__ = ["main"]

Expand All @@ -17,6 +21,20 @@ def version_callback(value: bool):
raise typer.Exit()


def backend_callback(ctx: typer.Context, backend: ECBackends):
init_backend(backend)
# Dynamically drop any method not implemented
not_implemented = [
mthd.replace("_", "-") for mthd in ec_backend.get_notimplemented()
]
for command in not_implemented:
typer_commands = ctx.command.commands # type: ignore
if command in typer_commands:
typer_commands.pop(command)

return backend.value


@cli.callback()
def main(
ctx: typer.Context,
Expand All @@ -28,39 +46,74 @@ def main(
help="Log the version of ec and exit",
),
repo: str = typer.Option(
"",
ECContext().repo,
"-r",
"--repo",
help="service/ioc instances repository",
help="Service instances repository",
envvar=ENV.repo.value,
),
namespace: str = typer.Option(
"", "-n", "--namespace", help="kubernetes namespace to use"
target: str = typer.Option(
ECContext().target,
"-t",
"--target",
help="K8S namespace or ARGOCD <project>/<root-app>",
envvar=ENV.target.value,
),
log_level: str = typer.Option(
"WARN", help="Log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)"
backend: ECBackends = typer.Option(
ECBackends.ARGOCD,
"-b",
"--backend",
callback=backend_callback,
is_eager=True,
help="Backend to use",
envvar=ENV.backend.value,
expose_value=True,
),
verbose: bool = typer.Option(
globals.EC_VERBOSE, "-v", "--verbose", help="print the commands we run"
False,
"-v",
"--verbose",
help="Print the commands we run",
envvar=ENV.verbose.value,
show_default=True,
),
dryrun: bool = typer.Option(
False,
"--dryrun",
help="Print the commands we run without execution",
envvar=ENV.dryrun.value,
show_default=True,
),
debug: bool = typer.Option(
globals.EC_DEBUG,
False,
"-d",
"--debug",
help="Enable debug logging to console and retain temporary files",
help="Enable debug logging, retain temp files",
envvar=ENV.debug.value,
show_default=True,
),
log_level: ECLogLevels = typer.Option(
ECLogLevels.WARNING,
help="Log level",
envvar=ENV.log_level.value,
),
log_url: str = typer.Option(
ECContext().log_url,
help="Log url",
envvar=ENV.log_url.value,
),
):
"""Edge Containers assistant CLI"""
init_logging(ECLogLevels.DEBUG if debug else log_level)
init_shell(verbose, dryrun)
init_cleanup(debug)

globals.EC_VERBOSE, globals.EC_DEBUG = bool(verbose), bool(debug)

init_logging(log_level.upper())

# create a context dictionary to pass to all sub commands
repo = repo or globals.EC_SERVICES_REPO
namespace = namespace or globals.EC_K8S_NAMESPACE
ctx.ensure_object(globals.Context)
context = globals.Context(namespace=namespace, beamline_repo=repo)
ctx.obj = context
context = ECContext(
repo=repo,
target=target,
log_url=log_url,
)
ec_backend.set_context(context)


# test with:
Expand Down
Loading

0 comments on commit 748e9a7

Please sign in to comment.