Merge pull request 'develop' (#34) from develop into main
ogcore-debian-package/pipeline/head There was a failure building this commit Details
ogcore-debian-package/pipeline/tag There was a failure building this commit Details

Reviewed-on: #34
main 0.14.0
Manuel Aranda Rosales 2025-06-02 07:47:07 +02:00
commit 150b437656
42 changed files with 417 additions and 139 deletions

View File

@ -1,4 +1,19 @@
# Changelog # Changelog
## [0.14.0] - 2025-06-02
### Added
- Se ha añadido la funcionalidad de mover equipos entre aulas y grupos.
- Se ha añadido la funcionalidad para eliminar imagen cache.
- Se ha añadido la funcionalidad para iniciar sesion.
### Improved
- Se ha cambiado la restriccion que comprobaba los puertos de MULTICAST.
- Se ha modificado el tiempo en el script, que se encarga de comprobar el estado de los equipos. Ahora es de 1 min en lugar de 3.
### Fixed
- Se ha corregido un bug que hacia que al modificar un cliente o eliminarlo, no se actualizaba su estado en la subred.
- Se ha corregido un bug que hacia que al modificar un cliente, no se actualizara su fichero de arranque.
---
## [0.13.1] - 2025-05-23 ## [0.13.1] - 2025-05-23
### Fixed ### Fixed
- Variable de entorno "SSl_ENABLED" desactivada por defecto. - Variable de entorno "SSl_ENABLED" desactivada por defecto.

View File

@ -32,7 +32,7 @@ resources:
class: ApiPlatform\Metadata\Post class: ApiPlatform\Metadata\Post
method: POST method: POST
input: App\Dto\Input\ChangeOrganizationalUnitInput input: App\Dto\Input\ChangeOrganizationalUnitInput
uriTemplate: /clients/change-organizational-units uriTemplate: /clients/change-organizational-unit
controller: App\Controller\ChangeOrganizationalUnitAction controller: App\Controller\ChangeOrganizationalUnitAction
agent_status: agent_status:
@ -50,13 +50,20 @@ resources:
uriTemplate: /clients/server/{uuid}/get-pxe uriTemplate: /clients/server/{uuid}/get-pxe
controller: App\Controller\OgBoot\PxeBootFile\GetAction controller: App\Controller\OgBoot\PxeBootFile\GetAction
login_client: boot_client:
class: ApiPlatform\Metadata\Post class: ApiPlatform\Metadata\Post
method: POST method: POST
input: App\Dto\Input\MultipleClientsInput input: App\Dto\Input\BootClientsInput
uriTemplate: /clients/server/login-client uriTemplate: /clients/server/boot-client
controller: App\Controller\OgAgent\LoginAction controller: App\Controller\OgAgent\LoginAction
remove_cache_image:
class: ApiPlatform\Metadata\Post
method: POST
input: App\Dto\Input\BootClientsInput
uriTemplate: /clients/server/remove-cache-image
controller: App\Controller\OgAgent\RemoveCacheImageAction
reboot_client: reboot_client:
class: ApiPlatform\Metadata\Post class: ApiPlatform\Metadata\Post
method: POST method: POST

View File

@ -4,6 +4,7 @@ resources:
input: App\Dto\Input\PartitionPostInput input: App\Dto\Input\PartitionPostInput
output: App\Dto\Output\PartitionOutput output: App\Dto\Output\PartitionOutput
order: order:
diskNumber: 'ASC'
partitionNumber: 'ASC' partitionNumber: 'ASC'
normalizationContext: normalizationContext:
groups: ['default', 'partition:read'] groups: ['default', 'partition:read']

View File

@ -9,6 +9,8 @@ resources:
filters: filters:
- 'api_platform.filter.trace.order' - 'api_platform.filter.trace.order'
- 'api_platform.filter.trace.search' - 'api_platform.filter.trace.search'
- 'api_platform.filter.trace.date'
ApiPlatform\Metadata\Get: ApiPlatform\Metadata\Get:
provider: App\State\Provider\TraceProvider provider: App\State\Provider\TraceProvider

View File

@ -33,6 +33,7 @@ security:
- { path: ^/og-repository/webhook, roles: PUBLIC_ACCESS } - { path: ^/og-repository/webhook, roles: PUBLIC_ACCESS }
- { path: ^/og-lives/install/webhook, roles: PUBLIC_ACCESS } - { path: ^/og-lives/install/webhook, roles: PUBLIC_ACCESS }
- { path: ^/auth/refresh, roles: PUBLIC_ACCESS } - { path: ^/auth/refresh, roles: PUBLIC_ACCESS }
- { path: ^/validate, roles: PUBLIC_ACCESS }
- { path: ^/menu-browser, roles: PUBLIC_ACCESS } - { path: ^/menu-browser, roles: PUBLIC_ACCESS }
- { path: ^/menu/, roles: PUBLIC_ACCESS } - { path: ^/menu/, roles: PUBLIC_ACCESS }
- { path: ^/, roles: IS_AUTHENTICATED_FULLY } - { path: ^/, roles: IS_AUTHENTICATED_FULLY }

View File

@ -304,6 +304,11 @@ services:
$orderParameterName: 'order' $orderParameterName: 'order'
tags: [ 'api_platform.filter' ] tags: [ 'api_platform.filter' ]
api_platform.filter.trace.date:
parent: 'api_platform.doctrine.orm.date_filter'
arguments: [ { 'executedAt': ~, 'createdAt': ~ } ]
tags: [ 'api_platform.filter' ]
api_platform.filter.user.order: api_platform.filter.user.order:
parent: 'api_platform.doctrine.orm.order_filter' parent: 'api_platform.doctrine.orm.order_filter'
arguments: arguments:

View File

@ -8,6 +8,9 @@
"UDS_AUTH_USERNAME": "test", "UDS_AUTH_USERNAME": "test",
"UDS_AUTH_PASSWORD": "test", "UDS_AUTH_PASSWORD": "test",
"UDS_URL": "https:\/\/localhost:8087\/uds\/rest\/", "UDS_URL": "https:\/\/localhost:8087\/uds\/rest\/",
"SSL_ENABLED": "false" "SSL_ENABLED": "true",
"OG_BOOT_IP": "127.0.0.1",
"OG_BOOT_API_PORT": "8082",
"OG_BOOT_PXE_PORT": "8085"
} }
} }

View File

