Skip to content

Commit 3fcb42c

Browse files
committed
feat(infra): add scripts front deployment
1 parent 4666e8a commit 3fcb42c

File tree

5 files changed

+344
-162
lines changed

5 files changed

+344
-162
lines changed

scripts/docker.sh

Lines changed: 82 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,52 +6,108 @@ source "$(dirname "${BASH_SOURCE[0]}")/config.sh"
66
deploy() {
77
mkdir -p "$SCRIPT_DIR/backups" "$SCRIPT_DIR/logs"
88

9-
if use_local_db; then
10-
echo -e "${YELLOW}DB/Redis en local → Docker ne lancera que backend/monitoring${NC}"
11-
else
12-
echo -e "${GREEN}Lancement de Postgres+Redis via Docker…${NC}"
13-
docker-compose -f docker-compose.prod.yml --env-file "$ENV_FILE" build
14-
docker-compose -f docker-compose.prod.yml --env-file "$ENV_FILE" up -d postgres redis
15-
fi
9+
echo -n "➡️ Déployer le backend ? [Y/n] : "
10+
read ans_back
11+
ans_back=${ans_back:-Y}
1612

17-
if use_local_db; then
18-
start_backend_local
13+
if [[ "$ans_back" =~ ^[Yy]$ ]]; then
14+
if use_local_db; then
15+
echo -e "${YELLOW}DB/Redis en local → Docker ne lancera que backend/monitoring${NC}"
16+
start_backend_local
17+
else
18+
echo -e "${GREEN}Lancement de Postgres+Redis via Docker…${NC}"
19+
docker-compose -f docker-compose.prod.yml --env-file "$ENV_FILE" build
20+
docker-compose -f docker-compose.prod.yml --env-file "$ENV_FILE" up -d postgres redis
21+
docker-compose -f docker-compose.prod.yml --env-file "$ENV_FILE" up -d backend
22+
fi
23+
24+
if use_monitoring; then
25+
docker-compose -f docker-compose.monitoring.yml --env-file "$ENV_FILE" up -d
26+
echo -e "${GREEN}Monitoring lancé${NC}"
27+
else
28+
echo -e "${YELLOW}Monitoring non lancé${NC}"
29+
fi
30+
echo -e "${GREEN}✅ Backend déployé${NC}"
1931
else
20-
docker-compose -f docker-compose.prod.yml --env-file "$ENV_FILE" up -d backend
32+
echo -e "${YELLOW}Backend ignoré${NC}"
2133
fi
2234

23-
if use_monitoring; then
24-
docker-compose -f docker-compose.monitoring.yml --env-file "$ENV_FILE" up -d
25-
echo -e "${GREEN}Monitoring lancé${NC}"
35+
echo -n "➡️ Déployer le frontend ? [Y/n] : "
36+
read ans_front
37+
ans_front=${ans_front:-Y}
38+
39+
if [[ "$ans_front" =~ ^[Yy]$ ]]; then
40+
"$SCRIPT_DIR/scripts/start-front.sh" start
41+
echo -e "${GREEN}✅ Frontend déployé${NC}"
2642
else
27-
echo -e "${YELLOW}Monitoring non lancé${NC}"
43+
echo -e "${YELLOW}Frontend ignoré${NC}"
2844
fi
2945

3046
echo -e "${GREEN}✨ Déploiement terminé${NC}"
3147
}
3248

3349
start() {
34-
if use_local_db; then
35-
start_backend_local
50+
echo -n "➡️ Lancer le backend ? [Y/n] : "
51+
read ans_back
52+
ans_back=${ans_back:-Y}
53+
54+
if [[ "$ans_back" =~ ^[Yy]$ ]]; then
55+
if use_local_db; then
56+
start_backend_local
57+
else
58+
docker-compose -f docker-compose.prod.yml --env-file "$ENV_FILE" up -d postgres redis backend
59+
fi
60+
if use_monitoring; then
61+
docker-compose -f docker-compose.monitoring.yml --env-file "$ENV_FILE" up -d
62+
fi
63+
echo -e "${GREEN}🚦 Backend démarré${NC}"
3664
else
37-
docker-compose -f docker-compose.prod.yml --env-file "$ENV_FILE" up -d postgres redis backend
65+
echo -e "${YELLOW}Backend ignoré${NC}"
3866
fi
39-
if use_monitoring; then
40-
docker-compose -f docker-compose.monitoring.yml --env-file "$ENV_FILE" up -d
67+
68+
echo -n "➡️ Lancer le frontend ? [Y/n] : "
69+
read ans_front
70+
ans_front=${ans_front:-Y}
71+
72+
if [[ "$ans_front" =~ ^[Yy]$ ]]; then
73+
"$SCRIPT_DIR/scripts/start-front.sh" start
74+
echo -e "${GREEN}🚦 Frontend démarré${NC}"
75+
else
76+
echo -e "${YELLOW}Frontend ignoré${NC}"
4177
fi
42-
echo -e "${GREEN}🚦 Services démarrés${NC}"
4378
}
4479

