-
Notifications
You must be signed in to change notification settings - Fork 180
Feat/incremental message sync #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
c39f9d7
71669ed
6c03226
6a527cb
04425c5
31ad16c
cbc840c
0d451a3
51f3bcf
c097bd7
7caa47a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,364 @@ | ||||||||
| #!/bin/bash | ||||||||
| set -e | ||||||||
|
|
||||||||
| # Colors | ||||||||
| RED='\033[0;31m' | ||||||||
| GREEN='\033[0;32m' | ||||||||
| YELLOW='\033[1;33m' | ||||||||
| BLUE='\033[0;34m' | ||||||||
| NC='\033[0m' # No Color | ||||||||
|
|
||||||||
| # Helpers | ||||||||
| info() { echo -e "${BLUE}$1${NC}"; } | ||||||||
| success() { echo -e "${GREEN}✓ $1${NC}"; } | ||||||||
| warn() { echo -e "${YELLOW}⚠ $1${NC}"; } | ||||||||
| error() { echo -e "${RED}✗ $1${NC}"; } | ||||||||
| prompt() { echo -en "${YELLOW}$1${NC}"; } | ||||||||
|
|
||||||||
| # Get script directory | ||||||||
| SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | ||||||||
| cd "$SCRIPT_DIR" | ||||||||
|
|
||||||||
| echo "" | ||||||||
| echo -e "${BLUE}═══════════════════════════════════════════${NC}" | ||||||||
| echo -e "${BLUE} Happy Server Deployment Script ${NC}" | ||||||||
| echo -e "${BLUE}═══════════════════════════════════════════${NC}" | ||||||||
| echo "" | ||||||||
|
|
||||||||
| # ───────────────────────────────────────────────────────────── | ||||||||
| # Environment Detection | ||||||||
| # ───────────────────────────────────────────────────────────── | ||||||||
|
|
||||||||
| info "Detecting environment..." | ||||||||
|
|
||||||||
| # Check Docker | ||||||||
| if ! command -v docker &> /dev/null; then | ||||||||
| error "Docker is not installed" | ||||||||
| exit 1 | ||||||||
| fi | ||||||||
| success "Docker installed" | ||||||||
|
|
||||||||
| # Check docker compose | ||||||||
| if docker compose version &> /dev/null; then | ||||||||
| COMPOSE_CMD="docker compose" | ||||||||
| success "Docker Compose installed" | ||||||||
| elif command -v docker-compose &> /dev/null; then | ||||||||
| COMPOSE_CMD="docker-compose" | ||||||||
| success "Docker Compose (standalone) installed" | ||||||||
| else | ||||||||
| error "Docker Compose is not installed" | ||||||||
| exit 1 | ||||||||
| fi | ||||||||
|
|
||||||||
| # Check system Caddy | ||||||||
| SYSTEM_CADDY=false | ||||||||
| if systemctl is-active --quiet caddy 2>/dev/null; then | ||||||||
| SYSTEM_CADDY=true | ||||||||
| success "System Caddy detected (active)" | ||||||||
| elif command -v caddy &> /dev/null; then | ||||||||
| SYSTEM_CADDY=true | ||||||||
| success "System Caddy detected (installed)" | ||||||||
| else | ||||||||
| warn "No system Caddy - will use Docker Caddy" | ||||||||
| fi | ||||||||
|
|
||||||||
| # Check existing PostgreSQL data | ||||||||
| EXISTING_DATA=false | ||||||||
| if docker volume inspect happy-server_postgres_data &> /dev/null; then | ||||||||
| EXISTING_DATA=true | ||||||||
| warn "Existing PostgreSQL data found" | ||||||||
| else | ||||||||
| info "No existing database data" | ||||||||
| fi | ||||||||
|
|
||||||||
| # Check if app is running | ||||||||
| APP_RUNNING=false | ||||||||
| if $COMPOSE_CMD ps 2>/dev/null | grep -q "app.*Up"; then | ||||||||
| APP_RUNNING=true | ||||||||
| info "App is currently running" | ||||||||
| fi | ||||||||
|
|
||||||||
| echo "" | ||||||||
|
|
||||||||
| # ───────────────────────────────────────────────────────────── | ||||||||
| # Interactive Prompts | ||||||||
| # ───────────────────────────────────────────────────────────── | ||||||||
|
|
||||||||
| # Domain | ||||||||
| DEFAULT_DOMAIN="" | ||||||||
| if [ -f /etc/caddy/Caddyfile ]; then | ||||||||
| DEFAULT_DOMAIN=$(grep -oP '^\S+(?=\s*\{)' /etc/caddy/Caddyfile 2>/dev/null | head -1 || true) | ||||||||
| fi | ||||||||
| if [ -z "$DEFAULT_DOMAIN" ]; then | ||||||||
| DEFAULT_DOMAIN=$(hostname -f 2>/dev/null || echo "localhost") | ||||||||
| fi | ||||||||
|
|
||||||||
| prompt "Enter domain name [$DEFAULT_DOMAIN]: " | ||||||||
| read -r DOMAIN | ||||||||
| DOMAIN=${DOMAIN:-$DEFAULT_DOMAIN} | ||||||||
| echo "" | ||||||||
|
|
||||||||
| # Existing data handling | ||||||||
| RESET_DATA=false | ||||||||
| if [ "$EXISTING_DATA" = true ]; then | ||||||||
| echo "Existing database found. What would you like to do?" | ||||||||
| echo " 1) Keep existing data (default)" | ||||||||
| echo " 2) Reset everything (WARNING: destroys all data)" | ||||||||
| prompt "Choice [1]: " | ||||||||
| read -r DATA_CHOICE | ||||||||
| if [ "$DATA_CHOICE" = "2" ]; then | ||||||||
| prompt "Are you sure? Type 'yes' to confirm: " | ||||||||
| read -r CONFIRM | ||||||||
| if [ "$CONFIRM" = "yes" ]; then | ||||||||
| RESET_DATA=true | ||||||||
| warn "Will reset all data" | ||||||||
| else | ||||||||
| info "Keeping existing data" | ||||||||
| fi | ||||||||
| fi | ||||||||
| echo "" | ||||||||
| fi | ||||||||
|
|
||||||||
| # PostgreSQL password | ||||||||
| echo "PostgreSQL password:" | ||||||||
| echo " 1) Use default (postgres) - for development" | ||||||||
| echo " 2) Enter custom password" | ||||||||
| echo " 3) Generate random password" | ||||||||
| prompt "Choice [1]: " | ||||||||
| read -r PW_CHOICE | ||||||||
|
|
||||||||
| case "$PW_CHOICE" in | ||||||||
| 2) | ||||||||
| prompt "Enter password: " | ||||||||
| read -rs POSTGRES_PASSWORD | ||||||||
| echo "" | ||||||||
| ;; | ||||||||
| 3) | ||||||||
| POSTGRES_PASSWORD=$(openssl rand -base64 24 | tr -d '/+=' | head -c 24) | ||||||||
| info "Generated password: $POSTGRES_PASSWORD" | ||||||||
| ;; | ||||||||
| *) | ||||||||
| POSTGRES_PASSWORD="postgres" | ||||||||
| ;; | ||||||||
| esac | ||||||||
|
Comment on lines
+123
to
+143
|
||||||||
| echo "" | ||||||||
|
|
||||||||
| # ───────────────────────────────────────────────────────────── | ||||||||
| # Create/Update .env file | ||||||||
| # ───────────────────────────────────────────────────────────── | ||||||||
|
|
||||||||
| info "Creating .env file..." | ||||||||
|
|
||||||||
| cat > .env <<EOF | ||||||||
| # Generated by deploy.sh on $(date) | ||||||||
| POSTGRES_PASSWORD=$POSTGRES_PASSWORD | ||||||||
| DATABASE_URL=postgresql://postgres:$POSTGRES_PASSWORD@postgres:5432/handy | ||||||||
| DOMAIN=$DOMAIN | ||||||||
| EOF | ||||||||
|
|
||||||||
| success ".env file created" | ||||||||
|
|
||||||||
| # Ensure .env is in .gitignore | ||||||||
| if [ -f .gitignore ]; then | ||||||||
| if ! grep -q "^\.env$" .gitignore; then | ||||||||
| echo ".env" >> .gitignore | ||||||||
| success "Added .env to .gitignore" | ||||||||
| fi | ||||||||
| else | ||||||||
| echo ".env" > .gitignore | ||||||||
| success "Created .gitignore with .env" | ||||||||
| fi | ||||||||
|
|
||||||||
| # ───────────────────────────────────────────────────────────── | ||||||||
| # Docker Deployment | ||||||||
| # ───────────────────────────────────────────────────────────── | ||||||||
|
|
||||||||
| echo "" | ||||||||
| info "Starting Docker deployment..." | ||||||||
|
|
||||||||
| # Reset if requested | ||||||||
| if [ "$RESET_DATA" = true ]; then | ||||||||
| warn "Stopping and removing all containers and volumes..." | ||||||||
| $COMPOSE_CMD down -v --remove-orphans 2>/dev/null || true | ||||||||
| fi | ||||||||
|
|
||||||||
| # Build app | ||||||||
| info "Building app image..." | ||||||||
| $COMPOSE_CMD build app | ||||||||
|
|
||||||||
| # Start infrastructure | ||||||||
| info "Starting infrastructure (postgres, redis, minio)..." | ||||||||
| $COMPOSE_CMD up -d postgres redis minio | ||||||||
|
|
||||||||
| # Wait for postgres to be healthy | ||||||||
| info "Waiting for PostgreSQL to be ready..." | ||||||||
| for i in {1..60}; do | ||||||||
| if $COMPOSE_CMD exec -T postgres pg_isready -U postgres &> /dev/null; then | ||||||||
| success "PostgreSQL is ready" | ||||||||
| break | ||||||||
| fi | ||||||||
| if [ $i -eq 60 ]; then | ||||||||
| error "PostgreSQL failed to start within 60 seconds" | ||||||||
| exit 1 | ||||||||
| fi | ||||||||
| sleep 1 | ||||||||
| done | ||||||||
|
|
||||||||
| # Fix PostgreSQL password | ||||||||
| info "Configuring PostgreSQL password..." | ||||||||
| $COMPOSE_CMD exec -T postgres psql -U postgres -c "ALTER USER postgres WITH PASSWORD '$POSTGRES_PASSWORD';" > /dev/null | ||||||||
|
||||||||
| $COMPOSE_CMD exec -T postgres psql -U postgres -c "ALTER USER postgres WITH PASSWORD '$POSTGRES_PASSWORD';" > /dev/null | |
| ESCAPED_POSTGRES_PASSWORD=$(printf "%s" "$POSTGRES_PASSWORD" | sed "s/'/''/g") | |
| $COMPOSE_CMD exec -T postgres psql -U postgres -c "ALTER USER postgres WITH PASSWORD '${ESCAPED_POSTGRES_PASSWORD}';" > /dev/null |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The generated password is echoed to stdout in plaintext when option 3 is selected. This could be logged or visible in terminal history, creating a security risk. Consider displaying a message indicating the password has been saved to the .env file instead of displaying it directly.