Skip to content

Commit

Permalink
Add developer container option for deploy-local
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelldls committed May 30, 2024
1 parent 11eca91 commit fce02ce
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 19 deletions.
7 changes: 5 additions & 2 deletions src/edge_containers_cli/cmds/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from edge_containers_cli.git import create_version_map
from edge_containers_cli.globals import EC_K8S_NAMESPACE
from edge_containers_cli.logging import log
from edge_containers_cli.utils import cleanup_temp, drop_path
from edge_containers_cli.utils import check_instance_path, cleanup_temp, drop_path


class ErrorHandlingTyper(typer.Typer):
Expand Down Expand Up @@ -140,14 +140,17 @@ def deploy_local(
resolve_path=True,
autocompletion=force_plain_completion,
),
developer: bool = typer.Option(
False, "--developer", help="Deploy ioc developer image"
),
yes: bool = typer.Option(False, "-y", "--yes", help="Skip confirmation prompt"),
wait: bool = typer.Option(False, "--wait", help="Waits for readiness"),
args: str = typer.Option("", help="Additional args for helm, 'must be quoted'"),
):
"""
Deploy a local IOC/service helm chart directly to the cluster with dated beta version
"""
commands(ctx).deploy_local(svc_instance, yes, args)
commands(ctx).deploy_local(svc_instance, yes, args, developer)


@cli.command()
Expand Down
2 changes: 1 addition & 1 deletion src/edge_containers_cli/cmds/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def delete(self, service_name):
def template(self, svc_instance: Path, args: str):
raise NotImplementedError

def deploy_local(self, svc_instance: Path, yes: bool, args: str):
def deploy_local(self, svc_instance: Path, yes: bool, args: str, developer: bool):
raise NotImplementedError

def deploy(self, service_name: str, version: str, args: str):
Expand Down
33 changes: 22 additions & 11 deletions src/edge_containers_cli/cmds/helm.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,14 @@ def __init__(
self.service_name = service_name
self.beamline_repo = repo
self.namespace = namespace
self.args = args
self.version = version or local_version()
self.template = template

if template:
self.args = args
else:
self.args = f"{args} --reset-values" # Helm persists --set values

self.tmp = Path(tempfile.mkdtemp())

def __del__(self):
Expand Down Expand Up @@ -97,15 +101,8 @@ def _do_deploy(self, service_folder: Path):
interactive=False,
)

# Determine package name
with open("Chart.yaml") as fp:
chart_yaml = YAML(typ="safe").load(fp)
package_path = (
service_folder / f'{chart_yaml["name"]}-{chart_yaml["version"]}.tgz'
)

# use helm to install the chart
self._install(package_path)
self._install(service_folder / get_package(service_folder))

def _install(self, helm_chart: Path):
"""
Expand All @@ -118,10 +115,24 @@ def _install(self, helm_chart: Path):
f"bash -c "
f'"'
f"helm {helm_cmd} {self.service_name} {helm_chart} "
f"--namespace {self.namespace} {self.args}"
f" 2> >(grep -v 'found symbolic link' >&2) "
f"--namespace {self.namespace} {self.args} "
f"2> >(grep -v 'found symbolic link' >&2) "
f'"'
)

output = shell.run_command(cmd, interactive=False)
typer.echo(output)


def get_package(chart: Path) -> str:
with open(chart / "Chart.yaml") as fp:
chart_yaml = YAML(typ="safe").load(fp)
package_name = f'{chart_yaml["name"]}-{chart_yaml["version"]}.tgz'
return package_name


def get_image(chart: Path) -> str:
with open(chart / "values.yaml") as fp:
chart_yaml = YAML(typ="safe").load(fp)
image_spec = chart_yaml["shared"]["ioc-instance"]["image"]
return image_spec
21 changes: 19 additions & 2 deletions src/edge_containers_cli/cmds/k8s_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,23 @@
Relies on the Helm class for deployment aspects.
"""

import re
from datetime import datetime
from io import StringIO
from pathlib import Path
from typing import Optional

