165 lines
7.1 KiB
PHP
165 lines
7.1 KiB
PHP
<?php
|
|
|
|
namespace App\Controller\OgAgent\Webhook;
|
|
|
|
use App\Controller\OgRepository\Image\CreateAuxFilesAction;
|
|
use App\Entity\Image;
|
|
use App\Entity\OperativeSystem;
|
|
use App\Entity\Partition;
|
|
use App\Entity\Software;
|
|
use App\Entity\SoftwareProfile;
|
|
use App\Entity\Trace;
|
|
use App\Model\ClientStatus;
|
|
use App\Model\CommandTypes;
|
|
use App\Model\ImageStatus;
|
|
use App\Model\SoftwareTypes;
|
|
use App\Model\TraceStatus;
|
|
use App\Service\CreatePartitionService;
|
|
use App\Service\Trace\CreateService;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Psr\Log\LoggerInterface;
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
|
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;
|
|
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
|
|
|
#[AsController]
|
|
class ClientsController extends AbstractController
|
|
{
|
|
public function __construct(
|
|
protected readonly EntityManagerInterface $entityManager,
|
|
public readonly CreateAuxFilesAction $createAuxFilesAction,
|
|
protected readonly CreatePartitionService $createPartitionService,
|
|
protected readonly LoggerInterface $logger,
|
|
protected readonly CreateService $createService,
|
|
)
|
|
{
|
|
}
|
|
|
|
/**
|
|
* @throws TransportExceptionInterface
|
|
* @throws ServerExceptionInterface
|
|
* @throws RedirectionExceptionInterface
|
|
* @throws ClientExceptionInterface
|
|
*/
|
|
#[Route('/opengnsys/rest/clients/status/webhook', methods: ['POST'])]
|
|
public function index(Request $request): JsonResponse
|
|
{
|
|
$data = $request->toArray();
|
|
$requiredFields = ['nfn', 'idi', 'dsk', 'par', 'ids', 'res', 'der', 'job_id'];
|
|
|
|
foreach ($requiredFields as $field) {
|
|
if (!isset($data[$field])) {
|
|
return new JsonResponse(['message' => "Missing parameter: $field"], Response::HTTP_BAD_REQUEST);
|
|
}
|
|
}
|
|
|
|
$this->logger->info('Webhook data received', $data);
|
|
|
|
if ($data['nfn'] === 'RESPUESTA_CrearImagen') {
|
|
$trace = $this->entityManager->getRepository(Trace::class)->findOneBy(['jobId' => $data['job_id']]);
|
|
$image = $this->entityManager->getRepository(Image::class)->findOneBy(['uuid' => $data['idi']]);
|
|
|
|
if (!$image) {
|
|
$this->logger->error('Image not found', $data);
|
|
return new JsonResponse(['message' => 'Image not found'], Response::HTTP_NOT_FOUND);
|
|
}
|
|
|
|
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());
|
|
$image->setStatus(ImageStatus::AUX_FILES_PENDING);
|
|
$image->setCreated(true);
|
|
$this->logger->info('Start partition creation. ', ['image' => (string) $image->getUuid()]);
|
|
if (isset($data['cfg'])) {
|
|
$this->createPartitionService->__invoke($data, $image->getClient());
|
|
}
|
|
$this->logger->info('Starting software profile creation. ', ['image' => (string) $image->getUuid()]);
|
|
$this->createSoftwareProfile($data['inv_sft'], $image);
|
|
$this->logger->info('Start aux files ogrepo API ', ['image' => (string) $image->getUuid()]);
|
|
try {
|
|
$this->createAuxFilesAction->__invoke($image);
|
|
} catch (\Exception $e) {
|
|
$this->logger->error('Error creating aux files', ['image' => (string) $image->getUuid(), 'error' => $e->getMessage()]);
|
|
}
|
|
$this->logger->info('End aux files ogrepo API ', ['image' => (string) $image->getUuid()]);
|
|
} else {
|
|
$trace->setStatus(TraceStatus::FAILED);
|
|
$trace->setFinishedAt(new \DateTime());
|
|
$trace->setOutput($data['der']);
|
|
$image->setCreated(false);
|
|
$image->setStatus(ImageStatus::FAILED);
|
|
$this->logger->error('Image updated failed', $data);
|
|
}
|
|
|
|
$this->entityManager->persist($image);
|
|
$client = $trace->getClient();
|
|
$client->setStatus(ClientStatus::OG_LIVE);
|
|
$this->entityManager->persist($trace);
|
|
$this->entityManager->flush();
|
|
$this->logger->info('Image updated. Success.', ['image' => (string) $image->getUuid()]);
|
|
}
|
|
|
|
if ($data['nfn'] === 'RESPUESTA_RestaurarImagen') {
|
|
$trace = $this->entityManager->getRepository(Trace::class)->findOneBy(['jobId' => $data['job_id']]);
|
|
$image = $this->entityManager->getRepository(Image::class)->findOneBy(['uuid' => $data['idi']]);
|
|
|
|
if ($data['res'] === 1) {
|
|
$trace->setStatus(TraceStatus::SUCCESS);
|
|
$trace->setFinishedAt(new \DateTime());
|
|
$image->setStatus(ImageStatus::PENDING);
|
|
} else {
|
|
$trace->setStatus(TraceStatus::FAILED);
|
|
$trace->setFinishedAt(new \DateTime());
|
|
$trace->setOutput($data['der']);
|
|
}
|
|
|
|
$this->entityManager->persist($image);
|
|
$this->entityManager->persist($trace);
|
|
$this->entityManager->flush();
|
|
}
|
|
|
|
return new JsonResponse(data: 'Webhook finished', status: Response::HTTP_OK);
|
|
}
|
|
|
|
public function createSoftwareProfile(string $base64Data, Image $image): void
|
|
{
|
|
$decodedData = base64_decode($base64Data);
|
|
$this->logger->info('Software profile decoded', ['data' => '']);
|
|
$softwareList = array_map('trim', explode("\n", $decodedData));
|
|
$softwareList = array_filter($softwareList);
|
|
$existingSoftware = $this->entityManager->getRepository(Software::class)->findBy(['name' => $softwareList]);
|
|
$existingNames = array_map(fn($software) => $software->getName(), $existingSoftware);
|
|
$newSoftwareNames = array_diff($softwareList, $existingNames);
|
|
|
|
foreach ($newSoftwareNames as $software) {
|
|
$softwareEntity = new Software();
|
|
$softwareEntity->setName($software);
|
|
$softwareEntity->setType(SoftwareTypes::APPLICATION);
|
|
$this->entityManager->persist($softwareEntity);
|
|
}
|
|
|
|
$softwareProfile = new SoftwareProfile();
|
|
$softwareProfile->setDescription('Perfil: ' . $image->getName());
|
|
$softwareProfile->setOrganizationalUnit($image->getClient()->getOrganizationalUnit());
|
|
|
|
foreach ($existingSoftware as $softwareEntity) {
|
|
$softwareEntity->addSoftwareProfile($softwareProfile);
|
|
}
|
|
|
|
$this->entityManager->persist($softwareProfile);
|
|
$this->entityManager->flush();
|
|
}
|
|
} |