Some improvements
testing/ogcore-api/pipeline/head There was a failure building this commit Details

pull/32/head
Manuel Aranda Rosales 2025-05-14 13:25:57 +02:00
parent 36c2abc98f
commit 326ff47edd
17 changed files with 281 additions and 32 deletions

View File

@ -1,5 +1,13 @@
# Changelog # Changelog
<<<<<<< HEAD ## [0.12.1] - 2025-05-14
### Improved
- Se ha eliminado la restriccion en el formulario de crear/editar repositorio, que hacia que la comprobara el formato de IP. Ahora tambien puede ser DNS.
- Mejora en el script de ejecutar tareas.
- Ahora al editar la mac de un cliente, se borra el fichero de arranque antiguo.
- Se ha añadido una restriccion en plantillas para que tan solo haya 1 por defecto
---
## [0.12.0] - 2025-05-13 ## [0.12.0] - 2025-05-13
### Added ### Added
- Se ha añadido nueva API para poder gestionar las tareas y acciones programadas. - Se ha añadido nueva API para poder gestionar las tareas y acciones programadas.
@ -12,11 +20,11 @@
## Fixed ## Fixed
- Se ha corregido el bug en la creacion de clientes masivos donde no se le asignaba la plantilla PXE. - Se ha corregido el bug en la creacion de clientes masivos donde no se le asignaba la plantilla PXE.
- Se ha corregido un bug en el DTO de clientes, que hacia que PHP diera un timeout por bucle infinito. - Se ha corregido un bug en el DTO de clientes, que hacia que PHP diera un timeout por bucle infinito.
=======
---
## [0.11.2] - 2025-04-23 ## [0.11.2] - 2025-04-23
### Fixed ### Fixed
- Se ha cambiado la forma en guardar la fecha al recibir "ping" de los clientes. - Se ha cambiado la forma en guardar la fecha al recibir "ping" de los clientes.
>>>>>>> main
--- ---
## [0.11.1] - 2025-04-16 ## [0.11.1] - 2025-04-16

View File

@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20250514051344 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}
public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE client ADD token VARCHAR(255) DEFAULT NULL');
}
public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE client DROP token');
}
}

View File

@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20250514101117 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}
public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('CREATE UNIQUE INDEX UNIQ_IDENTIFIER_NAME ON command (name)');
}
public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('DROP INDEX UNIQ_IDENTIFIER_NAME ON command');
}
}

View File

@ -10,6 +10,7 @@ use App\Dto\Input\CommandExecuteInput;
use App\Dto\Input\DeployImageInput; use App\Dto\Input\DeployImageInput;
use App\Dto\Output\ClientOutput; use App\Dto\Output\ClientOutput;
use App\Entity\CommandTask; use App\Entity\CommandTask;
use App\Model\ClientStatus;
use App\Repository\CommandTaskRepository; use App\Repository\CommandTaskRepository;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Attribute\AsCommand;
@ -59,23 +60,21 @@ class RunScheduledCommandTasksCommand extends Command
usort($scripts, fn($a, $b) => $a->getExecutionOrder() <=> $b->getExecutionOrder()); usort($scripts, fn($a, $b) => $a->getExecutionOrder() <=> $b->getExecutionOrder());
foreach ($scripts as $script) { foreach ($scripts as $script) {
try {
$output->writeln(" - Ejecutando script de tipo {$script->getType()} con orden {$script->getExecutionOrder()}"); $output->writeln(" - Ejecutando script de tipo {$script->getType()} con orden {$script->getExecutionOrder()}");
if ($script->getType() === 'run-script') { if ($script->getType() === 'run-script') {
$input = new CommandExecuteInput(); $input = new CommandExecuteInput();
foreach ($task->getOrganizationalUnit()?->getClients() as $client) { foreach ($task->getOrganizationalUnit()?->getClients() as $client) {
if ($client->getStatus() !== ClientStatus::OG_LIVE) {
continue;
}
$input->clients[] = new ClientOutput($client); $input->clients[] = new ClientOutput($client);
} }
$input->script = $script->getContent(); $input->script = $script->getContent();
$this->runScriptAction->__invoke($input); $this->runScriptAction->__invoke($input);
} }
} catch (TransportExceptionInterface $e) {
$output->writeln("Error ejecutando script: " . $e->getMessage());
continue;
}
} }
$task->setLastExecution(new \DateTime()); $task->setLastExecution(new \DateTime());

View File

