Updated pxe logic

pull/16/head
Manuel Aranda Rosales 2024-11-26 10:46:37 +01:00
parent c33528f78f
commit 49259255dd
10 changed files with 91 additions and 102 deletions

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace App\Controller\OgBoot; namespace App\Controller\OgBoot;
use App\Controller\OgBoot\PxeBootFile\PostAction;
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\DependencyInjection\Attribute\Autowire;
@ -25,7 +26,8 @@ abstract class AbstractOgBootController extends AbstractController
protected string $ogBootApiUrl, protected string $ogBootApiUrl,
#[Autowire(env: 'OG_CORE_IP')] #[Autowire(env: 'OG_CORE_IP')]
protected string $ogCoreIP, protected string $ogCoreIP,
protected readonly EntityManagerInterface $entityManager protected readonly EntityManagerInterface $entityManager,
protected readonly HttpClientInterface $httpClient,
) )
{ {
} }
@ -36,7 +38,7 @@ abstract class AbstractOgBootController extends AbstractController
* @throws RedirectionExceptionInterface * @throws RedirectionExceptionInterface
* @throws ClientExceptionInterface * @throws ClientExceptionInterface
*/ */
public function createRequest (HttpClientInterface $httpClient, string $method, string $url, array $params = []): JsonResponse|array public function createRequest (string $method, string $url, array $params = []): JsonResponse|array
{ {
$params = array_merge($params, [ $params = array_merge($params, [
'headers' => [ 'headers' => [
@ -46,7 +48,7 @@ abstract class AbstractOgBootController extends AbstractController
]); ]);
try { try {
$response = $httpClient->request($method, $url, $params); $response = $this->httpClient->request($method, $url, $params);
return json_decode($response->getContent(), true); return json_decode($response->getContent(), true);
} catch (ClientExceptionInterface | ServerExceptionInterface $e) { } catch (ClientExceptionInterface | ServerExceptionInterface $e) {

View File

@ -54,7 +54,9 @@ class SyncAction extends AbstractOgBootController
*/ */
private function extracted(OgLive|null $ogLiveEntity, mixed $ogLive): void private function extracted(OgLive|null $ogLiveEntity, mixed $ogLive): void
{ {
$ogLiveEntity->setName(str_replace(self::OG_BOOT_DIRECTORY, '', $ogLive['directory'])); if (!$ogLiveEntity){
$ogLiveEntity->setName(str_replace(self::OG_BOOT_DIRECTORY, '', $ogLive['directory']));
}
$ogLiveEntity->setInstalled(true); $ogLiveEntity->setInstalled(true);
$ogLiveEntity->setArchitecture($ogLive['architecture']); $ogLiveEntity->setArchitecture($ogLive['architecture']);
$ogLiveEntity->setDistribution($ogLive['distribution']); $ogLiveEntity->setDistribution($ogLive['distribution']);

View File

@ -26,57 +26,42 @@ class PostAction extends AbstractOgBootController
* @throws RedirectionExceptionInterface * @throws RedirectionExceptionInterface
* @throws ClientExceptionInterface * @throws ClientExceptionInterface
*/ */
public function __invoke(PxeTemplateSyncClientInput $input, PxeTemplate $pxeTemplate, HttpClientInterface $httpClient): JsonResponse public function __invoke(Client $client, PxeTemplate $pxeTemplate): JsonResponse
{ {
/** @var Client $client */ $params = [
$client = $input->client->getEntity(); 'json' => [
'template_name' => $pxeTemplate->getName(),
$data = [ 'mac' => strtolower($client->getMac()),
'template_name' => $pxeTemplate->getName(), 'lang' => 'es_ES.UTF_8',
'mac' => strtolower($client->getMac()), 'ip' => $client->getIp(),
'lang' => 'es_ES.UTF_8', 'server_ip' => $this->ogBootApiUrl,
'ip' => $client->getIp(), 'router' => $client->getOrganizationalUnit()->getNetworkSettings()->getRouter(),
'server_ip' => $this->ogBootApiUrl, 'netmask' => $client->getOrganizationalUnit()->getNetworkSettings() ? $client->getOrganizationalUnit()->getNetworkSettings()->getNetmask() : '255.255.255.0',
'router' => $client->getOrganizationalUnit()->getNetworkSettings()->getRouter(), 'computer_name' => $client->getName(),
'netmask' => $client->getOrganizationalUnit()->getNetworkSettings() ? $client->getOrganizationalUnit()->getNetworkSettings()->getNetmask() : '255.255.255.0', 'netiface' => $client->getNetiface(),
'computer_name' => $client->getName(), 'group' => $client->getOrganizationalUnit()->getName(),
'netiface' => $client->getNetiface(), 'ogrepo' => $client->getRepository()?->getIp() ,
'group' => $client->getOrganizationalUnit()->getName(), 'ogcore' => $this->ogCoreIP,
'ogrepo' => $client->getRepository()->getIp() , 'oglive' => $this->ogBootApiUrl,
'ogcore' => $this->ogCoreIP, 'oglog' => $client->getOrganizationalUnit()->getNetworkSettings()?->getOgLog(),
'oglive' => $this->ogBootApiUrl, 'ogshare' => $client->getOrganizationalUnit()->getNetworkSettings()?->getOgShare()
'oglog' => $client->getOrganizationalUnit()->getNetworkSettings()?->getOgLog(), ? $client->getOrganizationalUnit()->getNetworkSettings()?->getOgShare(): $this->ogBootApiUrl,
'ogshare' => $client->getOrganizationalUnit()->getNetworkSettings()?->getOgShare() 'oglivedir' => $client->getOgLive()->getFilename(),
? $client->getOrganizationalUnit()->getNetworkSettings()?->getOgShare(): $this->ogBootApiUrl, 'ogprof' => 'false',
'oglivedir' => $client->getOgLive()->getFilename(), 'hardprofile' => $client->getHardwareProfile() ? $client->getHardwareProfile()->getDescription() : 'default',
'ogprof' => 'false', 'ogntp' => $client->getOrganizationalUnit()->getNetworkSettings()?->getNtp(),
'hardprofile' => $client->getHardwareProfile() ? $client->getHardwareProfile()->getDescription() : 'default', 'ogdns' => $client->getOrganizationalUnit()->getNetworkSettings()?->getDns(),
'ogntp' => $client->getOrganizationalUnit()->getNetworkSettings()?->getNtp(), //optional 'ogProxy' => $client->getOrganizationalUnit()->getNetworkSettings()?->getProxy(),
'ogdns' => $client->getOrganizationalUnit()->getNetworkSettings()?->getDns(), //optional 'resolution' => '788'
'ogProxy' => $client->getOrganizationalUnit()->getNetworkSettings()?->getProxy(), //optional ]
// 'ogunit' => '', //eliminar
'resolution' => '788'
]; ];
try { $content = $this->createRequest('POST', 'http://'.$this->ogBootApiUrl.'/ogboot/v1/pxes', $params);
$response = $httpClient->request('POST', 'http://'.$this->ogBootApiUrl.'/ogboot/v1/pxes', [
'headers' => [
'accept' => 'application/json',
'Content-Type' => 'application/json',
],
'json' => $data
]);
} catch (TransportExceptionInterface $e) {
$client->setPxeSync(false);
$this->entityManager->persist($client);
$this->entityManager->flush();
return new JsonResponse( data: $e->getMessage(), status: Response::HTTP_INTERNAL_SERVER_ERROR);
}
$client->setPxeSync(true); $client->setPxeSync(true);
$this->entityManager->persist($client); $this->entityManager->persist($client);
$this->entityManager->flush(); $this->entityManager->flush();
return new JsonResponse(data: json_decode($response->getContent(), true), status: Response::HTTP_OK); return new JsonResponse(data: $content, status: Response::HTTP_OK);
} }
} }

View File

@ -2,10 +2,13 @@
namespace App\Controller\OgBoot\PxeTemplate; namespace App\Controller\OgBoot\PxeTemplate;
use App\Controller\OgBoot\AbstractOgBootController;
use App\Controller\OgDhcp\AbstractOgDhcpController; use App\Controller\OgDhcp\AbstractOgDhcpController;
use App\Dto\Input\PxeTemplateAddClientsInput; use App\Dto\Input\PxeTemplateAddClientsInput;
use App\Entity\Client; use App\Entity\Client;
use App\Entity\PxeTemplate; use App\Entity\PxeTemplate;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
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;
@ -16,8 +19,15 @@ use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface; use Symfony\Contracts\HttpClient\HttpClientInterface;
#[AsController] #[AsController]
class AddClientAction extends AbstractOgDhcpController class AddClientAction extends AbstractController
{ {
public function __construct(
private readonly \App\Controller\OgBoot\PxeBootFile\PostAction $postAction,
private readonly EntityManagerInterface $entityManager,
)
{
}
/** /**
* @throws TransportExceptionInterface * @throws TransportExceptionInterface
@ -25,11 +35,13 @@ class AddClientAction extends AbstractOgDhcpController
* @throws RedirectionExceptionInterface * @throws RedirectionExceptionInterface
* @throws ClientExceptionInterface * @throws ClientExceptionInterface
*/ */
public function __invoke(PxeTemplateAddClientsInput $input, PxeTemplate $pxeTemplate, HttpClientInterface $httpClient): JsonResponse public function __invoke(PxeTemplateAddClientsInput $input, PxeTemplate $pxeTemplate): JsonResponse
{ {
$clients = $input->clients; $clients = $input->clients;
foreach ($clients as $client) { foreach ($clients as $client) {
$this->postAction->__invoke($client->getEntity(), $pxeTemplate);
/** @var Client $clientEntity */ /** @var Client $clientEntity */
$clientEntity = $client->getEntity(); $clientEntity = $client->getEntity();
$clientEntity->setTemplate($pxeTemplate); $clientEntity->setTemplate($pxeTemplate);

View File

@ -24,22 +24,12 @@ class DeleteAction extends AbstractOgBootController
* @throws RedirectionExceptionInterface * @throws RedirectionExceptionInterface
* @throws ClientExceptionInterface * @throws ClientExceptionInterface
*/ */
public function __invoke(PxeTemplate $data, HttpClientInterface $httpClient, EntityManagerInterface $entityManager): JsonResponse public function __invoke(PxeTemplate $data): JsonResponse
{ {
try { $content = $this->createRequest('DELETE', 'http://'.$this->ogBootApiUrl.'/ogboot/v1/pxe-templates/'.$data->getName());
$response = $httpClient->request('DELETE', 'http://'.$this->ogBootApiUrl.'/ogboot/v1/pxe-templates/'.$data->getName(), [
'headers' => [
'accept' => 'application/json',
],
]);
} catch (TransportExceptionInterface $e) {
return new JsonResponse( data: 'An error occurred', status: Response::HTTP_INTERNAL_SERVER_ERROR);
}
if ($response->getStatusCode() === Response::HTTP_OK) { $this->entityManager->remove($data);
$entityManager->remove($data); $this->entityManager->flush();
$entityManager->flush();
}
return new JsonResponse(status: Response::HTTP_OK); return new JsonResponse(status: Response::HTTP_OK);
} }

View File

@ -24,39 +24,21 @@ class PostAction extends AbstractOgBootController
* @throws RedirectionExceptionInterface * @throws RedirectionExceptionInterface
* @throws ClientExceptionInterface * @throws ClientExceptionInterface
*/ */
public function __invoke(PxeTemplate $data, HttpClientInterface $httpClient, EntityManagerInterface $entityManager): JsonResponse public function __invoke(PxeTemplate $data): JsonResponse
{ {
try { $params = [
$response = $httpClient->request('POST', 'http://'.$this->ogBootApiUrl.'/ogboot/v1/pxe-templates', [ 'json' => [
'headers' => [ 'name_template' => $data->getName(),
'accept' => 'application/json', 'content_template' => $data->getTemplateContent(),
'Content-Type' => 'application/json', ]
], ];
'json' => [
'name_template' => $data->getName(),
'content_template' => $data->getTemplateContent(),
],
]);
if ($response->getStatusCode() === Response::HTTP_OK) { $content = $this->createRequest('POST', 'http://'.$this->ogBootApiUrl.'/ogboot/v1/pxe-templates' , $params);
$data->setSynchronized(true);
$entityManager->persist($data);
$entityManager->flush();
}
$data = json_decode($response->getContent(), true); $data->setSynchronized(true);
return new JsonResponse($data, Response::HTTP_OK); $this->entityManager->persist($data);
$this->entityManager->flush();
} catch (ClientExceptionInterface $e) { return new JsonResponse(data: $content, status: Response::HTTP_OK);
$errorResponse = $e->getResponse();
$errorContent = $errorResponse ? $errorResponse->getContent(false) : 'Client error occurred';
return new JsonResponse(['error' => $errorContent], Response::HTTP_BAD_REQUEST);
} catch (ServerExceptionInterface $e) {
$errorResponse = $e->getResponse();
$errorContent = $errorResponse ? $errorResponse->getContent(false) : 'Server error occurred';
return new JsonResponse(['error' => $errorContent], Response::HTTP_INTERNAL_SERVER_ERROR);
} catch (TransportExceptionInterface $e) {
return new JsonResponse(['error' => 'Transport error occurred'], Response::HTTP_INTERNAL_SERVER_ERROR);
}
} }
} }

View File

@ -26,19 +26,24 @@ class SyncAction extends AbstractOgBootController
* @throws RedirectionExceptionInterface * @throws RedirectionExceptionInterface
* @throws ClientExceptionInterface * @throws ClientExceptionInterface
*/ */
public function __invoke(HttpClientInterface $httpClient, EntityManagerInterface $entityManager): JsonResponse public function __invoke(): JsonResponse
{ {
$content = $this->createRequest($httpClient, 'GET', 'http://'.$this->ogBootApiUrl . '/ogboot/v1/pxe-templates'); $content = $this->createRequest('GET', 'http://'.$this->ogBootApiUrl . '/ogboot/v1/pxe-templates');
foreach ($content['message'] as $template) { foreach ($content['message'] as $template) {
$templateEntity = $this->entityManager->getRepository(PxeTemplate::class)->findOneBy(['name' => $template]); $templateEntity = $this->entityManager->getRepository(PxeTemplate::class)->findOneBy(['name' => $template]);
if ($templateEntity) { if ($templateEntity) {
$this->entityManager->persist($templateEntity); $this->entityManager->persist($templateEntity);
} else { } else {
$templateEntity = new PxeTemplate(); $templateEntity = new PxeTemplate();
$templateEntity->setName($template); $templateEntity->setName($template);
$templateEntity->setTemplateContent('');
} }
$templateContent = $this->createRequest('GET', 'http://'.$this->ogBootApiUrl . '/ogboot/v1/pxe-templates/'.$templateEntity->getName());
$templateEntity->setTemplateContent($templateContent['template_content']);
$templateEntity->setSynchronized(true);
$this->entityManager->persist($templateEntity); $this->entityManager->persist($templateEntity);
} }
$this->entityManager->flush(); $this->entityManager->flush();

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace App\Controller\OgDhcp; namespace App\Controller\OgDhcp;
use App\Controller\OgBoot\PxeBootFile\PostAction;
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;
@ -23,9 +24,10 @@ abstract class AbstractOgDhcpController extends AbstractController
{ {
public function __construct( public function __construct(
#[Autowire(env: 'OG_DHCP_API_URL')] #[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,
protected readonly HttpClientInterface $httpClient,
) )
{ {
} }

View File

@ -9,15 +9,18 @@ use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\Put; use ApiPlatform\Metadata\Put;
use ApiPlatform\State\ProcessorInterface; use ApiPlatform\State\ProcessorInterface;
use ApiPlatform\Validator\ValidatorInterface; use ApiPlatform\Validator\ValidatorInterface;
use App\Controller\OgBoot\PxeTemplate\PostAction;
use App\Dto\Input\PxeTemplateInput; use App\Dto\Input\PxeTemplateInput;
use App\Dto\Output\PxeTemplateOutput; use App\Dto\Output\PxeTemplateOutput;
use App\Repository\PxeTemplateRepository; use App\Repository\PxeTemplateRepository;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
readonly class PxeTemplateProcessor implements ProcessorInterface readonly class PxeTemplateProcessor implements ProcessorInterface
{ {
public function __construct( public function __construct(
private PxeTemplateRepository $pxeTemplateRepository, private PxeTemplateRepository $pxeTemplateRepository,
private ValidatorInterface $validator private ValidatorInterface $validator,
private PostAction $postAction,
) )
{ {
} }
@ -39,6 +42,7 @@ readonly class PxeTemplateProcessor implements ProcessorInterface
/** /**
* @throws \Exception * @throws \Exception
* @throws TransportExceptionInterface
*/ */
private function processCreateOrUpdate($data, Operation $operation, array $uriVariables = [], array $context = []): PxeTemplateOutput private function processCreateOrUpdate($data, Operation $operation, array $uriVariables = [], array $context = []): PxeTemplateOutput
{ {
@ -53,6 +57,11 @@ readonly class PxeTemplateProcessor implements ProcessorInterface
$pxeTemplate = $data->createOrUpdateEntity($entity); $pxeTemplate = $data->createOrUpdateEntity($entity);
$this->validator->validate($pxeTemplate); $this->validator->validate($pxeTemplate);
if (!$pxeTemplate->getId() ) {
$this->postAction->__invoke($pxeTemplate);
}
$this->pxeTemplateRepository->save($pxeTemplate); $this->pxeTemplateRepository->save($pxeTemplate);
return new PxeTemplateOutput($pxeTemplate); return new PxeTemplateOutput($pxeTemplate);

View File

@ -12,7 +12,7 @@ use ApiPlatform\Validator\ValidatorInterface;
use App\Dto\Input\SubnetInput; use App\Dto\Input\SubnetInput;
use App\Dto\Output\SubnetOutput; use App\Dto\Output\SubnetOutput;
use App\Repository\SubnetRepository; use App\Repository\SubnetRepository;
use App\Service\OgBoot\PxeBootFile\PostService; use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
readonly class SubnetProcessor implements ProcessorInterface readonly class SubnetProcessor implements ProcessorInterface
{ {