@ -20,7 +20,7 @@ use Symfony\Component\Mercure\Update;
#[AsCommand(name: 'opengnsys:check-client-availability', description: 'Check client availability')] #[AsCommand(name: 'opengnsys:check-client-availability', description: 'Check client availability')]
class CheckClientAvailability extends Command class CheckClientAvailability extends Command
{ {
const int THRESHOLD_MINUTES = 3; const int THRESHOLD_MINUTES = 1;
public function __construct( public function __construct(
private readonly HubInterface $hub, private readonly HubInterface $hub,
@ -34,16 +34,18 @@ class CheckClientAvailability extends Command
{ {
$io = new SymfonyStyle($input, $output); $io = new SymfonyStyle($input, $output);
$threshold = (new \DateTime())->modify(' - '.self::THRESHOLD_MINUTES . ' minutes'); $threshold = (new \DateTime())->modify(' - '.self::THRESHOLD_MINUTES . ' minutes');
$startQueryTime = microtime(true); $startQueryTime = microtime(true);
$validStatuses = [ClientStatus::OG_LIVE, ClientStatus::WINDOWS, ClientStatus::LINUX, ClientStatus::MACOS];
$query = $this->entityManager->createQuery( $query = $this->entityManager->createQuery(
'UPDATE App\Entity\Client c 'UPDATE App\Entity\Client c
SET c.status = :status SET c.status = :status
WHERE c.status = :currentStatus AND c.updatedAt < :threshold' WHERE c.status IN (:currentStatuses)
AND c.updatedAt < :threshold'
); );
$query->setParameter('status', ClientStatus::DISCONNECTED); $query->setParameter('status', ClientStatus::DISCONNECTED);
$query->setParameter('currentStatus', ClientStatus::OG_LIVE); $query->setParameter('currentStatuses', $validStatuses);
$query->setParameter('threshold', $threshold); $query->setParameter('threshold', $threshold);
$updatedCount = $query->execute(); $updatedCount = $query->execute();

View File

@ -2,7 +2,9 @@
namespace App\Controller; namespace App\Controller;
use App\Controller\OgBoot\PxeBootFile\PostAction;
use App\Dto\Input\ChangeOrganizationalUnitInput; use App\Dto\Input\ChangeOrganizationalUnitInput;
use App\Dto\Output\ClientOutput;
use App\Entity\Client; use App\Entity\Client;
use App\Repository\ClientRepository; use App\Repository\ClientRepository;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
@ -10,29 +12,37 @@ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
class ChangeOrganizationalUnitAction extends AbstractController class ChangeOrganizationalUnitAction extends AbstractController
{ {
public function __construct( public function __construct(
private readonly ClientRepository $clientRepository, private readonly EntityManagerInterface $entityManager,
private readonly EntityManagerInterface $entityManager private PostAction $postAction,
) )
{ {
} }
/**
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ClientExceptionInterface
*/
public function __invoke(ChangeOrganizationalUnitInput $input): JsonResponse public function __invoke(ChangeOrganizationalUnitInput $input): JsonResponse
{ {
foreach ($input->clients as $client) { foreach ($input->clients as $client) {
/** @var Client $client */ /** @var Client $clientEntity */
$clientEntity = $this->clientRepository->find($client->getEntity()->getId()); $clientEntity = $client->getEntity();
if (!$clientEntity) {
throw new NotFoundHttpException('Client not found');
}
$organizationalUnit = $input->organizationalUnit->getEntity(); $organizationalUnit = $input->organizationalUnit->getEntity();
$clientEntity->setOrganizationalUnit($organizationalUnit); $clientEntity->setOrganizationalUnit($organizationalUnit);
$this->entityManager->persist($clientEntity); $this->entityManager->persist($clientEntity);
$this->postAction->__invoke($clientEntity, $clientEntity->getTemplate());
} }
$this->entityManager->flush(); $this->entityManager->flush();

View File

@ -4,10 +4,12 @@ declare(strict_types=1);
namespace App\Controller\OgAgent; namespace App\Controller\OgAgent;
use App\Dto\Input\BootClientsInput;
use App\Dto\Input\MultipleClientsInput; use App\Dto\Input\MultipleClientsInput;
use App\Entity\Client; use App\Entity\Client;
use App\Entity\Command; use App\Entity\Command;
use App\Entity\Image; use App\Entity\Image;
use App\Entity\Partition;
use App\Entity\Trace; use App\Entity\Trace;
use App\Model\ClientStatus; use App\Model\ClientStatus;
use App\Model\CommandTypes; use App\Model\CommandTypes;
@ -35,8 +37,11 @@ class LoginAction extends AbstractOgAgentController
* @throws RedirectionExceptionInterface * @throws RedirectionExceptionInterface
* @throws ClientExceptionInterface * @throws ClientExceptionInterface
*/ */
public function __invoke(MultipleClientsInput $input): JsonResponse public function __invoke(BootClientsInput $input): JsonResponse
{ {
/** @var Partition $partition */
$partition = $input->partition->getEntity();
foreach ($input->clients as $clientEntity) { foreach ($input->clients as $clientEntity) {
/** @var Client $client */ /** @var Client $client */
$client = $clientEntity->getEntity(); $client = $clientEntity->getEntity();
@ -52,17 +57,18 @@ class LoginAction extends AbstractOgAgentController
$data = [ $data = [
'nfn' => 'IniciarSesion', 'nfn' => 'IniciarSesion',
'dsk' => '1', 'dsk' => (string) $partition->getDiskNumber(),
'par' => '1', 'par' => (string) $partition->getPartitionNumber(),
'ids' => '0' 'ids' => '0'
]; ];
$response = $this->createRequest( $response = $this->createRequest(
method: 'POST', method: 'POST',
url: 'http://'.$client->getIp().':8000/opengnsys/IniciarSesion', url: 'https://'.$client->getIp().':8000/opengnsys/IniciarSesion',
params: [ params: [
'json' => $data, 'json' => $data,
] ],
token: $client->getToken(),
); );
if (isset($response['error']) && $response['code'] === Response::HTTP_INTERNAL_SERVER_ERROR) { if (isset($response['error']) && $response['code'] === Response::HTTP_INTERNAL_SERVER_ERROR) {
@ -77,7 +83,7 @@ class LoginAction extends AbstractOgAgentController
$this->entityManager->persist($client); $this->entityManager->persist($client);
$this->entityManager->flush(); $this->entityManager->flush();
$this->createService->__invoke($client, CommandTypes::REBOOT, TraceStatus::SUCCESS, $jobId, []); $this->createService->__invoke($client, CommandTypes::LOGIN, TraceStatus::SUCCESS, $jobId, []);
} }
return new JsonResponse(data: [], status: Response::HTTP_OK); return new JsonResponse(data: [], status: Response::HTTP_OK);

View File

@ -55,7 +55,7 @@ class RebootAction extends AbstractOgAgentController
$response = $this->createRequest( $response = $this->createRequest(
method: 'POST', method: 'POST',
url: 'http://'.$client->getIp().':8000/'.$endpoint, url: 'https://'.$client->getIp().':8000/'.$endpoint,
params: [ params: [
'json' => $data, 'json' => $data,
], ],

View File

@ -0,0 +1,85 @@
<?php
namespace App\Controller\OgAgent;
use App\Dto\Input\BootClientsInput;
use App\Entity\Client;
use App\Entity\Partition;
use App\Model\ClientStatus;
use App\Model\CommandTypes;
use App\Model\TraceStatus;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Validator\Exception\ValidatorException;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
class RemoveCacheImageAction extends AbstractOgAgentController
{
/**
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ClientExceptionInterface
*/
public function __invoke(BootClientsInput $input): JsonResponse
{
/** @var Partition $partition */
$partition = $input->partition->getEntity();
foreach ($input->clients as $clientEntity) {
/** @var Client $client */
$client = $clientEntity->getEntity();
if (!$partition->getImage()) {
throw new ValidatorException('Image is required');
}
if (!$client->getIp()) {
throw new ValidatorException('IP is required');
}
if ($client->getStatus() !== ClientStatus::OG_LIVE) {
throw new ValidatorException('Client is not in OG_LIVE status');
}
$script = `rm%20-r%20/opt/opengnsys/cache/opt/opengnsys/images/{$partition->getImage()->getName()}.*@'`;
$data = [
'nfn' => 'EjecutarScript',
'scp' => base64_encode($script),
'ids' => '0'
];
$response = $this->createRequest(
method: 'POST',
url: 'https://'.$client->getIp().':8000/opengnsys/EjecutarScript',
params: [
'json' => $data,
],
token: $client->getToken(),
);
if (isset($response['error']) && $response['code'] === Response::HTTP_INTERNAL_SERVER_ERROR) {
throw new ValidatorException('Error logging in: '.$response['error']);
}
$this->logger->info('Login client', ['client' => $client->getId()]);
$jobId = $response['job_id'];
$this->entityManager->persist($client);
$this->entityManager->flush();
$inputData = [
'script' => $script,
];
$this->createService->__invoke($client, CommandTypes::RUN_SCRIPT, TraceStatus::SUCCESS, $jobId, $inputData);
}
return new JsonResponse(data: [], status: Response::HTTP_OK);
}
}

View File

@ -9,6 +9,7 @@ use App\Service\Trace\CreateService;
use App\Service\Utils\ExtractOgLiveFilenameDateService; use App\Service\Utils\ExtractOgLiveFilenameDateService;
use App\Service\Utils\SimplifyOgLiveFilenameService; use App\Service\Utils\SimplifyOgLiveFilenameService;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\Attribute\Autowire; use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\JsonResponse;
@ -25,8 +26,12 @@ use Symfony\Contracts\HttpClient\HttpClientInterface;
abstract class AbstractOgBootController extends AbstractController abstract class AbstractOgBootController extends AbstractController
{ {
public function __construct( public function __construct(
#[Autowire(env: 'OG_BOOT_API_URL')] #[Autowire(env: 'OG_BOOT_IP')]
protected string $ogBootApiUrl, protected string $ogBootIp,
#[Autowire(env: 'OG_BOOT_PXE_PORT')]
protected string $ogBootPxePort,
#[Autowire(env: 'OG_BOOT_API_PORT')]
protected string $ogBootApiPort,
#[Autowire(env: 'OG_CORE_IP')] #[Autowire(env: 'OG_CORE_IP')]
protected string $ogCoreIP, protected string $ogCoreIP,
#[Autowire(env: 'OG_LOG_IP')] #[Autowire(env: 'OG_LOG_IP')]
@ -36,6 +41,8 @@ abstract class AbstractOgBootController extends AbstractController
protected readonly CreateService $createService, protected readonly CreateService $createService,
protected readonly SimplifyOgLiveFilenameService $simplifyOgLiveFilenameService, protected readonly SimplifyOgLiveFilenameService $simplifyOgLiveFilenameService,
protected readonly ExtractOgLiveFilenameDateService $extractOgLiveFilenameDateService, protected readonly ExtractOgLiveFilenameDateService $extractOgLiveFilenameDateService,
protected readonly LoggerInterface $logger,
) )
{ {
} }
@ -56,15 +63,25 @@ abstract class AbstractOgBootController extends AbstractController
]); ]);
try { try {
$response = $this->httpClient->request($method, $url, $params); $response = $this->httpClient->request($method, 'http://'.$this->ogBootIp.':'.$this->ogBootApiPort.$url, $params);
return json_decode($response->getContent(), true); return json_decode($response->getContent(), true);
} catch (ClientExceptionInterface | ServerExceptionInterface $e) { } catch (ClientExceptionInterface | ServerExceptionInterface $e) {
$response = $e->getResponse(); $this->logger->error(sprintf('Client/Server error in request to %s: %s', $url, $e->getMessage()));
$content = json_decode($response->getContent(false), true);
throw new HttpException($response->getStatusCode(), $content['error'] ?? 'An error occurred'); return [
'code' => Response::HTTP_INTERNAL_SERVER_ERROR,
'error' => 'Client/Server error',
'details' => $e->getMessage(),
];
} catch (TransportExceptionInterface $e) { } catch (TransportExceptionInterface $e) {
throw new HttpException(Response::HTTP_INTERNAL_SERVER_ERROR, $e->getMessage()); $this->logger->error(sprintf('Transport error in request to %s: %s', $url, $e->getMessage()));
return [
'code' => Response::HTTP_INTERNAL_SERVER_ERROR,
'error' => 'Transport error',
'details' => $e->getMessage(),
];
} }
} }
} }

View File

@ -29,7 +29,11 @@ class GetAction extends AbstractOgBootController
throw new ValidatorException('Checksum is required'); throw new ValidatorException('Checksum is required');
} }
$content = $this->createRequest('GET', 'http://'.$this->ogBootApiUrl.'/ogboot/v1/oglives/'.$data->getChecksum()); $content = $this->createRequest('GET', '/ogboot/v1/oglives/'.$data->getChecksum());
if (isset($content['error']) && $content['error'] === Response::HTTP_INTERNAL_SERVER_ERROR ) {
throw new ValidatorException('An error occurred: ' . $content['error']);
}
return new JsonResponse(data: $content, status: Response::HTTP_OK); return new JsonResponse(data: $content, status: Response::HTTP_OK);
} }

View File

@ -23,7 +23,11 @@ class GetCollectionAction extends AbstractOgBootController
*/ */
public function __invoke(): JsonResponse public function __invoke(): JsonResponse
{ {
$content = $this->createRequest('GET', 'http://'.$this->ogBootApiUrl.'/ogboot/v1/oglives'); $content = $this->createRequest('GET', '/ogboot/v1/oglives');
if (isset($content['error']) && $content['error'] === Response::HTTP_INTERNAL_SERVER_ERROR ) {
throw new ValidatorException('An error occurred: ' . $content['error']);
}
return new JsonResponse(data: $content, status: Response::HTTP_OK); return new JsonResponse(data: $content, status: Response::HTTP_OK);
} }

View File

@ -6,6 +6,7 @@ use App\Controller\OgBoot\AbstractOgBootController;
use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\AsController; use Symfony\Component\HttpKernel\Attribute\AsController;
use Symfony\Component\Validator\Exception\ValidatorException;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface; use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
@ -22,7 +23,11 @@ class GetDefaultAction extends AbstractOgBootController
*/ */
public function __invoke(): JsonResponse public function __invoke(): JsonResponse
{ {
$content = $this->createRequest('GET', 'http://'.$this->ogBootApiUrl.'/ogboot/v1/oglives/default'); $content = $this->createRequest('GET', '/ogboot/v1/oglives/default');
if (isset($content['error']) && $content['error'] === Response::HTTP_INTERNAL_SERVER_ERROR ) {
throw new ValidatorException('An error occurred: ' . $content['error']);
}
return new JsonResponse(status: Response::HTTP_OK); return new JsonResponse(status: Response::HTTP_OK);
} }

View File

@ -23,7 +23,7 @@ class GetIsosAction extends AbstractOgBootController
*/ */
public function __invoke(): JsonResponse public function __invoke(): JsonResponse
{ {
$content = $this->createRequest('GET', 'http://'.$this->ogBootApiUrl.'/ogboot/v1/oglives/isos'); $content = $this->createRequest('GET', '/ogboot/v1/oglives/isos');
if (!isset($content['message']) || !is_array($content['message'])) { if (!isset($content['message']) || !is_array($content['message'])) {
return new JsonResponse(data: ['error' => 'Invalid response'], status: Response::HTTP_BAD_REQUEST); return new JsonResponse(data: ['error' => 'Invalid response'], status: Response::HTTP_BAD_REQUEST);

View File

@ -45,7 +45,11 @@ class InstallAction extends AbstractOgBootController
'uuid' => 'InstallOgLive_'.$data->getUuid() 'uuid' => 'InstallOgLive_'.$data->getUuid()
]; ];
$content = $this->createRequest('POST', 'http://'.$this->ogBootApiUrl.'/ogboot/v1/oglives/install', $params); $content = $this->createRequest('POST', '/ogboot/v1/oglives/install', $params);
if (isset($content['error']) && $content['error'] === Response::HTTP_INTERNAL_SERVER_ERROR ) {
throw new ValidatorException('An error occurred: ' . $content['error']);
}
$this->createService->__invoke(null, CommandTypes::INSTALL_OGLIVE, TraceStatus::IN_PROGRESS, 'InstallOgLive_'.$data->getUuid(), $inputData); $this->createService->__invoke(null, CommandTypes::INSTALL_OGLIVE, TraceStatus::IN_PROGRESS, 'InstallOgLive_'.$data->getUuid(), $inputData);

View File

@ -36,7 +36,11 @@ class SetDefaultAction extends AbstractOgBootController
] ]
]; ];
$content = $this->createRequest('PUT', 'http://'.$this->ogBootApiUrl.'/ogboot/v1/oglives/default', $params); $content = $this->createRequest('PUT', '/ogboot/v1/oglives/default', $params);
if (isset($content['error']) && $content['error'] === Response::HTTP_INTERNAL_SERVER_ERROR ) {
throw new ValidatorException('An error occurred: ' . $content['error']);
}
$oldDefaultOgLive = $this->entityManager->getRepository(OgLive::class)->findBy(['isDefault' => true]); $oldDefaultOgLive = $this->entityManager->getRepository(OgLive::class)->findBy(['isDefault' => true]);

View File

@ -30,7 +30,11 @@ class SyncAction extends AbstractOgBootController
*/ */
public function __invoke(): JsonResponse public function __invoke(): JsonResponse
{ {
$content = $this->createRequest('GET', 'http://'.$this->ogBootApiUrl . '/ogboot/v1/oglives'); $content = $this->createRequest('GET', '/ogboot/v1/oglives');
if (isset($content['error']) && $content['error'] === Response::HTTP_INTERNAL_SERVER_ERROR ) {
throw new ValidatorException('An error occurred: ' . $content['error']);
}
$allOgLives = $this->entityManager->getRepository(OgLive::class)->findAll(); $allOgLives = $this->entityManager->getRepository(OgLive::class)->findAll();
$apiChecksums = array_map(fn($ogLive) => $ogLive['id'], $content['message']['installed_ogLives']); $apiChecksums = array_map(fn($ogLive) => $ogLive['id'], $content['message']['installed_ogLives']);
@ -76,7 +80,9 @@ class SyncAction extends AbstractOgBootController
*/ */
private function extracted(OgLive $ogLiveEntity, mixed $ogLive): void private function extracted(OgLive $ogLiveEntity, mixed $ogLive): void
{ {
$ogLiveEntity->setName($this->simplifyOgLiveFilenameService->__invoke(str_replace(self::OG_BOOT_DIRECTORY, '', $ogLive['directory']))); $name = $this->simplifyOgLiveFilenameService->__invoke(str_replace(self::OG_BOOT_DIRECTORY, '', $ogLive['directory']));
$ogLiveEntity->setName($name ? $name : str_replace(self::OG_BOOT_DIRECTORY, '', $ogLive['directory']));
$ogLiveEntity->setDate(new \DateTime($this->extractOgLiveFilenameDateService->__invoke(str_replace(self::OG_BOOT_DIRECTORY, '', $ogLive['directory'])))); $ogLiveEntity->setDate(new \DateTime($this->extractOgLiveFilenameDateService->__invoke(str_replace(self::OG_BOOT_DIRECTORY, '', $ogLive['directory']))));
$ogLiveEntity->setInstalled(true); $ogLiveEntity->setInstalled(true);
$ogLiveEntity->setArchitecture($ogLive['architecture']); $ogLiveEntity->setArchitecture($ogLive['architecture']);

View File

@ -31,7 +31,11 @@ class UninstallAction extends AbstractOgBootController
throw new ValidatorException('Checksum is required'); throw new ValidatorException('Checksum is required');
} }
$content = $this->createRequest( 'DELETE', 'http://'.$this->ogBootApiUrl.'/ogboot/v1/oglives/'.$data->getChecksum()); $content = $this->createRequest( 'DELETE', '/ogboot/v1/oglives/'.$data->getChecksum());
if (isset($content['error']) && $content['error'] === Response::HTTP_INTERNAL_SERVER_ERROR ) {
throw new ValidatorException('An error occurred: ' . $content['error']);
}
$this->entityManager->remove($data); $this->entityManager->remove($data);
$this->entityManager->flush(); $this->entityManager->flush();

View File

@ -23,19 +23,13 @@ class DeleteAction extends AbstractOgBootController
*/ */
public function __invoke(string $mac): JsonResponse public function __invoke(string $mac): JsonResponse
{ {
try { $response = $this->createRequest('DELETE', '/ogboot/v1/pxes/'.$mac);
$response = $this->httpClient->request('DELETE', 'http://'.$this->ogBootApiUrl.'/ogboot/v1/pxes/'.$mac, [
'headers' => [ if (isset($content['error']) && $content['code'] === Response::HTTP_INTERNAL_SERVER_ERROR) {
'accept' => 'application/json',
],
]);
} catch (TransportExceptionInterface $e) {
return new JsonResponse( data: 'An error occurred', status: Response::HTTP_INTERNAL_SERVER_ERROR); return new JsonResponse( data: 'An error occurred', status: Response::HTTP_INTERNAL_SERVER_ERROR);
} }
$data = json_decode($response->getContent(), true); return new JsonResponse( data: [], status: Response::HTTP_OK);
return new JsonResponse( data: $data, status: Response::HTTP_OK);
} }
} }

View File

@ -26,18 +26,12 @@ class GetAction extends AbstractOgBootController
*/ */
public function __invoke(Client $client): JsonResponse public function __invoke(Client $client): JsonResponse
{ {
try { $response = $this->createRequest('GET', '/ogboot/v1/pxes/'.$client->getName());
$response = $this->httpClient->request('GET', 'http://'.$this->ogBootApiUrl.'/ogboot/v1/pxes/'.$client->getMac(), [
'headers' => [ if (isset($content['error']) && $content['code'] === Response::HTTP_INTERNAL_SERVER_ERROR) {
'accept' => 'application/json',
],
]);
} catch (TransportExceptionInterface $e) {
return new JsonResponse( data: 'An error occurred', status: Response::HTTP_INTERNAL_SERVER_ERROR); return new JsonResponse( data: 'An error occurred', status: Response::HTTP_INTERNAL_SERVER_ERROR);
} }
$data = json_decode($response->getContent(), true); return new JsonResponse( data: $response, status: Response::HTTP_OK);
return new JsonResponse( data: $data, status: Response::HTTP_OK);
} }
} }

View File

@ -26,7 +26,7 @@ class GetCollectionAction extends AbstractOgBootController
*/ */
public function __invoke(HttpClientInterface $httpClient): JsonResponse public function __invoke(HttpClientInterface $httpClient): JsonResponse
{ {
$content = $this->createRequest('GET', 'http://'.$this->ogBootApiUrl.'/ogboot/v1/pxes'); $content = $this->createRequest('GET', '/ogboot/v1/pxes');
return new JsonResponse(data: $content, status: Response::HTTP_OK); return new JsonResponse(data: $content, status: Response::HTTP_OK);
} }

View File

@ -28,13 +28,13 @@ class PostAction extends AbstractOgBootController
*/ */
public function __invoke(Client $client, PxeTemplate $pxeTemplate): JsonResponse public function __invoke(Client $client, PxeTemplate $pxeTemplate): JsonResponse
{ {
$ogRepoIp = $this->ogBootApiUrl; $ogRepoIp = $this->ogBootIp;
$ogRepoIp = $client->getRepository()?->getIp() $ogRepoIp = $client->getRepository()?->getIp()
?? $client->getOrganizationalUnit()?->getNetworkSettings()?->getRepository()?->getIp(); ?? $client->getOrganizationalUnit()?->getNetworkSettings()?->getRepository()?->getIp();
$ogLive = $client->getOgLive()?->getFilename() $ogLive = $client->getOgLive()?->getFilename()
?? $client->getOrganizationalUnit()?->getNetworkSettings()?->getOgLive()->getFilename(); ?? $client->getOrganizationalUnit()?->getNetworkSettings()?->getOgLive()?->getFilename();
$params = [ $params = [
'json' => [ 'json' => [
@ -42,7 +42,9 @@ class PostAction extends AbstractOgBootController
'mac' => strtolower($client->getMac()), 'mac' => strtolower($client->getMac()),
'lang' => 'es_ES.UTF-8', 'lang' => 'es_ES.UTF-8',
'ip' => $client->getIp(), 'ip' => $client->getIp(),
'server_ip' => $this->ogBootApiUrl, 'server_ip' => $this->ogBootIp,
'server_api_port' => $this->ogBootApiPort,
'server_pxe_port' => $this->ogBootPxePort,
'router' => $client->getOrganizationalUnit()->getNetworkSettings()->getRouter(), 'router' => $client->getOrganizationalUnit()->getNetworkSettings()->getRouter(),
'netmask' => $client->getOrganizationalUnit()->getNetworkSettings() ? $client->getOrganizationalUnit()->getNetworkSettings()->getNetmask() : '255.255.255.0', 'netmask' => $client->getOrganizationalUnit()->getNetworkSettings() ? $client->getOrganizationalUnit()->getNetworkSettings()->getNetmask() : '255.255.255.0',
'computer_name' => $client->getName(), 'computer_name' => $client->getName(),
@ -50,10 +52,10 @@ class PostAction extends AbstractOgBootController
'group' => $client->getOrganizationalUnit()->getName(), 'group' => $client->getOrganizationalUnit()->getName(),
'ogrepo' => $ogRepoIp, 'ogrepo' => $ogRepoIp,
'ogcore' => $this->ogCoreIP, 'ogcore' => $this->ogCoreIP,
'oglive' => $this->ogBootApiUrl, 'oglive' => $this->ogBootIp,
'oglog' => $this->ogLogIp, 'oglog' => $this->ogLogIp,
'ogshare' => $client->getOrganizationalUnit()->getNetworkSettings()?->getOgShare() 'ogshare' => $client->getOrganizationalUnit()->getNetworkSettings()?->getOgShare()
? $client->getOrganizationalUnit()->getNetworkSettings()?->getOgShare(): $this->ogBootApiUrl, ? $client->getOrganizationalUnit()->getNetworkSettings()?->getOgShare(): $this->ogBootIp,
'oglivedir' => $ogLive, 'oglivedir' => $ogLive,
'ogprof' => 'false', 'ogprof' => 'false',
'hardprofile' => $client->getHardwareProfile() ? $client->getHardwareProfile()->getDescription() : 'default', 'hardprofile' => $client->getHardwareProfile() ? $client->getHardwareProfile()->getDescription() : 'default',
@ -64,7 +66,7 @@ class PostAction extends AbstractOgBootController
] ]
]; ];
$content = $this->createRequest('POST', 'http://'.$this->ogBootApiUrl.'/ogboot/v1/pxes', $params); $content = $this->createRequest('POST', '/ogboot/v1/pxes', $params);
$client->setPxeSync(true); $client->setPxeSync(true);
$this->entityManager->persist($client); $this->entityManager->persist($client);

View File

@ -10,6 +10,7 @@ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\AsController; use Symfony\Component\HttpKernel\Attribute\AsController;
use Symfony\Component\Validator\Exception\ValidatorException;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface; use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
@ -30,7 +31,11 @@ class DeleteAction extends AbstractOgBootController
$this->entityManager->remove($data); $this->entityManager->remove($data);
$this->entityManager->flush(); $this->entityManager->flush();
$content = $this->createRequest('DELETE', 'http://'.$this->ogBootApiUrl.'/ogboot/v1/pxe-templates/'.$data->getName()); $content = $this->createRequest('DELETE', '/ogboot/v1/pxe-templates/'.$data->getName());
if (isset($content['error']) && $content['error'] === Response::HTTP_INTERNAL_SERVER_ERROR ) {
throw new ValidatorException('An error occurred: ' . $content['error']);
}
$defaultTemplateEntity = $this->entityManager->getRepository(PxeTemplate::class)->findOneBy(['isDefault' => true]); $defaultTemplateEntity = $this->entityManager->getRepository(PxeTemplate::class)->findOneBy(['isDefault' => true]);

View File

@ -25,13 +25,9 @@ class GetAction extends AbstractOgBootController
*/ */
public function __invoke(PxeTemplate $template, HttpClientInterface $httpClient): JsonResponse public function __invoke(PxeTemplate $template, HttpClientInterface $httpClient): JsonResponse
{ {
try { $response = $this->createRequest('GET', '/ogboot/v1/pxe-templates/'.$template->getName());
$response = $httpClient->request('GET', 'http://'.$this->ogBootApiUrl.'/ogboot/v1/pxe-templates/'.$template->getName(), [
'headers' => [ if ($response->getStatusCode() !== Response::HTTP_OK) {
'accept' => 'application/json',
],
]);
} catch (TransportExceptionInterface $e) {
return new JsonResponse( data: 'An error occurred', status: Response::HTTP_INTERNAL_SERVER_ERROR); return new JsonResponse( data: 'An error occurred', status: Response::HTTP_INTERNAL_SERVER_ERROR);
} }

View File

@ -7,6 +7,7 @@ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\AsController; use Symfony\Component\HttpKernel\Attribute\AsController;
use Symfony\Component\Validator\Exception\ValidatorException;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface; use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
@ -24,18 +25,12 @@ class GetCollectionAction extends AbstractOgBootController
*/ */
public function __invoke(HttpClientInterface $httpClient): JsonResponse public function __invoke(HttpClientInterface $httpClient): JsonResponse
{ {
try { $content = $this->createRequest('GET', '/ogboot/v1/pxe-templates');
$response = $httpClient->request('GET', 'http://'.$this->ogBootApiUrl.'/ogboot/v1/pxe-templates', [
'headers' => [ if (isset($content['error']) && $content['error'] === Response::HTTP_INTERNAL_SERVER_ERROR ) {
'accept' => 'application/json', throw new ValidatorException('An error occurred: ' . $content['error']);
],
]);
} catch (TransportExceptionInterface $e) {
return new JsonResponse( data: 'An error occurred', status: Response::HTTP_INTERNAL_SERVER_ERROR);
} }
$data = json_decode($response->getContent(), true); return new JsonResponse( data: $content, status: Response::HTTP_OK);
return new JsonResponse( data: $data, status: Response::HTTP_OK);
} }
} }

View File

@ -9,6 +9,7 @@ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\AsController; use Symfony\Component\HttpKernel\Attribute\AsController;
use Symfony\Component\Validator\Exception\ValidatorException;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface; use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
@ -33,7 +34,11 @@ class PostAction extends AbstractOgBootController
] ]
]; ];
$content = $this->createRequest('POST', 'http://'.$this->ogBootApiUrl.'/ogboot/v1/pxe-templates' , $params); $content = $this->createRequest('POST', '/ogboot/v1/pxe-templates' , $params);
if (isset($content['error']) && $content['error'] === Response::HTTP_INTERNAL_SERVER_ERROR ) {
throw new ValidatorException('An error occurred: ' . $content['error']);
}
$data->setSynchronized(true); $data->setSynchronized(true);
$this->entityManager->persist($data); $this->entityManager->persist($data);

