ogcore/src/Controller/OgAgent/OrganizationalUnitStatusAct...

120 lines
4.1 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Controller\OgAgent;
use App\Controller\OgRepository\Git\CreateRepositoryAction;
use App\Controller\OgRepository\Git\SshKeyAction;
use App\Entity\OrganizationalUnit;
use App\Model\ClientStatus;
use App\Repository\ClientRepository;
use App\Service\CreatePartitionService;
use App\Service\Trace\CreateService;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\AsController;
use Symfony\Contracts\HttpClient\HttpClientInterface;
#[AsController]
class OrganizationalUnitStatusAction extends AbstractOgAgentController
{
public function __construct(
EntityManagerInterface $entityManager,
HttpClientInterface $httpClient,
CreatePartitionService $createPartitionService,
LoggerInterface $logger,
CreateService $createService,
CreateRepositoryAction $createRepositoryAction,
SshKeyAction $sshKeyAction,
#[Autowire(env: 'SSL_ENABLED')] string $sslEnabled,
private readonly ClientRepository $clientRepository,
) {
parent::__construct($entityManager, $httpClient, $createPartitionService, $logger, $createService, $createRepositoryAction, $sshKeyAction, $sslEnabled);
}
public function __invoke(OrganizationalUnit $organizationalUnit): JsonResponse
{
$clients = $this->clientRepository->findClientsByOrganizationalUnitAndDescendants(
$organizationalUnit->getId()
);
if (empty($clients)) {
return new JsonResponse(['results' => []], Response::HTTP_OK);
}
$options = $this->buildRequestOptions(['json' => ['full-config' => false]]);
$pending = [];
foreach ($clients as $client) {
if (!$client->getIp()) {
continue;
}
$clientOptions = array_merge($options, [
'headers' => array_merge($options['headers'] ?? [], [
'Authorization' => $client->getToken(),
]),
]);
$pending[] = [
'client' => $client,
'response' => $this->httpClient->request(
'POST',
'https://' . $client->getIp() . ':8000/opengnsys/status',
$clientOptions
),
];
}
$results = [];
foreach ($pending as $item) {
$client = $item['client'];
try {
$data = $item['response']->toArray(throw: true);
if (isset($data['cfg'])) {
$this->createPartitionService->__invoke($data, $client);
}
if ($client->getStatus() !== ClientStatus::LINUX
&& $client->getStatus() !== ClientStatus::MACOS
&& $client->getStatus() !== ClientStatus::WINDOWS) {
$client->setStatus(ClientStatus::OG_LIVE);
}
$this->entityManager->persist($client);
$results[] = [
'uuid' => $client->getUuid(),
'ip' => $client->getIp(),
'status' => $client->getStatus(),
'success' => true,
];
} catch (\Throwable $e) {
$this->logger->error('Error checking client status', [
'client' => $client->getUuid(),
'error' => $e->getMessage(),
]);
$client->setStatus(ClientStatus::OFF);
$this->entityManager->persist($client);
$results[] = [
'uuid' => $client->getUuid(),
'ip' => $client->getIp(),
'status' => ClientStatus::OFF,
'success' => false,
];
}
}
$this->entityManager->flush();
return new JsonResponse(['results' => $results], Response::HTTP_OK);
}
}