run-compose.sh 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #!/bin/bash
  2. # Define color and formatting codes
  3. BOLD='\033[1m'
  4. GREEN='\033[1;32m'
  5. WHITE='\033[1;37m'
  6. RED='\033[0;31m'
  7. NC='\033[0m' # No Color
  8. # Unicode character for tick mark
  9. TICK='\u2713'
  10. # Detect GPU driver
  11. get_gpu_driver() {
  12. # Detect NVIDIA GPUs
  13. if lspci | grep -i nvidia >/dev/null; then
  14. echo "nvidia"
  15. return
  16. fi
  17. # Detect AMD GPUs (including GCN architecture check for amdgpu vs radeon)
  18. if lspci | grep -i amd >/dev/null; then
  19. # List of known GCN and later architecture cards
  20. # This is a simplified list, and in a real-world scenario, you'd want a more comprehensive one
  21. local gcn_and_later=("Radeon HD 7000" "Radeon HD 8000" "Radeon R5" "Radeon R7" "Radeon R9" "Radeon RX")
  22. # Get GPU information
  23. local gpu_info=$(lspci | grep -i 'vga.*amd')
  24. for model in "${gcn_and_later[@]}"; do
  25. if echo "$gpu_info" | grep -iq "$model"; then
  26. echo "amdgpu"
  27. return
  28. fi
  29. done
  30. # Default to radeon if no GCN or later architecture is detected
  31. echo "radeon"
  32. return
  33. fi
  34. # Detect Intel GPUs
  35. if lspci | grep -i intel >/dev/null; then
  36. echo "i915"
  37. return
  38. fi
  39. # If no known GPU is detected
  40. echo "Unknown or unsupported GPU driver"
  41. exit 1
  42. }
  43. # Function for rolling animation
  44. show_loading() {
  45. local spin='-\|/'
  46. local i=0
  47. printf " "
  48. while kill -0 $1 2>/dev/null; do
  49. i=$(( (i+1) %4 ))
  50. printf "\b${spin:$i:1}"
  51. sleep .1
  52. done
  53. # Replace the spinner with a tick
  54. printf "\b${GREEN}${TICK}${NC}"
  55. }
  56. # Usage information
  57. usage() {
  58. echo "Usage: $0 [OPTIONS]"
  59. echo "Options:"
  60. echo " --enable-gpu[count=COUNT] Enable GPU support with the specified count."
  61. echo " --enable-api[port=PORT] Enable API and expose it on the specified port."
  62. echo " --webui[port=PORT] Set the port for the web user interface."
  63. echo " --data[folder=PATH] Bind mount for ollama data folder (by default will create the 'ollama' volume)."
  64. echo " --build Build the docker image before running the compose project."
  65. echo " -q, --quiet Run script in headless mode."
  66. echo " -h, --help Show this help message."
  67. echo ""
  68. echo "Examples:"
  69. echo " $0 --enable-gpu[count=1]"
  70. echo " $0 --enable-api[port=11435]"
  71. echo " $0 --enable-gpu[count=1] --enable-api[port=12345] --webui[port=3000]"
  72. echo " $0 --enable-gpu[count=1] --enable-api[port=12345] --webui[port=3000] --data[folder=./ollama-data]"
  73. echo ""
  74. echo "This script configures and runs a docker-compose setup with optional GPU support, API exposure, and web UI configuration."
  75. echo "About the gpu to use, the script automatically detects it using the "lspci" command."
  76. echo "In this case the gpu detected is: $(get_gpu_driver)"
  77. }
  78. # Default values
  79. gpu_count=1
  80. api_port=11435
  81. webui_port=3000
  82. headless=false
  83. build_image=false
  84. # Function to extract value from the parameter
  85. extract_value() {
  86. echo "$1" | sed -E 's/.*\[.*=(.*)\].*/\1/; t; s/.*//'
  87. }
  88. # Parse arguments
  89. while [[ $# -gt 0 ]]; do
  90. key="$1"
  91. case $key in
  92. --enable-gpu*)
  93. enable_gpu=true
  94. value=$(extract_value "$key")
  95. gpu_count=${value:-1}
  96. ;;
  97. --enable-api*)
  98. enable_api=true
  99. value=$(extract_value "$key")
  100. api_port=${value:-11435}
  101. ;;
  102. --webui*)
  103. value=$(extract_value "$key")
  104. webui_port=${value:-3000}
  105. ;;
  106. --data*)
  107. value=$(extract_value "$key")
  108. data_dir=${value:-"./ollama-data"}
  109. ;;
  110. --build)
  111. build_image=true
  112. ;;
  113. -q|--quiet)
  114. headless=true
  115. ;;
  116. -h|--help)
  117. usage
  118. exit
  119. ;;
  120. *)
  121. # Unknown option
  122. echo "Unknown option: $key"
  123. usage
  124. exit 1
  125. ;;
  126. esac
  127. shift # past argument or value
  128. done
  129. DEFAULT_COMPOSE_COMMAND="docker compose -f docker-compose.yaml"
  130. if [[ $enable_gpu == true ]]; then
  131. # Validate and process command-line arguments
  132. if [[ -n $gpu_count ]]; then
  133. if ! [[ $gpu_count =~ ^[0-9]+$ ]]; then
  134. echo "Invalid GPU count: $gpu_count"
  135. exit 1
  136. fi
  137. echo "Enabling GPU with $gpu_count GPUs"
  138. # Add your GPU allocation logic here
  139. export OLLAMA_GPU_DRIVER=$(get_gpu_driver)
  140. export OLLAMA_GPU_COUNT=$gpu_count # Set OLLAMA_GPU_COUNT environment variable
  141. fi
  142. DEFAULT_COMPOSE_COMMAND+=" -f docker-compose.gpu.yaml"
  143. fi
  144. if [[ $enable_api == true ]]; then
  145. DEFAULT_COMPOSE_COMMAND+=" -f docker-compose.api.yaml"
  146. if [[ -n $api_port ]]; then
  147. export OLLAMA_WEBAPI_PORT=$api_port # Set OLLAMA_WEBAPI_PORT environment variable
  148. fi
  149. fi
  150. if [[ -n $data_dir ]]; then
  151. DEFAULT_COMPOSE_COMMAND+=" -f docker-compose.data.yaml"
  152. export OLLAMA_DATA_DIR=$data_dir # Set OLLAMA_DATA_DIR environment variable
  153. fi
  154. DEFAULT_COMPOSE_COMMAND+=" up -d"
  155. DEFAULT_COMPOSE_COMMAND+=" --remove-orphans"
  156. DEFAULT_COMPOSE_COMMAND+=" --force-recreate"
  157. if [[ -n $build_image ]]; then
  158. DEFAULT_COMPOSE_COMMAND+=" --build"
  159. fi
  160. DEFAULT_COMPOSE_COMMAND+=" > /dev/null 2>&1"
  161. # Recap of environment variables
  162. echo
  163. echo -e "${WHITE}${BOLD}Current Setup:${NC}"
  164. echo -e " ${GREEN}${BOLD}GPU Driver:${NC} ${OLLAMA_GPU_DRIVER:-Not Enabled}"
  165. echo -e " ${GREEN}${BOLD}GPU Count:${NC} ${OLLAMA_GPU_COUNT:-Not Enabled}"
  166. echo -e " ${GREEN}${BOLD}WebAPI Port:${NC} ${OLLAMA_WEBAPI_PORT:-Not Enabled}"
  167. echo -e " ${GREEN}${BOLD}Data Folder:${NC} ${data_dir:-Using ollama volume}"
  168. echo -e " ${GREEN}${BOLD}WebUI Port:${NC} $webui_port"
  169. echo
  170. if [[ $headless == true ]]; then
  171. echo -ne "${WHITE}${BOLD}Running in headless mode... ${NC}"
  172. choice="y"
  173. else
  174. # Ask for user acceptance
  175. echo -ne "${WHITE}${BOLD}Do you want to proceed with current setup? (Y/n): ${NC}"
  176. read -n1 -s choice
  177. fi
  178. echo -e " ${GREEN}${BOLD}WebUI Port:${NC} $webui_port"
  179. echo
  180. # Ask for user acceptance
  181. echo -ne "${WHITE}${BOLD}Do you want to proceed with current setup? (Y/n): ${NC}"
  182. read -n1 -s choice
  183. if [[ $choice == "" || $choice == "y" ]]; then
  184. # Execute the command with the current user
  185. eval "$DEFAULT_COMPOSE_COMMAND" &
  186. # Capture the background process PID
  187. PID=$!
  188. # Display the loading animation
  189. show_loading $PID
  190. # Wait for the command to finish
  191. wait $PID
  192. echo
  193. # Check exit status
  194. if [ $? -eq 0 ]; then
  195. echo -e "${GREEN}${BOLD}Compose project started successfully.${NC}"
  196. else
  197. echo -e "${RED}${BOLD}There was an error starting the compose project.${NC}"
  198. fi
  199. else
  200. echo "Aborted."
  201. fi
  202. echo