View File

@ -28,7 +28,7 @@ class SyncAction extends AbstractOgBootController
*/ */
public function __invoke(): JsonResponse public function __invoke(): JsonResponse
{ {
$content = $this->createRequest('GET', 'http://' . $this->ogBootApiUrl . '/ogboot/v1/pxe-templates'); $content = $this->createRequest('GET', '/ogboot/v1/pxe-templates');
$templateNamesFromApi = $content['message']; $templateNamesFromApi = $content['message'];
$existingTemplates = $this->entityManager->getRepository(PxeTemplate::class)->findAll(); $existingTemplates = $this->entityManager->getRepository(PxeTemplate::class)->findAll();
@ -49,7 +49,11 @@ class SyncAction extends AbstractOgBootController
$templateEntity->setName($templateName); $templateEntity->setName($templateName);
} }
$templateContent = $this->createRequest('GET', 'http://' . $this->ogBootApiUrl . '/ogboot/v1/pxe-templates/' . $templateEntity->getName()); $templateContent = $this->createRequest('GET', '/ogboot/v1/pxe-templates/' . $templateEntity->getName());
if (isset($templateContent['error']) && $templateContent['error'] === Response::HTTP_INTERNAL_SERVER_ERROR ) {
throw new ValidatorException('An error occurred: ' . $templateContent['error']);
}
$templateEntity->setTemplateContent($templateContent['template_content']); $templateEntity->setTemplateContent($templateContent['template_content']);
$templateEntity->setSynchronized(true); $templateEntity->setSynchronized(true);

