From e27e4b408f3b55aea72b9471c05df3839de89e38 Mon Sep 17 00:00:00 2001 From: Manuel Aranda Date: Wed, 4 Dec 2024 08:51:21 +0100 Subject: [PATCH] refs #917. Partition assistant --- src/Controller/OgAgent/CreateImageAction.php | 24 +++++++++++++++++++- src/Service/CreatePartitionService.php | 13 +++++++++-- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/Controller/OgAgent/CreateImageAction.php b/src/Controller/OgAgent/CreateImageAction.php index 4e01293..206f1a7 100644 --- a/src/Controller/OgAgent/CreateImageAction.php +++ b/src/Controller/OgAgent/CreateImageAction.php @@ -11,9 +11,11 @@ use App\Entity\Trace; use App\Model\ClientStatus; use App\Model\CommandTypes; use App\Model\ImageStatus; +use App\Model\PartitionTypes; use App\Model\TraceStatus; use App\Service\Trace\CreateService; use Doctrine\ORM\EntityManagerInterface; +use Exception; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; @@ -35,6 +37,12 @@ class CreateImageAction extends AbstractController { } + /** + * @throws TransportExceptionInterface + * @throws ServerExceptionInterface + * @throws RedirectionExceptionInterface + * @throws ClientExceptionInterface + */ public function __invoke(Image $image): JsonResponse { if (!$image->getClient()->getIp()) { @@ -46,7 +54,7 @@ class CreateImageAction extends AbstractController $data = [ 'dsk' => (string) $partitionInfo['numDisk'], 'par' => (string) $partitionInfo['numPartition'], - 'cpt' => "83", + 'cpt' => null, 'idi' => $image->getUuid(), 'nci' => $image->getName(), 'ipr' => $image->getRepository()->getIp(), @@ -54,6 +62,20 @@ class CreateImageAction extends AbstractController 'ids' => '0' ]; + $partitionTypes = PartitionTypes::getPartitionTypes(); + + $partitionCode = $partitionInfo['partitionCode']; + $cptKey = array_search($partitionCode, array_column($partitionTypes, 'name'), true); + + if ($cptKey !== false) { + $keys = array_keys($partitionTypes); + $partitionTypeCode = $keys[$cptKey]; + + $data['cpt'] = dechex($partitionTypeCode); + } else { + throw new Exception("El tipo de partición '$partitionCode' no se encontró en la lista."); + } + try { $response = $this->httpClient->request('POST', 'https://'.$image->getClient()->getIp().':8000/CloningEngine/CrearImagen', [ 'verify_peer' => false, diff --git a/src/Service/CreatePartitionService.php b/src/Service/CreatePartitionService.php index 77cd2b4..97c782e 100644 --- a/src/Service/CreatePartitionService.php +++ b/src/Service/CreatePartitionService.php @@ -7,6 +7,7 @@ use App\Entity\OperativeSystem; use App\Entity\Partition; use App\Model\PartitionTypes; use Doctrine\ORM\EntityManagerInterface; +use Exception; class CreatePartitionService { @@ -52,12 +53,20 @@ class CreatePartitionService $partitionEntity->setPartitionNumber($cfg['par']); $partitionEntity->setSize($cfg['tam']); - if (isset($cfg['cpt']) && $cfg['fsi'] !== '') { - $partitionEntity->setPartitionCode(PartitionTypes::getPartitionType(hexdec((integer)$cfg['cpt']))['name']); + if (isset($cfg['cpt']) && $cfg['cpt'] !== '') { + $decimalValue = hexdec($cfg['cpt']); + $partitionType = PartitionTypes::getPartitionType($decimalValue); + + if ($partitionType) { + $partitionEntity->setPartitionCode($partitionType['name']); + } else { + throw new Exception("El tipo de partición con código {$decimalValue} no se encontró."); + } } else { $partitionEntity->setPartitionCode(PartitionTypes::getPartitionType(0)['name']); } + $partitionEntity->setFilesystem($cfg['fsi']); $partitionEntity->setMemoryUsage(((int) $cfg['uso']) * 100); $this->entityManager->persist($partitionEntity);