File size: 4,843 Bytes
344e369
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/bin/bash
# ══════════════════════════════════════════════════════
# ΨͺΨ¨ΩŠΨ§Ω† Ψ§Ω„Ψ·Ψ¨ΩŠ β€” Server Setup & Deploy Script
# Run on Oracle Cloud ARM VM (Ubuntu 22.04)
#
# First time:  bash deploy.sh setup
# Update code: bash deploy.sh deploy
# View logs:   bash deploy.sh logs
# ══════════════════════════════════════════════════════

set -e
REPO_URL="${REPO_URL:-https://github.com/YOUR_USERNAME/tebyan-medical.git}"
APP_DIR="/opt/tebyan"
COMPOSE="docker compose -f docker-compose.prod.yml"

# ── Colors ────────────────────────────────────────────
GREEN='\033[0;32m'; YELLOW='\033[1;33m'; RED='\033[0;31m'; NC='\033[0m'
ok()   { echo -e "${GREEN}βœ“ $1${NC}"; }
warn() { echo -e "${YELLOW}⚠ $1${NC}"; }
err()  { echo -e "${RED}βœ— $1${NC}"; exit 1; }

# ══════════════════════════════════════════════════════
case "${1:-help}" in

# ── SETUP: run once on a fresh server ────────────────
setup)
  echo "━━━ ΨͺΨ¨ΩŠΨ§Ω† Ψ§Ω„Ψ·Ψ¨ΩŠ β€” Server Setup ━━━"

  # Docker
  if ! command -v docker &>/dev/null; then
    curl -fsSL https://get.docker.com | sh
    usermod -aG docker "$USER"
    ok "Docker installed"
  else
    ok "Docker already installed"
  fi

  # Docker Compose v2
  if ! docker compose version &>/dev/null; then
    apt-get install -y docker-compose-plugin
    ok "Docker Compose plugin installed"
  fi

  # Firewall
  ufw allow 22/tcp   # SSH
  ufw allow 80/tcp   # HTTP
  ufw allow 443/tcp  # HTTPS
  ufw --force enable
  ok "Firewall configured (22, 80, 443)"

  # Clone repo
  if [ ! -d "$APP_DIR" ]; then
    git clone "$REPO_URL" "$APP_DIR"
    ok "Repository cloned to $APP_DIR"
  else
    warn "Directory $APP_DIR already exists β€” skipping clone"
  fi

  # .env
  if [ ! -f "$APP_DIR/.env" ]; then
    cp "$APP_DIR/.env.production.example" "$APP_DIR/.env"
    warn "Created $APP_DIR/.env β€” FILL IN YOUR KEYS NOW:"
    warn "  nano $APP_DIR/.env"
    warn "Then run: bash $APP_DIR/deploy.sh ssl"
  else
    ok ".env already exists"
  fi

  echo ""
  echo "Next steps:"
  echo "  1. nano $APP_DIR/.env          ← fill in your API keys"
  echo "  2. DOMAIN=yourdomain.com EMAIL=you@example.com bash $APP_DIR/nginx/init-letsencrypt.sh"
  echo "  3. bash $APP_DIR/deploy.sh deploy"
  ;;

# ── DEPLOY: pull latest and restart ──────────────────
deploy)
  echo "━━━ Deploying ΨͺΨ¨ΩŠΨ§Ω† Ψ§Ω„Ψ·Ψ¨ΩŠ ━━━"
  cd "$APP_DIR"

  [ ! -f .env ] && err ".env not found. Run: bash deploy.sh setup"

  git pull origin main
  ok "Code updated"

  $COMPOSE build --pull
  ok "Images built"

  $COMPOSE up -d
  ok "Containers started"

  echo "Waiting for health checks..."
  sleep 10
  $COMPOSE ps
  ;;

# ── SSL: get Let's Encrypt cert ───────────────────────
ssl)
  [ -z "$DOMAIN" ] && err "Set DOMAIN=yourdomain.com before running"
  [ -z "$EMAIL"  ] && err "Set EMAIL=you@example.com before running"
  cd "$APP_DIR"
  DOMAIN="$DOMAIN" EMAIL="$EMAIL" bash nginx/init-letsencrypt.sh
  ;;

# ── LOGS ──────────────────────────────────────────────
logs)
  cd "$APP_DIR"
  $COMPOSE logs -f --tail=100 "${2:-}"
  ;;

# ── STATUS ────────────────────────────────────────────
status)
  cd "$APP_DIR"
  $COMPOSE ps
  echo ""
  curl -s http://localhost/health | python3 -m json.tool 2>/dev/null || echo "Backend not reachable"
  ;;

# ── STOP ──────────────────────────────────────────────
stop)
  cd "$APP_DIR"
  $COMPOSE down
  ok "All containers stopped"
  ;;

# ── HELP ──────────────────────────────────────────────
*)
  echo "Usage: bash deploy.sh [setup|deploy|ssl|logs|status|stop]"
  echo ""
  echo "  setup   β€” Install Docker, clone repo, configure firewall"
  echo "  deploy  β€” Pull latest code and restart containers"
  echo "  ssl     β€” Get Let's Encrypt certificate (set DOMAIN= EMAIL= first)"
  echo "  logs    β€” Follow container logs (optional: logs backend)"
  echo "  status  β€” Show container status and health"
  echo "  stop    β€” Stop all containers"
  ;;
esac