diff --git a/Jenkins/Jenkinsfile-deb-pkg b/Jenkins/Jenkinsfile-deb-pkg index 2b4384a..75a6f40 100644 --- a/Jenkins/Jenkinsfile-deb-pkg +++ b/Jenkins/Jenkinsfile-deb-pkg @@ -61,6 +61,19 @@ pipeline { } } + stage('Generate Changelog (Devel)') { + when { + branch 'devel' + } + steps { + script { + def devName = params.DEV_NAME ? params.DEV_NAME : env.DEFAULT_DEV_NAME + def devEmail = params.DEV_EMAIL ? params.DEV_EMAIL : env.DEFAULT_DEV_EMAIL + generateDebianChangelog(env.BUILD_DIR, devName, devEmail, "devel") + } + }} + } + stage('Build') { steps { script { @@ -96,6 +109,19 @@ pipeline { } } } + + stage ('Publish to Debian Repository (Devel)') { + when { + branch 'devel' + } + agent { label 'debian-repo' } + steps { + script { + def versionPattern = "-${env.BUILD_NUMBER}~devel" + publicarEnAptly('/var/tmp/opengnsys/debian-repo/ogcore', 'devel', versionPattern) + } + } + } } post { success { @@ -108,6 +134,13 @@ pipeline { string(name: 'TRIGGERED_BY', value: "${env.JOB_NAME}-${env.BUILD_NUMBER}") ] } + if (env.BRANCH_NAME == 'devel') { + build job: 'Aptly publish develop repository', + wait: false, + parameters: [ + string(name: 'TRIGGERED_BY', value: "${env.JOB_NAME}-${env.BUILD_NUMBER}") + ] + } } } diff --git a/debian/ogcore.postinst b/debian/ogcore.postinst index f31fdfd..85ddfe1 100644 --- a/debian/ogcore.postinst +++ b/debian/ogcore.postinst @@ -79,12 +79,24 @@ if [ "$1" = "configure" ] && [ -z "$2" ]; then ln -s /opt/opengnsys/ogcore/etc/nginx/sites-available/mercure.conf /etc/nginx/sites-enabled/mercure.conf ln -s /opt/opengnsys/ogcore/etc/php/8.3/fpm/pool.d/ogcore-fpm.conf /etc/php/8.3/fpm/pool.d/ogcore-fpm.conf ln -s /opt/opengnsys/ogcore/etc/systemd/system/og-mercure.service /etc/systemd/system/og-mercure.service + 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 + + echo ">>> Configurando journal-pull" + touch /opt/opengnsys/ogcore/etc/journal-pull-sources.conf + chmod +x /opt/opengnsys/ogcore/etc/bin/journal-pull-sync.sh + mkdir -p /var/log/journal/remote + chown systemd-journal-remote:systemd-journal-remote /var/log/journal/remote echo ">>> Configurando permisos de archivos" chown opengnsys:www-data /opt/opengnsys/ chown -R opengnsys:www-data /opt/opengnsys/ogcore + chown www-data:www-data /opt/opengnsys/ogcore/etc/journal-pull-sources.conf + chmod 664 /opt/opengnsys/ogcore/etc/journal-pull-sources.conf systemctl daemon-reload systemctl enable og-mercure + systemctl enable --now journal-pull-sync.path systemctl restart mariadb.service systemctl restart og-mercure systemctl restart nginx diff --git a/etc/bin/journal-pull-sync.sh b/etc/bin/journal-pull-sync.sh new file mode 100644 index 0000000..ecdf344 --- /dev/null +++ b/etc/bin/journal-pull-sync.sh @@ -0,0 +1,30 @@ +#!/bin/bash +# Syncs journal-pull@ systemd instances with /etc/opengnsys/journal-pull-sources.conf. +# Triggered by journal-pull-sync.path whenever the config file changes. +set -euo pipefail + +CONF=/opt/opengnsys/ogcore/etc/journal-pull-sources.conf + +[[ -f "$CONF" ]] || exit 0 + +mapfile -t WANTED < <(grep -v '^\s*#' "$CONF" | grep -v '^\s*$' | sort -u) +mapfile -t CURRENT < <( + systemctl list-units 'journal-pull@*.service' --all --plain --no-legend \ + | awk '{print $1}' | sed 's/journal-pull@//;s/\.service//' | sort -u +) + +for ip in "${WANTED[@]:-}"; do + [[ -z "$ip" ]] && continue + if ! printf '%s\n' "${CURRENT[@]:-}" | grep -qxF "$ip"; then + systemctl enable --now "journal-pull@${ip}.service" + echo "journal-pull-sync: enabled journal-pull@${ip}" + fi +done + +for ip in "${CURRENT[@]:-}"; do + [[ -z "$ip" ]] && continue + if ! printf '%s\n' "${WANTED[@]:-}" | grep -qxF "$ip"; then + systemctl disable --now "journal-pull@${ip}.service" + echo "journal-pull-sync: disabled journal-pull@${ip}" + fi +done diff --git a/etc/systemd/system/journal-pull-sync.path b/etc/systemd/system/journal-pull-sync.path new file mode 100644 index 0000000..cb9ab4f --- /dev/null +++ b/etc/systemd/system/journal-pull-sync.path @@ -0,0 +1,8 @@ +[Unit] +Description=Watch journal-pull sources config + +[Path] +PathModified=/opt/opengnsys/ogcore/etc/journal-pull-sources.conf + +[Install] +WantedBy=multi-user.target diff --git a/etc/systemd/system/journal-pull-sync.service b/etc/systemd/system/journal-pull-sync.service new file mode 100644 index 0000000..1ce8ff2 --- /dev/null +++ b/etc/systemd/system/journal-pull-sync.service @@ -0,0 +1,7 @@ +[Unit] +Description=Sync journal-pull instances from config +After=network-online.target + +[Service] +Type=oneshot +ExecStart=/opt/opengnsys/ogcore/etc/bin/journal-pull-sync.sh diff --git a/etc/systemd/system/journal-pull@.service b/etc/systemd/system/journal-pull@.service new file mode 100644 index 0000000..91086c0 --- /dev/null +++ b/etc/systemd/system/journal-pull@.service @@ -0,0 +1,14 @@ +[Unit] +Description=Pull OpenGNSYS journal from %i +After=network-online.target +Wants=network-online.target + +[Service] +User=systemd-journal-remote +Group=systemd-journal-remote +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 --output=/var/log/journal/remote/%i.journal +Restart=on-failure +RestartSec=10 + +[Install] +WantedBy=multi-user.target diff --git a/src/EventSubscriber/JournalPullSubscriber.php b/src/EventSubscriber/JournalPullSubscriber.php new file mode 100644 index 0000000..dcdf5ac --- /dev/null +++ b/src/EventSubscriber/JournalPullSubscriber.php @@ -0,0 +1,80 @@ + */ + private array $pendingIpChanges = []; + + public function __construct(private readonly JournalPullService $journalPullService) {} + + private function supports(object $entity): bool + { + return $entity instanceof BootServer + || $entity instanceof DhcpServer + || $entity instanceof ImageRepository; + } + + public function postPersist(PostPersistEventArgs $args): void + { + $entity = $args->getObject(); + if (!$this->supports($entity)) { + return; + } + $this->journalPullService->add($entity->getIp()); + } + + public function postRemove(PostRemoveEventArgs $args): void + { + $entity = $args->getObject(); + if (!$this->supports($entity)) { + return; + } + $this->journalPullService->remove($entity->getIp()); + } + + public function preUpdate(PreUpdateEventArgs $args): void + { + $entity = $args->getObject(); + if (!$this->supports($entity) || !$args->hasChangedField('ip')) { + return; + } + $this->pendingIpChanges[spl_object_id($entity)] = [ + 'old' => $args->getOldValue('ip'), + 'new' => $args->getNewValue('ip'), + ]; + } + + public function postUpdate(PostUpdateEventArgs $args): void + { + $entity = $args->getObject(); + if (!$this->supports($entity)) { + return; + } + $oid = spl_object_id($entity); + if (!isset($this->pendingIpChanges[$oid])) { + return; + } + ['old' => $oldIp, 'new' => $newIp] = $this->pendingIpChanges[$oid]; + unset($this->pendingIpChanges[$oid]); + + $this->journalPullService->remove($oldIp); + $this->journalPullService->add($newIp); + } +} diff --git a/src/Service/JournalPullService.php b/src/Service/JournalPullService.php new file mode 100644 index 0000000..7ef1f55 --- /dev/null +++ b/src/Service/JournalPullService.php @@ -0,0 +1,56 @@ +readIps(); + if (in_array($ip, $ips, true)) { + return; + } + $ips[] = $ip; + $this->writeIps($ips); + $this->logger->info('journal-pull: added {ip}', ['ip' => $ip]); + } + + public function remove(string $ip): void + { + $ips = $this->readIps(); + $filtered = array_values(array_filter($ips, fn($i) => $i !== $ip)); + if (count($filtered) === count($ips)) { + return; + } + $this->writeIps($filtered); + $this->logger->info('journal-pull: removed {ip}', ['ip' => $ip]); + } + + private function readIps(): array + { + if (!file_exists(self::CONFIG_FILE)) { + return []; + } + $lines = file(self::CONFIG_FILE, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); + return array_values(array_filter($lines, fn($l) => !str_starts_with(trim($l), '#'))); + } + + private function writeIps(array $ips): void + { + $dir = dirname(self::CONFIG_FILE); + if (!is_dir($dir)) { + mkdir($dir, 0755, true); + } + file_put_contents( + self::CONFIG_FILE, + $ips ? implode("\n", $ips) . "\n" : '', + LOCK_EX + ); + } +}