125 lines
4.3 KiB
PHP
125 lines
4.3 KiB
PHP
<?php
|
|
|
|
namespace App\Controller\OgAgent;
|
|
|
|
use App\Entity\Client;
|
|
use App\Entity\OperativeSystem;
|
|
use App\Entity\Partition;
|
|
use App\Model\ClientStatus;
|
|
use App\Model\OgLiveStatus;
|
|
use App\Service\CreatePartitionService;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
use Symfony\Component\HttpClient\HttpClient;
|
|
use Symfony\Component\HttpClient\Internal\ClientState;
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Symfony\Component\HttpKernel\Attribute\AsController;
|
|
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;
|
|
|
|
#[AsController]
|
|
class StatusAction extends AbstractController
|
|
{
|
|
public function __construct(
|
|
protected readonly EntityManagerInterface $entityManager,
|
|
protected readonly HttpClientInterface $httpClient,
|
|
protected readonly CreatePartitionService $createPartitionService
|
|
) {}
|
|
|
|
/**
|
|
* @throws TransportExceptionInterface
|
|
* @throws ServerExceptionInterface
|
|
* @throws RedirectionExceptionInterface
|
|
* @throws ClientExceptionInterface
|
|
*/
|
|
public function __invoke(Client $client): JsonResponse
|
|
{
|
|
if (!$client->getIp()) {
|
|
throw new ValidatorException('IP is required');
|
|
}
|
|
|
|
if ($client->getStatus() === ClientStatus::OG_LIVE || $client->getStatus() === ClientStatus::OFF) {
|
|
$response = $this->getOgLiveStatus($client);
|
|
}
|
|
|
|
if ($client->getStatus() === ClientStatus::LINUX) {
|
|
$response = $this->getSOStatus($client);
|
|
}
|
|
|
|
return new JsonResponse(
|
|
data: ['status' => $response],
|
|
status: $response === Response::HTTP_OK ? Response::HTTP_OK : Response::HTTP_INTERNAL_SERVER_ERROR
|
|
);
|
|
}
|
|
|
|
public function getOgLiveStatus (Client $client): JsonResponse|int
|
|
{
|
|
try {
|
|
$response = $this->httpClient->request('POST', 'https://' . $client->getIp() . ':8000/ogAdmClient/status', [
|
|
'verify_peer' => false,
|
|
'verify_host' => false,
|
|
'timeout' => 10,
|
|
'headers' => [
|
|
'Content-Type' => 'application/json',
|
|
],
|
|
'json' => [],
|
|
]);
|
|
$statusCode = $response->getStatusCode();
|
|
$client->setStatus($statusCode === Response::HTTP_OK ? ClientStatus::OG_LIVE : ClientStatus::OFF);
|
|
|
|
} catch (TransportExceptionInterface $e) {
|
|
$client->setStatus(ClientStatus::OFF);
|
|
|
|
return Response::HTTP_INTERNAL_SERVER_ERROR;
|
|
}
|
|
|
|
$data = json_decode($response->getContent(), true);
|
|
|
|
if (isset($data['cfg'])) {
|
|
$this->createPartitionService->__invoke($data, $client);
|
|
}
|
|
|
|
$this->entityManager->persist($client);
|
|
$this->entityManager->flush();
|
|
|
|
return Response::HTTP_OK;
|
|
}
|
|
|
|
public function getSOStatus (Client $client): JsonResponse|int
|
|
{
|
|
try {
|
|
$response = $this->httpClient->request('POST', 'https://' . $client->getIp() . ':8000/opengnsys/status', [
|
|
'verify_peer' => false,
|
|
'verify_host' => false,
|
|
'timeout' => 10,
|
|
'headers' => [
|
|
'Content-Type' => 'application/json',
|
|
],
|
|
'json' => [],
|
|
]);
|
|
$statusCode = $response->getStatusCode();
|
|
$client->setStatus($statusCode === Response::HTTP_OK ? ClientStatus::LINUX : ClientStatus::OFF);
|
|
|
|
} catch (TransportExceptionInterface $e) {
|
|
$client->setStatus(ClientStatus::OFF);
|
|
|
|
return Response::HTTP_INTERNAL_SERVER_ERROR;
|
|
}
|
|
|
|
$data = json_decode($response->getContent(), true);
|
|
|
|
if (isset($data['cfg'])) {
|
|
$this->createPartitionService->__invoke($data, $client);
|
|
}
|
|
|
|
$this->entityManager->persist($client);
|
|
$this->entityManager->flush();
|
|
|
|
return Response::HTTP_OK;
|
|
}
|
|
} |