setup_app.sh 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #!/bin/bash
  2. #ident "@(#)$Format:LocalFoodAI_lanfr144:setup_app.sh:%an:%ae:%ad:%cn:%ce:%cd:%H:%D:%N$""
  3. # ==============================================================================
  4. # Local Food AI - WSL Application Setup Script
  5. # Run inside the WSL Ubuntu environment
  6. # ==============================================================================
  7. set -e
  8. GREEN='\033[0;32m'
  9. BLUE='\033[0;34m'
  10. YELLOW='\033[1;33m'
  11. RED='\033[0;31m'
  12. NC='\033[0m'
  13. log_info() {
  14. echo -e "${BLUE}[INFO] - $1${NC}"
  15. }
  16. log_success() {
  17. echo -e "${GREEN}[SUCCESS] - $1${NC}"
  18. }
  19. log_warn() {
  20. echo -e "${YELLOW}[WARNING] - $1${NC}"
  21. }
  22. log_error() {
  23. echo -e "${RED}[ERROR] - $1${NC}"
  24. }
  25. # 1. Check if running inside WSL
  26. if ! grep -qi "microsoft" /proc/sys/kernel/osrelease 2>/dev/null && ! grep -qi "wsl" /proc/sys/kernel/osrelease 2>/dev/null; then
  27. log_warn "This script is optimized for WSL. Continuing anyway..."
  28. fi
  29. # 2. Navigate to WSL home directory if run from Windows mount to prevent permission bugs
  30. if [[ "$PWD" == /mnt/* ]]; then
  31. log_warn "You are running this script from a Windows filesystem mount ($PWD)."
  32. log_info "Redirecting to your Unix home directory (~)..."
  33. cd ~
  34. fi
  35. # 3. Clean existing Docker installations
  36. log_info "Step 1/6: Checking and removing old Docker packages..."
  37. sudo apt remove -y docker.io docker-compose docker-compose-v2 docker-doc podman-docker containerd runc || true
  38. # 4. Install Docker dependencies & Repository
  39. log_info "Step 2/6: Setting up Docker repository..."
  40. sudo apt update
  41. sudo apt install -y ca-certificates curl ed
  42. sudo install -m 0755 -d /etc/apt/keyrings
  43. sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
  44. sudo chmod a+r /etc/apt/keyrings/docker.asc
  45. sudo tee /etc/apt/sources.list.d/docker.sources <<EOF
  46. Types: deb
  47. URIs: https://download.docker.com/linux/ubuntu
  48. Suites: $(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}")
  49. Components: stable
  50. Architectures: $(dpkg --print-architecture)
  51. Signed-By: /etc/apt/keyrings/docker.asc
  52. EOF
  53. # 5. Install Docker CE
  54. log_info "Step 3/6: Installing Docker CE and plugins..."
  55. sudo apt update
  56. sudo apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
  57. # 6. Switch IP Tables to Legacy for WSL Docker bridging
  58. log_info "Step 4/6: Switching iptables configuration to legacy..."
  59. # Choose option 1 (iptables-legacy) automatically using update-alternatives
  60. sudo update-alternatives --set iptables /usr/sbin/iptables-legacy
  61. # 7. Update DNS and WSL network configurations
  62. log_info "Step 5/6: Configuring resolv.conf and wsl.conf for stable DNS..."
  63. # Modify resolv.conf to use Cloudflare DNS
  64. echo "1,\$ s/^/#/
  65. \$ a
  66. nameserver 1.1.1.1
  67. .
  68. w
  69. q" | sudo ed /etc/resolv.conf
  70. # Modify wsl.conf to prevent overwrite
  71. echo "\$ a
  72. # Added these 2 lines:
  73. [network]
  74. generateResolvConf = false
  75. .
  76. w
  77. q" | sudo ed /etc/wsl.conf
  78. # 8. User group configuration
  79. log_info "Step 6/6: Configuring Docker group permissions..."
  80. grep "^docker:" /etc/group || sudo addgroup docker
  81. sudo usermod -aG docker "$USER"
  82. log_success "Application Environment Setup Complete!"
  83. log_info "Please reboot your WSL instance to apply changes."
  84. log_info "To reboot WSL, execute this command:"
  85. log_info "cd /mnt/c/ && cmd.exe /c start \"rebooting WSL\" cmd /c \"timeout 5 && wsl -d \$WSL_DISTRO_NAME\" && wsl.exe --terminate \$WSL_DISTRO_NAME"