mise à jour update

This commit is contained in:
2026-03-18 08:27:05 +01:00
parent d48cb923eb
commit 7de4c86feb
2 changed files with 83 additions and 12 deletions

View File

@@ -1,7 +1,8 @@
#!/usr/bin/env php
<?php
/**
* Moteur de mise à jour (Wrapper pour le script Bash)
* Moteur de mise à jour - Version Simplifiée et Autonome
* Pilotage du script Bash + Initialisation des Configs + Cron
*/
require_once __DIR__ . '/../lib/monitoring-lib.php';
@@ -18,7 +19,7 @@ if (!file_exists($install_script)) {
exit(1);
}
// On ouvre un pipe pour lire la sortie du Bash en temps réel
// 1. Exécution du moteur de synchronisation Bash
$command = "bash " . escapeshellarg($install_script) . " --auto 2>&1";
$handle = popen($command, 'r');
@@ -26,10 +27,7 @@ if ($handle) {
while (!feof($handle)) {
$line = fgets($handle);
if ($line) {
// 1. On affiche à l'écran en temps réel
echo $line;
// 2. On loggue les erreurs importantes dans le JSONL
echo $line; // Affichage en temps réel à l'écran
if (strpos($line, '[ERR]') !== false) {
log_error("update_process_error", trim($line));
}
@@ -41,18 +39,91 @@ if ($handle) {
}
if ($exit_code === 0) {
// Tâches de finalisation PHP
echo "\e[1m--- Finalisation des configurations ---\e[0m\n";
run_local_conf_sync(); // Audit des fichiers .local
ensure_crontab_entries(); // Vérification du cron
// 2. Initialisation des fichiers .local manquants
ensure_local_configs();
// 3. Vérification du Crontab
ensure_crontab_entries();
log_info("update_finished", "Mise à jour réussie");
echo "\e[32m[OK]\e[0m Mise à jour terminée avec succès.\n";
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]);
echo "\e[31m[ERR]\e[0m Échec de la mise à jour (Code: $exit_code).\n";
echo "\e[31m[ERR]\e[0m Échec de la mise à jour.\n";
exit($exit_code);
}
/**
* 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';
$configs = [
'monitoring.conf.php' => 'monitoring.local.conf.php',
'alert-engine.conf.php' => 'alert-engine.conf.local.php',
'autoupdate.conf.php' => 'autoupdate.local.conf.php'
];
foreach ($configs as $src => $dst) {
$src_path = $conf_dir . '/' . $src;
$dst_path = $conf_dir . '/' . $dst;
if (!file_exists($dst_path) && file_exists($src_path)) {
if (copy($src_path, $dst_path)) {
chmod($dst_path, 0600);
log_notice("config_auto_init", "Création auto de $dst");
echo "\e[32m[OK]\e[0m Fichier créé : $dst\n";
}
}
}
}
/**
* Assure la présence des tâches dans le crontab de root
*/
function ensure_crontab_entries() {
global $MONITORING_BASE_DIR;
$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",
"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"
];
$current_cron = shell_exec("crontab -l 2>/dev/null") ?: "";
$lines = explode("\n", trim($current_cron));
$updated = false;
foreach ($required_jobs as $job) {
$parts = explode(' ', $job);
$script_path = $parts[5]; // Chemin du script
$found = false;
foreach ($lines as $line) {
if (strpos($line, $script_path) !== false) {
$found = true;
break;
}
}
if (!$found) {
$lines[] = $job;
$updated = true;
echo "\e[32m[OK]\e[0m Ajout au cron : " . basename($script_path) . "\n";
}
}
if ($updated) {
$tmp_cron = tempnam(sys_get_temp_dir(), 'cron');
file_put_contents($tmp_cron, implode("\n", $lines) . "\n");
exec("crontab " . escapeshellarg($tmp_cron));
unlink($tmp_cron);
}
}
exit_with_status();

View File

@@ -5,7 +5,7 @@ a724b2673400cc539a3ed7e6285b346ca68b302d5bd119f8290ed87c17a1fe33 755 bin/install
97a91b13b0776acb3326010821ffcc163e96a97e3c326ea77f11efdb7baf159a 755 bin/log-cli.php
ea5a5d55bb877ae88da6e1cd1b798026a1de1d9845dc42af4b19685ad6a128c6 755 bin/monitoring.php
97d407d75a26bd2ebbb86a2e5f8dab8b24639e8a9164f42bd554ba7728ab8cb5 755 bin/monitoring-update-config.php
20b878b240461d015e296014a85c90aa291e11c479441e21516ef57727689c91 755 bin/monitoring-update.php
b4bd7ead3075867f38cf5be31058120c086f6e6fd608d296cceec16bd4aec1cc 755 bin/monitoring-update.php
83db39c8d0cfd6f6e9d3cc5b961a67db29dc73666304a91e0d4a6d5831c623cb 644 conf/alert-engine.conf
69fc1e3506aec7ad3e5d9fbc74587ab4e6381f3e6840f3e38c526f4752858bd4 644 conf/alert-engine.conf.php
caaa8f6031d66bc43a897ac2804124ce2050a64523734195d5505ae863836bf4 644 conf/monitoring.conf