| #!/bin/bash |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| set -e |
|
|
| |
| RED='\033[0;31m' |
| GREEN='\033[0;32m' |
| YELLOW='\033[1;33m' |
| BLUE='\033[0;34m' |
| NC='\033[0m' |
|
|
| |
| GITHUB_RAW_URL="https://raw.githubusercontent.com/Wei-Shaw/sub2api/main/deploy" |
|
|
| |
| print_info() { |
| echo -e "${BLUE}[INFO]${NC} $1" |
| } |
|
|
| print_success() { |
| echo -e "${GREEN}[SUCCESS]${NC} $1" |
| } |
|
|
| print_warning() { |
| echo -e "${YELLOW}[WARNING]${NC} $1" |
| } |
|
|
| print_error() { |
| echo -e "${RED}[ERROR]${NC} $1" |
| } |
|
|
| |
| generate_secret() { |
| openssl rand -hex 32 |
| } |
|
|
| |
| command_exists() { |
| command -v "$1" >/dev/null 2>&1 |
| } |
|
|
| |
| main() { |
| echo "" |
| echo "==========================================" |
| echo " Sub2API Deployment Preparation" |
| echo "==========================================" |
| echo "" |
|
|
| |
| if ! command_exists openssl; then |
| print_error "openssl is not installed. Please install openssl first." |
| exit 1 |
| fi |
|
|
| |
| if [ -f "docker-compose.yml" ] && [ -f ".env" ]; then |
| print_warning "Deployment files already exist in current directory." |
| read -p "Overwrite existing files? (y/N): " -r |
| echo |
| if [[ ! $REPLY =~ ^[Yy]$ ]]; then |
| print_info "Cancelled." |
| exit 0 |
| fi |
| fi |
|
|
| |
| print_info "Downloading docker-compose.yml..." |
| if command_exists curl; then |
| curl -sSL "${GITHUB_RAW_URL}/docker-compose.local.yml" -o docker-compose.yml |
| elif command_exists wget; then |
| wget -q "${GITHUB_RAW_URL}/docker-compose.local.yml" -O docker-compose.yml |
| else |
| print_error "Neither curl nor wget is installed. Please install one of them." |
| exit 1 |
| fi |
| print_success "Downloaded docker-compose.yml" |
|
|
| |
| print_info "Downloading .env.example..." |
| if command_exists curl; then |
| curl -sSL "${GITHUB_RAW_URL}/.env.example" -o .env.example |
| else |
| wget -q "${GITHUB_RAW_URL}/.env.example" -O .env.example |
| fi |
| print_success "Downloaded .env.example" |
|
|
| |
| print_info "Generating secure secrets..." |
| echo "" |
|
|
| |
| JWT_SECRET=$(generate_secret) |
| TOTP_ENCRYPTION_KEY=$(generate_secret) |
| POSTGRES_PASSWORD=$(generate_secret) |
|
|
| |
| cp .env.example .env |
|
|
| |
| if sed --version >/dev/null 2>&1; then |
| |
| sed -i "s/^JWT_SECRET=.*/JWT_SECRET=${JWT_SECRET}/" .env |
| sed -i "s/^TOTP_ENCRYPTION_KEY=.*/TOTP_ENCRYPTION_KEY=${TOTP_ENCRYPTION_KEY}/" .env |
| sed -i "s/^POSTGRES_PASSWORD=.*/POSTGRES_PASSWORD=${POSTGRES_PASSWORD}/" .env |
| else |
| |
| sed -i '' "s/^JWT_SECRET=.*/JWT_SECRET=${JWT_SECRET}/" .env |
| sed -i '' "s/^TOTP_ENCRYPTION_KEY=.*/TOTP_ENCRYPTION_KEY=${TOTP_ENCRYPTION_KEY}/" .env |
| sed -i '' "s/^POSTGRES_PASSWORD=.*/POSTGRES_PASSWORD=${POSTGRES_PASSWORD}/" .env |
| fi |
|
|
| |
| print_info "Creating data directories..." |
| mkdir -p data postgres_data redis_data |
| print_success "Created data directories" |
|
|
| |
| chmod 600 .env |
| echo "" |
|
|
| |
| echo "==========================================" |
| echo " Preparation Complete!" |
| echo "==========================================" |
| echo "" |
| echo "Generated secure credentials:" |
| echo " POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}" |
| echo " JWT_SECRET: ${JWT_SECRET}" |
| echo " TOTP_ENCRYPTION_KEY: ${TOTP_ENCRYPTION_KEY}" |
| echo "" |
| print_warning "These credentials have been saved to .env file." |
| print_warning "Please keep them secure and do not share publicly!" |
| echo "" |
| echo "Directory structure:" |
| echo " docker-compose.yml - Docker Compose configuration" |
| echo " .env - Environment variables (generated secrets)" |
| echo " .env.example - Example template (for reference)" |
| echo " data/ - Application data (will be created on first run)" |
| echo " postgres_data/ - PostgreSQL data" |
| echo " redis_data/ - Redis data" |
| echo "" |
| echo "Next steps:" |
| echo " 1. (Optional) Edit .env to customize configuration" |
| echo " 2. Start services:" |
| echo " docker-compose up -d" |
| echo "" |
| echo " 3. View logs:" |
| echo " docker-compose logs -f sub2api" |
| echo "" |
| echo " 4. Access Web UI:" |
| echo " http://localhost:8080" |
| echo "" |
| print_info "If admin password is not set in .env, it will be auto-generated." |
| print_info "Check logs for the generated admin password on first startup." |
| echo "" |
| } |
|
|
| |
| main "$@" |
|
|