| 1 | #!/bin/bash |
| 2 | |
| 3 | # Copyright (c) Meta Platforms, Inc. and affiliates. |
| 4 | # All rights reserved. |
| 5 | # |
| 6 | # This source code is licensed under the terms described in the LICENSE file in |
| 7 | # the root directory of this source tree. |
| 8 | |
| 9 | LLAMA_MODELS_DIR=${LLAMA_MODELS_DIR:-} |
| 10 | LLAMA_STACK_DIR=${LLAMA_STACK_DIR:-} |
| 11 | TEST_PYPI_VERSION=${TEST_PYPI_VERSION:-} |
| 12 | |
| 13 | if [ -n "$LLAMA_STACK_DIR" ]; then |
| 14 | echo "Using llama-stack-dir=$LLAMA_STACK_DIR" |
| 15 | fi |
| 16 | if [ -n "$LLAMA_MODELS_DIR" ]; then |
| 17 | echo "Using llama-models-dir=$LLAMA_MODELS_DIR" |
| 18 | fi |
| 19 | |
| 20 | if [ "$#" -lt 2 ]; then |
| 21 | echo "Usage: $0 <distribution_type> <build_name> <pip_dependencies> [<special_pip_deps>]" >&2 |
| 22 | echo "Example: $0 <distribution_type> mybuild 'numpy pandas scipy'" >&2 |
| 23 | exit 1 |
| 24 | fi |
| 25 | |
| 26 | special_pip_deps="$3" |
| 27 | |
| 28 | set -euo pipefail |
| 29 | |
| 30 | build_name="$1" |
| 31 | env_name="llamastack-$build_name" |
| 32 | pip_dependencies="$2" |
| 33 | |
| 34 | # Define color codes |
| 35 | RED='\033[0;31m' |
| 36 | GREEN='\033[0;32m' |
| 37 | NC='\033[0m' # No Color |
| 38 | |
| 39 | # this is set if we actually create a new conda in which case we need to clean up |
| 40 | ENVNAME="" |
| 41 | |
| 42 | SCRIPT_DIR=$(dirname "$(readlink -f "$0")") |
| 43 | source "$SCRIPT_DIR/common.sh" |
| 44 | |
| 45 | ensure_conda_env_python310() { |
| 46 | local env_name="$1" |
| 47 | local pip_dependencies="$2" |
| 48 | local special_pip_deps="$3" |
| 49 | local python_version="3.10" |
| 50 | |
| 51 | # Check if conda command is available |
| 52 | if ! command -v conda &>/dev/null; then |
| 53 | printf "${RED}Error: conda command not found. Is Conda installed and in your PATH?${NC}" >&2 |
| 54 | exit 1 |
| 55 | fi |
| 56 | |
| 57 | # Check if the environment exists |
| 58 | if conda env list | grep -q "^${env_name} "; then |
| 59 | printf "Conda environment '${env_name}' exists. Checking Python version...\n" |
| 60 | |
| 61 | # Check Python version in the environment |
| 62 | current_version=$(conda run -n "${env_name}" python --version 2>&1 | cut -d' ' -f2 | cut -d'.' -f1,2) |
| 63 | |
| 64 | if [ "$current_version" = "$python_version" ]; then |
| 65 | printf "Environment '${env_name}' already has Python ${python_version}. No action needed.\n" |
| 66 | else |
| 67 | printf "Updating environment '${env_name}' to Python ${python_version}...\n" |
| 68 | conda install -n "${env_name}" python="${python_version}" -y |
| 69 | fi |
| 70 | else |
| 71 | printf "Conda environment '${env_name}' does not exist. Creating with Python ${python_version}...\n" |
| 72 | conda create -n "${env_name}" python="${python_version}" -y |
| 73 | |
| 74 | ENVNAME="${env_name}" |
| 75 | # setup_cleanup_handlers |
| 76 | fi |
| 77 | |
| 78 | eval "$(conda shell.bash hook)" |
| 79 | conda deactivate && conda activate "${env_name}" |
| 80 | |
| 81 | if [ -n "$TEST_PYPI_VERSION" ]; then |
| 82 | # these packages are damaged in test-pypi, so install them first |
| 83 | $CONDA_PREFIX/bin/pip install fastapi libcst |
| 84 | $CONDA_PREFIX/bin/pip install --extra-index-url https://test.pypi.org/simple/ \ |
| 85 | llama-models==$TEST_PYPI_VERSION llama-stack==$TEST_PYPI_VERSION \ |
| 86 | $pip_dependencies |
| 87 | if [ -n "$special_pip_deps" ]; then |
| 88 | $CONDA_PREFIX/bin/pip install --no-deps "$special_pip_deps" |
| 89 | fi |
| 90 | else |
| 91 | # Re-installing llama-stack in the new conda environment |
| 92 | if [ -n "$LLAMA_STACK_DIR" ]; then |
| 93 | if [ ! -d "$LLAMA_STACK_DIR" ]; then |
| 94 | printf "${RED}Warning: LLAMA_STACK_DIR is set but directory does not exist: $LLAMA_STACK_DIR${NC}\n" >&2 |
| 95 | exit 1 |
| 96 | fi |
| 97 | |
| 98 | printf "Installing from LLAMA_STACK_DIR: $LLAMA_STACK_DIR\n" |
| 99 | $CONDA_PREFIX/bin/pip install --no-cache-dir -e "$LLAMA_STACK_DIR" |
| 100 | else |
| 101 | $CONDA_PREFIX/bin/pip install --no-cache-dir llama-stack |
| 102 | fi |
| 103 | |
| 104 | if [ -n "$LLAMA_MODELS_DIR" ]; then |
| 105 | if [ ! -d "$LLAMA_MODELS_DIR" ]; then |
| 106 | printf "${RED}Warning: LLAMA_MODELS_DIR is set but directory does not exist: $LLAMA_MODELS_DIR${NC}\n" >&2 |
| 107 | exit 1 |
| 108 | fi |
| 109 | |
| 110 | printf "Installing from LLAMA_MODELS_DIR: $LLAMA_MODELS_DIR\n" |
| 111 | $CONDA_PREFIX/bin/pip uninstall -y llama-models |
| 112 | $CONDA_PREFIX/bin/pip install --no-cache-dir -e "$LLAMA_MODELS_DIR" |
| 113 | fi |
| 114 | |
| 115 | # Install pip dependencies |
| 116 | printf "Installing pip dependencies\n" |
| 117 | $CONDA_PREFIX/bin/pip install $pip_dependencies |
| 118 | if [ -n "$special_pip_deps" ]; then |
| 119 | IFS='#' read -ra parts <<< "$special_pip_deps" |
| 120 | for part in "${parts[@]}"; do |
| 121 | echo "$part" |
| 122 | $CONDA_PREFIX/bin/pip install $part |
| 123 | done |
| 124 | fi |
| 125 | fi |
| 126 | } |
| 127 | |
| 128 | ensure_conda_env_python310 "$env_name" "$pip_dependencies" "$special_pip_deps" |