start.sh 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #!/bin/bash
  2. # Start script for ui-apt-mirror
  3. # This script handles loading the Docker image and starting the container
  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. IMAGE_NAME="ui-apt-mirror"
  13. CONTAINER_NAME="ui-apt-mirror"
  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. print_status "Detecting system architecture..." >&2
  31. local arch=$(uname -m)
  32. case $arch in
  33. x86_64)
  34. echo "amd64"
  35. ;;
  36. aarch64|arm64)
  37. echo "arm64"
  38. ;;
  39. *)
  40. print_error "Unsupported architecture: $arch"
  41. exit 1
  42. ;;
  43. esac
  44. }
  45. # Function to validate dist directory
  46. validate_dist() {
  47. local arch=$1
  48. local tar_file="${DIST_DIR}/${IMAGE_NAME}-${arch}.tar.gz"
  49. print_status "Validating distribution files..."
  50. if [ ! -d "$DIST_DIR" ]; then
  51. print_error "Distribution directory '$DIST_DIR' not found."
  52. print_error "Please run ./build.sh first to build the images."
  53. exit 1
  54. fi
  55. if [ ! -f "$tar_file" ]; then
  56. print_error "Image file '$tar_file' not found."
  57. print_error "Please run ./build.sh first to build the images."
  58. exit 1
  59. fi
  60. print_success "Found image file: $tar_file"
  61. }
  62. # Function to load image
  63. load_image() {
  64. local arch=$1
  65. local tar_file="${DIST_DIR}/${IMAGE_NAME}-${arch}.tar.gz"
  66. print_status "Loading Docker image..."
  67. # Extract and load the image
  68. gunzip -c "$tar_file" | docker load
  69. print_success "Image loaded successfully."
  70. }
  71. # Function to start container
  72. start_container() {
  73. print_status "Starting container..."
  74. # Start with docker-compose
  75. docker compose -f docker-compose.yml up -d
  76. print_success "Container started successfully."
  77. }
  78. # Function to show usage
  79. show_usage() {
  80. echo "Usage: $0 [OPTIONS]"
  81. echo ""
  82. echo "Options:"
  83. echo " --help Show this help message"
  84. echo ""
  85. echo "This script will:"
  86. echo " 1. Detect your system architecture"
  87. echo " 2. Validate that required image files exist"
  88. echo " 3. Load the appropriate Docker image"
  89. echo " 4. Start the container"
  90. echo ""
  91. echo "Prerequisites:"
  92. echo " - Docker installed and running"
  93. echo " - Built images in dist/ directory (run ./build.sh first)"
  94. echo " - docker-compose.yml file exists (run ./setup.sh first)"
  95. }
  96. # Main execution
  97. main() {
  98. # Parse command line arguments
  99. while [[ $# -gt 0 ]]; do
  100. case $1 in
  101. --help)
  102. show_usage
  103. exit 0
  104. ;;
  105. *)
  106. print_error "Unknown option: $1"
  107. show_usage
  108. exit 1
  109. ;;
  110. esac
  111. done
  112. print_status "Starting ui-apt-mirror..."
  113. # Detect architecture
  114. local arch=$(detect_architecture)
  115. print_success "Detected architecture: $arch"
  116. # Validate dist directory
  117. validate_dist "$arch"
  118. # Load image
  119. load_image "$arch"
  120. # Start container
  121. start_container
  122. print_success "Container started successfully!"
  123. }
  124. # Run main function with all arguments
  125. main "$@"