uninstall.sh 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #!/bin/bash
  2. # Stable Diffusion REST Server Uninstallation Script
  3. # This script removes the systemd service and optionally cleans up files
  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. LOG_DIR="/var/log/stable-diffusion-rest"
  15. QUEUE_DIR="/var/lib/stable-diffusion-rest/queue"
  16. OUTPUT_DIR="/var/lib/stable-diffusion-rest/output"
  17. DATA_DIR="/var/lib/stable-diffusion-rest"
  18. REMOVE_DATA=false
  19. REMOVE_USER=false
  20. USER_TO_REMOVE=""
  21. # Function to print colored output
  22. print_info() {
  23. echo -e "${GREEN}[INFO]${NC} $1"
  24. }
  25. print_warning() {
  26. echo -e "${YELLOW}[WARNING]${NC} $1"
  27. }
  28. print_error() {
  29. echo -e "${RED}[ERROR]${NC} $1"
  30. }
  31. # Function to check if script is run as root
  32. check_root() {
  33. if [[ $EUID -ne 0 ]]; then
  34. print_error "This script must be run as root (use sudo)"
  35. exit 1
  36. fi
  37. }
  38. # Function to parse command line arguments
  39. parse_args() {
  40. while [[ $# -gt 0 ]]; do
  41. case $1 in
  42. --remove-data)
  43. REMOVE_DATA=true
  44. shift
  45. ;;
  46. --remove-user)
  47. REMOVE_USER=true
  48. shift
  49. ;;
  50. --help)
  51. echo "Usage: $0 [options]"
  52. echo ""
  53. echo "Options:"
  54. echo " --remove-data Also remove queue, output, and log directories"
  55. echo " --remove-user Also remove the system user (asks for confirmation)"
  56. echo " --help Show this help message"
  57. echo ""
  58. echo "Example:"
  59. echo " sudo $0"
  60. echo " sudo $0 --remove-data --remove-user"
  61. exit 0
  62. ;;
  63. *)
  64. print_error "Unknown option: $1"
  65. echo "Use --help for usage information"
  66. exit 1
  67. ;;
  68. esac
  69. done
  70. }
  71. # Function to stop and disable service
  72. stop_service() {
  73. if systemctl is-active --quiet "$SERVICE_NAME"; then
  74. print_info "Stopping service..."
  75. systemctl stop "$SERVICE_NAME"
  76. fi
  77. if systemctl is-enabled --quiet "$SERVICE_NAME" 2>/dev/null; then
  78. print_info "Disabling service..."
  79. systemctl disable "$SERVICE_NAME"
  80. fi
  81. }
  82. # Function to remove service file
  83. remove_service() {
  84. if [[ -f "$SERVICE_FILE" ]]; then
  85. print_info "Removing service file..."
  86. # Extract user from service file before removing
  87. if [[ -f "$SERVICE_FILE" ]]; then
  88. USER_TO_REMOVE=$(grep "^User=" "$SERVICE_FILE" | cut -d= -f2)
  89. fi
  90. rm -f "$SERVICE_FILE"
  91. systemctl daemon-reload
  92. else
  93. print_warning "Service file not found: $SERVICE_FILE"
  94. fi
  95. }
  96. # Function to remove installation directory
  97. remove_install_dir() {
  98. if [[ -d "$INSTALL_DIR" ]]; then
  99. print_info "Removing installation directory: $INSTALL_DIR"
  100. rm -rf "$INSTALL_DIR"
  101. fi
  102. }
  103. # Function to remove data directories
  104. remove_data_dirs() {
  105. if [[ "$REMOVE_DATA" == true ]]; then
  106. echo ""
  107. print_warning "This will remove all queue data, generated outputs, and logs"
  108. read -p "Are you sure? (y/n): " confirm
  109. if [[ "$confirm" =~ ^[Yy]$ ]]; then
  110. if [[ -d "$LOG_DIR" ]]; then
  111. print_info "Removing log directory: $LOG_DIR"
  112. rm -rf "$LOG_DIR"
  113. fi
  114. if [[ -d "$DATA_DIR" ]]; then
  115. print_info "Removing data directory: $DATA_DIR"
  116. rm -rf "$DATA_DIR"
  117. fi
  118. fi
  119. else
  120. echo ""
  121. print_info "Data directories preserved:"
  122. print_info " Queue: $QUEUE_DIR"
  123. print_info " Output: $OUTPUT_DIR"
  124. print_info " Logs: $LOG_DIR"
  125. print_info ""
  126. print_info "To remove these, run: sudo $0 --remove-data"
  127. fi
  128. }
  129. # Function to remove system user
  130. remove_system_user() {
  131. if [[ "$REMOVE_USER" == true && -n "$USER_TO_REMOVE" ]]; then
  132. if id "$USER_TO_REMOVE" &>/dev/null; then
  133. echo ""
  134. print_warning "This will remove the system user: $USER_TO_REMOVE"
  135. read -p "Are you sure? (y/n): " confirm
  136. if [[ "$confirm" =~ ^[Yy]$ ]]; then
  137. print_info "Removing system user: $USER_TO_REMOVE"
  138. userdel "$USER_TO_REMOVE" || print_warning "Failed to remove user (may not be a system account)"
  139. fi
  140. fi
  141. elif [[ "$REMOVE_USER" == true ]]; then
  142. print_warning "Could not determine which user to remove"
  143. fi
  144. }
  145. # Main uninstallation flow
  146. main() {
  147. echo "============================================"
  148. echo " Stable Diffusion REST Server Uninstaller"
  149. echo "============================================"
  150. echo ""
  151. check_root
  152. parse_args "$@"
  153. echo ""
  154. print_info "Uninstallation Summary:"
  155. print_info " Service file: $SERVICE_FILE"
  156. print_info " Install dir: $INSTALL_DIR"
  157. if [[ "$REMOVE_DATA" == true ]]; then
  158. print_info " Remove data: YES"
  159. else
  160. print_info " Remove data: NO (use --remove-data to remove)"
  161. fi
  162. if [[ "$REMOVE_USER" == true ]]; then
  163. print_info " Remove user: YES (will ask for confirmation)"
  164. else
  165. print_info " Remove user: NO (use --remove-user to remove)"
  166. fi
  167. echo ""
  168. read -p "Proceed with uninstallation? (y/n): " proceed
  169. if [[ ! "$proceed" =~ ^[Yy]$ ]]; then
  170. print_warning "Uninstallation cancelled"
  171. exit 0
  172. fi
  173. stop_service
  174. remove_service
  175. remove_install_dir
  176. remove_data_dirs
  177. remove_system_user
  178. echo ""
  179. print_info "============================================"
  180. print_info " Uninstallation Complete!"
  181. print_info "============================================"
  182. echo ""
  183. }
  184. main "$@"