Repository navigation
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·162 lines (149 loc) · 6.33 KB
/
Copy pathdeploy.sh
File metadata and controls
executable file
·162 lines (149 loc) · 6.33 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env bash
# =============================================================================
# MedReader Agent — One-shot deployment from GitHub
# =============================================================================
# Run ON YOUR SERVER as root or sudo user:
#
# curl -fsSL https://raw.githubusercontent.com/Cymene1205/Medreader/main/deploy.sh | sudo bash
#
# — or, if you've cloned the repo onto the server —
# git clone https://github.com/Cymene1205/Medreader.git /opt/medreader
# cd /opt/medreader && sudo bash deploy.sh
#
# What this script does:
# 1. Installs Docker + Docker Compose if missing
# 2. Clones (or git pulls) the latest code from GitHub
# 3. Generates .env.production (random NEXTAUTH_SECRET + server IP)
# 4. Migrates existing SQLite DB + uploads (if found)
# 5. docker compose up -d --build
# 6. Waits for health check, prints the URL
#
# After the first run, re-running this script just git-pulls + rebuilds.
# Data in /opt/medreader/data and /opt/medreader/uploads is preserved.
# =============================================================================
set -euo pipefail
# --- Config -----------------------------------------------------------------
APP_DIR="${APP_DIR:-/opt/medreader}"
REPO_URL="${REPO_URL:-https://github.com/Cymene1205/Medreader.git}"
BRANCH="${BRANCH:-main}"
SERVER_IP="${SERVER_IP:-47.253.133.131}"
PORT="${PORT:-3000}"
# Colors
RED=$'\033[0;31m'; GREEN=$'\033[0;32m'; YELLOW=$'\033[1;33m'; BLUE=$'\033[0;34m'; NC=$'\033[0m'
log() { echo "${GREEN}[deploy]${NC} $*"; }
warn() { echo "${YELLOW}[warn]${NC} $*"; }
err() { echo "${RED}[err]${NC} $*" >&2; }
step() { echo "${BLUE}[step]${NC} $*"; }
# --- Pre-flight -------------------------------------------------------------
if [[ $EUID -ne 0 ]] && ! sudo -n true 2>/dev/null; then
err "needs root or passwordless sudo — re-run as: sudo bash $0"
exit 1
fi
SUDO=""
[[ $EUID -ne 0 ]] && SUDO="sudo"
# --- 1. Docker --------------------------------------------------------------
step "1/6 Ensure Docker is installed"
if ! command -v docker >/dev/null 2>&1; then
log "Docker not found — installing via get.docker.com"
curl -fsSL https://get.docker.com | ${SUDO} sh
${SUDO} systemctl enable --now docker
${SUDO} usermod -aG docker "${USER:-$(whoami)}" || true
warn "added you to docker group; may need to log out + back in"
else
log "Docker $(docker --version | awk '{print $3}' | tr -d ',') already installed"
fi
if ! docker compose version >/dev/null 2>&1; then
err "docker compose plugin missing — install manually:"
err " sudo apt-get update && sudo apt-get install docker-compose-plugin"
exit 1
fi
# --- 2. Clone / pull from GitHub --------------------------------------------
step "2/6 Sync code to ${APP_DIR}"
if [[ -d "${APP_DIR}/.git" ]]; then
log "existing repo — pulling latest from ${BRANCH}"
cd "${APP_DIR}"
${SUDO} git fetch --prune origin 2>&1 | tail -3
${SUDO} git reset --hard "origin/${BRANCH}" 2>&1 | tail -3
${SUDO} chown -R "$(whoami)" . 2>/dev/null || true
else
log "no repo at ${APP_DIR} — cloning from GitHub"
${SUDO} mkdir -p "${APP_DIR}"
${SUDO} chown "$(whoami)" "${APP_DIR}"
git clone --depth 1 --branch "${BRANCH}" "${REPO_URL}" "${APP_DIR}"
cd "${APP_DIR}"
fi
log "current commit: $(git rev-parse --short HEAD) — $(git log -1 --format='%s')"
# --- 3. .env.production -----------------------------------------------------
step "3/6 Prepare .env.production"
if [[ ! -f .env.production ]]; then
log "no .env.production found — creating from template"
cp .env.production.example .env.production
SECRET="$(openssl rand -base64 32)"
sed -i "s|NEXTAUTH_SECRET=PLEASE_REPLACE_WITH_RANDOM_32_BYTES|NEXTAUTH_SECRET=${SECRET}|" .env.production
sed -i "s|NEXTAUTH_URL=http://.*|NEXTAUTH_URL=http://${SERVER_IP}:${PORT}|" .env.production
warn "edit .env.production to add DEEPSEEK_API_KEY / MINERU_API_TOKEN if needed"
warn " vi ${APP_DIR}/.env.production"
else
log ".env.production already exists — leaving it"
fi
# --- 4. Migrate existing data -----------------------------------------------
step "4/6 Ensure data + uploads dirs exist"
mkdir -p data uploads
# If there's a dev-mode db/ dir (file-based SQLite at db/custom.db), copy it in.
# Skipped if data/custom.db already exists (don't overwrite prod data).
if [[ -f db/custom.db && ! -f data/custom.db ]]; then
log "copying db/custom.db → data/custom.db (preserves dev users + papers)"
cp db/custom.db data/custom.db
[[ -f db/custom.db-journal ]] && cp db/custom.db-journal data/ || true
[[ -f db/custom.db-wal ]] && cp db/custom.db-wal data/ || true
[[ -f db/custom.db-shm ]] && cp db/custom.db-shm data/ || true
else
log "no migration needed — data/ already has custom.db or no dev db to import"
fi
# --- 5. Build + start -------------------------------------------------------
step "5/6 docker compose up -d --build (2-5 min on first run)"
docker compose up -d --build
# --- 6. Health check --------------------------------------------------------
step "6/6 Wait for health check"
log "waiting for container to become healthy..."
HEALTHY=0
for i in $(seq 1 40); do
STATUS="$(docker inspect --format='{{.State.Health.Status}}' medreader 2>/dev/null || echo "none")"
case "${STATUS}" in
healthy)
echo
log "container is healthy"
HEALTHY=1
break
;;
unhealthy)
err "container became unhealthy — recent logs:"
docker compose logs --tail=80 medreader
exit 1
;;
*)
printf "."
sleep 3
;;
esac
done
if [[ "${HEALTHY}" -ne 1 ]]; then
warn "health check timed out — container may still be starting"
warn "check logs: docker compose logs -f medreader"
fi
echo
log "============================================================"
log " MedReader Agent is running"
log " → http://${SERVER_IP}:${PORT}"
log " commit: $(git rev-parse --short HEAD)"
log "============================================================"
echo
log "Useful commands:"
echo " cd ${APP_DIR}"
echo " docker compose logs -f # follow logs"
echo " docker compose restart # restart after env change"
echo " docker compose down # stop"
echo " sudo bash deploy.sh # pull + rebuild (re-run this script)"
echo
log "If port 3000 is unreachable, open it in Alibaba Cloud security group:"
echo " protocol: TCP port: 3000/3000 source: 0.0.0.0/0"