upgrade.sh 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. #!/bin/bash
  2. # Upgrade script for ui-apt-mirror
  3. # Downloads and installs the latest version from https://ui-apt-mirror.dbashkatov.com/
  4. set -e
  5. # Colors for output
  6. RED='\033[0;31m'
  7. GREEN='\033[0;32m'
  8. YELLOW='\033[1;33m'
  9. BLUE='\033[0;34m'
  10. NC='\033[0m' # No Color
  11. # Configuration
  12. WEBSITE_URL="https://ui-apt-mirror.dbashkatov.com"
  13. TEMP_DIR="./tmp"
  14. DIST_DIR="dist"
  15. # Function to print colored output
  16. print_status() {
  17. echo -e "${BLUE}[INFO]${NC} $1"
  18. }
  19. print_success() {
  20. echo -e "${GREEN}[SUCCESS]${NC} $1"
  21. }
  22. print_warning() {
  23. echo -e "${YELLOW}[WARNING]${NC} $1"
  24. }
  25. print_error() {
  26. echo -e "${RED}[ERROR]${NC} $1"
  27. }
  28. # Function to detect system architecture
  29. detect_architecture() {
  30. local arch=$(uname -m)
  31. case $arch in
  32. x86_64)
  33. echo "amd64"
  34. ;;
  35. aarch64|arm64)
  36. echo "arm64"
  37. ;;
  38. *)
  39. print_error "Unsupported architecture: $arch"
  40. exit 1
  41. ;;
  42. esac
  43. }
  44. # Function to check connectivity to the website
  45. check_connectivity() {
  46. print_status "Checking connectivity to $WEBSITE_URL..."
  47. if curl -s --head --fail "$WEBSITE_URL" > /dev/null 2>&1; then
  48. print_success "Connectivity check passed"
  49. return 0
  50. else
  51. print_error "Cannot connect to $WEBSITE_URL"
  52. print_error "Please check your internet connection and try again"
  53. exit 1
  54. fi
  55. }
  56. # Function to get user choice for architecture
  57. get_architecture_choice() {
  58. local current_arch=$(detect_architecture)
  59. echo ""
  60. echo "Current system architecture: $current_arch"
  61. echo ""
  62. echo "Choose download option:"
  63. echo " 1. Download current architecture only ($current_arch)"
  64. echo " 2. Download all architectures (amd64 + arm64)"
  65. echo ""
  66. while true; do
  67. read -p "Enter your choice (1 or 2): " choice
  68. case $choice in
  69. 1)
  70. ARCH_CHOICE="current"
  71. DOWNLOAD_ARCH=$current_arch
  72. DOWNLOAD_URL="$WEBSITE_URL/downloads/ui-apt-mirror-$current_arch.tar"
  73. print_success "Selected: Current architecture ($current_arch)"
  74. break
  75. ;;
  76. 2)
  77. ARCH_CHOICE="all"
  78. DOWNLOAD_ARCH="all"
  79. DOWNLOAD_URL="$WEBSITE_URL/downloads/ui-apt-mirror-all.tar"
  80. print_success "Selected: All architectures"
  81. break
  82. ;;
  83. *)
  84. print_error "Invalid choice. Please enter 1 or 2."
  85. ;;
  86. esac
  87. done
  88. }
  89. # Function to download the latest version
  90. download_latest() {
  91. print_status "Downloading latest version..."
  92. print_status "URL: $DOWNLOAD_URL"
  93. # Create temp directory
  94. mkdir -p "$TEMP_DIR"
  95. # Download the file
  96. if curl -L -o "$TEMP_DIR/ui-apt-mirror.tar" "$DOWNLOAD_URL"; then
  97. print_success "Download completed successfully"
  98. else
  99. print_error "Download failed"
  100. rm -rf "$TEMP_DIR"
  101. exit 1
  102. fi
  103. }
  104. # Function to extract and install
  105. extract_and_install() {
  106. print_status "Extracting downloaded archive..."
  107. # Extract to temp directory
  108. if tar -xzf "$TEMP_DIR/ui-apt-mirror.tar" -C "$TEMP_DIR"; then
  109. print_success "Archive extracted successfully"
  110. else
  111. print_error "Failed to extract archive"
  112. rm -rf "$TEMP_DIR"
  113. exit 1
  114. fi
  115. # Check if dist directory exists in extracted content
  116. if [ ! -d "$TEMP_DIR/dist" ]; then
  117. print_error "Invalid archive format: dist directory not found"
  118. rm -rf "$TEMP_DIR"
  119. exit 1
  120. fi
  121. # Create dist directory if it doesn't exist
  122. mkdir -p "$DIST_DIR"
  123. # Move image files to dist directory
  124. print_status "Installing new image files..."
  125. for image_file in "$TEMP_DIR"/dist/*.tar.gz; do
  126. if [ -f "$image_file" ]; then
  127. local filename=$(basename "$image_file")
  128. print_status "Installing $filename..."
  129. cp "$image_file" "$DIST_DIR/"
  130. print_success "Installed $filename"
  131. fi
  132. done
  133. # Check if any files were installed
  134. if [ -z "$(ls -A "$DIST_DIR"/*.tar.gz 2>/dev/null)" ]; then
  135. print_error "No image files found in the downloaded archive"
  136. rm -rf "$TEMP_DIR"
  137. exit 1
  138. fi
  139. print_success "Image files installed successfully"
  140. # Copy additional project files
  141. print_status "Installing additional project files..."
  142. # List of files to copy
  143. local files_to_copy=(
  144. "setup.sh"
  145. "start.sh"
  146. "docker-compose.src.yml"
  147. "README.md"
  148. )
  149. # Copy individual files
  150. for file in "${files_to_copy[@]}"; do
  151. if [ -f "$TEMP_DIR/$file" ]; then
  152. print_status "Installing $file..."
  153. cp "$TEMP_DIR/$file" "./"
  154. print_success "Installed $file"
  155. else
  156. print_warning "File $file not found in downloaded archive"
  157. fi
  158. done
  159. print_success "Additional project files installed successfully"
  160. }
  161. # Function to run setup
  162. run_setup() {
  163. print_status "Running setup script..."
  164. if [ -f "./setup.sh" ]; then
  165. print_status "Starting setup process..."
  166. ./setup.sh
  167. else
  168. print_error "setup.sh not found in current directory"
  169. rm -rf "$TEMP_DIR"
  170. exit 1
  171. fi
  172. }
  173. # Function to cleanup
  174. cleanup() {
  175. print_status "Cleaning up temporary files..."
  176. rm -rf "$TEMP_DIR"
  177. print_success "Cleanup completed"
  178. }
  179. # Function to show usage
  180. show_usage() {
  181. echo "Usage: $0 [OPTIONS]"
  182. echo ""
  183. echo "Options:"
  184. echo " --help Show this help message"
  185. echo ""
  186. echo "This script will:"
  187. echo " 1. Check connectivity to $WEBSITE_URL"
  188. echo " 2. Ask user to choose architecture (current or all)"
  189. echo " 3. Download the latest version"
  190. echo " 4. Extract and install new image files"
  191. echo " 5. Run setup.sh to deploy the upgrade"
  192. echo " 6. Clean up temporary files"
  193. echo ""
  194. echo "Prerequisites:"
  195. echo " - Internet connection"
  196. echo " - curl for downloading"
  197. echo " - tar for extraction"
  198. echo " - setup.sh in current directory"
  199. }
  200. # Main execution
  201. main() {
  202. # Parse command line arguments
  203. while [[ $# -gt 0 ]]; do
  204. case $1 in
  205. --help)
  206. show_usage
  207. exit 0
  208. ;;
  209. *)
  210. print_error "Unknown option: $1"
  211. show_usage
  212. exit 1
  213. ;;
  214. esac
  215. done
  216. print_status "Starting ui-apt-mirror upgrade process..."
  217. # Check connectivity
  218. check_connectivity
  219. # Get user choice for architecture
  220. get_architecture_choice
  221. # Download latest version
  222. download_latest
  223. # Extract and install
  224. extract_and_install
  225. # Run setup
  226. run_setup
  227. # Cleanup
  228. cleanup
  229. print_success "Upgrade completed successfully!"
  230. echo ""
  231. print_status "The new version is now running. You can access it at:"
  232. echo " - Main Repository: http://mirror.intra"
  233. echo " - Admin Panel: http://admin.mirror.intra"
  234. echo " - File Repository: http://files.mirror.intra"
  235. }
  236. # Run main function with all arguments
  237. main "$@"