|
| 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}" |
0 commit comments