run-compose.sh 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 using lspci or nvidia-smi
  13. if lspci | grep -i nvidia >/dev/null || nvidia-smi >/dev/null 2>&1; 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 " --playwright Enable Playwright support for web scraping."
  65. echo " --build Build the docker image before running the compose project."
  66. echo " --drop Drop the compose project."
  67. echo " -q, --quiet Run script in headless mode."
  68. echo " -h, --help Show this help message."
  69. echo ""
  70. echo "Examples:"
  71. echo " $0 --drop"
  72. echo " $0 --enable-gpu[count=1]"
  73. echo " $0 --enable-gpu[count=all]"
  74. echo " $0 --enable-api[port=11435]"
  75. echo " $0 --enable-gpu[count=1] --enable-api[port=12345] --webui[port=3000]"
  76. echo " $0 --enable-gpu[count=1] --enable-api[port=12345] --webui[port=3000] --data[folder=./ollama-data]"
  77. echo " $0 --enable-gpu[count=1] --enable-api[port=12345] --webui[port=3000] --data[folder=./ollama-data] --build"
  78. echo ""
  79. echo "This script configures and runs a docker-compose setup with optional GPU support, API exposure, and web UI configuration."
  80. echo "About the gpu to use, the script automatically detects it using the "lspci" command."
  81. echo "In this case the gpu detected is: $(get_gpu_driver)"
  82. }
  83. # Default values
  84. gpu_count=1
  85. api_port=11435
  86. webui_port=3000
  87. headless=false
  88. build_image=false
  89. kill_compose=false
  90. enable_playwright=false
  91. # Function to extract value from the parameter
  92. extract_value() {
  93. echo "$1" | sed -E 's/.*\[.*=(.*)\].*/\1/; t; s/.*//'
  94. }
  95. # Parse arguments
  96. while [[ $# -gt 0 ]]; do
  97. key="$1"
  98. case $key in
  99. --enable-gpu*)
  100. enable_gpu=true
  101. value=$(extract_value "$key")
  102. gpu_count=${value:-1}
  103. ;;
  104. --enable-api*)
  105. enable_api=true
  106. value=$(extract_value "$key")
  107. api_port=${value:-11435}
  108. ;;
  109. --webui*)
  110. value=$(extract_value "$key")
  111. webui_port=${value:-3000}
  112. ;;
  113. --data*)
  114. value=$(extract_value "$key")
  115. data_dir=${value:-"./ollama-data"}
  116. ;;
  117. --playwright)
  118. enable_playwright=true
  119. ;;
  120. --drop)
  121. kill_compose=true
  122. ;;
  123. --build)
  124. build_image=true
  125. ;;
  126. -q|--quiet)
  127. headless=true
  128. ;;
  129. -h|--help)
  130. usage
  131. exit
  132. ;;
  133. *)
  134. # Unknown option
  135. echo "Unknown option: $key"
  136. usage
  137. exit 1
  138. ;;
  139. esac
  140. shift # past argument or value
  141. done
  142. if [[ $kill_compose == true ]]; then
  143. docker compose down --remove-orphans
  144. echo -e "${GREEN}${BOLD}Compose project dropped successfully.${NC}"
  145. exit
  146. else
  147. DEFAULT_COMPOSE_COMMAND="docker compose -f docker-compose.yaml"
  148. if [[ $enable_gpu == true ]]; then
  149. # Validate and process command-line arguments
  150. if [[ -n $gpu_count ]]; then
  151. if ! [[ $gpu_count =~ ^([0-9]+|all)$ ]]; then
  152. echo "Invalid GPU count: $gpu_count"
  153. exit 1
  154. fi
  155. echo "Enabling GPU with $gpu_count GPUs"
  156. # Add your GPU allocation logic here
  157. export OLLAMA_GPU_DRIVER=$(get_gpu_driver)
  158. export OLLAMA_GPU_COUNT=$gpu_count # Set OLLAMA_GPU_COUNT environment variable
  159. fi
  160. DEFAULT_COMPOSE_COMMAND+=" -f docker-compose.gpu.yaml"
  161. fi
  162. if [[ $enable_api == true ]]; then
  163. DEFAULT_COMPOSE_COMMAND+=" -f docker-compose.api.yaml"
  164. if [[ -n $api_port ]]; then
  165. export OLLAMA_WEBAPI_PORT=$api_port # Set OLLAMA_WEBAPI_PORT environment variable
  166. fi
  167. fi
  168. if [[ -n $data_dir ]]; then
  169. DEFAULT_COMPOSE_COMMAND+=" -f docker-compose.data.yaml"
  170. export OLLAMA_DATA_DIR=$data_dir # Set OLLAMA_DATA_DIR environment variable
  171. fi
  172. if [[ $enable_playwright == true ]]; then
  173. DEFAULT_COMPOSE_COMMAND+=" -f docker-compose.playwright.yaml"
  174. fi
  175. if [[ -n $webui_port ]]; then
  176. export OPEN_WEBUI_PORT=$webui_port # Set OPEN_WEBUI_PORT environment variable
  177. fi
  178. DEFAULT_COMPOSE_COMMAND+=" up -d"
  179. DEFAULT_COMPOSE_COMMAND+=" --remove-orphans"
  180. DEFAULT_COMPOSE_COMMAND+=" --force-recreate"
  181. if [[ $build_image == true ]]; then
  182. DEFAULT_COMPOSE_COMMAND+=" --build"
  183. fi
  184. fi
  185. # Recap of environment variables
  186. echo
  187. echo -e "${WHITE}${BOLD}Current Setup:${NC}"
  188. echo -e " ${GREEN}${BOLD}GPU Driver:${NC} ${OLLAMA_GPU_DRIVER:-Not Enabled}"
  189. echo -e " ${GREEN}${BOLD}GPU Count:${NC} ${OLLAMA_GPU_COUNT:-Not Enabled}"
  190. echo -e " ${GREEN}${BOLD}WebAPI Port:${NC} ${OLLAMA_WEBAPI_PORT:-Not Enabled}"
  191. echo -e " ${GREEN}${BOLD}Data Folder:${NC} ${data_dir:-Using ollama volume}"
  192. echo -e " ${GREEN}${BOLD}WebUI Port:${NC} $webui_port"
  193. echo -e " ${GREEN}${BOLD}Playwright:${NC} ${enable_playwright:-false}"
  194. echo
  195. if [[ $headless == true ]]; then
  196. echo -ne "${WHITE}${BOLD}Running in headless mode... ${NC}"
  197. choice="y"
  198. else
  199. # Ask for user acceptance
  200. echo -ne "${WHITE}${BOLD}Do you want to proceed with current setup? (Y/n): ${NC}"
  201. read -n1 -s choice
  202. fi
  203. echo
  204. if [[ $choice == "" || $choice == "y" ]]; then
  205. # Execute the command with the current user
  206. eval "$DEFAULT_COMPOSE_COMMAND" &
  207. # Capture the background process PID
  208. PID=$!
  209. # Display the loading animation
  210. #show_loading $PID
  211. # Wait for the command to finish
  212. wait $PID
  213. echo
  214. # Check exit status
  215. if [ $? -eq 0 ]; then
  216. echo -e "${GREEN}${BOLD}Compose project started successfully.${NC}"
  217. else
  218. echo -e "${RED}${BOLD}There was an error starting the compose project.${NC}"
  219. fi
  220. else
  221. echo "Aborted."
  222. fi
  223. echo