install.sh 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. #!/bin/bash
  2. # Stable Diffusion REST Server Installation Script
  3. # This script installs the server as a systemd service
  4. set -e
  5. # Colors for output
  6. RED='\033[0;31m'
  7. GREEN='\033[0;32m'
  8. YELLOW='\033[1;33m'
  9. NC='\033[0m' # No Color
  10. # Default values
  11. SERVICE_NAME="stable-diffusion-rest"
  12. SERVICE_FILE="/etc/systemd/system/${SERVICE_NAME}.service"
  13. INSTALL_DIR="/opt/stable-diffusion-rest"
  14. DEFAULT_USER="sd-rest"
  15. CREATE_USER=false
  16. ENABLE_FILE_LOGGING=false
  17. LOG_DIR="/var/log/stable-diffusion-rest"
  18. LOG_FILE="${LOG_DIR}/server.log"
  19. QUEUE_DIR="/var/lib/stable-diffusion-rest/queue"
  20. OUTPUT_DIR="/var/lib/stable-diffusion-rest/output"
  21. MODELS_DIR=""
  22. USER_SPECIFIED=""
  23. NO_SYSTEM_USER=false
  24. CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  25. # Function to print colored output
  26. print_info() {
  27. echo -e "${GREEN}[INFO]${NC} $1"
  28. }
  29. print_warning() {
  30. echo -e "${YELLOW}[WARNING]${NC} $1"
  31. }
  32. print_error() {
  33. echo -e "${RED}[ERROR]${NC} $1"
  34. }
  35. # Function to check if script is run as root
  36. check_root() {
  37. if [[ $EUID -ne 0 ]]; then
  38. print_error "This script must be run as root (use sudo)"
  39. exit 1
  40. fi
  41. }
  42. # Function to parse command line arguments
  43. parse_args() {
  44. while [[ $# -gt 0 ]]; do
  45. case $1 in
  46. --user)
  47. USER_SPECIFIED="$2"
  48. shift 2
  49. ;;
  50. --no-system-user)
  51. NO_SYSTEM_USER=true
  52. shift
  53. ;;
  54. --models-dir)
  55. MODELS_DIR="$2"
  56. shift 2
  57. ;;
  58. --install-dir)
  59. INSTALL_DIR="$2"
  60. shift 2
  61. ;;
  62. --help)
  63. echo "Usage: $0 [options]"
  64. echo ""
  65. echo "Options:"
  66. echo " --user <username> Specify system user to run the service (default: sd-rest)"
  67. echo " --no-system-user Do not ask for user, use current user"
  68. echo " --models-dir <path> Path to models directory (required)"
  69. echo " --install-dir <path> Installation directory (default: /opt/stable-diffusion-rest)"
  70. echo " --help Show this help message"
  71. echo ""
  72. echo "Example:"
  73. echo " sudo $0 --user sd-rest --models-dir /data/SD_MODELS"
  74. exit 0
  75. ;;
  76. *)
  77. print_error "Unknown option: $1"
  78. echo "Use --help for usage information"
  79. exit 1
  80. ;;
  81. esac
  82. done
  83. }
  84. # Function to ask for system user
  85. ask_for_user() {
  86. if [[ -n "$USER_SPECIFIED" ]]; then
  87. DEFAULT_USER="$USER_SPECIFIED"
  88. elif [[ "$NO_SYSTEM_USER" == true ]]; then
  89. DEFAULT_USER="$SUDO_USER"
  90. if [[ -z "$DEFAULT_USER" ]]; then
  91. DEFAULT_USER=$(whoami)
  92. fi
  93. print_info "Using current user: $DEFAULT_USER"
  94. return
  95. fi
  96. echo ""
  97. read -p "Enter system user to run the service [$DEFAULT_USER]: " input_user
  98. if [[ -n "$input_user" ]]; then
  99. DEFAULT_USER="$input_user"
  100. fi
  101. # Check if user exists
  102. if ! id "$DEFAULT_USER" &>/dev/null; then
  103. print_warning "User '$DEFAULT_USER' does not exist"
  104. read -p "Create user '$DEFAULT_USER'? (y/n): " create_choice
  105. if [[ "$create_choice" =~ ^[Yy]$ ]]; then
  106. CREATE_USER=true
  107. else
  108. print_error "Cannot proceed without a valid user"
  109. exit 1
  110. fi
  111. fi
  112. }
  113. # Function to ask for models directory
  114. ask_for_models_dir() {
  115. if [[ -z "$MODELS_DIR" ]]; then
  116. echo ""
  117. read -p "Enter path to models directory: " MODELS_DIR
  118. if [[ -z "$MODELS_DIR" ]]; then
  119. print_error "Models directory is required"
  120. exit 1
  121. fi
  122. fi
  123. if [[ ! -d "$MODELS_DIR" ]]; then
  124. print_error "Models directory does not exist: $MODELS_DIR"
  125. exit 1
  126. fi
  127. }
  128. # Function to ask for file logging
  129. ask_for_file_logging() {
  130. echo ""
  131. read -p "Enable file-based logging? (y/n) [n]: " logging_choice
  132. if [[ "$logging_choice" =~ ^[Yy]$ ]]; then
  133. ENABLE_FILE_LOGGING=true
  134. read -p "Enter log file path [$LOG_FILE]: " input_log
  135. if [[ -n "$input_log" ]]; then
  136. LOG_FILE="$input_log"
  137. fi
  138. LOG_DIR=$(dirname "$LOG_FILE")
  139. fi
  140. }
  141. # Function to create system user
  142. create_system_user() {
  143. if [[ "$CREATE_USER" == true ]]; then
  144. print_info "Creating system user: $DEFAULT_USER"
  145. useradd --system --no-create-home --shell /sbin/nologin "$DEFAULT_USER" || true
  146. fi
  147. }
  148. # Function to create directories
  149. create_directories() {
  150. print_info "Creating directories..."
  151. mkdir -p "$INSTALL_DIR"
  152. mkdir -p "$QUEUE_DIR"
  153. mkdir -p "$OUTPUT_DIR"
  154. if [[ "$ENABLE_FILE_LOGGING" == true ]]; then
  155. mkdir -p "$LOG_DIR"
  156. chown -R "$DEFAULT_USER:$DEFAULT_USER" "$LOG_DIR"
  157. fi
  158. chown -R "$DEFAULT_USER:$DEFAULT_USER" "$QUEUE_DIR"
  159. chown -R "$DEFAULT_USER:$DEFAULT_USER" "$OUTPUT_DIR"
  160. }
  161. # Function to copy binary and files
  162. install_files() {
  163. print_info "Installing server binary..."
  164. # Check if binary exists in build directory
  165. if [[ ! -f "$CURRENT_DIR/build/src/stable-diffusion-rest-server" ]]; then
  166. print_error "Server binary not found. Please build the project first:"
  167. print_error " mkdir -p build && cd build"
  168. print_error " cmake .."
  169. print_error " cmake --build . --parallel"
  170. exit 1
  171. fi
  172. # Copy binary
  173. cp "$CURRENT_DIR/build/src/stable-diffusion-rest-server" "$INSTALL_DIR/"
  174. chmod +x "$INSTALL_DIR/stable-diffusion-rest-server"
  175. # Copy sd binary if it exists
  176. if [[ -f "$CURRENT_DIR/build/stable-diffusion.cpp-install/bin/sd" ]]; then
  177. mkdir -p "$INSTALL_DIR/bin"
  178. cp "$CURRENT_DIR/build/stable-diffusion.cpp-install/bin/sd" "$INSTALL_DIR/bin/"
  179. chmod +x "$INSTALL_DIR/bin/sd"
  180. fi
  181. chown -R "$DEFAULT_USER:$DEFAULT_USER" "$INSTALL_DIR"
  182. }
  183. # Function to generate systemd service file
  184. generate_service_file() {
  185. print_info "Generating systemd service file..."
  186. # Build logging options
  187. LOGGING_OPTIONS=""
  188. if [[ "$ENABLE_FILE_LOGGING" == true ]]; then
  189. LOGGING_OPTIONS="--enable-file-logging --log-file $LOG_FILE"
  190. fi
  191. # Replace placeholders in template
  192. sed -e "s|{{USER}}|$DEFAULT_USER|g" \
  193. -e "s|{{INSTALL_DIR}}|$INSTALL_DIR|g" \
  194. -e "s|{{MODELS_DIR}}|$MODELS_DIR|g" \
  195. -e "s|{{QUEUE_DIR}}|$QUEUE_DIR|g" \
  196. -e "s|{{OUTPUT_DIR}}|$OUTPUT_DIR|g" \
  197. -e "s|{{LOG_DIR}}|$LOG_DIR|g" \
  198. -e "s|{{LOGGING_OPTIONS}}|$LOGGING_OPTIONS|g" \
  199. "$CURRENT_DIR/stable-diffusion-rest.service.template" > "$SERVICE_FILE"
  200. print_info "Service file created: $SERVICE_FILE"
  201. }
  202. # Function to enable and start service
  203. setup_service() {
  204. print_info "Reloading systemd daemon..."
  205. systemctl daemon-reload
  206. echo ""
  207. read -p "Enable service to start on boot? (y/n) [y]: " enable_choice
  208. if [[ -z "$enable_choice" || "$enable_choice" =~ ^[Yy]$ ]]; then
  209. systemctl enable "$SERVICE_NAME"
  210. print_info "Service enabled"
  211. fi
  212. echo ""
  213. read -p "Start service now? (y/n) [y]: " start_choice
  214. if [[ -z "$start_choice" || "$start_choice" =~ ^[Yy]$ ]]; then
  215. systemctl start "$SERVICE_NAME"
  216. print_info "Service started"
  217. # Show status
  218. sleep 2
  219. systemctl status "$SERVICE_NAME" --no-pager -l
  220. fi
  221. }
  222. # Main installation flow
  223. main() {
  224. echo "============================================"
  225. echo " Stable Diffusion REST Server Installer"
  226. echo "============================================"
  227. echo ""
  228. check_root
  229. parse_args "$@"
  230. ask_for_user
  231. ask_for_models_dir
  232. ask_for_file_logging
  233. echo ""
  234. print_info "Installation Summary:"
  235. print_info " User: $DEFAULT_USER"
  236. print_info " Install Dir: $INSTALL_DIR"
  237. print_info " Models Dir: $MODELS_DIR"
  238. print_info " Queue Dir: $QUEUE_DIR"
  239. print_info " Output Dir: $OUTPUT_DIR"
  240. print_info " File Logging: $ENABLE_FILE_LOGGING"
  241. if [[ "$ENABLE_FILE_LOGGING" == true ]]; then
  242. print_info " Log File: $LOG_FILE"
  243. fi
  244. echo ""
  245. read -p "Proceed with installation? (y/n): " proceed
  246. if [[ ! "$proceed" =~ ^[Yy]$ ]]; then
  247. print_warning "Installation cancelled"
  248. exit 0
  249. fi
  250. create_system_user
  251. create_directories
  252. install_files
  253. generate_service_file
  254. setup_service
  255. echo ""
  256. print_info "============================================"
  257. print_info " Installation Complete!"
  258. print_info "============================================"
  259. echo ""
  260. print_info "Service commands:"
  261. print_info " Start: systemctl start $SERVICE_NAME"
  262. print_info " Stop: systemctl stop $SERVICE_NAME"
  263. print_info " Restart: systemctl restart $SERVICE_NAME"
  264. print_info " Status: systemctl status $SERVICE_NAME"
  265. print_info " Logs: journalctl -u $SERVICE_NAME -f"
  266. echo ""
  267. }
  268. main "$@"