View File

@ -7,6 +7,7 @@ use App\Entity\Subnet;
use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\AsController; use Symfony\Component\HttpKernel\Attribute\AsController;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\Validator\Exception\ValidatorException; use Symfony\Component\Validator\Exception\ValidatorException;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
@ -29,11 +30,23 @@ class DeleteAction extends AbstractOgDhcpController
throw new ValidatorException('Data Id is required'); throw new ValidatorException('Data Id is required');
} }
$content = $this->createRequest('DELETE', 'http://'.$this->ogDhcpApiUrl.'/ogdhcp/v1/subnets/'.$data->getServerId()); try {
$content = $this->createRequest(
'DELETE',
'http://' . $this->ogDhcpApiUrl . '/ogdhcp/v1/subnets/' . $data->getServerId()
);
} catch (HttpException $e) {
if ($e->getStatusCode() === 404) {
$content = ['message' => 'Subnet not found on external API, proceeding with local deletion'];
} else {
throw $e;
}
}
$this->entityManager->remove($data); $this->entityManager->remove($data);
$this->entityManager->flush(); $this->entityManager->flush();
return new JsonResponse(data: $content, status: Response::HTTP_OK); return new JsonResponse(data: $content, status: Response::HTTP_OK);
} }
} }

View File

@ -53,17 +53,15 @@ class PostHostAction extends AbstractOgDhcpController
$params $params
); );
// Guardar resultado exitoso
$success[] = [ $success[] = [
'client' => $clientEntity->getName(), 'client' => $clientEntity->getName(),
'response' => $content 'response' => $content
]; ];
// Persistir solo si la llamada fue exitosa
$subnet->addClient($clientEntity); $subnet->addClient($clientEntity);
$this->entityManager->persist($subnet); $this->entityManager->persist($subnet);
$this->entityManager->flush(); $this->entityManager->flush();
} catch (\Throwable $e) { // Capturar cualquier error sin interrumpir } catch (\Throwable $e) {
$errors[] = [ $errors[] = [
'client' => $clientEntity->getName(), 'client' => $clientEntity->getName(),
'error' => $e->getMessage() 'error' => $e->getMessage()

View File

@ -24,26 +24,23 @@ class PutHostAction extends AbstractOgDhcpController
* @throws RedirectionExceptionInterface * @throws RedirectionExceptionInterface
* @throws ClientExceptionInterface * @throws ClientExceptionInterface
*/ */
public function __invoke(SubnetAddHostInput $input, Subnet $subnet): JsonResponse public function __invoke(string $mac, Client $client): JsonResponse
{ {
$clients = $input->clients; $subnet = $client->getSubnet();
foreach ($clients as $client) {
/** @var Client $clientEntity */ /** @var Client $clientEntity */
$clientEntity = $client->getEntity();
$data = [ $data = [
'host' => $clientEntity->getName(), 'hostname' => $client->getName(),
'oldMacAddress' => '', 'oldMacAddress' => strtolower($mac),
'macAddress' => '', 'macAddress' => strtolower($client->getMac()),
'address' => '', 'address' => $client->getIp(),
]; ];
$params = [ $params = [
'json' => $data 'json' => $data
]; ];
$content = $this->createRequest('PUT', 'http://'.$this->ogDhcpApiUrl.'/ogdhcp/v1/subnets/'.$subnet->getId().'/hosts', $params); $content = $this->createRequest('PUT', 'http://'.$this->ogDhcpApiUrl.'/ogdhcp/v1/subnets/'.$subnet->getServerId().'/hosts', $params);
}
return new JsonResponse(status: Response::HTTP_OK); return new JsonResponse(status: Response::HTTP_OK);
} }

View File

@ -0,0 +1,21 @@
<?php
namespace App\Dto\Input;
use App\Dto\Output\ClientOutput;
use App\Dto\Output\PartitionOutput;
use Symfony\Component\Serializer\Annotation\Groups;
final class BootClientsInput
{
/**
* @var ClientOutput[]
*/
#[Groups(['client:write'])]
public array $clients = [];
#[Groups(['client:write'])]
public ?PartitionOutput $partition = null;
}

View File

@ -2,6 +2,7 @@
namespace App\Dto\Input; namespace App\Dto\Input;
use App\Dto\Output\ClientOutput;
use App\Dto\Output\OrganizationalUnitOutput; use App\Dto\Output\OrganizationalUnitOutput;
use App\Entity\Client; use App\Entity\Client;
use Symfony\Component\Serializer\Annotation\Groups; use Symfony\Component\Serializer\Annotation\Groups;
@ -10,13 +11,13 @@ use Symfony\Component\Validator\Constraints as Assert;
final class ChangeOrganizationalUnitInput final class ChangeOrganizationalUnitInput
{ {
/** /**
* @var Client[] * @var ClientOutput[]
*/ */
#[Assert\GreaterThan(1)] #[Assert\GreaterThan(1)]
#[Groups(['user-group:write'])] #[Groups(['client:write'])]
public ?array $clients = []; public ?array $clients = [];
#[Assert\NotNull] #[Assert\NotNull]
#[Groups(['user-group:write'])] #[Groups(['client:write'])]
public ?OrganizationalUnitOutput $organizationalUnit = null; public ?OrganizationalUnitOutput $organizationalUnit = null;
} }

View File

@ -18,7 +18,7 @@ class ImageImageRepositoryOutput extends AbstractOutput
#[Groups(['image-image-repository:read', 'image:read'])] #[Groups(['image-image-repository:read', 'image:read'])]
public string $status; public string $status;
#[Groups(['image-image-repository:read', 'image:read', 'partition:read'])] #[Groups(['image-image-repository:read', 'image:read', 'partition:read', 'client:read'])]
public string $name; public string $name;
#[Groups(['image-image-repository:read', 'image:read'])] #[Groups(['image-image-repository:read', 'image:read'])]

View File

@ -4,6 +4,7 @@ namespace App\EventListener;
use App\Controller\OgBoot\PxeBootFile\DeleteAction; use App\Controller\OgBoot\PxeBootFile\DeleteAction;
use App\Controller\OgBoot\PxeBootFile\PostAction; use App\Controller\OgBoot\PxeBootFile\PostAction;
use App\Controller\OgDhcp\Subnet\PutHostAction;
use App\Entity\Client; use App\Entity\Client;
use Doctrine\Bundle\DoctrineBundle\Attribute\AsEntityListener; use Doctrine\Bundle\DoctrineBundle\Attribute\AsEntityListener;
use Doctrine\ORM\Event\PreUpdateEventArgs; use Doctrine\ORM\Event\PreUpdateEventArgs;
@ -18,6 +19,7 @@ readonly class ClientMacListener
{ {
public function __construct( public function __construct(
private DeleteAction $deleteAction, private DeleteAction $deleteAction,
private PutHostAction $putHostAction,
) )
{ {
@ -45,7 +47,7 @@ readonly class ClientMacListener
return; return;
} }
$this->putHostAction->__invoke($oldMac, $client);
$this->deleteAction->__invoke($oldMac); $this->deleteAction->__invoke($oldMac);
} }
} }

View File

@ -4,8 +4,11 @@ namespace App\EventSubscriber;
use ApiPlatform\Symfony\EventListener\EventPriorities; use ApiPlatform\Symfony\EventListener\EventPriorities;
use App\Controller\OgBoot\PxeBootFile\PostAction; use App\Controller\OgBoot\PxeBootFile\PostAction;
use App\Controller\OgDhcp\Subnet\PutHostAction;
use App\Dto\Output\ClientOutput; use App\Dto\Output\ClientOutput;
use App\Entity\Client; use App\Entity\Client;
use App\Entity\PxeTemplate;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\ViewEvent; use Symfony\Component\HttpKernel\Event\ViewEvent;
@ -18,7 +21,9 @@ use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
final readonly class ClientSubscriber implements EventSubscriberInterface final readonly class ClientSubscriber implements EventSubscriberInterface
{ {
public function __construct( public function __construct(
private readonly EntityManagerInterface $entityManager,
private PostAction $postAction, private PostAction $postAction,
private PutHostAction $putHostAction,
) )
{ {
@ -49,14 +54,19 @@ final readonly class ClientSubscriber implements EventSubscriberInterface
/** @var Client $client */ /** @var Client $client */
$client = $clientOutput->getEntity(); $client = $clientOutput->getEntity();
$template = !$client->getTemplate() ? $template = $client->getTemplate() ?
$client->getTemplate() : $client->getTemplate() :
$client->getOrganizationalUnit()?->getNetworkSettings()?->getPxeTemplate(); $client->getOrganizationalUnit()?->getNetworkSettings()?->getPxeTemplate();
if ($template === null) {
$template = $this->entityManager->getRepository(PxeTemplate::class)->findOneBy(['isDefault' => true]);
if ($template === null) { if ($template === null) {
return; return;
} }
}
$this->postAction->__invoke($client, $template); $this->postAction->__invoke($client, $template);
$this->putHostAction->__invoke($client->getMac(), $client);
} }
} }

View File

@ -63,15 +63,25 @@ class MercureSubscriber implements EventSubscriberInterface
'status' => $client->getStatus(), 'status' => $client->getStatus(),
]; ];
try {
$update = new Update( $update = new Update(
'clients', 'clients',
json_encode($data) json_encode($data)
); );
$this->hub->publish($update); $this->hub->publish($update);
$this->logger->info('Evento Mercure disparado', [ $this->logger->info('Evento Mercure disparado', [
'method' => $method, 'method' => $method,
'path' => $request->getPathInfo() 'path' => $request->getPathInfo()
]); ]);
} catch (\Exception $e) {
$this->logger->error('Error setting method for Mercure update', [
'method' => $method,
'path' => $request->getPathInfo(),
'error' => $e->getMessage(),
]);
return;
}
} }
} }

