develop #90

Merged
maranda merged 2 commits from develop into main 2026-04-21 11:31:50 +02:00
8 changed files with 320 additions and 20 deletions

View File

@ -121,6 +121,13 @@ resources:
uriTemplate: /clients/{uuid}/get-partition-script-data
controller: App\Controller\OgAgent\GetPartitionScriptDataAction
delete_client_lease:
class: ApiPlatform\Metadata\Delete
method: DELETE
input: false
uriTemplate: /clients/{uuid}/delete-lease
controller: App\Controller\OgDhcp\Client\DeleteClientLeaseAction
properties:
App\Entity\Client:

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

@ -97,6 +97,25 @@ resources:
uriTemplate: /og-dhcp/server/{uuid}/delete-host/{clientUuid}
controller: App\Controller\OgDhcp\Subnet\DeleteHostAction
delete_subnet_leases:
shortName: Subnet Server Leases
description: Delete all leases of a Subnet
class: ApiPlatform\Metadata\Delete
method: DELETE
input: false
uriTemplate: /og-dhcp/server/{uuid}/delete-leases
controller: App\Controller\OgDhcp\Subnet\DeleteSubnetLeasesAction
delete_client_lease:
shortName: Subnet Server Leases
description: Delete lease of a specific client
class: ApiPlatform\Metadata\Delete
method: DELETE
input: false
read: false
uriTemplate: /og-dhcp/server/{uuid}/delete-lease/{clientUuid}
controller: App\Controller\OgDhcp\Subnet\DeleteClientLeaseAction
properties:
App\Entity\Subnet:

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);
}
}

View File

@ -0,0 +1,48 @@
<?php
namespace App\Controller\OgDhcp\Client;
use App\Controller\OgDhcp\AbstractOgDhcpController;
use App\Entity\Client;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\AsController;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
#[AsController]
class DeleteClientLeaseAction extends AbstractOgDhcpController
{
/**
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ClientExceptionInterface
*/
public function __invoke(Client $data): JsonResponse
{
if (!$data->getIp()) {
throw new BadRequestHttpException('Client has no IP address assigned');
}
$subnet = $data->getSubnet();
if (!$subnet) {
throw new BadRequestHttpException('Client has no subnet associated');
}
$dhcpServer = $subnet->getDhcpServer();
if (!$dhcpServer) {
throw new BadRequestHttpException('Subnet has no DHCP server associated');
}
$port = $dhcpServer->getApiPort() ?? 8081;
$url = sprintf('http://%s:%d/ogdhcp/v1/leases/%s', $dhcpServer->getIp(), $port, $data->getIp());
$content = $this->createRequest('DELETE', $url);
return new JsonResponse(data: $content, status: Response::HTTP_OK);
}
}

View File

@ -0,0 +1,50 @@
<?php
namespace App\Controller\OgDhcp\Subnet;
use App\Controller\OgDhcp\AbstractOgDhcpController;
use App\Entity\Client;
use App\Entity\Subnet;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\AsController;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
#[AsController]
class DeleteClientLeaseAction extends AbstractOgDhcpController
{
/**
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ClientExceptionInterface
*/
public function __invoke(Subnet $data, string $clientUuid): JsonResponse
{
$client = $this->entityManager->getRepository(Client::class)->findOneBy(['uuid' => $clientUuid]);
if (!$client) {
throw new BadRequestHttpException('Client not found');
}
if (!$client->getIp()) {
throw new BadRequestHttpException('Client has no IP address assigned');
}
$dhcpServer = $data->getDhcpServer();
if (!$dhcpServer) {
throw new BadRequestHttpException('Subnet has no DHCP server associated');
}
$port = $dhcpServer->getApiPort() ?? 8081;
$url = sprintf('http://%s:%d/ogdhcp/v1/leases/%s', $dhcpServer->getIp(), $port, $client->getIp());
$content = $this->createRequest('DELETE', $url);
return new JsonResponse(data: $content, status: Response::HTTP_OK);
}
}

View File

@ -0,0 +1,43 @@
<?php
namespace App\Controller\OgDhcp\Subnet;
use App\Controller\OgDhcp\AbstractOgDhcpController;
use App\Entity\Subnet;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\AsController;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
#[AsController]
class DeleteSubnetLeasesAction extends AbstractOgDhcpController
{
/**
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ClientExceptionInterface
*/
public function __invoke(Subnet $data): JsonResponse
{
if (!$data->getServerId()) {
throw new BadRequestHttpException('Subnet has no serverId assigned in DHCP server');
}
$dhcpServer = $data->getDhcpServer();
if (!$dhcpServer) {
throw new BadRequestHttpException('Subnet has no DHCP server associated');
}
$port = $dhcpServer->getApiPort() ?? 8081;
$url = sprintf('http://%s:%d/ogdhcp/v1/subnets/%d/leases', $dhcpServer->getIp(), $port, $data->getServerId());
$content = $this->createRequest('DELETE', $url);
return new JsonResponse(data: $content, status: Response::HTTP_OK);
}
}