From d20e1b27895a131617e93f77ff015e2d07932249 Mon Sep 17 00:00:00 2001 From: Nicolas Arenas Date: Mon, 18 May 2026 11:31:37 +0200 Subject: [PATCH] =?UTF-8?q?fix(ogcore):=20robustez=20y=20rotaci=C3=B3n=20d?= =?UTF-8?q?e=20logs=20en=20infraestructura=20journal-pull?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - journal-pull@.service: Restart=always para reconectar tras salida limpia, ExecStartPre elimina el fichero solo si está vacío para evitar cursor inválido - JournalPullSubscriber: protección contra borrado de IP compartida entre componentes al eliminar un servidor; comprueba que ninguna otra entidad BootServer, DhcpServer o ImageRepository usa la misma IP antes de eliminarla de journal-pull-sources.conf - logrotate.d/journal-pull: rotación diaria con 10 días de retención para los ficheros de journal remoto; nocompress para ficheros binarios; para/arranca los servicios journal-pull@ durante la rotación - debian/control: añadidos logrotate y systemd-journal-remote como dependencias - debian/ogcore.postinst: enlace de logrotate config, permisos 775 en api/var/ --- debian/control | 2 +- debian/ogcore.postinst | 2 ++ etc/logrotate.d/journal-pull | 18 ++++++++++++ etc/systemd/system/journal-pull@.service | 3 +- src/EventSubscriber/JournalPullSubscriber.php | 28 +++++++++++++++++-- 5 files changed, 48 insertions(+), 5 deletions(-) create mode 100644 etc/logrotate.d/journal-pull diff --git a/debian/control b/debian/control index c85ee6b..08f372b 100644 --- a/debian/control +++ b/debian/control @@ -10,7 +10,7 @@ Architecture: all Depends: ${misc:Depends}, mariadb-server, systemd, nginx, libzip-dev, zip, unzip, php8.3-opcache, php8.3-bcmath, php8.3-cli, php8.3-curl, php8.3-fpm, php8.3-gd, php8.3-ldap, php8.3-mbstring, php8.3-mysql, php8.3-common, - php8.3-xml, php8.3-zip, mercure, systemd-journal-remote + php8.3-xml, php8.3-zip, mercure, systemd-journal-remote, logrotate Description: OpenGnsys Core OgCore package is responsible about management of Opengnsys platform OpenGnsys Core is a platform for system management. diff --git a/debian/ogcore.postinst b/debian/ogcore.postinst index 0b3de69..68ab6df 100644 --- a/debian/ogcore.postinst +++ b/debian/ogcore.postinst @@ -82,6 +82,7 @@ if [ "$1" = "configure" ] && [ -z "$2" ]; then ln -s /opt/opengnsys/ogcore/etc/systemd/system/journal-pull@.service /etc/systemd/system/journal-pull@.service ln -s /opt/opengnsys/ogcore/etc/systemd/system/journal-pull-sync.path /etc/systemd/system/journal-pull-sync.path ln -s /opt/opengnsys/ogcore/etc/systemd/system/journal-pull-sync.service /etc/systemd/system/journal-pull-sync.service + ln -s /opt/opengnsys/ogcore/etc/logrotate.d/journal-pull /etc/logrotate.d/journal-pull echo ">>> Configurando journal-pull" touch /opt/opengnsys/ogcore/etc/journal-pull-sources.conf @@ -92,6 +93,7 @@ if [ "$1" = "configure" ] && [ -z "$2" ]; then echo ">>> Configurando permisos de archivos" chown opengnsys:www-data /opt/opengnsys/ chown -R opengnsys:www-data /opt/opengnsys/ogcore + chmod -R 775 /opt/opengnsys/ogcore/api/var/ chown www-data:www-data /opt/opengnsys/ogcore/etc/journal-pull-sources.conf chmod 664 /opt/opengnsys/ogcore/etc/journal-pull-sources.conf usermod -aG systemd-journal opengnsys diff --git a/etc/logrotate.d/journal-pull b/etc/logrotate.d/journal-pull new file mode 100644 index 0000000..d0db540 --- /dev/null +++ b/etc/logrotate.d/journal-pull @@ -0,0 +1,18 @@ +/var/log/journal/remote/*.journal { + daily + rotate 10 + dateext + missingok + nocompress + sharedscripts + prerotate + systemctl stop $(systemctl list-units --plain --no-legend \ + --state=active 'journal-pull@*.service' \ + | awk '{print $1}') 2>/dev/null || true + endscript + postrotate + systemctl start $(systemctl list-units --plain --no-legend \ + --state=inactive 'journal-pull@*.service' \ + | awk '{print $1}') 2>/dev/null || true + endscript +} diff --git a/etc/systemd/system/journal-pull@.service b/etc/systemd/system/journal-pull@.service index fa9e91b..b15e956 100644 --- a/etc/systemd/system/journal-pull@.service +++ b/etc/systemd/system/journal-pull@.service @@ -6,8 +6,9 @@ Wants=network-online.target [Service] User=systemd-journal-remote Group=systemd-journal-remote +ExecStartPre=/bin/sh -c 'f=/var/log/journal/remote/%i.journal; [ -f "$f" ] && journalctl --file="$f" -n 1 --no-pager 2>/dev/null | grep -q "." || rm -f "$f"' ExecStart=/usr/lib/systemd/systemd-journal-remote --url=http://%i:19531/entries?SYSLOG_IDENTIFIER=ogcore&SYSLOG_IDENTIFIER=oggui&SYSLOG_IDENTIFIER=ogdhcp&SYSLOG_IDENTIFIER=ogboot&SYSLOG_IDENTIFIER=ogrepo-api&SYSLOG_IDENTIFIER=ogrepo-api_DEBUG&SYSLOG_IDENTIFIER=kea-dhcp4&SYSLOG_IDENTIFIER=kea-ctrl-agent&SYSLOG_IDENTIFIER=DhcpLFC&SYSLOG_IDENTIFIER=kea-dhcp6&SYSLOG_IDENTIFIER=tftpd-hpa&SYSLOG_IDENTIFIER=%%28smbd%%29&SYSLOG_IDENTIFIER=%%28nmbd%%29 --split-mode=none --output=/var/log/journal/remote/%i.journal -Restart=on-failure +Restart=always RestartSec=10 [Install] diff --git a/src/EventSubscriber/JournalPullSubscriber.php b/src/EventSubscriber/JournalPullSubscriber.php index dcdf5ac..5233bf6 100644 --- a/src/EventSubscriber/JournalPullSubscriber.php +++ b/src/EventSubscriber/JournalPullSubscriber.php @@ -7,6 +7,7 @@ use App\Entity\DhcpServer; use App\Entity\ImageRepository; use App\Service\JournalPullService; use Doctrine\Bundle\DoctrineBundle\Attribute\AsDoctrineListener; +use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\Event\PostPersistEventArgs; use Doctrine\ORM\Event\PostRemoveEventArgs; use Doctrine\ORM\Event\PostUpdateEventArgs; @@ -22,7 +23,10 @@ class JournalPullSubscriber /** @var array */ private array $pendingIpChanges = []; - public function __construct(private readonly JournalPullService $journalPullService) {} + public function __construct( + private readonly JournalPullService $journalPullService, + private readonly EntityManagerInterface $entityManager, + ) {} private function supports(object $entity): bool { @@ -46,7 +50,23 @@ class JournalPullSubscriber if (!$this->supports($entity)) { return; } - $this->journalPullService->remove($entity->getIp()); + if (!$this->isIpStillInUse($entity->getIp(), $entity)) { + $this->journalPullService->remove($entity->getIp()); + } + } + + private function isIpStillInUse(string $ip, object $excludedEntity): bool + { + $types = [BootServer::class, DhcpServer::class, ImageRepository::class]; + foreach ($types as $class) { + $results = $this->entityManager->getRepository($class)->findBy(['ip' => $ip]); + foreach ($results as $entity) { + if ($entity !== $excludedEntity) { + return true; + } + } + } + return false; } public function preUpdate(PreUpdateEventArgs $args): void @@ -74,7 +94,9 @@ class JournalPullSubscriber ['old' => $oldIp, 'new' => $newIp] = $this->pendingIpChanges[$oid]; unset($this->pendingIpChanges[$oid]); - $this->journalPullService->remove($oldIp); + if (!$this->isIpStillInUse($oldIp, $entity)) { + $this->journalPullService->remove($oldIp); + } $this->journalPullService->add($newIp); } }