103 lines
3.9 KiB
PHP
103 lines
3.9 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 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
|
|
) {}
|
|
|
|
/**
|
|
* @throws TransportExceptionInterface
|
|
* @throws ServerExceptionInterface
|
|
* @throws RedirectionExceptionInterface
|
|
* @throws ClientExceptionInterface
|
|
*/
|
|
public function __invoke(Client $client): JsonResponse
|
|
{
|
|
if (!$client->getIp()) {
|
|
throw new ValidatorException('IP is required');
|
|
}
|
|
|
|
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 new JsonResponse(
|
|
data: ['error' => $e->getMessage()],
|
|
status: Response::HTTP_INTERNAL_SERVER_ERROR
|
|
);
|
|
}
|
|
|
|
$data = json_decode($response->getContent(), true);
|
|
|
|
if (isset($data['cfg'])) {
|
|
foreach ($data['cfg'] as $cfg) {
|
|
$partitionEntity = $this->entityManager->getRepository(Partition::class)
|
|
->findOneBy(['client' => $client, 'diskNumber' => $cfg['disk'], 'partitionNumber' => $cfg['par']]);
|
|
|
|
if (!$partitionEntity) {
|
|
$partitionEntity = new Partition();
|
|
}
|
|
|
|
if (isset($cfg['soi']) && $cfg['soi'] !== '') {
|
|
$operativeSystem = $this->entityManager->getRepository(OperativeSystem::class)
|
|
->findOneBy(['name' => $cfg['soi']]);
|
|
|
|
if (!$operativeSystem) {
|
|
$operativeSystem = new OperativeSystem();
|
|
$operativeSystem->setName($cfg['soi']);
|
|
$this->entityManager->persist($operativeSystem);
|
|
}
|
|
$partitionEntity->setOperativeSystem($operativeSystem);
|
|
}
|
|
|
|
$partitionEntity->setClient($client);
|
|
$partitionEntity->setDiskNumber($cfg['disk']);
|
|
$partitionEntity->setPartitionNumber($cfg['par']);
|
|
$partitionEntity->setSize($cfg['tam']);
|
|
$partitionEntity->setMemoryUsage(((int) $cfg['uso']) * 100);
|
|
$this->entityManager->persist($partitionEntity);
|
|
}
|
|
}
|
|
|
|
$this->entityManager->persist($client);
|
|
$this->entityManager->flush();
|
|
|
|
return new JsonResponse(status: Response::HTTP_OK);
|
|
}
|
|
} |