refs #2973. Refactor classroom status
testing/ogcore-api/pipeline/head There was a failure building this commit Details
testing/ogcore-api/pipeline/pr-main Build queued... Details

pull/90/head
Manuel Aranda Rosales 2026-04-20 18:39:31 +02:00
parent 0e23464524
commit dca1377a08
3 changed files with 153 additions and 20 deletions

View File

@ -40,6 +40,14 @@ resources:
uriTemplate: /organizational-units/{uuid}/propagate-network-settings
controller: App\Controller\PropagateNetworkSettingsAction
agent_status:
class: ApiPlatform\Metadata\Post
method: POST
input: false
output: false
uriTemplate: /organizational-units/{uuid}/agent/status
controller: App\Controller\OgAgent\OrganizationalUnitStatusAction
properties:
App\Entity\OrganizationalUnit:
id:

View File

@ -30,11 +30,35 @@ abstract class AbstractOgAgentController extends AbstractController
protected readonly CreateRepositoryAction $createRepositoryAction,
protected readonly SshKeyAction $sshKeyAction,
#[Autowire(env: 'SSL_ENABLED')]
private readonly string $sslEnabled,
protected readonly string $sslEnabled,
)
{
}
protected function buildRequestOptions(array $params = [], ?string $token = null): array
{
$options = array_merge($params, [
'headers' => [
'accept' => 'application/json',
'Content-Type' => 'application/json',
'Authorization' => $token,
],
'verify_peer' => false,
'verify_host' => false,
'timeout' => 10,
]);
if ($this->sslEnabled === 'true') {
$options['verify_peer'] = true;
$options['verify_host'] = false;
$options['cafile'] = '/opt/opengnsys/ogcore/etc/certificates/ca.crt';
$options['local_cert'] = '/opt/opengnsys/ogcore/etc/certificates/ogcore.crt';
$options['local_pk'] = '/opt/opengnsys/ogcore/etc/certificates/ogcore.key';
}
return $options;
}
/**
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
@ -43,25 +67,7 @@ abstract class AbstractOgAgentController extends AbstractController
*/
public function createRequest(string $method, string $url, array $params = [], string $token = null): array
{
$params = array_merge($params, [
'headers' => [
'accept' => 'application/json',
'Content-Type' => 'application/json',
'Authorization' => $token,
],
'verify_peer' => false,
'verify_host' => false,
'timeout' => 10
]);
if ($this->sslEnabled === 'true') {
$params['verify_peer'] = true;
$params['verify_host'] = false;
$params['cafile'] = '/opt/opengnsys/ogcore/etc/certificates/ca.crt';
$params['local_cert'] = '/opt/opengnsys/ogcore/etc/certificates/ogcore.crt';
$params['local_pk'] = '/opt/opengnsys/ogcore/etc/certificates/ogcore.key';
}
$params = $this->buildRequestOptions($params, $token);
try {
$response = $this->httpClient->request($method, $url, $params);

View File

@ -0,0 +1,119 @@
<?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);
}
}