run-compose.sh 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 ""
  64. echo "Examples:"
  65. echo " $0 --enable-gpu[count=1]"
  66. echo " $0 --enable-api[port=11435]"
  67. echo " $0 --enable-gpu[count=1] --enable-api[port=12345] --webui[port=3000]"
  68. echo ""
  69. echo "This script configures and runs a docker-compose setup with optional GPU support, API exposure, and web UI configuration."
  70. echo "About the gpu to use, the script automatically detects it using the "lspci" command."
  71. echo "In this case the gpu detected is: $(get_gpu_driver)"
  72. }
  73. # Default values
  74. gpu_count=1
  75. api_port=11435
  76. webui_port=3000
  77. # Function to extract value from the parameter
  78. extract_value() {
  79. echo "$1" | sed -E 's/.*\[.*=(.*)\].*/\1/; t; s/.*//'
  80. }
  81. # Check if no arguments were provided
  82. # if [ $# -eq 0 ]; then
  83. # usage
  84. # exit 1
  85. # fi
  86. # Parse arguments
  87. while [[ $# -gt 0 ]]; do
  88. key="$1"
  89. case $key in
  90. --enable-gpu*)
  91. enable_gpu=true
  92. value=$(extract_value "$key")
  93. gpu_count=${value:-1}
  94. ;;
  95. --enable-api*)
  96. enable_api=true
  97. value=$(extract_value "$key")
  98. api_port=${value:-11435}
  99. ;;
  100. --webui*)
  101. value=$(extract_value "$key")
  102. webui_port=${value:-3000}
  103. ;;
  104. -h|--help)
  105. usage
  106. exit
  107. ;;
  108. *)
  109. # Unknown option
  110. echo "Unknown option: $key"
  111. usage
  112. exit 1
  113. ;;
  114. esac
  115. shift # past argument or value
  116. done
  117. DEFAULT_COMPOSE_COMMAND="docker compose -f docker-compose.yaml"
  118. if [[ $enable_gpu == true ]]; then
  119. # Validate and process command-line arguments
  120. if [[ -n $gpu_count ]]; then
  121. if ! [[ $gpu_count =~ ^[0-9]+$ ]]; then
  122. echo "Invalid GPU count: $gpu_count"
  123. exit 1
  124. fi
  125. echo "Enabling GPU with $gpu_count GPUs"
  126. # Add your GPU allocation logic here
  127. export OLLAMA_GPU_DRIVER=$(get_gpu_driver)
  128. fi
  129. DEFAULT_COMPOSE_COMMAND+=" -f docker-compose.gpu.yaml"
  130. fi
  131. if [[ $enable_api == true ]]; then
  132. DEFAULT_COMPOSE_COMMAND+=" -f docker-compose.api.yaml"
  133. if [[ -n $api_port ]]; then
  134. export OLLAMA_WEBAPI_PORT=$api_port # Set OLLAMA_WEBAPI_PORT environment variable
  135. fi
  136. fi
  137. DEFAULT_COMPOSE_COMMAND+=" up -d > /dev/null 2>&1"
  138. # Recap of environment variables
  139. echo
  140. echo -e "${WHITE}${BOLD}Current Setup:${NC}"
  141. echo -e " ${GREEN}${BOLD}GPU Driver:${NC} ${OLLAMA_GPU_DRIVER:-Not Enabled}"
  142. echo -e " ${GREEN}${BOLD}WebAPI Port:${NC} ${OLLAMA_WEBAPI_PORT:-Not Enabled}"
  143. echo -e " ${GREEN}${BOLD}WebUI Port:${NC} $webui_port"
  144. echo
  145. # Ask for user acceptance
  146. echo -ne "${WHITE}${BOLD}Do you want to proceed with current setup? (Y/n): ${NC}"
  147. read -n1 -s choice
  148. if [[ $choice == "" || $choice == "y" ]]; then
  149. # Execute the command with the current user
  150. eval "docker compose down > /dev/null 2>&1; $DEFAULT_COMPOSE_COMMAND" &
  151. # Capture the background process PID
  152. PID=$!
  153. # Display the loading animation
  154. show_loading $PID
  155. # Wait for the command to finish
  156. wait $PID
  157. echo
  158. # Check exit status
  159. if [ $? -eq 0 ]; then
  160. echo -e "${GREEN}${BOLD}Compose project started successfully.${NC}"
  161. else
  162. echo -e "${RED}${BOLD}There was an error starting the compose project.${NC}"
  163. fi
  164. else
  165. echo "Aborted."
  166. fi
  167. echo