simplificaiton config
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
#!/usr/bin/env php
|
||||
<?php
|
||||
/**
|
||||
* Moteur de mise à jour - Version Simplifiée et Autonome
|
||||
* Moteur de mise à jour - Version Supervisor
|
||||
* Pilotage du script Bash + Initialisation des Configs + Cron
|
||||
* Copyright (C) 2026 Cédric Abonnel
|
||||
*/
|
||||
|
||||
require_once __DIR__ . '/../lib/monitoring-lib.php';
|
||||
|
||||
// Sécurité : Un seul update à la fois
|
||||
@@ -19,7 +21,8 @@ if (!file_exists($install_script)) {
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// 1. Exécution du moteur de synchronisation Bash
|
||||
// 1. Exécution du moteur de synchronisation Bash (il gère téléchargement et purge)
|
||||
// On utilise popen pour lire la sortie en temps réel à l'écran
|
||||
$command = "bash " . escapeshellarg($install_script) . " --auto 2>&1";
|
||||
$handle = popen($command, 'r');
|
||||
|
||||
@@ -27,9 +30,9 @@ if ($handle) {
|
||||
while (!feof($handle)) {
|
||||
$line = fgets($handle);
|
||||
if ($line) {
|
||||
echo $line; // Affichage en temps réel à l'écran
|
||||
echo $line; // Affichage direct à l'écran (avec les couleurs du Bash)
|
||||
if (strpos($line, '[ERR]') !== false) {
|
||||
log_error("update_process_error", trim($line));
|
||||
log_error("update_bash_error", trim($line));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -42,15 +45,17 @@ if ($exit_code === 0) {
|
||||
echo "\e[1m--- Finalisation des configurations ---\e[0m\n";
|
||||
|
||||
// 2. Initialisation des fichiers .local manquants
|
||||
// Cela garantit que le système peut tourner même sans config manuelle préalable
|
||||
ensure_local_configs();
|
||||
|
||||
// 3. Vérification du Crontab
|
||||
// Utilise la Regex pour éviter les doublons avec tes anciennes versions (tirets vs underscores)
|
||||
ensure_crontab_entries();
|
||||
|
||||
log_info("update_finished", "Mise à jour réussie");
|
||||
log_info("update_finished", "Mise à jour et configuration réussies");
|
||||
echo "\e[32m[OK]\e[0m Système à jour et configuré.\n";
|
||||
} else {
|
||||
log_error("update_failed", "Le script de synchronisation a échoué", ["code" => $exit_code]);
|
||||
log_error("update_failed", "Le script Bash a retourné une erreur", ["code" => $exit_code]);
|
||||
echo "\e[31m[ERR]\e[0m Échec de la mise à jour.\n";
|
||||
exit($exit_code);
|
||||
}
|
||||
@@ -59,9 +64,9 @@ if ($exit_code === 0) {
|
||||
* Initialise les fichiers .local.conf.php s'ils n'existent pas
|
||||
*/
|
||||
function ensure_local_configs() {
|
||||
global $MONITORING_BASE_DIR;
|
||||
$conf_dir = $MONITORING_BASE_DIR . '/conf';
|
||||
global $MONITORING_CONF_DIR;
|
||||
|
||||
// Mapping Source => Destination
|
||||
$configs = [
|
||||
'monitoring.conf.php' => 'monitoring.local.conf.php',
|
||||
'alert-engine.conf.php' => 'alert-engine.conf.local.php',
|
||||
@@ -69,14 +74,14 @@ function ensure_local_configs() {
|
||||
];
|
||||
|
||||
foreach ($configs as $src => $dst) {
|
||||
$src_path = $conf_dir . '/' . $src;
|
||||
$dst_path = $conf_dir . '/' . $dst;
|
||||
$src_path = $MONITORING_CONF_DIR . '/' . $src;
|
||||
$dst_path = $MONITORING_CONF_DIR . '/' . $dst;
|
||||
|
||||
if (!file_exists($dst_path) && file_exists($src_path)) {
|
||||
if (copy($src_path, $dst_path)) {
|
||||
chmod($dst_path, 0600);
|
||||
chmod($dst_path, 0600); // Protection des secrets
|
||||
log_notice("config_auto_init", "Création auto de $dst");
|
||||
echo "\e[32m[OK]\e[0m Fichier créé : $dst\n";
|
||||
echo "\e[32m[OK]\e[0m Fichier config créé : $dst\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -88,9 +93,10 @@ function ensure_local_configs() {
|
||||
function ensure_crontab_entries() {
|
||||
global $MONITORING_BASE_DIR;
|
||||
|
||||
// Notre "Vérité" pour le planning du serveur
|
||||
$required_jobs = [
|
||||
"*/5 * * * * php {$MONITORING_BASE_DIR}/bin/check_disk.php > /dev/null 2>&1",
|
||||
"*/15 * * * * php {$MONITORING_BASE_DIR}/bin/check_smart.sh > /dev/null 2>&1",
|
||||
"*/15 * * * * bash {$MONITORING_BASE_DIR}/bin/check_smart.sh > /dev/null 2>&1",
|
||||
"10 3 * * * php {$MONITORING_BASE_DIR}/bin/monitoring-update.php > /dev/null 2>&1",
|
||||
"* * * * * php {$MONITORING_BASE_DIR}/bin/alert-engine.php > /dev/null 2>&1"
|
||||
];
|
||||
@@ -100,34 +106,35 @@ function ensure_crontab_entries() {
|
||||
$updated = false;
|
||||
|
||||
foreach ($required_jobs as $job) {
|
||||
// Extraction intelligente du nom du script (ex: check_disk.php)
|
||||
// On cherche simplement si le chemin vers /bin/nom_du_script est dans le cron
|
||||
preg_match('/\/bin\/([a-z0-9_-]+\.(php|sh))/i', $job, $matches);
|
||||
$script_name = $matches[0] ?? $job;
|
||||
|
||||
$found = false;
|
||||
foreach ($lines as $line) {
|
||||
if (strpos($line, $script_name) !== false) {
|
||||
$found = true;
|
||||
break;
|
||||
// On cherche le nom du script dans la ligne pour éviter les doublons
|
||||
// (ex: cherche "/bin/check_disk.php" dans la ligne du cron)
|
||||
if (preg_match('/\/bin\/([a-z0-9_-]+\.(php|sh))/i', $job, $matches)) {
|
||||
$script_pattern = $matches[0];
|
||||
|
||||
$found = false;
|
||||
foreach ($lines as $line) {
|
||||
if (strpos($line, $script_pattern) !== false) {
|
||||
$found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!$found) {
|
||||
$lines[] = $job;
|
||||
$updated = true;
|
||||
echo "\e[32m[OK]\e[0m Ajout au cron : $script_name\n";
|
||||
if (!$found) {
|
||||
$lines[] = $job;
|
||||
$updated = true;
|
||||
echo "\e[32m[OK]\e[0m Ajout au cron : " . basename($script_pattern) . "\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($updated) {
|
||||
// On s'assure qu'il n'y a pas de lignes vides inutiles et on joint avec un saut de ligne final
|
||||
// Filtrage des lignes vides et reconstruction propre
|
||||
$content = implode("\n", array_filter($lines, 'trim')) . "\n";
|
||||
$tmp_cron = tempnam(sys_get_temp_dir(), 'cron');
|
||||
file_put_contents($tmp_cron, $content);
|
||||
exec("crontab " . escapeshellarg($tmp_cron));
|
||||
unlink($tmp_cron);
|
||||
echo "\e[32m[OK]\e[0m Crontab mis à jour avec succès.\n";
|
||||
echo "\e[32m[OK]\e[0m Crontab de root mis à jour.\n";
|
||||
} else {
|
||||
echo "[INFO] Crontab déjà à jour.\n";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user