refs #1154. API env_vars
testing/ogcore-api/pipeline/head There was a failure building this commit Details

pull/13/head
Manuel Aranda Rosales 2024-11-15 19:01:49 +01:00
parent 116a773b6e
commit 404ee80d68
11 changed files with 129 additions and 28 deletions

View File

@ -4,22 +4,16 @@ imports:
parameters: parameters:
services: services:
App\DependencyInjection\JsonEnvVarLoader:
tags: ['container.env_var_loader']
_defaults: _defaults:
autowire: true # Automatically injects dependencies in your services. autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc. autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
bind:
$ogBootApiUrl: '%env(OGBOOT_API_URL)%'
$ogDhcpApiUrl: '%env(OGDHCP_API_URL)%'
$udsAPIurl: '%env(UDS_URL)%'
$udsAuthLogin: '%env(UDS_AUTH_LOGIN)%'
$udsAuthUsername: '%env(UDS_AUTH_USERNAME)%'
$udsAuthPassword: '%env(UDS_AUTH_PASSWORD)%'
App\: App\:
resource: '../src/' resource: '../src/'
exclude: exclude:
- '../src/DependencyInjection/'
- '../src/Entity/' - '../src/Entity/'
- '../src/Kernel.php' - '../src/Kernel.php'

View File

@ -1,5 +0,0 @@
xdebug.mode=debug
xdebug.start_with_request=trigger
xdebug.discover_client_host = 1
xdebug.client_host=${XDEBUG_CLIENT_HOST}
xdebug.client_port=${XDEBUG_CLIENT_PORT}

View File

@ -0,0 +1,65 @@
<?php
declare(strict_types=1);
namespace App\Controller;
use App\DependencyInjection\JsonEnvVarLoader;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Routing\Attribute\Route;
class EnvDataController extends AbstractController
{
private const string ENV_VARS_FILE = 'env.json';
public function __construct(
private readonly KernelInterface $kernel
)
{
}
#[Route('/env-vars', methods: ['GET'])]
public function getEnvVars(): JsonResponse
{
$projectDir = $this->kernel->getProjectDir();
$fileName = $projectDir . DIRECTORY_SEPARATOR . self::ENV_VARS_FILE;
if (!is_file($fileName)) {
throw new \RuntimeException('File not found: '.$fileName);
}
$content = json_decode(file_get_contents($fileName), true);
return new JsonResponse(['vars' => $content['vars']]);
}
#[Route('/env-vars', methods: ['POST'])]
public function updateEnvVars(Request $request): JsonResponse
{
$data = json_decode($request->getContent(), true);
$projectDir = $this->kernel->getProjectDir();
$fileName = $projectDir . DIRECTORY_SEPARATOR . self::ENV_VARS_FILE;
if (!isset($data['vars']) || !is_array($data['vars'])) {
return new JsonResponse(['error' => 'Invalid payload'], Response::HTTP_BAD_REQUEST);
}
$json = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
if ($json === false) {
throw new \RuntimeException('Failed to encode JSON: ' . json_last_error_msg());
}
if (file_put_contents($fileName, $json) === false) {
throw new \RuntimeException('Failed to write to file: ' . self::ENV_VARS_FILE);
}
return new JsonResponse(['message' => 'Variables updated successfully']);
}
}

View File

