Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 42 additions & 7 deletions docker/entrypoint-binary.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,50 @@
#!/bin/sh

mkdir -p /config 2>/dev/null || true

if [ ! -f /config/config.jsonc ]; then
echo "No config.jsonc found in /config, generating template..."
/usr/local/bin/llm-rosetta-gateway --config /config/config.jsonc init
echo "Edit /config/config.jsonc with your API keys and restart the container."
fi
# The config volume; the variable lets a test point the script elsewhere.
CONFIG_DIR=${CONFIG_DIR:-/config}
mkdir -p "$CONFIG_DIR" 2>/dev/null || true

if [ "${1#-}" != "$1" ]; then
set -- /usr/local/bin/llm-rosetta-gateway "$@"
fi

# Whether this invocation starts the server: the default CMD, an argument list
# that holds only options, and an explicit `serve` (also `gateway serve`). Every
# other command (init, add, ext, gateway usage, a shell) is a one-off and must
# not write a config template or tell the operator to restart the container.
starts_server() {
case "$1" in
llm-rosetta-gateway | */llm-rosetta-gateway) bare_serves=1 ;;
llm-rosetta | */llm-rosetta) bare_serves=0 ;;
*) return 1 ;;
esac
shift
first=""
second=""
while [ "$#" -gt 0 ] && [ -z "$second" ]; do
case "$1" in
--config | -c | --host | --port | --socket | -S | --tls-cert | --tls-key | --proxy | --data-dir | --log-level)
[ "$#" -gt 1 ] && shift
;;
-*) ;;
*)
if [ -z "$first" ]; then first=$1; else second=$1; fi
;;
esac
shift
done
case "$first" in
"") [ "$bare_serves" = 1 ] ;;
serve) return 0 ;;
gateway) [ "$second" = serve ] ;;
*) return 1 ;;
esac
}

if starts_server "$@" && [ ! -f "$CONFIG_DIR/config.jsonc" ]; then
echo "No config.jsonc found in $CONFIG_DIR, generating template..."
/usr/local/bin/llm-rosetta-gateway --config "$CONFIG_DIR/config.jsonc" init
echo "Edit $CONFIG_DIR/config.jsonc with your API keys and restart the container."
fi

exec "$@"
53 changes: 44 additions & 9 deletions docker/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
#!/bin/sh

mkdir -p /config 2>/dev/null || true
# The config volume; the variable lets a test point the script elsewhere.
CONFIG_DIR=${CONFIG_DIR:-/config}
mkdir -p "$CONFIG_DIR" 2>/dev/null || true

# Resolve HOME for the runtime uid. appuser (uid 1000) has a passwd home; a uid
# that Compose's `user:` directive selects has no passwd entry, and the image
# then leaves HOME at "/", which nothing can write. Keep the passwd home when
# one exists, otherwise take HOME from the environment when it names one (the
# source compose file passes /config/home) and fall back to /config/home. This
# source compose file passes /config/home) and fall back to $CONFIG_DIR/home. This
# runs as the target uid, never as root.
passwd_home=$(awk -F: -v uid="$(id -u)" '$3 == uid { print $6; exit }' /etc/passwd 2>/dev/null)
if [ -n "$passwd_home" ] && [ -d "$passwd_home" ]; then
HOME="$passwd_home"
else
case "$HOME" in
"" | "/") HOME=/config/home ;;
"" | "/") HOME=$CONFIG_DIR/home ;;
esac
fi
export HOME
Expand All @@ -26,14 +28,47 @@ if [ ! -w "$HOME" ]; then
exit 1
fi

if [ ! -f /config/config.jsonc ]; then
echo "No config.jsonc found in /config, generating template..."
llm-rosetta-gateway --config /config/config.jsonc init
echo "Edit /config/config.jsonc with your API keys and restart the container."
fi

if [ "${1#-}" != "$1" ]; then
set -- llm-rosetta-gateway "$@"
fi

# Whether this invocation starts the server: the default CMD, an argument list
# that holds only options, and an explicit `serve` (also `gateway serve`). Every
# other command (init, add, ext, gateway usage, a shell) is a one-off and must
# not write a config template or tell the operator to restart the container.
starts_server() {
case "$1" in
llm-rosetta-gateway | */llm-rosetta-gateway) bare_serves=1 ;;
llm-rosetta | */llm-rosetta) bare_serves=0 ;;
*) return 1 ;;
esac
shift
first=""
second=""
while [ "$#" -gt 0 ] && [ -z "$second" ]; do
case "$1" in
--config | -c | --host | --port | --socket | -S | --tls-cert | --tls-key | --proxy | --data-dir | --log-level)
[ "$#" -gt 1 ] && shift
;;
-*) ;;
*)
if [ -z "$first" ]; then first=$1; else second=$1; fi
;;
esac
shift
done
case "$first" in
"") [ "$bare_serves" = 1 ] ;;
serve) return 0 ;;
gateway) [ "$second" = serve ] ;;
*) return 1 ;;
esac
}

if starts_server "$@" && [ ! -f "$CONFIG_DIR/config.jsonc" ]; then
echo "No config.jsonc found in $CONFIG_DIR, generating template..."
llm-rosetta-gateway --config "$CONFIG_DIR/config.jsonc" init
echo "Edit $CONFIG_DIR/config.jsonc with your API keys and restart the container."
fi

