docker-deploy.sh 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #!/bin/bash
  2. # =============================================================================
  3. # Sub2API Docker Deployment Preparation Script
  4. # =============================================================================
  5. # This script prepares deployment files for Sub2API:
  6. # - Downloads docker-compose.local.yml and .env.example
  7. # - Generates secure secrets (JWT_SECRET, TOTP_ENCRYPTION_KEY, POSTGRES_PASSWORD)
  8. # - Creates necessary data directories
  9. #
  10. # After running this script, you can start services with:
  11. # docker-compose up -d
  12. # =============================================================================
  13. set -e
  14. # Colors for output
  15. RED='\033[0;31m'
  16. GREEN='\033[0;32m'
  17. YELLOW='\033[1;33m'
  18. BLUE='\033[0;34m'
  19. NC='\033[0m' # No Color
  20. # GitHub raw content base URL
  21. GITHUB_RAW_URL="https://raw.githubusercontent.com/Wei-Shaw/sub2api/main/deploy"
  22. # Print colored message
  23. print_info() {
  24. echo -e "${BLUE}[INFO]${NC} $1"
  25. }
  26. print_success() {
  27. echo -e "${GREEN}[SUCCESS]${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. # Generate random secret
  36. generate_secret() {
  37. openssl rand -hex 32
  38. }
  39. # Check if command exists
  40. command_exists() {
  41. command -v "$1" >/dev/null 2>&1
  42. }
  43. # Main installation function
  44. main() {
  45. echo ""
  46. echo "=========================================="
  47. echo " Sub2API Deployment Preparation"
  48. echo "=========================================="
  49. echo ""
  50. # Check if openssl is available
  51. if ! command_exists openssl; then
  52. print_error "openssl is not installed. Please install openssl first."
  53. exit 1
  54. fi
  55. # Check if deployment already exists
  56. if [ -f "docker-compose.yml" ] && [ -f ".env" ]; then
  57. print_warning "Deployment files already exist in current directory."
  58. read -p "Overwrite existing files? (y/N): " -r
  59. echo
  60. if [[ ! $REPLY =~ ^[Yy]$ ]]; then
  61. print_info "Cancelled."
  62. exit 0
  63. fi
  64. fi
  65. # Download docker-compose.local.yml and save as docker-compose.yml
  66. print_info "Downloading docker-compose.yml..."
  67. if command_exists curl; then
  68. curl -sSL "${GITHUB_RAW_URL}/docker-compose.local.yml" -o docker-compose.yml
  69. elif command_exists wget; then
  70. wget -q "${GITHUB_RAW_URL}/docker-compose.local.yml" -O docker-compose.yml
  71. else
  72. print_error "Neither curl nor wget is installed. Please install one of them."
  73. exit 1
  74. fi
  75. print_success "Downloaded docker-compose.yml"
  76. # Download .env.example
  77. print_info "Downloading .env.example..."
  78. if command_exists curl; then
  79. curl -sSL "${GITHUB_RAW_URL}/.env.example" -o .env.example
  80. else
  81. wget -q "${GITHUB_RAW_URL}/.env.example" -O .env.example
  82. fi
  83. print_success "Downloaded .env.example"
  84. # Generate .env file with auto-generated secrets
  85. print_info "Generating secure secrets..."
  86. echo ""
  87. # Generate secrets
  88. JWT_SECRET=$(generate_secret)
  89. TOTP_ENCRYPTION_KEY=$(generate_secret)
  90. POSTGRES_PASSWORD=$(generate_secret)
  91. # Create .env from .env.example
  92. cp .env.example .env
  93. # Update .env with generated secrets (cross-platform compatible)
  94. if sed --version >/dev/null 2>&1; then
  95. # GNU sed (Linux)
  96. sed -i "s/^JWT_SECRET=.*/JWT_SECRET=${JWT_SECRET}/" .env
  97. sed -i "s/^TOTP_ENCRYPTION_KEY=.*/TOTP_ENCRYPTION_KEY=${TOTP_ENCRYPTION_KEY}/" .env
  98. sed -i "s/^POSTGRES_PASSWORD=.*/POSTGRES_PASSWORD=${POSTGRES_PASSWORD}/" .env
  99. else
  100. # BSD sed (macOS)
  101. sed -i '' "s/^JWT_SECRET=.*/JWT_SECRET=${JWT_SECRET}/" .env
  102. sed -i '' "s/^TOTP_ENCRYPTION_KEY=.*/TOTP_ENCRYPTION_KEY=${TOTP_ENCRYPTION_KEY}/" .env
  103. sed -i '' "s/^POSTGRES_PASSWORD=.*/POSTGRES_PASSWORD=${POSTGRES_PASSWORD}/" .env
  104. fi
  105. # Create data directories
  106. print_info "Creating data directories..."
  107. mkdir -p data postgres_data redis_data
  108. print_success "Created data directories"
  109. # Set secure permissions for .env file (readable/writable only by owner)
  110. chmod 600 .env
  111. echo ""
  112. # Display completion message
  113. echo "=========================================="
  114. echo " Preparation Complete!"
  115. echo "=========================================="
  116. echo ""
  117. echo "Generated secure credentials:"
  118. echo " POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}"
  119. echo " JWT_SECRET: ${JWT_SECRET}"
  120. echo " TOTP_ENCRYPTION_KEY: ${TOTP_ENCRYPTION_KEY}"
  121. echo ""
  122. print_warning "These credentials have been saved to .env file."
  123. print_warning "Please keep them secure and do not share publicly!"
  124. echo ""
  125. echo "Directory structure:"
  126. echo " docker-compose.yml - Docker Compose configuration"
  127. echo " .env - Environment variables (generated secrets)"
  128. echo " .env.example - Example template (for reference)"
  129. echo " data/ - Application data (will be created on first run)"
  130. echo " postgres_data/ - PostgreSQL data"
  131. echo " redis_data/ - Redis data"
  132. echo ""
  133. echo "Next steps:"
  134. echo " 1. (Optional) Edit .env to customize configuration"
  135. echo " 2. Start services:"
  136. echo " docker-compose up -d"
  137. echo ""
  138. echo " 3. View logs:"
  139. echo " docker-compose logs -f sub2api"
  140. echo ""
  141. echo " 4. Access Web UI:"
  142. echo " http://localhost:8080"
  143. echo ""
  144. print_info "If admin password is not set in .env, it will be auto-generated."
  145. print_info "Check logs for the generated admin password on first startup."
  146. echo ""
  147. }
  148. # Run main function
  149. main "$@"