@ -6,6 +6,7 @@ namespace App\Controller\OgBoot;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\AsController; use Symfony\Component\HttpKernel\Attribute\AsController;
@ -20,7 +21,8 @@ use Symfony\Contracts\HttpClient\HttpClientInterface;
abstract class AbstractOgBootController extends AbstractController abstract class AbstractOgBootController extends AbstractController
{ {
public function __construct( public function __construct(
protected readonly string $ogBootApiUrl, #[Autowire(env: 'OG_BOOT_API_URL')]
private string $ogBootApiUrl,
protected readonly EntityManagerInterface $entityManager protected readonly EntityManagerInterface $entityManager
) )
{ {

View File

@ -36,23 +36,24 @@ class PostAction extends AbstractOgBootController
'mac' => strtolower($client->getMac()), 'mac' => strtolower($client->getMac()),
'lang' => 'es_ES.UTF_8', 'lang' => 'es_ES.UTF_8',
'ip' => $client->getIp(), 'ip' => $client->getIp(),
'server_ip' => '192.168.2.4', //'server_ip' => '192.168.2.4', //rootServer (ogCore)
'router' => $client->getOrganizationalUnit()->getNetworkSettings()->getRouter(), 'router' => $client->getOrganizationalUnit()->getNetworkSettings()->getRouter(),
'netmask' => $client->getOrganizationalUnit()->getNetworkSettings() ? $client->getOrganizationalUnit()->getNetworkSettings()->getNetmask() : '255.255.255.0', 'netmask' => $client->getOrganizationalUnit()->getNetworkSettings() ? $client->getOrganizationalUnit()->getNetworkSettings()->getNetmask() : '255.255.255.0',
'computer_name' => $client->getName(), 'computer_name' => $client->getName(),
'netiface' => $client->getNetiface(), 'netiface' => $client->getNetiface(),
'group' => $client->getOrganizationalUnit()->getName(), 'group' => $client->getOrganizationalUnit()->getName(),
'ogrepo' => $client->getRepository() ? $client->getRepository()->getIpAddress() : '192.168.2.4', 'ogrepo' => $client->getRepository() ? $client->getRepository()->getIp() : '192.168.2.4',
'oglive' => '192.168.2.4', //'ogcore' => 'parametro_consola',
//'oglive' => '192.168.2.4',
'oglog' => '192.168.2.4', 'oglog' => '192.168.2.4',
'ogshare' => '192.168.2.4', //'ogshare' => '192.168.2.4',
'oglivedir' => 'ogLive', 'oglivedir' => $client->getOgLive()->getFilename(),
'ogprof' => 'false', 'ogprof' => 'false', // cliente del profesor
'hardprofile' => $client->getHardwareProfile() ? $client->getHardwareProfile()->getDescription() : 'default', 'hardprofile' => $client->getHardwareProfile() ? $client->getHardwareProfile()->getDescription() : 'default',
'ogntp' => $client->getOrganizationalUnit()->getNetworkSettings()?->getNtp(), 'ogntp' => $client->getOrganizationalUnit()->getNetworkSettings()?->getNtp(), //optional
'ogdns' => $client->getOrganizationalUnit()->getNetworkSettings()?->getDns(), 'ogdns' => $client->getOrganizationalUnit()->getNetworkSettings()?->getDns(), //optional
'ogProxy' => $client->getOrganizationalUnit()->getNetworkSettings()?->getProxy(), 'ogProxy' => $client->getOrganizationalUnit()->getNetworkSettings()?->getProxy(), //optional
'ogunit' => '', // 'ogunit' => '', //eliminar
'resolution' => '788' 'resolution' => '788'
]; ];

View File

@ -7,6 +7,7 @@ namespace App\Controller\OgDhcp;
use App\Service\Utils\GetIpAddressAndNetmaskFromCIDRService; use App\Service\Utils\GetIpAddressAndNetmaskFromCIDRService;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\AsController; use Symfony\Component\HttpKernel\Attribute\AsController;
@ -21,6 +22,7 @@ use Symfony\Contracts\HttpClient\HttpClientInterface;
abstract class AbstractOgDhcpController extends AbstractController abstract class AbstractOgDhcpController extends AbstractController
{ {
public function __construct( public function __construct(
#[Autowire(env: 'OG_DHCP_API_URL')]
protected readonly string $ogDhcpApiUrl, protected readonly string $ogDhcpApiUrl,
protected readonly EntityManagerInterface $entityManager, protected readonly EntityManagerInterface $entityManager,
protected readonly GetIpAddressAndNetmaskFromCIDRService $getIpAddressAndNetmaskFromCIDRService protected readonly GetIpAddressAndNetmaskFromCIDRService $getIpAddressAndNetmaskFromCIDRService

View File

@ -8,6 +8,7 @@ use App\Model\OrganizationalUnitTypes;
use App\Service\UDS\UDSClient; use App\Service\UDS\UDSClient;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
@ -16,6 +17,7 @@ class RemoteCalendarSyncUdsAction extends AbstractController
{ {
public function __construct( public function __construct(
#[Autowire(env: 'UDS_URL')]
private readonly UDSClient $udsClient private readonly UDSClient $udsClient
) )
{ {

View File

@ -0,0 +1,31 @@
<?php
namespace App\DependencyInjection;
use Symfony\Component\DependencyInjection\EnvVarLoaderInterface;
use Symfony\Component\HttpKernel\KernelInterface;
final class JsonEnvVarLoader implements EnvVarLoaderInterface
{
private const string ENV_VARS_FILE = 'env.json';
public function __construct(
private readonly KernelInterface $kernel
)
{
}
public function loadEnvVars(): array
{
$projectDir = $this->kernel->getProjectDir();
$fileName = $projectDir . DIRECTORY_SEPARATOR . self::ENV_VARS_FILE;
if (!is_file($fileName)) {
throw new \RuntimeException('File not found: '.$fileName);
}
$content = json_decode(file_get_contents($fileName), true);
return $content['vars'];
}
}

View File

@ -2,6 +2,7 @@
namespace App\Service\OgBoot; namespace App\Service\OgBoot;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
@ -15,6 +16,7 @@ use Symfony\Component\HttpClient\HttpClient;
readonly class StatusService readonly class StatusService
{ {
public function __construct( public function __construct(
#[Autowire(env: 'OG_BOOT_API_URL')]
private string $ogBootApiUrl private string $ogBootApiUrl
) )
{ {

View File

@ -2,6 +2,7 @@
namespace App\Service\OgDhcp; namespace App\Service\OgDhcp;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\HttpClient\HttpClient; use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
@ -10,9 +11,10 @@ use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface; use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
class StatusService readonly class StatusService
{ {
public function __construct( public function __construct(
#[Autowire(env: 'OG_DHCP_API_URL')]
private string $ogDhcpApiUrl private string $ogDhcpApiUrl
) )
{ {

View File

@ -5,6 +5,7 @@ namespace App\Service\UDS;
use AllowDynamicProperties; use AllowDynamicProperties;
use App\Entity\OrganizationalUnit; use App\Entity\OrganizationalUnit;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\HttpClient\Exception\TransportException; use Symfony\Component\HttpClient\Exception\TransportException;
use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
@ -24,9 +25,13 @@ class UDSClient
public function __construct( public function __construct(
private HttpClientInterface $httpClient, private HttpClientInterface $httpClient,
private readonly EntityManagerInterface $entityManager, private readonly EntityManagerInterface $entityManager,
#[Autowire(env: 'UDS_URL')]
private string $udsAPIurl, private string $udsAPIurl,
#[Autowire(env: 'UDS_AUTH_LOGIN')]
private string $udsAuthLogin, private string $udsAuthLogin,
#[Autowire(env: 'UDS_AUTH_USERNAME')]
private string $udsAuthUsername, private string $udsAuthUsername,
#[Autowire(env: 'UDS_AUTH_PASSWORD')]
private string $udsAuthPassword, private string $udsAuthPassword,
) )
{ {