93 lines
3.3 KiB
PHP
93 lines
3.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Controller\OgAgent;
|
|
|
|
use App\Dto\Input\MultipleClientsInput;
|
|
use App\Entity\Client;
|
|
use App\Entity\Command;
|
|
use App\Entity\Image;
|
|
use App\Entity\Trace;
|
|
use App\Model\ClientStatus;
|
|
use App\Model\CommandTypes;
|
|
use App\Model\ImageStatus;
|
|
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 PowerOffAction extends AbstractController
|
|
{
|
|
public function __construct(
|
|
protected readonly EntityManagerInterface $entityManager,
|
|
protected readonly HttpClientInterface $httpClient,
|
|
protected readonly CreateService $createService,
|
|
protected readonly LoggerInterface $logger,
|
|
)
|
|
{
|
|
}
|
|
|
|
public function __invoke(MultipleClientsInput $input): JsonResponse
|
|
{
|
|
foreach ($input->clients as $clientEntity) {
|
|
/** @var Client $client */
|
|
$client = $clientEntity->getEntity();
|
|
|
|
if (!$client->getIp()) {
|
|
throw new ValidatorException('IP is required');
|
|
}
|
|
|
|
if ($client->getStatus() === ClientStatus::OFF) {
|
|
continue;
|
|
}
|
|
|
|
$endpoint = $client->getStatus() === ClientStatus::OG_LIVE ? 'opengnsys/Apagar' : 'opengnsys/poweroff';
|
|
|
|
$data = [
|
|
'nfn' => 'Apagar',
|
|
'ids' => '0'
|
|
];
|
|
|
|
try {
|
|
$response = $this->httpClient->request('POST', 'https://' . $client->getIp() . ':8000/'.$endpoint, [
|
|
'verify_peer' => false,
|
|
'verify_host' => false,
|
|
'headers' => [
|
|
'Content-Type' => 'application/json',
|
|
],
|
|
'json' => $data,
|
|
]);
|
|
$this->logger->info('Powering off client', ['client' => $client->getId()]);
|
|
$jobId = json_decode($response->getContent(), true)['job_id'];
|
|
|
|
} catch (ClientExceptionInterface | ServerExceptionInterface | TransportExceptionInterface | TransportException $e) {
|
|
$this->logger->error('Error power off client', [
|
|
'image' => $client->getIp(),
|
|
'error' => $e->getMessage()
|
|
]);
|
|
continue;
|
|
}
|
|
|
|
$client->setStatus(ClientStatus::TURNING_OFF);
|
|
$this->entityManager->persist($client);
|
|
$this->entityManager->flush();
|
|
|
|
$this->createService->__invoke($client, CommandTypes::SHUTDOWN, TraceStatus::SUCCESS, $jobId, []);
|
|
}
|
|
|
|
return new JsonResponse(data: [], status: Response::HTTP_OK);
|
|
}
|
|
}
|