80+
81+
4582
stop() {
46-
if use_local_db; then
47-
stop_backend_local
83+
echo -n "➡️ Arrêter le backend ? [Y/n] : "
84+
read ans_back
85+
ans_back=${ans_back:-Y}
86+
87+
if [[ "$ans_back" =~ ^[Yy]$ ]]; then
88+
if use_local_db; then
89+
stop_backend_local
90+
else
91+
docker-compose -f docker-compose.prod.yml --env-file "$ENV_FILE" down --remove-orphans
92+
fi
93+
if use_monitoring; then
94+
docker-compose -f docker-compose.monitoring.yml --env-file "$ENV_FILE" down --remove-orphans
95+
fi
96+
echo -e "${GREEN}🛑 Backend arrêté${NC}"
4897
else
49-
docker-compose -f docker-compose.prod.yml --env-file "$ENV_FILE" down --remove-orphans
98+
echo -e "${YELLOW}Backend non arrêté${NC}"
5099
fi
51-
if use_monitoring; then
52-
docker-compose -f docker-compose.monitoring.yml --env-file "$ENV_FILE" down --remove-orphans
100+
101+
echo -n "➡️ Arrêter le frontend ? [Y/n] : "
102+
read ans_front
103+
ans_front=${ans_front:-Y}
104+
105+
if [[ "$ans_front" =~ ^[Yy]$ ]]; then
106+
"$SCRIPT_DIR/scripts/start-front.sh" stop
107+
echo -e "${GREEN}🛑 Frontend arrêté${NC}"
108+
else
109+
echo -e "${YELLOW}Frontend non arrêté${NC}"
53110
fi
54-
echo -e "${GREEN}🛑 Services arrêtés${NC}"
55111
}
56112