exec "$@"
136 changes: 136 additions & 0 deletions tests/test_docker_entrypoints.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
"""The image entrypoints write a config template only when the server starts.

``docker/entrypoint.sh`` (the Python image) and ``docker/entrypoint-binary.sh``
(the binary image) used to generate ``config.jsonc`` and print "Edit ... restart
the container" before *any* command, including ``llm-rosetta ext install`` and
``llm-rosetta gateway --help``. The template belongs to the default start only:
the default CMD, an argument list of options, and an explicit ``serve``. The
tests run the scripts under ``sh`` with stub executables on ``PATH``; no Docker.
"""

from __future__ import annotations

import os
import stat
import subprocess
from pathlib import Path

import pytest

DOCKER = Path(__file__).resolve().parents[1] / "docker"
SCRIPTS = ("entrypoint.sh", "entrypoint-binary.sh")

_STUB = """#!/bin/sh
echo "$0 $*" >> "$STUB_LOG"
# The real ``init`` writes the template; a stub does the same.
case " $* " in
*" init "*) mkdir -p "$CONFIG_DIR"; : > "$CONFIG_DIR/config.jsonc" ;;
esac
"""

TEMPLATE_MESSAGE = "No config.jsonc found"

#: argv, where the server starts (and a missing config gets a template).
SERVES = [
["llm-rosetta-gateway", "--config", "/config/config.jsonc", "--host", "0.0.0.0"],
["--port", "9000"],
["--config", "init.jsonc", "--host", "0.0.0.0"],
["llm-rosetta-gateway"],
["llm-rosetta-gateway", "serve"],
["llm-rosetta-gateway", "--port", "9000", "serve"],
["llm-rosetta-gateway", "gateway", "serve"],
["llm-rosetta", "serve"],
["llm-rosetta", "serve", "--config", "x.jsonc"],
["llm-rosetta", "gateway", "serve"],
]

#: argv, a one-off command that must not write a template.
ONE_OFFS = [
["llm-rosetta", "gateway", "--help"],
["llm-rosetta", "gateway", "init"],
["llm-rosetta", "gateway", "usage", "--db", "/data"],
["llm-rosetta", "ext", "list"],
["llm-rosetta", "ext", "install", "acme"],
["llm-rosetta"],
["llm-rosetta", "gateway"],
["llm-rosetta-gateway", "init"],
["llm-rosetta-gateway", "--config", "serve.jsonc", "init"],
["llm-rosetta-gateway", "ext", "list"],
["llm-rosetta-gateway", "gateway", "init"],
["sh", "-c", "true"],
]


def _run(script: str, argv: list[str], tmp_path: Path, *, config: bool = False):
if not (DOCKER / script).is_file():
pytest.skip("the docker/ directory is not part of this checkout")
bin_dir = tmp_path / "bin"
bin_dir.mkdir(exist_ok=True)
for name in ("llm-rosetta-gateway", "llm-rosetta"):
stub = bin_dir / name
stub.write_text(_STUB)
stub.chmod(stub.stat().st_mode | stat.S_IXUSR)
config_dir = tmp_path / "config"
config_dir.mkdir(exist_ok=True)
if config:
(config_dir / "config.jsonc").write_text("{}")
log = tmp_path / "stub.log"
env = {
**os.environ,
"PATH": f"{bin_dir}{os.pathsep}{os.environ['PATH']}",
"CONFIG_DIR": str(config_dir),
"HOME": str(tmp_path / "home"),
"STUB_LOG": str(log),
}
result = subprocess.run(
["sh", str(DOCKER / script), *argv],
capture_output=True,
text=True,
env=env,
check=False,
timeout=30,
)
return result, (log.read_text() if log.exists() else "")


@pytest.mark.parametrize("script", SCRIPTS)
@pytest.mark.parametrize("argv", SERVES, ids=" ".join)
def test_the_server_start_writes_a_template_when_there_is_no_config(
script, argv, tmp_path
):
result, _log = _run(script, argv, tmp_path)

assert TEMPLATE_MESSAGE in result.stdout, result.stderr
assert "restart the container" in result.stdout


@pytest.mark.parametrize("script", SCRIPTS)
@pytest.mark.parametrize("argv", SERVES, ids=" ".join)
def test_the_server_start_leaves_an_existing_config_alone(script, argv, tmp_path):
result, _log = _run(script, argv, tmp_path, config=True)

assert TEMPLATE_MESSAGE not in result.stdout


@pytest.mark.parametrize("script", SCRIPTS)
@pytest.mark.parametrize("argv", ONE_OFFS, ids=" ".join)
def test_a_one_off_command_writes_no_template_and_prints_no_restart_note(
script, argv, tmp_path
):
result, log = _run(script, argv, tmp_path)

assert TEMPLATE_MESSAGE not in result.stdout
assert "restart the container" not in result.stdout
if argv[0] != "sh":
# The command is the only thing that ran: no ``init`` before it. (A
# command that is itself ``init`` writes its own config, as it should.)
assert log.count("\n") == 1, log


def test_the_python_entrypoint_runs_the_given_command_after_the_template(tmp_path):
result, log = _run("entrypoint.sh", ["--port", "9000"], tmp_path)

lines = log.splitlines()
assert lines[0].endswith(f"--config {tmp_path / 'config' / 'config.jsonc'} init")
assert lines[1].endswith("llm-rosetta-gateway --port 9000")
assert result.returncode == 0
Loading