From 0002ea4ccce07058a8073def1b2dd9f44aae6dab Mon Sep 17 00:00:00 2001 From: Manuel Aranda Date: Thu, 5 Dec 2024 13:05:19 +0100 Subject: [PATCH] Added router in subnet. Advanced bootfile changes, and partition assistant updates --- migrations/Version20241205110301.php | 31 +++++ .../OgAgent/PartitionAssistantAction.php | 107 +++++++++--------- .../OgAgent/Webhook/ClientsController.php | 6 +- src/Controller/OgDhcp/Subnet/PostAction.php | 1 + src/Controller/OgDhcp/Subnet/PutAction.php | 1 + src/Dto/Input/SubnetInput.php | 6 + src/Dto/Output/SubnetOutput.php | 4 + src/Entity/Subnet.php | 15 +++ src/Service/Trace/CreateService.php | 2 +- 9 files changed, 118 insertions(+), 55 deletions(-) create mode 100644 migrations/Version20241205110301.php diff --git a/migrations/Version20241205110301.php b/migrations/Version20241205110301.php new file mode 100644 index 0000000..88667b2 --- /dev/null +++ b/migrations/Version20241205110301.php @@ -0,0 +1,31 @@ +addSql('ALTER TABLE subnet ADD router 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 subnet DROP router'); + } +} diff --git a/src/Controller/OgAgent/PartitionAssistantAction.php b/src/Controller/OgAgent/PartitionAssistantAction.php index 2ebf04f..f11d2a0 100644 --- a/src/Controller/OgAgent/PartitionAssistantAction.php +++ b/src/Controller/OgAgent/PartitionAssistantAction.php @@ -47,24 +47,26 @@ class PartitionAssistantAction extends AbstractController /** @var Client $client */ $client = $input->partitions[0]->client->getEntity(); - $data = []; - $diskNumber = 0; - $cacheSize = 0; $disks = []; - $cpt = ''; - - $data = []; - $diskData = []; - foreach ($partitions as $partition) { - if ($partition->filesystem === 'CACHE') { - $cacheSize = $partition->size * 1024; - $disks[$partition->diskNumber] = $cacheSize; - } - $diskNumber = $partition->diskNumber; - $data[] = [ + if (!isset($disks[$diskNumber])) { + $disks[$diskNumber] = [ + 'diskData' => [], + 'partitionData' => [] + ]; + } + + if ($partition->filesystem === 'CACHE') { + $disks[$diskNumber]['diskData'] = [ + 'dis' => (string) $diskNumber, + 'che' => "0", + 'tch' => (string) ($partition->size * 1024), + ]; + } + + $disks[$diskNumber]['partitionData'][] = [ 'par' => (string) $partition->partitionNumber, 'cpt' => $partition->partitionCode, 'sfi' => $partition->filesystem, @@ -73,48 +75,47 @@ class PartitionAssistantAction extends AbstractController ]; } - foreach ($disks as $diskNumber => $size) { - $diskData[] = [ - 'dis' => (string) $diskNumber, - 'che' => "0", - 'tch' => (string) $size, + // Hacer una llamada por cada disco + foreach ($disks as $diskNumber => $diskInfo) { + $data = []; + if (!empty($diskInfo['diskData'])) { + $data[] = $diskInfo['diskData']; + } + $data = array_merge($data, $diskInfo['partitionData']); + + $result = [ + "nfn" => "Configurar", + "dsk" => (string) $diskNumber, + "cfg" => $data, + "ids" => "0" ]; + + try { + $response = $this->httpClient->request('POST', 'https://' . $client->getIp() . ':8000/CloningEngine/Configurar', [ + 'verify_peer' => false, + 'verify_host' => false, + 'headers' => [ + 'Content-Type' => 'application/json', + ], + 'json' => $result, + ]); + } catch (TransportExceptionInterface $e) { + return new JsonResponse( + data: ['error' => "Error en disco $diskNumber: " . $e->getMessage()], + status: Response::HTTP_INTERNAL_SERVER_ERROR + ); + } + + $jobId = json_decode($response->getContent(), true)['job_id']; + + $client->setStatus(ClientStatus::BUSY); + $this->entityManager->persist($client); + $this->entityManager->flush(); + + $this->createService->__invoke($client, CommandTypes::PARTITION_AND_FORMAT, TraceStatus::IN_PROGRESS, $jobId, []); } - $data = array_merge($diskData, $data); - - $result = [ - "nfn" => "Configurar", - "dsk" => "1", - "cfg" => $data, - "ids" => "0" - ]; - - try { - $response = $this->httpClient->request('POST', 'https://'.$client->getIp().':8000/CloningEngine/Configurar', [ - 'verify_peer' => false, - 'verify_host' => false, - 'headers' => [ - 'Content-Type' => 'application/json', - ], - 'json' => $result, - ]); - - } catch (TransportExceptionInterface $e) { - return new JsonResponse( - data: ['error' => $e->getMessage()], - status: Response::HTTP_INTERNAL_SERVER_ERROR - ); - } - - $jobId = json_decode($response->getContent(), true)['job_id']; - - $client->setStatus(ClientStatus::BUSY); - $this->entityManager->persist($client); - $this->entityManager->flush(); - - $this->createService->__invoke($client, CommandTypes::PARTITION_AND_FORMAT, TraceStatus::IN_PROGRESS, $jobId, []); - return new JsonResponse(data: $client, status: Response::HTTP_OK); } + } diff --git a/src/Controller/OgAgent/Webhook/ClientsController.php b/src/Controller/OgAgent/Webhook/ClientsController.php index aeca19e..ebe045c 100644 --- a/src/Controller/OgAgent/Webhook/ClientsController.php +++ b/src/Controller/OgAgent/Webhook/ClientsController.php @@ -124,6 +124,9 @@ class ClientsController extends AbstractController $trace->setFinishedAt(new \DateTime()); $image->setStatus(ImageStatus::PENDING); $client->setStatus(ClientStatus::OG_LIVE); + if (isset($data['cfg'])) { + $this->createPartitionService->__invoke($data,$client); + } } else { $trace->setStatus(TraceStatus::FAILED); $trace->setFinishedAt(new \DateTime()); @@ -145,7 +148,6 @@ class ClientsController extends AbstractController if ($data['res'] === 1) { $trace->setStatus(TraceStatus::SUCCESS); $trace->setFinishedAt(new \DateTime()); - $client->setStatus(ClientStatus::OG_LIVE); if (isset($data['cfg'])) { $this->createPartitionService->__invoke($data,$client); } @@ -155,6 +157,8 @@ class ClientsController extends AbstractController $trace->setOutput($data['der']); } + $client->setStatus(ClientStatus::OG_LIVE); + $this->entityManager->persist($client); $this->entityManager->persist($trace); $this->entityManager->flush(); diff --git a/src/Controller/OgDhcp/Subnet/PostAction.php b/src/Controller/OgDhcp/Subnet/PostAction.php index 7d3f6be..7a87fc6 100644 --- a/src/Controller/OgDhcp/Subnet/PostAction.php +++ b/src/Controller/OgDhcp/Subnet/PostAction.php @@ -32,6 +32,7 @@ class PostAction extends AbstractOgDhcpController 'address' => $data->getIpAddress(), 'nextServer' => $data->getNextServer(), 'bootFileName' => $data->getBootFileName(), + 'router' => $data->getRouter(), ] ]; diff --git a/src/Controller/OgDhcp/Subnet/PutAction.php b/src/Controller/OgDhcp/Subnet/PutAction.php index c7e39b8..fbaad21 100644 --- a/src/Controller/OgDhcp/Subnet/PutAction.php +++ b/src/Controller/OgDhcp/Subnet/PutAction.php @@ -35,6 +35,7 @@ class PutAction extends AbstractOgDhcpController 'address' => $data->getIpAddress(), 'nextServer' => $data->getNextServer(), 'bootFileName' => $data->getBootFileName(), + 'router' => $data->getRouter(), ] ]; diff --git a/src/Dto/Input/SubnetInput.php b/src/Dto/Input/SubnetInput.php index 59d9726..efbcf6a 100644 --- a/src/Dto/Input/SubnetInput.php +++ b/src/Dto/Input/SubnetInput.php @@ -32,6 +32,10 @@ final class SubnetInput #[ApiProperty(description: 'The next server of the subnet', example: "")] public ?string $nextServer = null; + #[Groups(['subnet:write'])] + #[ApiProperty(description: 'The router of the subnet', example: "")] + public ?string $router = null; + #[Groups(['subnet:write'])] #[ApiProperty(description: 'The boot file name of the subnet', example: "")] public ?string $bootFileName = null; @@ -52,6 +56,7 @@ final class SubnetInput $this->name = $subnet->getName(); $this->netmask = $subnet->getNetmask(); $this->ipAddress = $subnet->getIpAddress(); + $this->router = $subnet->getRouter(); $this->nextServer = $subnet->getNextServer(); $this->bootFileName = $subnet->getBootFileName(); @@ -73,6 +78,7 @@ final class SubnetInput $subnet->setIpAddress($this->ipAddress); $subnet->setNextServer($this->nextServer); $subnet->setBootFileName($this->bootFileName); + $subnet->setRouter($this->router); foreach ($this->organizationalUnits as $organizationalUnit) { $organizationalUnitToAdd[] = $organizationalUnit->getEntity(); diff --git a/src/Dto/Output/SubnetOutput.php b/src/Dto/Output/SubnetOutput.php index 31ef891..7e84c0b 100644 --- a/src/Dto/Output/SubnetOutput.php +++ b/src/Dto/Output/SubnetOutput.php @@ -27,6 +27,9 @@ final class SubnetOutput extends AbstractOutput #[Groups(['subnet:read'])] public ?string $bootFileName = null; + #[Groups(['subnet:read'])] + public ?string $router = null; + #[Groups(['subnet:read'])] public array $clients; @@ -50,6 +53,7 @@ final class SubnetOutput extends AbstractOutput $this->netmask = $subnet->getNetmask(); $this->ipAddress = $subnet->getIpAddress(); $this->nextServer = $subnet->getNextServer(); + $this->router = $subnet->getRouter(); $this->bootFileName = $subnet->getBootFileName(); $this->synchronized = $subnet->isSynchronized(); $this->serverId = $subnet->getServerId(); diff --git a/src/Entity/Subnet.php b/src/Entity/Subnet.php index 65d5794..08ef819 100644 --- a/src/Entity/Subnet.php +++ b/src/Entity/Subnet.php @@ -40,6 +40,9 @@ class Subnet extends AbstractEntity #[ORM\Column(nullable: true)] private ?int $serverId = null; + #[ORM\Column(length: 255, nullable: true)] + private ?string $router = null; + public function __construct() { parent::__construct(); @@ -187,4 +190,16 @@ class Subnet extends AbstractEntity return $this; } + + public function getRouter(): ?string + { + return $this->router; + } + + public function setRouter(?string $router): static + { + $this->router = $router; + + return $this; + } } diff --git a/src/Service/Trace/CreateService.php b/src/Service/Trace/CreateService.php index 12a3459..07a6de1 100644 --- a/src/Service/Trace/CreateService.php +++ b/src/Service/Trace/CreateService.php @@ -15,7 +15,7 @@ readonly class CreateService { } - public function __invoke(Client $client, ?string $command, string $status, string $jobId, ?array $input = []): Trace + public function __invoke(Client $client, ?string $command, string $status, ?string $jobId = '', ?array $input = []): Trace { $trace = new Trace(); $trace->setClient($client);