diff --git a/config/api_platform/GitRepository.yaml b/config/api_platform/GitRepository.yaml index f4b7cf4..6b0b09c 100644 --- a/config/api_platform/GitRepository.yaml +++ b/config/api_platform/GitRepository.yaml @@ -93,7 +93,7 @@ resources: description: 'ID del ImageRepository' git_deploy_image: - shortName: Git Repository + shortName: Deploy Git Image description: Deploy Git image class: ApiPlatform\Metadata\Post method: POST @@ -101,6 +101,15 @@ resources: uriTemplate: /git-repositories/deploy-image controller: App\Controller\DeployGitImageAction + git_update_image: + shortName: Update Git Image + description: Update Git image + class: ApiPlatform\Metadata\Post + method: POST + input: App\Dto\Input\UpdateGitImageInput + uriTemplate: /git-repositories/update-image + controller: App\Controller\UpdateGitImageAction + properties: App\Entity\GitRepository: id: diff --git a/src/Controller/OgAgent/CreateImageAction.php b/src/Controller/OgAgent/CreateImageAction.php index 32b96a6..9364fd2 100644 --- a/src/Controller/OgAgent/CreateImageAction.php +++ b/src/Controller/OgAgent/CreateImageAction.php @@ -81,8 +81,6 @@ class CreateImageAction extends AbstractOgAgentController } else { $repository = $image->getClient()->getRepository(); - // Para imágenes Git, no necesitamos crear entidades en la base de datos - // ya que los repositorios Git son datos externos return $this->createGitImage($image, $partitionInfo, $repository, $queue, $gitRepositoryName); } } diff --git a/src/Controller/OgAgent/UpdateGitImageAction.php b/src/Controller/OgAgent/UpdateGitImageAction.php new file mode 100644 index 0000000..1912e97 --- /dev/null +++ b/src/Controller/OgAgent/UpdateGitImageAction.php @@ -0,0 +1,80 @@ +getIp()) { + throw new BadRequestHttpException('IP is required'); + } + + if (!$input->gitRepository) { + throw new BadRequestHttpException('Git repository name is required for Git image update'); + } + + $partition = $input->partition->getEntity(); + $repository = $client->getRepository(); + + $data = [ + 'dsk' => (string) $partition->getDiskNumber(), + 'par' => (string) $partition->getPartitionNumber(), + 'nci' => $input->gitRepository, + 'ipr' => $repository->getIp(), + 'msg' => 'updating git image', + 'nfn' => 'ModificarImagenGit', + 'ids' => '0' + ]; + + $url = 'https://'.$client->getIp().':8000/opengnsys/ModificarImagenGit'; + + $response = $this->createRequest( + method: 'POST', + url: $url, + params: [ + 'json' => $data, + ], + token: $client->getToken(), + ); + + $this->logger->info('Updating Git image', [ + 'repository' => $input->gitRepository, + 'client' => $client->getIp(), + 'disk' => $partition->getDiskNumber(), + 'partition' => $partition->getPartitionNumber() + ]); + + if (isset($response['error']) && $response['code'] === Response::HTTP_INTERNAL_SERVER_ERROR) { + throw new BadRequestHttpException('Error updating Git image'); + } + + $jobId = $response['job_id']; + + $client->setStatus(ClientStatus::BUSY); + $this->entityManager->persist($client); + $this->entityManager->flush(); + + return $jobId; + } +} \ No newline at end of file diff --git a/src/Controller/OgAgent/Webhook/StatusController.php b/src/Controller/OgAgent/Webhook/StatusController.php index 75a998c..d3132f6 100644 --- a/src/Controller/OgAgent/Webhook/StatusController.php +++ b/src/Controller/OgAgent/Webhook/StatusController.php @@ -39,6 +39,7 @@ class StatusController extends AbstractController { const string CREATE_IMAGE = 'RESPUESTA_CrearImagen'; const string CREATE_IMAGE_GIT = 'RESPUESTA_CrearImagenGit'; + const string UPDATE_IMAGE_GIT = 'RESPUESTA_ModificarImagenGit'; const string RESTORE_IMAGE = 'RESPUESTA_RestaurarImagen'; const string RESTORE_IMAGE_GIT = 'RESPUESTA_RestaurarImagenGit'; const string CONFIGURE_IMAGE = 'RESPUESTA_Configurar'; @@ -115,6 +116,30 @@ class StatusController extends AbstractController $this->logger->info('Git image creation completed.', ['job_id' => $data['job_id'], 'success' => $data['res'] === 1]); } + if (isset($data['nfn']) && $data['nfn'] === self::UPDATE_IMAGE_GIT) { + $trace = $this->entityManager->getRepository(Trace::class)->findOneBy(['jobId' => $data['job_id']]); + + if (!$trace) { + $this->logger->error('Trace not found', $data); + return new JsonResponse(['message' => 'Trace not found'], Response::HTTP_NOT_FOUND); + } + + if ($data['res'] === 1) { + $trace->setStatus(TraceStatus::SUCCESS); + $trace->setFinishedAt(new \DateTime()); + } else { + $trace->setStatus(TraceStatus::FAILED); + $trace->setFinishedAt(new \DateTime()); + $trace->setOutput($data['der']); + } + + $client = $trace->getClient(); + $client->setStatus(ClientStatus::OG_LIVE); + $this->entityManager->persist($client); + $this->entityManager->persist($trace); + $this->entityManager->flush(); + $this->logger->info('Git image update completed.', ['job_id' => $data['job_id'], 'success' => $data['res'] === 1]); + } if (isset($data['nfn']) && $data['nfn'] === self::CREATE_IMAGE) { $trace = $this->entityManager->getRepository(Trace::class)->findOneBy(['jobId' => $data['job_id']]); diff --git a/src/Controller/UpdateGitImageAction.php b/src/Controller/UpdateGitImageAction.php new file mode 100644 index 0000000..b8a8e87 --- /dev/null +++ b/src/Controller/UpdateGitImageAction.php @@ -0,0 +1,89 @@ +validator->validate($input); + + if (!$input->client) { + throw new \InvalidArgumentException('Client is required'); + } + + if (!$input->partition) { + throw new \InvalidArgumentException('Partition is required'); + } + + $this->handleGitUpdate($input); + + return new JsonResponse(data: [], status: Response::HTTP_OK); + } + + private function handleGitUpdate(UpdateGitImageInput $input): void + { + $client = $input->client->getEntity(); + $partition = $input->partition->getEntity(); + + $inputData = $this->createInputData($input, $client, $partition); + $this->processUpdate($client, $input, $inputData, DeployMethodTypes::GIT); + } + + private function processUpdate(Client $client, UpdateGitImageInput $input, array $inputData, string $updateType): void + { + $agentJobId = $this->updateGitImageOgAgentAction->__invoke($input, $client); + + if (!$agentJobId) { + if ($input->queue) { + $this->createService->__invoke($client, CommandTypes::UPDATE_IMAGE_GIT, TraceStatus::PENDING, null, $inputData); + } + return; + } + + $this->createService->__invoke($client, CommandTypes::UPDATE_IMAGE_GIT, TraceStatus::IN_PROGRESS, $agentJobId, $inputData); + } + + private function createInputData(UpdateGitImageInput $input, Client $client, $partition): array + { + return [ + 'method' => 'ModificarImagenGit', + 'type' => 'git', + 'client' => $client->getUuid(), + 'diskNumber' => $partition->getDiskNumber(), + 'partitionNumber' => $partition->getPartitionNumber(), + 'repositoryName' => $input->gitRepository, + ]; + } +} \ No newline at end of file diff --git a/src/Dto/Input/ImageInput.php b/src/Dto/Input/ImageInput.php index f472a78..f8f1f60 100644 --- a/src/Dto/Input/ImageInput.php +++ b/src/Dto/Input/ImageInput.php @@ -25,10 +25,6 @@ final class ImageInput #[ApiProperty(description: 'The name of the image', example: "Image 1")] public ?string $name = null; - #[Groups(['image:write'])] - #[ApiProperty(description: 'The type of the image', example: "Server")] - public ?string $source = 'input'; - #[Groups(['image:write'])] #[ApiProperty(description: 'The type of the image', example: "Server")] public ?string $type = ''; diff --git a/src/Dto/Input/UpdateGitImageInput.php b/src/Dto/Input/UpdateGitImageInput.php new file mode 100644 index 0000000..4fa6bf9 --- /dev/null +++ b/src/Dto/Input/UpdateGitImageInput.php @@ -0,0 +1,29 @@ + 'Update Cache', self::CREATE_IMAGE => 'Create Image', self::CREATE_IMAGE_GIT => 'Create Image Git', + self::UPDATE_IMAGE_GIT => 'Update Image Git', self::CONVERT_IMAGE => 'Convert Image', self::CONVERT_IMAGE_TO_VIRTUAL => 'Convert Image to Virtual', self::CREATE_IMAGE_AUX_FILE => 'Create Image Aux File', diff --git a/src/State/Processor/ImageProcessor.php b/src/State/Processor/ImageProcessor.php index 24c94e2..290517a 100644 --- a/src/State/Processor/ImageProcessor.php +++ b/src/State/Processor/ImageProcessor.php @@ -78,7 +78,10 @@ readonly class ImageProcessor implements ProcessorInterface $response = $this->createImageActionController->__invoke($data->queue, $image, null, null, $data->gitRepository); } - $this->validator->validate($image); + if ($data->type !== 'git') { + $this->validator->validate($image); + } + $this->imageRepository->save($image); }