57113
status() {

scripts/setup-env-back.sh

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
#!/bin/bash
2+
set -e
3+
4+
source "$(dirname "${BASH_SOURCE[0]}")/config.sh"
5+
6+
echo -e "${BLUE}🔧 Initialisation de l'environnement${NC}"
7+
8+
if [ ! -f "$ENV_FILE" ]; then
9+
cat > "$ENV_FILE" <<EOF
10+
DB_HOST=localhost
11+
DB_PORT=5432
12+
DB_NAME=upstradb
13+
DB_USERNAME=upstra
14+
DB_PASSWORD=
15+
16+
REDIS_HOST=localhost
17+
REDIS_PORT=6379
18+
REDIS_PASSWORD=
19+
REDIS_USERNAME=default
20+
REDIS_TLS=
21+
22+
JWT_SECRET=
23+
JWT_REFRESH_SECRET=
24+
SESSION_SECRET=
25+
JWT_EXPIRATION=1h
26+
JWT_2FA_TOKEN_EXPIRATION=5m
27+
JWT_ACCESS_TOKEN_EXPIRATION=15m
28+
JWT_REFRESH_TOKEN_EXPIRATION=7d
29+
30+
FRONTEND_URL=http://localhost:5173
31+
BACKEND_URL=http://localhost:8080
32+
33+
RATE_LIMIT_GLOBAL_WINDOW_MS=900000
34+
RATE_LIMIT_GLOBAL_MAX=1000
35+
36+
RATE_LIMIT_AUTH_WINDOW_MS=900000
37+
RATE_LIMIT_AUTH_STRICT_MAX=5
38+
RATE_LIMIT_AUTH_MODERATE_MAX=10
39+
40+
RATE_LIMIT_SENSITIVE_WINDOW_MS=3600000
41+
RATE_LIMIT_SENSITIVE_MAX=3
42+
43+
RATE_LIMIT_API_WINDOW_MS=300000
44+
RATE_LIMIT_API_MAX=100
45+
46+
GITHUB_TOKEN=
47+
48+
FRONT_REPO=Upstra/infra-control_front
49+
BACK_REPO=Upstra/infra-control
50+
51+
USE_LOCAL_DB=true
52+
USE_MONITORING=false
53+
EOF
54+
echo -e "${GREEN}Fichier $ENV_FILE créé (template)${NC}"
55+
fi
56+
57+
set -a
58+
source "$ENV_FILE"
59+
set +a
60+
61+
read -p "Mot de passe PostgreSQL (vide→générer) : " dbpass
62+
if [ -z "$dbpass" ]; then
63+
dbpass=$(openssl rand -base64 20)
64+
echo -e "${YELLOW}Généré : $dbpass${NC}"
65+
fi
66+
sed -i "s|^DB_PASSWORD=.*|DB_PASSWORD=$dbpass|" "$ENV_FILE"
67+
68+
read -p "Mot de passe Redis (vide→générer) : " redispass
69+
if [ -z "$redispass" ]; then
70+
redispass=$(openssl rand -base64 20)
71+
echo -e "${YELLOW}Généré : $redispass${NC}"
72+
fi
73+
sed -i "s|^REDIS_PASSWORD=.*|REDIS_PASSWORD=$redispass|" "$ENV_FILE"
74+
75+
read -p "GitHub Token (pat_xxx…) : " tok && sed -i "s|^GITHUB_TOKEN=.*|GITHUB_TOKEN=$tok|" "$ENV_FILE"
76+
read -p "Frontend URL [http://localhost:5173] : " fu && [ -n "$fu" ] && sed -i "s|^FRONTEND_URL=.*|FRONTEND_URL=$fu|" "$ENV_FILE"
77+
read -p "Backend URL [http://localhost:8080] : " bu && [ -n "$bu" ] && sed -i "s|^BACKEND_URL=.*|BACKEND_URL=$bu|" "$ENV_FILE"
78+
79+
grep -q '^JWT_SECRET=' "$ENV_FILE" && grep -q '^JWT_SECRET=$' "$ENV_FILE" \
80+
&& sed -i "s|^JWT_SECRET=.*|JWT_SECRET=$(rand_hex)|" "$ENV_FILE"
81+
grep -q '^JWT_REFRESH_SECRET=' "$ENV_FILE" && grep -q '^JWT_REFRESH_SECRET=$' "$ENV_FILE" \
82+
&& sed -i "s|^JWT_REFRESH_SECRET=.*|JWT_REFRESH_SECRET=$(rand_hex)|" "$ENV_FILE"
83+
grep -q '^SESSION_SECRET=' "$ENV_FILE" && grep -q '^SESSION_SECRET=$' "$ENV_FILE" \
84+
&& sed -i "s|^SESSION_SECRET=.*|SESSION_SECRET=$(rand_hex)|" "$ENV_FILE"
85+
86+
read -p "Utiliser Postgres/Redis EN LOCAL ? (y/n) : " ans
87+
if [[ "$ans" =~ ^[Yy]$ ]]; then
88+
sed -i 's/^USE_LOCAL_DB=.*/USE_LOCAL_DB=true/' "$ENV_FILE"
89+
90+
read -p "→ Installer PostgreSQL + uuid-ossp localement ? (y/n) : " ipg
91+
if [[ "$ipg" =~ ^[Yy]$ ]]; then
92+
echo -e "${YELLOW}Installation PostgreSQL…${NC}"
93+
94+
sudo dnf install -y postgresql-server postgresql-contrib
95+
sudo postgresql-setup --initdb
96+
sudo systemctl enable --now postgresql
97+
98+
PGDATA=$(sudo -u postgres psql -tAc "show data_directory;" | xargs)
99+
100+
if ! grep -q '^local *all *postgres *peer' "$PGDATA/pg_hba.conf"; then
101+
sudo sed -i '1ilocal all postgres peer' "$PGDATA/pg_hba.conf"
102+
fi
103+
104+
sudo sed -i "s/^\(local.*all.*all.*\)peer/\1md5/" "$PGDATA/pg_hba.conf"
105+
sudo sed -i "s/^\(host.*all.*all.*127.0.0.1\/32.*\)ident/\1md5/" "$PGDATA/pg_hba.conf"
106+
sudo sed -i "s/^\(host.*all.*all.*::1\/128.*\)ident/\1md5/" "$PGDATA/pg_hba.conf"
107+
108+
if ! grep -q "$DB_USERNAME" "$PGDATA/pg_hba.conf"; then
109+
echo "host all $DB_USERNAME 127.0.0.1/32 md5" | sudo tee -a "$PGDATA/pg_hba.conf"
110+
fi
111+
sudo systemctl restart postgresql
112+
sudo -u postgres psql -c "CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\";"
113+
sudo -u postgres psql -tc "SELECT 1 FROM pg_roles WHERE rolname='$DB_USERNAME'" | grep -q 1 || \
114+
sudo -u postgres psql -c "CREATE USER $DB_USERNAME WITH PASSWORD '$DB_PASSWORD';"
115+
sudo -u postgres psql -tc "SELECT 1 FROM pg_database WHERE datname='$DB_NAME'" | grep -q 1 || \
116+
sudo -u postgres psql -c "CREATE DATABASE $DB_NAME OWNER $DB_USERNAME ENCODING 'UTF8' LC_COLLATE='en_US.UTF-8' LC_CTYPE='en_US.UTF-8' TEMPLATE=template0;"
117+
sudo -u postgres psql -c "ALTER USER $DB_USERNAME WITH SUPERUSER;"
118+
sudo -u postgres psql -c "ALTER USER $DB_USERNAME WITH PASSWORD '$DB_PASSWORD';"
119+
120+
echo -e "${GREEN}✅ PostgreSQL + uuid-ossp OK${NC}"
121+
fi
122+
read -p "→ Installer Redis localement ? (y/n) : " ir
123+
if [[ "$ir" =~ ^[Yy]$ ]]; then
124+
echo -e "${YELLOW}Installation Redis…${NC}"
125+
sudo dnf install -y redis
126+
sudo systemctl enable --now redis
127+
echo -e "${GREEN}✅ Redis OK${NC}"
128+
fi
129+
130+
else
131+
sed -i 's/^USE_LOCAL_DB=.*/USE_LOCAL_DB=false/' "$ENV_FILE"
132+
fi
133+
134+
read -p "Démarrer Monitoring (Prometheus + Grafana) ? (y/n) : " mon
135+
if [[ "$mon" =~ ^[Yy]$ ]]; then
136+
sed -i 's/^USE_MONITORING=.*/USE_MONITORING=true/' "$ENV_FILE"
137+
else
138+
sed -i 's/^USE_MONITORING=.*/USE_MONITORING=false/' "$ENV_FILE"
139+
fi
140+
141+
echo -e "${GREEN}✔ setup-env terminé — vérifiez $ENV_FILE${NC}"

scripts/setup-env-front.sh

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# Pour les couleurs (optionnel)
5+
RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m'; BLUE='\033[0;34m'; NC='\033[0m'
6+
7+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
8+
BACK_ENV="$SCRIPT_DIR/../.env"
9+
10+
if grep -q "^FRONT_DIR=" "$BACK_ENV"; then
11+
FRONT_DIR=$(grep "^FRONT_DIR=" "$BACK_ENV" | cut -d= -f2-)
12+
read -p "Chemin du dossier FRONT [actuel: $FRONT_DIR]: " path
13+
FRONT_DIR=${path:-$FRONT_DIR}
14+
else
15+
read -p "Chemin du dossier FRONT [par défaut: /home/upstra/infra-control_front]: " path
16+
FRONT_DIR=${path:-/home/upstra/infra-control_front}
17+
fi
18+
19+
if grep -q "^FRONT_DIR=" "$BACK_ENV"; then
20+
sed -i "s|^FRONT_DIR=.*|FRONT_DIR=$FRONT_DIR|" "$BACK_ENV"
21+
else
22+
echo "FRONT_DIR=$FRONT_DIR" >> "$BACK_ENV"
23+
fi
24+
25+
FRONT_ENV="$FRONT_DIR/.env"
26+
27+
echo -e "${BLUE}📝 Configuration de l'environnement FRONT${NC}"
28+
29+
read -p "VITE_API_URL [http://localhost:3000]: " VITE_API_URL
30+
VITE_API_URL=${VITE_API_URL:-http://localhost:3000}
31+
32+
read -p "VITE_WS_URL [http://localhost:3000]: " VITE_WS_URL
33+
VITE_WS_URL=${VITE_WS_URL:-http://localhost:3000}
34+
35+
read -p "VITE_FRONT_PORT [4173]: " VITE_FRONT_PORT
36+
VITE_FRONT_PORT=${VITE_FRONT_PORT:-4173}
37+
38+
if [ ! -d "$FRONT_DIR" ]; then
39+
echo -e "${RED}❌ Le dossier front '$FRONT_DIR' n'existe pas.${NC}"
40+
exit 1
41+
fi
42+
43+
cat > "$FRONT_ENV" <<EOF
44+
VITE_API_URL=$VITE_API_URL
45+
VITE_WS_URL=$VITE_WS_URL
46+
VITE_FRONT_PORT=$VITE_FRONT_PORT
47+
EOF
48+
49+
echo -e "${GREEN}✅ Fichier $FRONT_ENV généré !${NC}"

0 commit comments

Comments
 (0)