-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.sh
More file actions
178 lines (153 loc) · 5.39 KB
/
Copy pathdeploy.sh
File metadata and controls
178 lines (153 loc) · 5.39 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env bash
# ============ COBALT SECTOR — déploiement machine dédiée ============
# Installe et configure tout pour servir le jeu sur https://cobalt.forgenet.fr
# - nginx : sert le client buildé (dist/) + proxy WebSocket /ws -> :17771
# - pm2 : garde le serveur de jeu en vie (redémarrage auto, boot)
# - certbot : certificat SSL Let's Encrypt
#
# Usage (Ubuntu/Debian, depuis la racine du repo) :
# chmod +x deploy.sh && sudo ./deploy.sh
#
# Prérequis : le DNS de cobalt.forgenet.fr pointe déjà vers cette machine.
set -euo pipefail
DOMAIN="cobalt.forgenet.fr"
PORT=17771
APP_DIR="$(cd "$(dirname "$0")" && pwd)"
echo "== COBALT SECTOR — déploiement sur ${DOMAIN} =="
echo " Dossier : ${APP_DIR}"
# ---------- 1. Dépendances système ----------
echo "-- Installation des paquets (nginx, certbot)…"
apt-get update -qq
apt-get install -y -qq nginx certbot python3-certbot-nginx curl >/dev/null
if ! command -v node >/dev/null 2>&1; then
echo "-- Node.js absent : installation (NodeSource 20.x)…"
curl -fsSL https://deb.nodesource.com/setup_20.x | bash -
apt-get install -y -qq nodejs >/dev/null
fi
echo " Node $(node -v), npm $(npm -v)"
if ! command -v pm2 >/dev/null 2>&1; then
echo "-- Installation de pm2…"
npm install -g pm2 >/dev/null
fi
# ---------- 2. Build du jeu ----------
echo "-- npm ci + build du client…"
cd "$APP_DIR"
npm ci
npm run build
echo "-- Bundle du serveur de jeu…"
npx esbuild server/server.ts --bundle --platform=node --format=cjs \
--outfile=server/.server.cjs --external:ws
# ---------- 3. pm2 : le serveur de jeu tourne pour toujours ----------
echo "-- Démarrage sous pm2…"
pm2 delete cobalt >/dev/null 2>&1 || true
PORT=$PORT pm2 start server/.server.cjs --name cobalt --time
pm2 save
pm2 startup systemd -u root --hp /root >/dev/null 2>&1 || true
# ---------- 4. nginx (HTTP) : sert le jeu + défi ACME ----------
# Cette config de base permet à Let's Encrypt de valider le domaine ; elle est
# remplacée par la version HTTPS complète à l'étape 5 dès que le certificat existe.
echo "-- Configuration nginx…"
CERT_DIR="/etc/letsencrypt/live/${DOMAIN}"
write_http_conf() {
cat > /etc/nginx/sites-available/cobalt <<NGINX
server {
listen 80;
server_name ${DOMAIN};
root ${APP_DIR}/dist;
index index.html;
location / {
try_files \$uri \$uri/ /index.html;
}
location /ws {
proxy_pass http://127.0.0.1:${PORT};
proxy_http_version 1.1;
proxy_set_header Upgrade \$http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host \$host;
proxy_read_timeout 3600s;
proxy_send_timeout 3600s;
}
}
NGINX
}
# Config HTTPS complète, écrite PAR NOUS (plus de dépendance aux modifications
# de certbot --nginx : avant, chaque redéploiement écrasait le bloc SSL et le
# site perdait son HTTPS jusqu'au prochain coup de chance)
write_https_conf() {
cat > /etc/nginx/sites-available/cobalt <<NGINX
server {
listen 80;
server_name ${DOMAIN};
# défi ACME (renouvellements du certificat)
location /.well-known/acme-challenge/ {
root ${APP_DIR}/dist;
}
location / {
return 301 https://\$host\$request_uri;
}
}
server {
listen 443 ssl http2;
server_name ${DOMAIN};
ssl_certificate ${CERT_DIR}/fullchain.pem;
ssl_certificate_key ${CERT_DIR}/privkey.pem;
root ${APP_DIR}/dist;
index index.html;
location / {
try_files \$uri \$uri/ /index.html;
}
location /ws {
proxy_pass http://127.0.0.1:${PORT};
proxy_http_version 1.1;
proxy_set_header Upgrade \$http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host \$host;
proxy_read_timeout 3600s;
proxy_send_timeout 3600s;
}
}
NGINX
}
write_http_conf
ln -sf /etc/nginx/sites-available/cobalt /etc/nginx/sites-enabled/cobalt
rm -f /etc/nginx/sites-enabled/default
nginx -t
systemctl reload nginx
# ---------- 5. SSL ----------
echo "-- Certificat SSL (Let's Encrypt)…"
if [ ! -e "${CERT_DIR}/fullchain.pem" ]; then
# webroot : n'édite pas la config nginx, et le verrou certbot (« Another
# instance is already running ») se retente au lieu de tuer le déploiement
obtained=0
for attempt in 1 2 3 4 5 6; do
if certbot certonly --webroot -w "${APP_DIR}/dist" -d "$DOMAIN" \
--non-interactive --agree-tos --register-unsafely-without-email \
--deploy-hook "systemctl reload nginx"; then
obtained=1
break
fi
echo " certbot indisponible (verrou ?) — nouvel essai dans 10 s (${attempt}/6)…"
sleep 10
done
if [ "$obtained" = "0" ]; then
echo " ⚠ Échec de l'obtention du certificat. Si « Another instance of Certbot »"
echo " persiste, un processus est bloqué : pkill -f certbot ; puis relancez ./deploy.sh"
fi
fi
if [ -e "${CERT_DIR}/fullchain.pem" ]; then
echo "-- nginx : activation HTTPS…"
write_https_conf
nginx -t
systemctl reload nginx
else
echo " ⚠ Pas de certificat pour l'instant — le site reste servi en HTTP."
fi
# ---------- 6. Pare-feu (si ufw actif) ----------
if command -v ufw >/dev/null 2>&1 && ufw status | grep -q "Status: active"; then
ufw allow 'Nginx Full' >/dev/null || true
fi
echo ""
echo "== Terminé ! =="
echo " Jeu : https://${DOMAIN}"
echo " Serveur : pm2 status / pm2 logs cobalt"
echo " Redéployer après un git pull : sudo ./deploy.sh (idempotent)"