mise à jour update
This commit is contained in:
@@ -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();
|
||||
Reference in New Issue
Block a user