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