refs #1603. Backup image changes
testing/ogcore-api/pipeline/head There was a failure building this commit
Details
testing/ogcore-api/pipeline/head There was a failure building this commit
Details
parent
c3ebc64264
commit
d881c763cf
|
@ -63,6 +63,11 @@ class BackupImageAction extends AbstractOgRepositoryController
|
|||
|
||||
$this->createService->__invoke($image->getClient(), CommandTypes::BACKUP_IMAGE, TraceStatus::IN_PROGRESS, $content['job_id'], $inputData);
|
||||
|
||||
$imageImageRepository->setStatus(ImageStatus::BACKUP);
|
||||
$this->entityManager->persist($imageImageRepository);
|
||||
$this->entityManager->flush();
|
||||
|
||||
|
||||
return new JsonResponse(data: $content, status: Response::HTTP_OK);
|
||||
}
|
||||
}
|
|
@ -7,7 +7,6 @@ use App\Entity\Image;
|
|||
use App\Entity\ImageImageRepository;
|
||||
use App\Entity\ImageRepository;
|
||||
use App\Entity\Trace;
|
||||
use App\Model\CommandTypes;
|
||||
use App\Model\ImageStatus;
|
||||
use App\Model\TraceStatus;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
|
@ -18,123 +17,81 @@ use Symfony\Component\HttpFoundation\Request;
|
|||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpKernel\Attribute\AsController;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
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 ResponseController extends AbstractOgRepositoryController
|
||||
{
|
||||
/**
|
||||
* @throws RedirectionExceptionInterface
|
||||
* @throws ClientExceptionInterface
|
||||
* @throws TransportExceptionInterface
|
||||
* @throws ServerExceptionInterface
|
||||
*/
|
||||
#[Route('/og-repository/webhook', name: 'og_repository_webhook', methods: ['POST'])]
|
||||
public function repositoryWebhook(Request $request): JsonResponse
|
||||
{
|
||||
$data = json_decode($request->getContent(), true);
|
||||
|
||||
if (!isset($data['job_id'])) {
|
||||
return new JsonResponse(['message' => 'Invalid request'], Response::HTTP_BAD_REQUEST);
|
||||
return $this->jsonResponseError('Invalid request', Response::HTTP_BAD_REQUEST);
|
||||
}
|
||||
|
||||
$action = $data['job_id'];
|
||||
|
||||
if (str_starts_with($action, "CreateAuxiliarFiles_")) {
|
||||
$this->handleCreateAuxFiles($data);
|
||||
} elseif (str_starts_with($action, "TransferImage_")) {
|
||||
$this->processImageAction($data, 'transfer');
|
||||
} elseif (str_starts_with($action, "ExportImage_")) {
|
||||
$this->processImageAction($data, 'export');
|
||||
} elseif (str_starts_with($action, "BackupImage_")) {
|
||||
$this->processImageAction($data, 'backup');
|
||||
} else {
|
||||
return new JsonResponse(['message' => 'Invalid action'], Response::HTTP_BAD_REQUEST);
|
||||
}
|
||||
|
||||
return new JsonResponse($data, Response::HTTP_OK);
|
||||
return match (true) {
|
||||
str_starts_with($action, "CreateAuxiliarFiles_") => $this->handleImageRepositoryAction($data, true),
|
||||
str_starts_with($action, "TransferImage_"), str_starts_with($action, "ExportImage_") => $this->processImageAction($data),
|
||||
str_starts_with($action, "BackupImage_") => $this->handleImageRepositoryAction($data),
|
||||
default => $this->jsonResponseError('Invalid action', Response::HTTP_BAD_REQUEST),
|
||||
};
|
||||
}
|
||||
|
||||
private function handleCreateAuxFiles(array $data): void
|
||||
private function handleImageRepositoryAction(array $data, bool $setFullsum = false): JsonResponse
|
||||
{
|
||||
$trace = $this->entityManager->getRepository(Trace::class)->findOneBy(['jobId' => $data['job_id']]);
|
||||
$imageUuid = $trace->getInput()['imageUuid'];
|
||||
$trace = $this->getTrace($data['job_id']);
|
||||
if (!$trace) return $this->jsonResponseError('Trace not found');
|
||||
|
||||
/* @var ImageImageRepository $imageImageRepository */
|
||||
$imageImageRepository = $this->entityManager->getRepository(ImageImageRepository::class)->findOneBy(['uuid' => $imageUuid]);
|
||||
$imageImageRepository = $this->getImageImageRepository($trace);
|
||||
if (!$imageImageRepository) return $this->jsonResponseError('Image not found', Response::HTTP_NOT_FOUND, $trace);
|
||||
|
||||
if ($imageImageRepository === null) {
|
||||
$this->updateTraceStatus($trace, TraceStatus::FAILED, 'Image not found');
|
||||
return;
|
||||
if ($setFullsum) {
|
||||
$imageImageRepository->setImageFullsum($data['image_id']);
|
||||
}
|
||||
|
||||
$imageImageRepository->setImageFullsum($data['image_id']);
|
||||
$imageImageRepository->setStatus(ImageStatus::SUCCESS);
|
||||
$this->entityManager->persist($imageImageRepository);
|
||||
|
||||
$this->entityManager->persist($imageImageRepository);
|
||||
$this->updateTraceStatus($trace, TraceStatus::SUCCESS);
|
||||
|
||||
return new JsonResponse(['message' => 'Success'], Response::HTTP_OK);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws TransportExceptionInterface
|
||||
* @throws ServerExceptionInterface
|
||||
* @throws RedirectionExceptionInterface
|
||||
* @throws ClientExceptionInterface
|
||||
*/
|
||||
private function processImageAction(array $data, string $actionType): void
|
||||
private function processImageAction(array $data): JsonResponse
|
||||
{
|
||||
$imageImageRepository = null;
|
||||
|
||||
$trace = $this->entityManager->getRepository(Trace::class)->findOneBy(['jobId' => $data['job_id']]);
|
||||
$imageUuid = $trace->getInput()['imageUuid'];
|
||||
$repositoryUuid = $trace->getInput()['repositoryUuid'];
|
||||
|
||||
$image = $this->entityManager->getRepository(Image::class)->findOneBy(['uuid' => $imageUuid]);
|
||||
$repository = $this->entityManager->getRepository(ImageRepository::class)->findOneBy(['uuid' => $repositoryUuid]);
|
||||
$trace = $this->getTrace($data['job_id']);
|
||||
if (!$trace) return $this->jsonResponseError('Trace not found');
|
||||
|
||||
if ($data['success'] !== true) {
|
||||
$this->updateTraceStatus($trace, TraceStatus::FAILED, 'Action failed');
|
||||
return;
|
||||
return $this->jsonResponseError('Action failed', Response::HTTP_BAD_REQUEST, $trace);
|
||||
}
|
||||
|
||||
if ($image === null) {
|
||||
$this->updateTraceStatus($trace, TraceStatus::FAILED, 'Image not found');
|
||||
return;
|
||||
}
|
||||
$image = $this->getImage($trace);
|
||||
$repository = $this->getRepository($trace);
|
||||
if (!$image) return $this->jsonResponseError('Image not found', Response::HTTP_NOT_FOUND, $trace);
|
||||
if (!$repository) return $this->jsonResponseError('Repository not found', Response::HTTP_NOT_FOUND, $trace);
|
||||
|
||||
if ($repository === null) {
|
||||
$this->updateTraceStatus($trace, TraceStatus::FAILED, 'Repository not found');
|
||||
return;
|
||||
}
|
||||
$newImageRepo = new ImageImageRepository();
|
||||
$newImageRepo->setImage($image);
|
||||
$newImageRepo->setRepository($repository);
|
||||
$newImageRepo->setStatus(ImageStatus::SUCCESS);
|
||||
|
||||
if (isset($trace->getInput()['imageImageRepositoryUuid'])) {
|
||||
$imageImageRepositoryUuid = $trace->getInput()['imageImageRepositoryUuid'];
|
||||
$imageImageRepository = $this->entityManager->getRepository(ImageImageRepository::class)->findOneBy(['uuid' => $imageImageRepositoryUuid]);
|
||||
if ($trace->getInput()['imageImageRepositoryUuid'] ?? false) {
|
||||
$existingRepo = $this->entityManager->getRepository(ImageImageRepository::class)
|
||||
->findOneBy(['uuid' => $trace->getInput()['imageImageRepositoryUuid']]);
|
||||
|
||||
if ($imageImageRepository) {
|
||||
$this->updateTraceStatus($trace, TraceStatus::FAILED, 'Image repository not found');
|
||||
$imageImageRepository->setStatus(ImageStatus::SUCCESS);
|
||||
if ($existingRepo) {
|
||||
$newImageRepo->setImageFullsum($existingRepo->getImageFullsum());
|
||||
}
|
||||
}
|
||||
|
||||
$this->logger->info("Image $actionType successful", ['image' => $image->getName()]);
|
||||
|
||||
$newImageImageRepository = new ImageImageRepository();
|
||||
$newImageImageRepository->setImage($image);
|
||||
$newImageImageRepository->setRepository($repository);
|
||||
$newImageImageRepository->setStatus(ImageStatus::SUCCESS);
|
||||
|
||||
if ($imageImageRepository){
|
||||
$newImageImageRepository->setImageFullsum($imageImageRepository->getImageFullsum());
|
||||
}
|
||||
|
||||
$this->entityManager->persist($newImageImageRepository);
|
||||
$this->entityManager->persist($image);
|
||||
|
||||
$this->entityManager->persist($newImageRepo);
|
||||
$this->updateTraceStatus($trace, TraceStatus::SUCCESS);
|
||||
|
||||
return new JsonResponse(['message' => 'Success'], Response::HTTP_OK);
|
||||
}
|
||||
|
||||
private function updateTraceStatus(Trace $trace, string $status, string $output = null): void
|
||||
|
@ -148,9 +105,36 @@ class ResponseController extends AbstractOgRepositoryController
|
|||
|
||||
$this->entityManager->persist($trace);
|
||||
$this->entityManager->flush();
|
||||
}
|
||||
|
||||
if ($status === TraceStatus::FAILED) {
|
||||
new JsonResponse(['message' => $output], Response::HTTP_NOT_FOUND);
|
||||
private function getTrace(string $jobId): ?Trace
|
||||
{
|
||||
return $this->entityManager->getRepository(Trace::class)->findOneBy(['jobId' => $jobId]);
|
||||
}
|
||||
|
||||
private function getImage(Trace $trace): ?Image
|
||||
{
|
||||
return $this->entityManager->getRepository(Image::class)
|
||||
->findOneBy(['uuid' => $trace->getInput()['imageUuid']]);
|
||||
}
|
||||
|
||||
private function getRepository(Trace $trace): ?ImageRepository
|
||||
{
|
||||
return $this->entityManager->getRepository(ImageRepository::class)
|
||||
->findOneBy(['uuid' => $trace->getInput()['repositoryUuid']]);
|
||||
}
|
||||
|
||||
private function getImageImageRepository(Trace $trace): ?ImageImageRepository
|
||||
{
|
||||
return $this->entityManager->getRepository(ImageImageRepository::class)
|
||||
->findOneBy(['uuid' => $trace->getInput()['imageUuid']]);
|
||||
}
|
||||
|
||||
private function jsonResponseError(string $message, int $status = Response::HTTP_BAD_REQUEST, ?Trace $trace = null): JsonResponse
|
||||
{
|
||||
if ($trace) {
|
||||
$this->updateTraceStatus($trace, TraceStatus::FAILED, $message);
|
||||
}
|
||||
return new JsonResponse(['message' => $message], $status);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,6 +11,7 @@ final class ImageStatus
|
|||
public const string TRASH = 'trash';
|
||||
public const string FAILED = 'failed';
|
||||
public const string TRANSFERRING = 'transferring';
|
||||
public const string BACKUP = 'backup';
|
||||
|
||||
private const array STATUS = [
|
||||
self::PENDING => 'Pendiente',
|
||||
|
|
Loading…
Reference in New Issue