refs #1702. Updated ogLive sync. Deleted wrong or uninstalled oglives
testing/ogcore-api/pipeline/head There was a failure building this commit Details

pull/26/head
Manuel Aranda Rosales 2025-03-13 12:55:49 +01:00
parent 663b0d1928
commit 8bc9cb1006
5 changed files with 54 additions and 21 deletions

View File

@ -15,3 +15,11 @@ when@test:
framework:
profiler: { collect: false }
when@prod:
web_profiler:
toolbar: false
intercept_redirects: false
framework:
profiler: { collect: false }

View File

@ -82,7 +82,7 @@ services:
api_platform.filter.og_live.search:
parent: 'api_platform.doctrine.orm.search_filter'
arguments: [ { 'id': 'exact', 'name': 'partial', } ]
arguments: [ { 'id': 'exact', 'name': 'partial', 'status': 'exact' } ]
tags: [ 'api_platform.filter' ]
api_platform.filter.og_live.boolean:

View File

@ -20,16 +20,21 @@ use Symfony\Contracts\HttpClient\HttpClientInterface;
class SyncAction extends AbstractOgBootController
{
const string OG_BOOT_DIRECTORY = '/opt/opengnsys/ogboot/tftpboot//';
/**
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ClientExceptionInterface
* @throws \Exception
*/
public function __invoke(): JsonResponse
{
$content = $this->createRequest('GET', 'http://'.$this->ogBootApiUrl . '/ogboot/v1/oglives');
$allOgLives = $this->entityManager->getRepository(OgLive::class)->findAll();
$apiChecksums = array_map(fn($ogLive) => $ogLive['id'], $content['message']['installed_ogLives']);
foreach ($content['message']['installed_ogLives'] as $ogLive) {
$ogLiveEntity = $this->entityManager->getRepository(OgLive::class)->findOneBy(['checksum' => $ogLive['id']]);
@ -43,6 +48,17 @@ class SyncAction extends AbstractOgBootController
$this->extracted($ogLiveEntity, $ogLive);
$this->entityManager->persist($ogLiveEntity);
}
foreach ($allOgLives as $localOgLive) {
if ($localOgLive->getStatus() === OgLiveStatus::PENDING ) {
continue;
}
if (!in_array($localOgLive->getChecksum(), $apiChecksums)) {
$this->entityManager->remove($localOgLive);
}
}
$this->entityManager->flush();
if (isset($content['message']['default_oglive'])) {
@ -75,6 +91,13 @@ class SyncAction extends AbstractOgBootController
private function serDefaultOgLive(string $defaultOgLive): void
{
$oldDefaultOgLive = $this->entityManager->getRepository(OgLive::class)->findAll();
foreach ($oldDefaultOgLive as $oldOgLive) {
$oldOgLive->setIsDefault(false);
$this->entityManager->persist($oldOgLive);
}
$ogLiveEntity = $this->entityManager->getRepository(OgLive::class)->findOneBy(['filename' => $defaultOgLive]);
if (!$ogLiveEntity) {

View File

@ -6,6 +6,7 @@ use App\Controller\OgBoot\AbstractOgBootController;
use App\Entity\OgLive;
use App\Entity\Trace;
use App\Model\OgLiveStatus;
use App\Model\TraceStatus;
use App\Service\Utils\ExtractOgLiveFilenameDateService;
use App\Service\Utils\SymflipyOgLiveFilenameService;
use Doctrine\ORM\EntityManagerInterface;
@ -51,6 +52,8 @@ class InstallOgLiveResponseAction extends AbstractController
return new JsonResponse(['error' => 'Invalid JSON data'], Response::HTTP_BAD_REQUEST);
}
$this->logger->info('OgLive Webhook data received: '.json_encode($data));
$data = $data['webhookData'];
if ($data === null || !isset($data['message'], $data['ogCoreId'], $data['status'])) {
@ -69,8 +72,9 @@ class InstallOgLiveResponseAction extends AbstractController
}
if ($trace) {
$trace->setStatus($status === self::OG_LIVE_INSTALL_SUCCESS ? 'success' : 'failure');
$trace->setStatus($status === self::OG_LIVE_INSTALL_SUCCESS ? TraceStatus::SUCCESS : TraceStatus::FAILED);
$this->entityManager->persist($trace);
$this->entityManager->flush();
}
if ($ogLive->getStatus() === OgLiveStatus::ACTIVE) {
@ -86,12 +90,12 @@ class InstallOgLiveResponseAction extends AbstractController
/**
* @throws \Exception
*/
private function updateOgLive (OgLive $ogLive, array $details, string $status): void
private function updateOgLive (OgLive $ogLive, mixed $details, string $status): void
{
if ($status === self::OG_LIVE_INSTALL_SUCCESS) {
$ogLive->setName($this->symflipyOgLiveFilenameService->__invoke($details['filename']));
$ogLive->setDate(new \DateTime($this->extractOgLiveFilenameDateService->__invoke($details['filename'])));
$ogLive->setFilename(str_replace(self::OG_BOOT_DIRECTORY, '', $ogLive['directory']));
if ( is_array($details) && $status === self::OG_LIVE_INSTALL_SUCCESS) {
$ogLive->setName($this->symflipyOgLiveFilenameService->__invoke($details['directory']));
$ogLive->setDate(new \DateTime($this->extractOgLiveFilenameDateService->__invoke($details['directory'])));
$ogLive->setFilename(str_replace(self::OG_BOOT_DIRECTORY, '', $details['directory']));
$ogLive->setInstalled(true);
$ogLive->setSynchronized(true);
$ogLive->setChecksum($details['id']);
@ -100,21 +104,20 @@ class InstallOgLiveResponseAction extends AbstractController
$ogLive->setArchitecture($details['architecture']);
$ogLive->setRevision($details['revision']);
$ogLive->setDirectory($details['directory']);
$oldDefaultOgLive = $this->entityManager->getRepository(OgLive::class)->findAll();
foreach ($oldDefaultOgLive as $oldOgLive) {
$oldOgLive->setIsDefault(false);
$this->entityManager->persist($oldOgLive);
}
$ogLive->setIsDefault(true);
$this->entityManager->persist($ogLive);
}
$ogLive->setStatus($status === self::OG_LIVE_INSTALL_SUCCESS ? OgLiveStatus::ACTIVE : OgLiveStatus::FAILED);
$ogLive->setInstalled($status === self::OG_LIVE_INSTALL_SUCCESS);
$oldDefaultOgLive = $this->entityManager->getRepository(OgLive::class)->findBy(['isDefault' => true]);
foreach ($oldDefaultOgLive as $oldOgLive) {
$oldOgLive->setIsDefault(false);
$this->entityManager->persist($oldOgLive);
}
$ogLive->setIsDefault(true);
$this->entityManager->persist($ogLive);
$this->entityManager->flush();
}
}

View File

@ -8,13 +8,12 @@ final class OgLiveStatus
public const string ACTIVE = 'active';
public const string INACTIVE = 'inactive';
public const string DELETED = 'deleted';
public const string FAILED = 'failed';
private const array OG_LIVE_STATUSES = [
self::PENDING => 'Pendiente',
self::ACTIVE => 'Activo',
self::INACTIVE => 'Inactivo',
self::PENDING => 'Instalando',
self::ACTIVE => 'Instalada',
self::INACTIVE => 'Sin instalar',
self::DELETED => 'Eliminado',
self::FAILED => 'Fallido',
];