import polars
import typer
from ruamel.yaml import YAML

import edge_containers_cli.globals as globals
import edge_containers_cli.shell as shell
from edge_containers_cli.cmds.commands import Commands
from edge_containers_cli.cmds.helm import Helm
from edge_containers_cli.cmds.helm import Helm, get_image
from edge_containers_cli.cmds.kubectl import jsonpath_deploy_info, jsonpath_pod_info
from edge_containers_cli.logging import log
from edge_containers_cli.utils import append_arg


def check_service(service_name: str, namespace: str) -> str:
Expand Down Expand Up @@ -103,10 +106,24 @@ def template(self, svc_instance: Path, args: str):
)
chart.deploy_local(svc_instance)

def deploy_local(self, svc_instance: Path, yes: bool, args: str):
def deploy_local(
self,
svc_instance: Path,
yes: bool,
args: str,
developer: bool, # , sleep: bool
):
service_name = svc_instance.name.lower()

chart = Helm(self.namespace, service_name, args=args)

if developer:
image_spec = get_image(svc_instance)
dev_image = re.sub("-runtime:", "-developer:", image_spec)
chart.args = append_arg(
chart.args, "--set", f"shared.ioc-instance.image={dev_image}"
)

chart.deploy_local(svc_instance, yes)

def deploy(self, service_name: str, version: str, args: str):
Expand Down
10 changes: 7 additions & 3 deletions src/edge_containers_cli/cmds/local_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,14 @@ def delete(self, service_name):
raise typer.Abort()
self.docker.remove(service_name)

def _do_deploy(self, ioc_instance: Path, version: str, args: str):
def _do_deploy(
self, ioc_instance: Path, version: str, args: str, developer: bool = False
):
service_name, _ = check_instance_path(ioc_instance)

image = get_instance_image_name(ioc_instance)
if developer:
image = re.sub("-runtime:", "-developer:", image)
log.debug(f"deploying {ioc_instance} with image {image}")
config = ioc_instance / globals.CONFIG_FOLDER
service_name = ioc_instance.name
Expand Down Expand Up @@ -108,7 +112,7 @@ def _do_deploy(self, ioc_instance: Path, version: str, args: str):
)
raise typer.Exit(1)

def deploy_local(self, svc_instance: Path, yes: bool, args: str):
def deploy_local(self, svc_instance: Path, yes: bool, args: str, developer: bool):
"""
Use a local copy of an ioc instance definition to deploy a temporary
version of the IOC to the local docker instance
Expand All @@ -121,7 +125,7 @@ def deploy_local(self, svc_instance: Path, yes: bool, args: str):
)
if not typer.confirm("Are you sure ?"):
raise typer.Abort()
self._do_deploy(svc_instance, version, args)
self._do_deploy(svc_instance, version, args, developer=developer)

def deploy(self, service_name: str, version: str, args: str):
"""
Expand Down
9 changes: 9 additions & 0 deletions src/edge_containers_cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,12 @@ def local_version() -> str:
elapsed = (time_now - time_month).seconds
elapsed_base = hex(elapsed)[2:]
return datetime.strftime(time_now, f"%Y.%-m.{elapsed_base}-b")


def append_arg(command: str, arg: str, values: str) -> str:
"""Add values to a commandline parameter if already defined"""
if arg in command:
result = re.sub(arg, f"{arg} {values}", command)
else:
result = f"{command} {arg} {values}"
return result
21 changes: 21 additions & 0 deletions tests/test_utilities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""
tests for utility commands
"""

from edge_containers_cli.utils import (
append_arg,
)


def test_arg_append_existing():
input_ = "--arg1 A B --arg2 B --arg3 A B"
target = "--arg1 A B --arg2 A B --arg3 A B"
result = append_arg(input_, "--arg2", "A")
assert target == result


def test_arg_append_new():
input_ = "--arg1 A B --arg2 A B"
target = "--arg1 A B --arg2 A B --arg3 A"
result = append_arg(input_, "--arg3", "A")
assert target == result

0 comments on commit fce02ce

Please sign in to comment.