@ -54,10 +54,10 @@ class RunScriptAction extends AbstractController
], ],
'json' => $data, 'json' => $data,
]); ]);
$this->logger->info('Rebooting client', ['client' => $client->getId()]); $this->logger->info('Executing run-script', ['client' => $client->getId(), 'response' => $response->getContent()]);
} catch (TransportExceptionInterface $e) { } catch (TransportExceptionInterface $e) {
$this->logger->error('Error rebooting client', ['client' => $client->getId(), 'error' => $e->getMessage()]); $this->logger->error('Error executing run-script', ['client' => $client->getId(), 'error' => $e->getMessage()]);
return new JsonResponse( return new JsonResponse(
data: ['error' => $e->getMessage()], data: ['error' => $e->getMessage()],
status: Response::HTTP_INTERNAL_SERVER_ERROR status: Response::HTTP_INTERNAL_SERVER_ERROR

View File

@ -0,0 +1,41 @@
<?php
namespace App\Controller\OgBoot\PxeBootFile;
use App\Controller\OgBoot\AbstractOgBootController;
use App\Entity\Client;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\AsController;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
#[AsController]
class DeleteAction extends AbstractOgBootController
{
/**
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ClientExceptionInterface
*/
public function __invoke(string $mac): JsonResponse
{
try {
$response = $this->httpClient->request('DELETE', 'http://'.$this->ogBootApiUrl.'/ogboot/v1/pxes/'.$mac, [
'headers' => [
'accept' => 'application/json',
],
]);
} 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: $data, status: Response::HTTP_OK);
}
}

View File

@ -24,10 +24,10 @@ class GetAction extends AbstractOgBootController
* @throws RedirectionExceptionInterface * @throws RedirectionExceptionInterface
* @throws ClientExceptionInterface * @throws ClientExceptionInterface
*/ */
public function __invoke(Client $client, HttpClientInterface $httpClient): JsonResponse public function __invoke(Client $client): JsonResponse
{ {
try { try {
$response = $httpClient->request('GET', 'http://'.$this->ogBootApiUrl.'/ogboot/v1/pxes/'.$client->getMac(), [ $response = $this->httpClient->request('GET', 'http://'.$this->ogBootApiUrl.'/ogboot/v1/pxes/'.$client->getMac(), [
'headers' => [ 'headers' => [
'accept' => 'application/json', 'accept' => 'application/json',
], ],

View File

@ -22,7 +22,6 @@ final class ImageRepositoryInput
public ?string $name = null; public ?string $name = null;
#[Assert\NotBlank] #[Assert\NotBlank]
#[Assert\Ip]
#[Groups(['repository:write'])] #[Groups(['repository:write'])]
#[ApiProperty(description: 'The IP of the repository', example: "")] #[ApiProperty(description: 'The IP of the repository', example: "")]
public ?string $ip = null; public ?string $ip = null;

View File

@ -4,9 +4,11 @@ namespace App\Dto\Input;
use ApiPlatform\Metadata\ApiProperty; use ApiPlatform\Metadata\ApiProperty;
use App\Entity\PxeTemplate; use App\Entity\PxeTemplate;
use App\Validator\Constraints\PxeTemplateUniqueDefault;
use Symfony\Component\Serializer\Annotation\Groups; use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert; use Symfony\Component\Validator\Constraints as Assert;
#[PxeTemplateUniqueDefault]
final class PxeTemplateInput final class PxeTemplateInput
{ {
#[Assert\NotBlank(message: 'validators.pxe_template.name.not_blank')] #[Assert\NotBlank(message: 'validators.pxe_template.name.not_blank')]

View File

@ -89,6 +89,9 @@ class Client extends AbstractEntity
#[ORM\Column(length: 255, nullable: true)] #[ORM\Column(length: 255, nullable: true)]
private ?string $firmwareType = null; private ?string $firmwareType = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $token = null;
public function __construct() public function __construct()
{ {
parent::__construct(); parent::__construct();
@ -354,4 +357,16 @@ class Client extends AbstractEntity
return $this; return $this;
} }
public function getToken(): ?string
{
return $this->token;
}
public function setToken(?string $token): static
{
$this->token = $token;
return $this;
}
} }

View File

@ -7,8 +7,11 @@ use Doctrine\DBAL\Types\Types;
use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
#[ORM\Entity(repositoryClass: CommandRepository::class)] #[ORM\Entity(repositoryClass: CommandRepository::class)]
#[ORM\UniqueConstraint(name: 'UNIQ_IDENTIFIER_NAME', fields: ['name'])]
#[UniqueEntity(fields: ['name'], message: 'validators.command.name.unique')]
class Command extends AbstractEntity class Command extends AbstractEntity
{ {
use NameableTrait; use NameableTrait;

View File

@ -227,7 +227,6 @@ class CommandTask extends AbstractEntity
if ($type === 'none') { if ($type === 'none') {
$execDate = $schedule->getExecutionDate(); $execDate = $schedule->getExecutionDate();
if ($execDate !== null && $execDate > $now) {
if ($executionTime !== null) { if ($executionTime !== null) {
$execDateTime = \DateTime::createFromFormat( $execDateTime = \DateTime::createFromFormat(
'Y-m-d H:i:s', 'Y-m-d H:i:s',
@ -240,7 +239,6 @@ class CommandTask extends AbstractEntity
if ($closestDateTime === null || $execDateTime < $closestDateTime) { if ($closestDateTime === null || $execDateTime < $closestDateTime) {
$closestDateTime = $execDateTime; $closestDateTime = $execDateTime;
} }
}
} else { } else {
$details = $schedule->getRecurrenceDetails(); $details = $schedule->getRecurrenceDetails();
if ($details === null) { if ($details === null) {

View File

@ -0,0 +1,51 @@
<?php
namespace App\EventListener;
use App\Controller\OgBoot\PxeBootFile\DeleteAction;
use App\Controller\OgBoot\PxeBootFile\PostAction;
use App\Entity\Client;
use Doctrine\Bundle\DoctrineBundle\Attribute\AsEntityListener;
use Doctrine\ORM\Event\PreUpdateEventArgs;
use Doctrine\ORM\Events;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
#[AsEntityListener(event: Events::preUpdate, method: 'preUpdate', entity: Client::class)]
readonly class ClientMacListener
{
public function __construct(
private DeleteAction $deleteAction,
)
{
}
/**
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ClientExceptionInterface
*/
public function preUpdate(Client $client, PreUpdateEventArgs $event): void
{
$em = $event->getObjectManager();
$uow = $em->getUnitOfWork();
$changeSet = $uow->getEntityChangeSet($client);
if (!array_key_exists('mac', $changeSet)) {
return;
}
$oldMac = isset($changeSet['mac'][0]) ? $changeSet['mac'][0] : null;
if ($oldMac === null) {
return;
}
$this->deleteAction->__invoke($oldMac);
}
}

View File

@ -0,0 +1,23 @@
<?php
namespace App\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
#[\Attribute]
class PxeTemplateUniqueDefault extends Constraint
{
public string $message;
public function __construct(mixed $options = null, ?array $groups = null, mixed $payload = null)
{
parent::__construct($options, $groups, $payload);
$this->message = 'Ya hay un oglive marcado como predeterminado.';
}
public function getTargets(): string
{
return self::CLASS_CONSTRAINT;
}
}

View File

@ -0,0 +1,46 @@
<?php
namespace App\Validator\Constraints;
use App\Dto\Input\PxeTemplateInput;
use App\Dto\Input\RemoteCalendarRuleInput;
use App\Entity\OgLive;
use App\Entity\PxeTemplate;
use App\Entity\RemoteCalendarRule;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
class PxeTemplateUniqueDefaultValidator extends ConstraintValidator
{
public function __construct(
private readonly EntityManagerInterface $entityManager,
private readonly RequestStack $requestStack
)
{
}
public function validate(mixed $value, Constraint $constraint): void
{
$request = $this->requestStack->getCurrentRequest();
if (!$value instanceof PxeTemplateInput) {
return;
}
if ($value->isDefault === false) {
return;
}
$ogLiveDefault = $this->entityManager->getRepository(PxeTemplate::class)
->findOneBy([
'isDefault' => true,
]);
if ($ogLiveDefault) {
$this->context->buildViolation($constraint->message)->addViolation();
}
}
}

View File

@ -11,6 +11,7 @@ validators:
command: command:
name: name:
not_blank: 'The name should not be blank.' not_blank: 'The name should not be blank.'
unique: 'The name should be unique.'
script: script:
not_blank: 'The script should not be blank.' not_blank: 'The script should not be blank.'

View File

@ -11,6 +11,7 @@ validators:
command: command:
name: name:
not_blank: 'El nombre no debería estar vacío.' not_blank: 'El nombre no debería estar vacío.'
unique: 'El nombre debería ser único. Ya existe un comando con ese nombre.'
script: script:
not_blank: 'El script no debería estar vacío.' not_blank: 'El script no debería estar vacío.'