refs #2462. ogGit, updateImage
testing/ogcore-api/pipeline/head This commit looks good Details

develop
Manuel Aranda Rosales 2025-07-14 15:01:32 +02:00
parent c42c50e29b
commit 65ae79d976
9 changed files with 239 additions and 8 deletions

View File

@ -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:

View File

@ -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);
}
}

View File

@ -0,0 +1,80 @@
<?php
declare(strict_types=1);
namespace App\Controller\OgAgent;
use App\Dto\Input\UpdateGitImageInput;
use App\Entity\Client;
use App\Model\ClientStatus;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
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 UpdateGitImageAction extends AbstractOgAgentController
{
/**
* @throws RedirectionExceptionInterface
* @throws ClientExceptionInterface
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
*/
public function __invoke(UpdateGitImageInput $input, Client $client)
{
if (!$client->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;
}
}

View File

@ -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']]);

View File

@ -0,0 +1,89 @@
<?php
declare(strict_types=1);
namespace App\Controller;
use ApiPlatform\Validator\ValidatorInterface;
use App\Dto\Input\UpdateGitImageInput;
use App\Entity\Client;
use App\Model\CommandTypes;
use App\Model\DeployMethodTypes;
use App\Model\TraceStatus;
use App\Service\Trace\CreateService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
class UpdateGitImageAction extends AbstractController
{
public function __construct(
protected readonly EntityManagerInterface $entityManager,
protected readonly CreateService $createService,
protected readonly ValidatorInterface $validator,
public readonly \App\Controller\OgAgent\UpdateGitImageAction $updateGitImageOgAgentAction,
) {
}
/**
* @throws RedirectionExceptionInterface
* @throws ClientExceptionInterface
* @throws ServerExceptionInterface
*/
public function __invoke(UpdateGitImageInput $input): JsonResponse
{
$this->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,
];
}
}

View File

@ -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 = '';

View File

@ -0,0 +1,29 @@
<?php
declare(strict_types=1);
namespace App\Dto\Input;
use ApiPlatform\Metadata\ApiProperty;
use App\Dto\Output\ClientOutput;
use App\Dto\Output\PartitionOutput;
use Symfony\Component\Serializer\Annotation\Groups;
class UpdateGitImageInput
{
#[Groups(['git-repository:write'])]
#[ApiProperty(description: 'The client of the image')]
public ?ClientOutput $client = null;
#[Groups(['git-repository:write'])]
#[ApiProperty(description: 'The client of the image')]
public ?PartitionOutput $partition = null;
#[Groups(['git-repository:write'])]
#[ApiProperty(description: 'The name of the Git repository to use for this image', example: "mi-repositorio")]
public ?string $gitRepository = null;
#[Groups(['git-repository:write'])]
#[ApiProperty(description: 'Whether to queue the update', example: false)]
public bool $queue = false;
}

View File

@ -8,6 +8,7 @@ final class CommandTypes
public const string RESTORE_IMAGE = 'restore-image';
public const string CREATE_IMAGE = 'create-image';
public const string CREATE_IMAGE_GIT = 'create-image-git';
public const string UPDATE_IMAGE_GIT = 'update-image-git';
public const string CONVERT_IMAGE = 'convert-image';
public const string CREATE_IMAGE_AUX_FILE = 'create-image-aux-file';
public const string BACKUP_IMAGE = 'backup-image';
@ -34,6 +35,7 @@ final class CommandTypes
self::RESTORE_IMAGE => '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',

View File

@ -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);
}