-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
284 lines (238 loc) · 11.4 KB
/
install.sh
File metadata and controls
284 lines (238 loc) · 11.4 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#!/usr/bin/env bash
# =============================================================================
# BrewHome — Script d'installation
# Usage : sudo bash install.sh
# Distributions supportées : Debian/Ubuntu, Fedora/RHEL, Arch, Alpine Linux
# =============================================================================
set -euo pipefail
# ── Couleurs ──────────────────────────────────────────────────────────────────
RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m'
CYAN='\033[0;36m'; BOLD='\033[1m'; RESET='\033[0m'
info() { echo -e "${CYAN}[INFO]${RESET} $*"; }
success() { echo -e "${GREEN}[OK]${RESET} $*"; }
warn() { echo -e "${YELLOW}[WARN]${RESET} $*"; }
error() { echo -e "${RED}[ERREUR]${RESET} $*" >&2; exit 1; }
step() { echo -e "\n${BOLD}━━━ $* ━━━${RESET}"; }
# ── Vérifications préalables ──────────────────────────────────────────────────
[ "$(id -u)" -eq 0 ] || error "Ce script doit être exécuté en root (sudo bash install.sh)"
DISTRO=""
if [ -f /etc/os-release ]; then
. /etc/os-release
DISTRO="${ID:-}"
fi
# ── Variables ─────────────────────────────────────────────────────────────────
APP_NAME="brewhome"
INSTALL_DIR="/opt/${APP_NAME}"
APP_USER="brewhome"
APP_PORT=5000
# Répertoire source : dossier contenant ce script
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SRC_DIR="${SCRIPT_DIR}/brewhome"
[ -d "${SRC_DIR}" ] || error "Répertoire source introuvable : ${SRC_DIR}"
# =============================================================================
step "1/6 — Dépendances système"
# =============================================================================
if command -v apk &>/dev/null; then
info "Système Alpine Linux détecté"
apk update --quiet
# bash requis pour ce script, python3, pip, venv, curl
apk add --no-cache bash python3 py3-pip curl
# py3-virtualenv ou le module venv inclus dans python3 selon la version
python3 -m venv --help &>/dev/null || apk add --no-cache py3-virtualenv
NOLOGIN_SHELL="/sbin/nologin"
elif command -v apt-get &>/dev/null; then
info "Système Debian/Ubuntu détecté"
apt-get update -qq
apt-get install -y -qq python3 python3-venv python3-pip curl
NOLOGIN_SHELL="/usr/sbin/nologin"
elif command -v dnf &>/dev/null; then
info "Système Fedora/RHEL détecté"
dnf install -y -q python3 python3-pip curl
NOLOGIN_SHELL="/usr/sbin/nologin"
elif command -v pacman &>/dev/null; then
info "Système Arch Linux détecté"
pacman -Sy --noconfirm python python-pip curl
NOLOGIN_SHELL="/usr/sbin/nologin"
else
warn "Gestionnaire de paquets non reconnu — vérifiez manuellement que python3, pip et venv sont installés"
NOLOGIN_SHELL="/usr/sbin/nologin"
fi
python3 --version &>/dev/null || error "python3 introuvable après installation"
success "Python $(python3 --version 2>&1 | awk '{print $2}') disponible"
# ── Détection du gestionnaire de services ─────────────────────────────────────
if command -v rc-service &>/dev/null && [ -d /etc/init.d ]; then
SERVICE_MANAGER="openrc"
info "Gestionnaire de services : OpenRC"
else
SERVICE_MANAGER="systemd"
info "Gestionnaire de services : systemd"
fi
# =============================================================================
step "2/6 — Création de l'utilisateur système"
# =============================================================================
if id "${APP_USER}" &>/dev/null; then
info "Utilisateur '${APP_USER}' déjà existant"
else
if [ "${DISTRO}" = "alpine" ]; then
# Alpine utilise adduser (busybox), syntaxe différente de useradd
adduser -S -D -H -s "${NOLOGIN_SHELL}" "${APP_USER}"
else
useradd --system --no-create-home --shell "${NOLOGIN_SHELL}" "${APP_USER}"
fi
success "Utilisateur système '${APP_USER}' créé"
fi
# =============================================================================
step "3/6 — Copie des fichiers dans ${INSTALL_DIR}"
# =============================================================================
if [ -d "${INSTALL_DIR}" ]; then
warn "Le répertoire ${INSTALL_DIR} existe déjà — mise à jour des fichiers"
for db in brewhome.db brewhome_readings.db; do
if [ -f "${INSTALL_DIR}/${db}" ]; then
info "Base de données préservée : ${db}"
fi
done
fi
mkdir -p "${INSTALL_DIR}"
# Copie en excluant venv et les bases SQLite existantes
rsync -a --exclude='venv/' --exclude='*.db' "${SRC_DIR}/" "${INSTALL_DIR}/" 2>/dev/null \
|| cp -r "${SRC_DIR}/." "${INSTALL_DIR}/"
chown -R "${APP_USER}:${APP_USER}" "${INSTALL_DIR}"
success "Fichiers copiés dans ${INSTALL_DIR}"
# =============================================================================
step "4/6 — Environnement virtuel Python & dépendances"
# =============================================================================
VENV_DIR="${INSTALL_DIR}/venv"
if [ ! -d "${VENV_DIR}" ]; then
info "Création de l'environnement virtuel…"
python3 -m venv "${VENV_DIR}"
fi
info "Installation des dépendances Python…"
"${VENV_DIR}/bin/pip" install --quiet --upgrade pip
"${VENV_DIR}/bin/pip" install --quiet -r "${INSTALL_DIR}/requirements.txt"
chown -R "${APP_USER}:${APP_USER}" "${VENV_DIR}"
FLASK_VER=$(${VENV_DIR}/bin/pip show flask 2>/dev/null | grep ^Version | awk '{print $2}')
APScheduler_VER=$(${VENV_DIR}/bin/pip show apscheduler 2>/dev/null | grep ^Version | awk '{print $2}')
success "Dépendances installées (Flask ${FLASK_VER}, APScheduler ${APScheduler_VER})"
# =============================================================================
step "5/6 — Service ${SERVICE_MANAGER}"
# =============================================================================
if [ "${SERVICE_MANAGER}" = "openrc" ]; then
# ── OpenRC (Alpine Linux) ──────────────────────────────────────────────────
INIT_SCRIPT="/etc/init.d/${APP_NAME}"
LOG_FILE="/var/log/${APP_NAME}.log"
cat > "${INIT_SCRIPT}" <<EOF
#!/sbin/openrc-run
description="BrewHome - Gestion Brasserie"
command="${VENV_DIR}/bin/python"
command_args="app.py"
command_background=true
directory="${INSTALL_DIR}"
command_user="${APP_USER}:${APP_USER}"
pidfile="/run/${APP_NAME}.pid"
output_log="${LOG_FILE}"
error_log="${LOG_FILE}"
depend() {
need net
}
EOF
chmod +x "${INIT_SCRIPT}"
touch "${LOG_FILE}"
chown "${APP_USER}:${APP_USER}" "${LOG_FILE}"
rc-update add "${APP_NAME}" default 2>/dev/null || true
success "Script OpenRC créé et activé au démarrage (runlevel default)"
else
# ── systemd ───────────────────────────────────────────────────────────────
SERVICE_FILE="/etc/systemd/system/${APP_NAME}.service"
cat > "${SERVICE_FILE}" <<EOF
[Unit]
Description=BrewHome - Gestion Brasserie
After=network.target
StartLimitIntervalSec=60
StartLimitBurst=5
[Service]
Type=simple
User=${APP_USER}
Group=${APP_USER}
WorkingDirectory=${INSTALL_DIR}
ExecStart=${VENV_DIR}/bin/python app.py
Restart=on-failure
RestartSec=5
StandardOutput=journal
StandardError=journal
SyslogIdentifier=${APP_NAME}
# Sécurité
NoNewPrivileges=true
PrivateTmp=true
ProtectSystem=full
ReadWritePaths=${INSTALL_DIR}
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable "${APP_NAME}.service"
success "Service systemd créé et activé au démarrage"
fi
# =============================================================================
step "6/6 — Démarrage du service"
# =============================================================================
if [ "${SERVICE_MANAGER}" = "openrc" ]; then
rc-service "${APP_NAME}" start
sleep 2
if rc-service "${APP_NAME}" status 2>&1 | grep -q "started"; then
success "Service démarré avec succès"
else
warn "Le service n'a pas démarré — consultez les logs :"
echo " tail -n 30 /var/log/${APP_NAME}.log"
fi
else
systemctl restart "${APP_NAME}.service"
sleep 2
if systemctl is-active --quiet "${APP_NAME}.service"; then
success "Service démarré avec succès"
else
warn "Le service n'a pas démarré — consultez les logs :"
echo " journalctl -u ${APP_NAME} -n 30 --no-pager"
fi
fi
# =============================================================================
# Récupération de l'IP locale
# =============================================================================
LOCAL_IP=$(ip route get 1.1.1.1 2>/dev/null | awk '{for(i=1;i<=NF;i++) if ($i=="src") print $(i+1)}' | head -1)
[ -z "${LOCAL_IP}" ] && LOCAL_IP="<IP-du-serveur>"
# =============================================================================
echo ""
echo -e "${GREEN}${BOLD}╔══════════════════════════════════════════════════════╗${RESET}"
echo -e "${GREEN}${BOLD}║ BrewHome installé avec succès ! ║${RESET}"
echo -e "${GREEN}${BOLD}╚══════════════════════════════════════════════════════╝${RESET}"
echo ""
echo -e " ${BOLD}Accès web${RESET}"
echo -e " ${CYAN}http://${LOCAL_IP}:${APP_PORT}${RESET}"
echo -e " ${CYAN}http://localhost:${APP_PORT}${RESET}"
echo ""
echo -e " ${BOLD}Répertoire d'installation${RESET}"
echo -e " ${INSTALL_DIR}"
echo ""
if [ "${SERVICE_MANAGER}" = "openrc" ]; then
echo -e " ${BOLD}Commandes du service (OpenRC)${RESET}"
echo -e " Démarrer : ${YELLOW}rc-service ${APP_NAME} start${RESET}"
echo -e " Arrêter : ${YELLOW}rc-service ${APP_NAME} stop${RESET}"
echo -e " Redémarrer : ${YELLOW}rc-service ${APP_NAME} restart${RESET}"
echo -e " Statut : ${YELLOW}rc-service ${APP_NAME} status${RESET}"
echo -e " Logs live : ${YELLOW}tail -f /var/log/${APP_NAME}.log${RESET}"
echo ""
echo -e " ${BOLD}Activer / désactiver au démarrage${RESET}"
echo -e " Activer : ${YELLOW}rc-update add ${APP_NAME} default${RESET}"
echo -e " Désactiver : ${YELLOW}rc-update del ${APP_NAME} default${RESET}"
else
echo -e " ${BOLD}Commandes du service (systemd)${RESET}"
echo -e " Démarrer : ${YELLOW}sudo systemctl start ${APP_NAME}${RESET}"
echo -e " Arrêter : ${YELLOW}sudo systemctl stop ${APP_NAME}${RESET}"
echo -e " Redémarrer : ${YELLOW}sudo systemctl restart ${APP_NAME}${RESET}"
echo -e " Statut : ${YELLOW}sudo systemctl status ${APP_NAME}${RESET}"
echo -e " Logs live : ${YELLOW}sudo journalctl -u ${APP_NAME} -f${RESET}"
echo ""
echo -e " ${BOLD}Activer / désactiver au démarrage${RESET}"
echo -e " Activer : ${YELLOW}sudo systemctl enable ${APP_NAME}${RESET}"
echo -e " Désactiver : ${YELLOW}sudo systemctl disable ${APP_NAME}${RESET}"
fi
echo ""