Skip to content

Commit e9bcb0e

Browse files
committed
feat: export distribution container build artifacts
Add a new --export-dir flag to the `llama stack build` command that allows users to export container build artifacts to a specified directory instead of building the container directly. This feature is useful for: - Building containers in different environments - Sharing build configurations - Customizing the build process The exported tarball includes: - Containerfile (Dockerfile) - Run configuration file (if building from config) - External provider files (if specified) - Build script for assistance The tarball is named with a timestamp for uniqueness: <distro-name>_<timestamp>.tar.gz Documentation has been updated in building_distro.md to reflect this new functionality as well as integration tests. Signed-off-by: Sébastien Han <[email protected]>
1 parent 047303e commit e9bcb0e

File tree

7 files changed

+187
-23
lines changed

7 files changed

+187
-23
lines changed

.github/workflows/providers-build.yml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,3 +198,55 @@ jobs:
198198
'source /etc/os-release && echo "$ID"' \
199199
| grep -qE '^(rhel|ubi)$' \
200200
|| { echo "Base image is not UBI 9!"; exit 1; }
201+
202+
export-build:
203+
runs-on: ubuntu-latest
204+
steps:
205+
- name: Checkout repository
206+
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
207+
208+
- name: Set up Python
209+
uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0
210+
with:
211+
python-version: '3.10'
212+
213+
- name: Install uv
214+
uses: astral-sh/setup-uv@0c5e2b8115b80b4c7c5ddf6ffdd634974642d182 # v5.4.1
215+
with:
216+
python-version: "3.10"
217+
218+
- name: Install LlamaStack
219+
run: |
220+
uv venv
221+
source .venv/bin/activate
222+
uv pip install -e .
223+
224+
- name: Pin template to UBI9 base
225+
run: |
226+
yq -i '
227+
.image_type = "container" |
228+
.image_name = "ubi9-test" |
229+
.distribution_spec.container_image = "registry.access.redhat.com/ubi9:latest"
230+
' llama_stack/templates/starter/build.yaml
231+
232+
- name: Test the export
233+
run: |
234+
# Support for USE_COPY_NOT_MOUNT=true LLAMA_STACK_DIR=. will be added in the future
235+
uv run llama stack build --config llama_stack/templates/starter/build.yaml --export-dir export
236+
for file in export/*; do
237+
echo "File: $file"
238+
if [[ "$file" == *.tar.gz ]]; then
239+
echo "Tarball found"
240+
tarball_found=1
241+
tar -xzvf "$file" -C export
242+
else
243+
continue
244+
fi
245+
break
246+
done
247+
if [ -z "$tarball_found" ]; then
248+
echo "Tarball not found"
249+
exit 1
250+
fi
251+
cd export
252+
docker build -t export-test -f ./Containerfile .

docs/source/distributions/building_distro.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ The main points to consider are:
5353

5454
```
5555
llama stack build -h
56-
usage: llama stack build [-h] [--config CONFIG] [--template TEMPLATE] [--list-templates] [--image-type {conda,container,venv}] [--image-name IMAGE_NAME] [--print-deps-only] [--run]
56+
usage: llama stack build [-h] [--config CONFIG] [--template TEMPLATE] [--list-templates] [--image-type {conda,container,venv}] [--image-name IMAGE_NAME] [--print-deps-only] [--run] [--export-dir EXPORT_DIR]
5757
5858
Build a Llama stack container
5959
@@ -71,6 +71,8 @@ options:
7171
found. (default: None)
7272
--print-deps-only Print the dependencies for the stack only, without building the stack (default: False)
7373
--run Run the stack after building using the same image type, name, and other applicable arguments (default: False)
74+
--export-dir EXPORT_DIR
75+
Export the build artifacts to a specified directory instead of building the container. This will create a tarball containing the Dockerfile and all necessary files to build the container. (default: None)
7476
7577
```
7678

@@ -260,6 +262,24 @@ Containerfile created successfully in /tmp/tmp.viA3a3Rdsg/ContainerfileFROM pyth
260262
You can now edit ~/meta-llama/llama-stack/tmp/configs/ollama-run.yaml and run `llama stack run ~/meta-llama/llama-stack/tmp/configs/ollama-run.yaml`
261263
```
262264

265+
You can also export the build artifacts to a specified directory instead of building the container directly. This is useful when you want to:
266+
- Build the container in a different environment
267+
- Share the build configuration with others
268+
- Customize the build process
269+
270+
To export the build artifacts, use the `--export-dir` flag:
271+
272+
```
273+
llama stack build --config my-build.yaml --image-type container --export-dir ./my-build
274+
```
275+
276+
This will create a tarball in the specified directory containing:
277+
- The Dockerfile (named Containerfile)
278+
- The run configuration file (if building from a config)
279+
- Any external provider files (if specified in the config)
280+
281+
The tarball will be named with a timestamp to ensure uniqueness, for example: `<distro-name>_<timestamp>.tar.gz`
282+
263283
After this step is successful, you should be able to find the built container image and test it with `llama stack run <path/to/run.yaml>`.
264284
:::
265285

llama_stack/cli/stack/_build.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ def run_stack_build_command(args: argparse.Namespace) -> None:
230230
image_name=image_name,
231231
config_path=args.config,
232232
template_name=args.template,
233+
export_dir=args.export_dir,
233234
)
234235

235236
except (Exception, RuntimeError) as exc:
@@ -343,6 +344,7 @@ def _run_stack_build_command_from_build_config(
343344
image_name: str | None = None,
344345
template_name: str | None = None,
345346
config_path: str | None = None,
347+
export_dir: str | None = None,
346348
) -> str:
347349
image_name = image_name or build_config.image_name
348350
if build_config.image_type == LlamaStackImageType.CONTAINER.value:
@@ -385,6 +387,7 @@ def _run_stack_build_command_from_build_config(
385387
image_name,
386388
template_or_config=template_name or config_path or str(build_file_path),
387389
run_config=run_config_file,
390+
export_dir=export_dir,
388391
)
389392
if return_code != 0:
390393
raise RuntimeError(f"Failed to build image {image_name}")

llama_stack/cli/stack/build.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# the root directory of this source tree.
66
import argparse
77
import textwrap
8+
from pathlib import Path
89

910
from llama_stack.cli.stack.utils import ImageType
1011
from llama_stack.cli.subcommand import Subcommand
@@ -82,6 +83,13 @@ def _add_arguments(self):
8283
help="Build a config for a list of providers and only those providers. This list is formatted like: api1=provider1,api2=provider2. Where there can be multiple providers per API.",
8384
)
8485

86+
self.parser.add_argument(
87+
"--export-dir",
88+
type=Path,
89+
default=None,
90+
help="Export the build artifacts to a specified directory instead of building the container. This will create a directory containing the Dockerfile and all necessary files to build the container.",
91+
)
92+
8593
def _run_stack_build_command(self, args: argparse.Namespace) -> None:
8694
# always keep implementation completely silo-ed away from CLI so CLI
8795
# can be fast to load and reduces dependencies

llama_stack/distribution/build.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ def build_image(
9393
image_name: str,
9494
template_or_config: str,
9595
run_config: str | None = None,
96+
export_dir: str | None = None,
9697
):
9798
container_base = build_config.distribution_spec.container_image or "python:3.10-slim"
9899

@@ -108,11 +109,18 @@ def build_image(
108109
container_base,
109110
" ".join(normal_deps),
110111
]
112+
if export_dir is not None:
113+
args.append("--export-dir")
114+
args.append(f"{export_dir}")
111115

112116
# When building from a config file (not a template), include the run config path in the
113117
# build arguments
114118
if run_config is not None:
115119
args.append(run_config)
120+
121+
if special_deps:
122+
args.append("--special-pip-deps")
123+
# The content is added after all the image_type conditions
116124
elif build_config.image_type == LlamaStackImageType.CONDA.value:
117125
script = str(importlib.resources.files("llama_stack") / "distribution/build_conda_env.sh")
118126
args = [

llama_stack/distribution/build_container.sh

Lines changed: 93 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ BUILD_CONTEXT_DIR=$(pwd)
2626

2727
if [ "$#" -lt 4 ]; then
2828
# This only works for templates
29-
echo "Usage: $0 <template_or_config> <image_name> <container_base> <pip_dependencies> [<run_config>] [<special_pip_deps>]" >&2
29+
echo "Usage: $0 <template_or_config> <image_name> <container_base> <pip_dependencies> [<run_config>] --special-pip-deps <special_pip_deps> --export-dir <export_dir>" >&2
3030
exit 1
3131
fi
3232
set -euo pipefail
@@ -43,23 +43,37 @@ shift
4343
# Handle optional arguments
4444
run_config=""
4545
special_pip_deps=""
46-
47-
# Check if there are more arguments
48-
# The logics is becoming cumbersom, we should refactor it if we can do better
49-
if [ $# -gt 0 ]; then
50-
# Check if the argument ends with .yaml
51-
if [[ "$1" == *.yaml ]]; then
52-
run_config="$1"
53-
shift
54-
# If there's another argument after .yaml, it must be special_pip_deps
55-
if [ $# -gt 0 ]; then
56-
special_pip_deps="$1"
57-
fi
58-
else
59-
# If it's not .yaml, it must be special_pip_deps
60-
special_pip_deps="$1"
61-
fi
62-
fi
46+
export_dir=""
47+
48+
# Process remaining arguments
49+
while [[ $# -gt 0 ]]; do
50+
case "$1" in
51+
*.yaml)
52+
run_config="$1"
53+
shift
54+
;;
55+
--export-dir)
56+
if [ -z "${2:-}" ]; then
57+
echo "Error: --export-dir requires a value" >&2
58+
exit 1
59+
fi
60+
export_dir="$2"
61+
shift 2
62+
;;
63+
--special-pip-deps)
64+
if [ -z "${2:-}" ]; then
65+
echo "Error: --special-pip-deps requires a value" >&2
66+
exit 1
67+
fi
68+
special_pip_deps="$2"
69+
shift 2
70+
;;
71+
*)
72+
echo "Unknown argument: $1" >&2
73+
exit 1
74+
;;
75+
esac
76+
done
6377

6478
# Define color codes
6579
RED='\033[0;31m'
@@ -83,8 +97,8 @@ add_to_container() {
8397
fi
8498
}
8599

86-
# Check if container command is available
87-
if ! is_command_available $CONTAINER_BINARY; then
100+
# Check if container command is available only if not running in export mode
101+
if ! is_command_available $CONTAINER_BINARY && [ -z "$export_dir" ]; then
88102
printf "${RED}Error: ${CONTAINER_BINARY} command not found. Is ${CONTAINER_BINARY} installed and in your PATH?${NC}" >&2
89103
exit 1
90104
fi
@@ -96,7 +110,7 @@ FROM $container_base
96110
WORKDIR /app
97111
98112
# We install the Python 3.11 dev headers and build tools so that any
99-
# Cextension wheels (e.g. polyleven, faisscpu) can compile successfully.
113+
# C-extension wheels (e.g. polyleven, faiss-cpu) can compile successfully.
100114
101115
RUN dnf -y update && dnf install -y iputils git net-tools wget \
102116
vim-minimal python3.11 python3.11-pip python3.11-wheel \
@@ -270,6 +284,64 @@ printf "Containerfile created successfully in %s/Containerfile\n\n" "$TEMP_DIR"
270284
cat "$TEMP_DIR"/Containerfile
271285
printf "\n"
272286

287+
create_export_tarball() {
288+
local export_dir="$1"
289+
local image_name="$2"
290+
local run_config="$3"
291+
local external_providers_dir="$4"
292+
local TEMP_DIR="$5"
293+
local BUILD_CONTEXT_DIR="$6"
294+
295+
mkdir -p "$export_dir"
296+
local timestamp=$(date '+%Y-%m-%d_%H-%M-%S')
297+
local tar_name="${image_name//[^a-zA-Z0-9]/_}_${timestamp}.tar.gz"
298+
299+
# If a run config is provided, copy it to the export directory otherwise it's a template build and
300+
# we don't need to copy anything
301+
if [ -n "$run_config" ]; then
302+
mv "$run_config" "$TEMP_DIR"/run.yaml
303+
fi
304+
305+
# Create the archive with all files
306+
echo "Creating tarball with the following files:"
307+
echo "- Containerfile"
308+
309+
# Capture both stdout and stderr from tar command
310+
local tar_cmd="tar -czf \"$export_dir/$tar_name\" -C \"$TEMP_DIR\" Containerfile"
311+
312+
if [ -n "$run_config" ]; then
313+
echo "- run.yaml"
314+
tar_cmd="$tar_cmd -C \"$BUILD_CONTEXT_DIR\" \"$(basename run.yaml)\""
315+
fi
316+
317+
if [ -n "$external_providers_dir" ] && [ -d "$external_providers_dir" ]; then
318+
echo "- providers.d directory"
319+
tar_cmd="$tar_cmd -C \"$BUILD_CONTEXT_DIR\" providers.d"
320+
fi
321+
322+
local tar_output=$(eval "$tar_cmd" 2>&1)
323+
local tar_status=$?
324+
325+
if [ $tar_status -ne 0 ]; then
326+
echo "ERROR: Failed to create tarball" >&2
327+
echo "Tar command output:" >&2
328+
echo "$tar_output" >&2
329+
return 1
330+
fi
331+
rm -rf providers.d run.yaml
332+
333+
echo "Build artifacts tarball created: $export_dir/$tar_name"
334+
return 0
335+
}
336+
337+
# If export_dir is specified, copy all necessary files and exit
338+
if [ -n "$export_dir" ]; then
339+
if ! create_export_tarball "$export_dir" "$image_name" "$run_config" "$external_providers_dir" "$TEMP_DIR" "$BUILD_CONTEXT_DIR"; then
340+
exit 1
341+
fi
342+
exit 0
343+
fi
344+
273345
# Start building the CLI arguments
274346
CLI_ARGS=()
275347

tests/unit/distribution/test_build_path.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@
1616
def test_container_build_passes_path(monkeypatch, tmp_path):
1717
called_with = {}
1818

19-
def spy_build_image(cfg, build_file_path, image_name, template_or_config, run_config=None):
19+
def spy_build_image(cfg, build_file_path, image_name, template_or_config, run_config=None, export_dir=None):
2020
called_with["path"] = template_or_config
2121
called_with["run_config"] = run_config
22+
called_with["export_dir"] = export_dir
2223
return 0
2324

2425
monkeypatch.setattr(

0 commit comments

Comments
 (0)