| 1 | #!/bin/bash |
| 2 | |
| 3 | LLAMA_MODELS_DIR=${LLAMA_MODELS_DIR:-} |
| 4 | LLAMA_TOOLCHAIN_DIR=${LLAMA_TOOLCHAIN_DIR:-} |
| 5 | TEST_PYPI_VERSION=${TEST_PYPI_VERSION:-} |
| 6 | |
| 7 | if [ "$#" -ne 4 ]; then |
| 8 | echo "Usage: $0 <distribution_type> <build_name> <docker_base> <pip_dependencies> |
| 9 | echo "Example: $0 distribution_type my-fastapi-app python:3.9-slim 'fastapi uvicorn' |
| 10 | exit 1 |
| 11 | fi |
| 12 | |
| 13 | distribution_type=$1 |
| 14 | build_name="$2" |
| 15 | image_name="llamastack-$build_name" |
| 16 | docker_base=$3 |
| 17 | pip_dependencies=$4 |
| 18 | |
| 19 | # Define color codes |
| 20 | RED='\033[0;31m' |
| 21 | GREEN='\033[0;32m' |
| 22 | NC='\033[0m' # No Color |
| 23 | |
| 24 | set -euo pipefail |
| 25 | |
| 26 | SCRIPT_DIR=$(dirname "$(readlink -f "$0")") |
| 27 | REPO_DIR=$(dirname $(dirname "$SCRIPT_DIR")) |
| 28 | |
| 29 | TEMP_DIR=$(mktemp -d) |
| 30 | |
| 31 | add_to_docker() { |
| 32 | local input |
| 33 | output_file="$TEMP_DIR/Dockerfile" |
| 34 | if [ -t 0 ]; then |
| 35 | printf '%s\n' "$1" >>"$output_file" |
| 36 | else |
| 37 | # If stdin is not a terminal, read from it (heredoc) |
| 38 | cat >>"$output_file" |
| 39 | fi |
| 40 | } |
| 41 | |
| 42 | add_to_docker <<EOF |
| 43 | FROM $docker_base |
| 44 | WORKDIR /app |
| 45 | |
| 46 | RUN apt-get update && apt-get install -y \ |
| 47 | iputils-ping net-tools iproute2 dnsutils telnet \ |
| 48 | curl wget telnet \ |
| 49 | procps psmisc lsof \ |
| 50 | traceroute \ |
| 51 | bubblewrap \ |
| 52 | && rm -rf /var/lib/apt/lists/* |
| 53 | |
| 54 | EOF |
| 55 | |
| 56 | toolchain_mount="/app/llama-toolchain-source" |
| 57 | models_mount="/app/llama-models-source" |
| 58 | |
| 59 | if [ -n "$LLAMA_TOOLCHAIN_DIR" ]; then |
| 60 | if [ ! -d "$LLAMA_TOOLCHAIN_DIR" ]; then |
| 61 | echo "${RED}Warning: LLAMA_TOOLCHAIN_DIR is set but directory does not exist: $LLAMA_TOOLCHAIN_DIR${NC}" >&2 |
| 62 | exit 1 |
| 63 | fi |
| 64 | add_to_docker "RUN pip install $toolchain_mount" |
| 65 | else |
| 66 | add_to_docker "RUN pip install llama-toolchain" |
| 67 | fi |
| 68 | |
| 69 | if [ -n "$LLAMA_MODELS_DIR" ]; then |
| 70 | if [ ! -d "$LLAMA_MODELS_DIR" ]; then |
| 71 | echo "${RED}Warning: LLAMA_MODELS_DIR is set but directory does not exist: $LLAMA_MODELS_DIR${NC}" >&2 |
| 72 | exit 1 |
| 73 | fi |
| 74 | |
| 75 | add_to_docker <<EOF |
| 76 | RUN pip uninstall -y llama-models |
| 77 | RUN pip install $models_mount |
| 78 | |
| 79 | EOF |
| 80 | fi |
| 81 | |
| 82 | if [ -n "$pip_dependencies" ]; then |
| 83 | add_to_docker "RUN pip install $pip_dependencies" |
| 84 | fi |
| 85 | |
| 86 | add_to_docker <<EOF |
| 87 | |
| 88 | # This would be good in production but for debugging flexibility lets not add it right now |
| 89 | # We need a more solid production ready entrypoint.sh anyway |
| 90 | # |
| 91 | # ENTRYPOINT ["python", "-m", "llama_toolchain.core.server"] |
| 92 | |
| 93 | EOF |
| 94 | |
| 95 | printf "Dockerfile created successfully in $TEMP_DIR/Dockerfile" |
| 96 | cat $TEMP_DIR/Dockerfile |
| 97 | printf "\n" |
| 98 | |
| 99 | mounts="" |
| 100 | if [ -n "$LLAMA_TOOLCHAIN_DIR" ]; then |
| 101 | mounts="$mounts -v $(readlink -f $LLAMA_TOOLCHAIN_DIR):$toolchain_mount" |
| 102 | fi |
| 103 | if [ -n "$LLAMA_MODELS_DIR" ]; then |
| 104 | mounts="$mounts -v $(readlink -f $LLAMA_MODELS_DIR):$models_mount" |
| 105 | fi |
| 106 | set -x |
| 107 | podman build -t $image_name -f "$TEMP_DIR/Dockerfile" "$REPO_DIR" $mounts |
| 108 | set +x |
| 109 | |
| 110 | printf "${GREEN}Succesfully setup Podman image. Configuring build...${NC}" |
| 111 | echo "You can run it with: podman run -p 8000:8000 $image_name" |
| 112 | |
| 113 | $CONDA_PREFIX/bin/python3 -m llama_toolchain.cli.llama stack configure $distribution_type --name "$build_name" --type container |