122 lines
4.7 KiB
PHP
122 lines
4.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Controller\OgAgent;
|
|
|
|
use App\Dto\Input\DeployImageInput;
|
|
use App\Entity\Client;
|
|
use App\Entity\Command;
|
|
use App\Entity\Image;
|
|
use App\Entity\ImageImageRepository;
|
|
use App\Entity\Partition;
|
|
use App\Entity\Trace;
|
|
use App\Model\ClientStatus;
|
|
use App\Model\DeployMethodTypes;
|
|
use App\Model\TraceStatus;
|
|
use App\Service\Trace\CreateService;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Psr\Log\LoggerInterface;
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
use Symfony\Component\HttpClient\Exception\TransportException;
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Symfony\Component\Routing\Attribute\Route;
|
|
use Symfony\Component\Validator\Exception\ValidatorException;
|
|
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;
|
|
|
|
class DeployImageAction extends AbstractController
|
|
{
|
|
public function __construct(
|
|
protected readonly EntityManagerInterface $entityManager,
|
|
protected readonly HttpClientInterface $httpClient,
|
|
protected readonly CreateService $createService,
|
|
protected readonly LoggerInterface $logger,
|
|
)
|
|
{
|
|
}
|
|
|
|
/**
|
|
* @throws RedirectionExceptionInterface
|
|
* @throws ClientExceptionInterface
|
|
* @throws TransportExceptionInterface
|
|
* @throws ServerExceptionInterface
|
|
*/
|
|
public function __invoke(ImageImageRepository $imageImageRepository, DeployImageInput $input, Client $client, string $method)
|
|
{
|
|
$image = $imageImageRepository->getImage();
|
|
|
|
if (!$client->getIp()) {
|
|
throw new ValidatorException('IP is required');
|
|
}
|
|
|
|
$method = match ($input->method) {
|
|
DeployMethodTypes::MULTICAST_UFTP_DIRECT, DeployMethodTypes::MULTICAST_UDPCAST_DIRECT, => 'multicast-direct',
|
|
DeployMethodTypes::MULTICAST, DeployMethodTypes::MULTICAST_UFTP, DeployMethodTypes::MULTICAST_UDPCAST => 'multicast',
|
|
DeployMethodTypes::UNICAST_DIRECT => 'unicast-direct',
|
|
DeployMethodTypes::UNICAST => 'unicast',
|
|
DeployMethodTypes::TORRENT => 'torrent',
|
|
default => throw new ValidatorException('Invalid method'),
|
|
};
|
|
|
|
$mcastMode = $input->mcastMode.'-duplex';
|
|
$mcastSpeed = $input->mcastSpeed.'M';
|
|
|
|
$ptcMulticastValue = "$method $input->mcastPort:$mcastMode:$input->mcastIp:$mcastSpeed:$input->maxClients:$input->maxTime";
|
|
$ptcTorrentValue = "$method $input->p2pMode:$input->p2pTime";
|
|
$ptcUnicastValue = $method;
|
|
|
|
$ptcValue = match ($input->method) {
|
|
DeployMethodTypes::MULTICAST, DeployMethodTypes::MULTICAST_UFTP, DeployMethodTypes::MULTICAST_UFTP_DIRECT, DeployMethodTypes::MULTICAST_UDPCAST, DeployMethodTypes::MULTICAST_UDPCAST_DIRECT => $ptcMulticastValue,
|
|
DeployMethodTypes::UNICAST, DeployMethodTypes::UNICAST_DIRECT => $ptcUnicastValue,
|
|
DeployMethodTypes::TORRENT => $ptcTorrentValue,
|
|
default => throw new ValidatorException('Invalid method'),
|
|
};
|
|
|
|
$repository = $imageImageRepository->getRepository();
|
|
|
|
$data = [
|
|
'dsk' => (string) $input->diskNumber,
|
|
'par' => (string) $input->partitionNumber,
|
|
'ifs' => "1",
|
|
'idi' => $image->getUuid(),
|
|
'nci' => $image->getName(),
|
|
'ipr' => $repository->getIp(),
|
|
'nfn' => 'RestaurarImagen',
|
|
'ptc' => $ptcValue,
|
|
'ids' => '0'
|
|
];
|
|
|
|
try {
|
|
$response = $this->httpClient->request('POST', 'https://'.$client->getIp().':8000/CloningEngine/RestaurarImagen', [
|
|
'verify_peer' => false,
|
|
'verify_host' => false,
|
|
'headers' => [
|
|
'Content-Type' => 'application/json',
|
|
],
|
|
'json' => $data,
|
|
]);
|
|
$this->logger->info('Deploying image', ['image' => $image->getId()]);
|
|
|
|
$jobId = json_decode($response->getContent(), true)['job_id'];
|
|
} catch (ClientExceptionInterface | ServerExceptionInterface | TransportExceptionInterface | TransportException $e) {
|
|
$this->logger->error('Error deploying image', [
|
|
'image' => $image->getId() ?? 'unknown',
|
|
'error' => $e->getMessage()
|
|
]);
|
|
return null;
|
|
}
|
|
|
|
|
|
$client->setStatus(ClientStatus::BUSY);
|
|
$this->entityManager->persist($client);
|
|
$this->entityManager->flush();
|
|
|
|
return $jobId;
|
|
}
|
|
}
|