View File

@ -16,8 +16,12 @@ use Symfony\Component\HttpClient\HttpClient;
readonly class StatusService readonly class StatusService
{ {
public function __construct( public function __construct(
#[Autowire(env: 'OG_BOOT_API_URL')] #[Autowire(env: 'OG_BOOT_IP')]
private string $ogBootApiUrl protected string $ogBootIp,
#[Autowire(env: 'OG_BOOT_PXE_PORT')]
protected string $ogBootPxePort,
#[Autowire(env: 'OG_BOOT_API_PORT')]
protected string $ogBootApiPort,
) )
{ {
} }
@ -31,12 +35,12 @@ readonly class StatusService
public function __invoke() public function __invoke()
{ {
$httpClient = HttpClient::create([ $httpClient = HttpClient::create([
'verify_peer' => false, // Ignorar la verificación del certificado SSL 'verify_peer' => false,
'verify_host' => false, // Ignorar la verificación del nombre del host 'verify_host' => false,
]); ]);
try { try {
$response = $httpClient->request('GET', 'http://'.$this->ogBootApiUrl.'/ogboot/v1/status', [ $response = $httpClient->request('GET', 'http://'.$this->ogBootIp.':'.$this->ogBootApiPort.'/ogboot/v1/status', [
'headers' => [ 'headers' => [
'accept' => 'application/json', 'accept' => 'application/json',
], ],

View File

@ -9,18 +9,34 @@ use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\Put; use ApiPlatform\Metadata\Put;
use ApiPlatform\State\ProcessorInterface; use ApiPlatform\State\ProcessorInterface;
use ApiPlatform\Validator\ValidatorInterface; use ApiPlatform\Validator\ValidatorInterface;
use App\Controller\OgBoot\PxeBootFile\DeleteAction;
use App\Controller\OgDhcp\Subnet\DeleteHostAction;
use App\Dto\Input\ClientInput; use App\Dto\Input\ClientInput;
use App\Dto\Output\ClientOutput; use App\Dto\Output\ClientOutput;
use App\Dto\Output\UserGroupOutput; use App\Dto\Output\UserGroupOutput;
use App\Entity\OgLive;
use App\Entity\PxeTemplate;
use App\Repository\ClientRepository; use App\Repository\ClientRepository;
use App\Repository\MenuRepository; use App\Repository\MenuRepository;
use App\Repository\OgLiveRepository;
use App\Repository\PxeTemplateRepository;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
readonly class ClientProcessor implements ProcessorInterface readonly class ClientProcessor implements ProcessorInterface
{ {
public function __construct( public function __construct(
private ClientRepository $clientRepository, private ClientRepository $clientRepository,
private MenuRepository $menuRepository, private MenuRepository $menuRepository,
private ValidatorInterface $validator private PxeTemplateRepository $pxeTemplateRepository,
private OgLiveRepository $ogLiveRepository,
private ValidatorInterface $validator,
private DeleteHostAction $deleteHostAction,
private DeleteAction $deletePxeAction,
private KernelInterface $kernel,
) )
{ {
} }
@ -55,6 +71,8 @@ readonly class ClientProcessor implements ProcessorInterface
} }
$defaultMenu = $this->menuRepository->findOneBy(['isDefault' => true]); $defaultMenu = $this->menuRepository->findOneBy(['isDefault' => true]);
$defaultPxe = $this->pxeTemplateRepository->findOneBy(['isDefault' => true]);
$defaultPxeOgLive = $this->ogLiveRepository->findOneBy(['isDefault' => true]);
$client = $data->createOrUpdateEntity($entity); $client = $data->createOrUpdateEntity($entity);
@ -62,16 +80,35 @@ readonly class ClientProcessor implements ProcessorInterface
$client->setMenu($defaultMenu); $client->setMenu($defaultMenu);
} }
if ($defaultPxe) {
$client->setTemplate($defaultPxe);
}
if ($defaultPxeOgLive) {
$client->setOgLive($defaultPxeOgLive);
}
$this->validator->validate($client); $this->validator->validate($client);
$this->clientRepository->save($client); $this->clientRepository->save($client);
return new ClientOutput($client); return new ClientOutput($client);
} }
/**
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ClientExceptionInterface
*/
private function processDelete($data, Operation $operation, array $uriVariables = [], array $context = []): null private function processDelete($data, Operation $operation, array $uriVariables = [], array $context = []): null
{ {
$user = $this->clientRepository->findOneByUuid($uriVariables['uuid']); $client = $this->clientRepository->findOneByUuid($uriVariables['uuid']);
$this->clientRepository->delete($user); $this->clientRepository->delete($client);
if ($this->kernel->getEnvironment() !== 'test') {
$this->deleteHostAction->__invoke($client->getSubnet(), $client->getUuid());
$this->deletePxeAction->__invoke($client->getUuid());
}
return null; return null;
} }

View File

@ -9,8 +9,8 @@ use Symfony\Component\Validator\ConstraintValidator;
class OrganizationalUnitMulticastPortValidator extends ConstraintValidator class OrganizationalUnitMulticastPortValidator extends ConstraintValidator
{ {
CONST int minPort = 9000; public const int minPort = 9000;
CONST int maxPort = 9050; public const int maxPort = 9098;
public function validate($value, Constraint $constraint): void public function validate($value, Constraint $constraint): void
{ {
@ -18,7 +18,12 @@ class OrganizationalUnitMulticastPortValidator extends ConstraintValidator
return; return;
} }
if (!(self::minPort <= $value) && ($value <= self::maxPort)) { if ($value % 2 !== 0) {
$this->context->buildViolation('El puerto debe ser un número par y encontrarse entre el 9000 y el 9098.')->addViolation();
return;
}
if ($value < self::minPort || $value > self::maxPort) {
$this->context->buildViolation($constraint->message)->addViolation(); $this->context->buildViolation($constraint->message)->addViolation();
return; return;
} }