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;
use App\Controller\OgBoot\PxeBootFile\PostAction;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
@ -25,7 +26,8 @@ abstract class AbstractOgBootController extends AbstractController
protected string $ogBootApiUrl,
#[Autowire(env: 'OG_CORE_IP')]
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 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, [
'headers' => [
@ -46,7 +48,7 @@ abstract class AbstractOgBootController extends AbstractController
]);
try {
$response = $httpClient->request($method, $url, $params);
$response = $this->httpClient->request($method, $url, $params);
return json_decode($response->getContent(), true);
} catch (ClientExceptionInterface | ServerExceptionInterface $e) {

View File

@ -54,7 +54,9 @@ class SyncAction extends AbstractOgBootController
*/
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->setArchitecture($ogLive['architecture']);
$ogLiveEntity->setDistribution($ogLive['distribution']);

View File

@ -26,57 +26,42 @@ class PostAction extends AbstractOgBootController
* @throws RedirectionExceptionInterface
* @throws ClientExceptionInterface
*/
public function __invoke(PxeTemplateSyncClientInput $input, PxeTemplate $pxeTemplate, HttpClientInterface $httpClient): JsonResponse
public function __invoke(Client $client, PxeTemplate $pxeTemplate): JsonResponse
{
/** @var Client $client */
$client = $input->client->getEntity();
$data = [
'template_name' => $pxeTemplate->getName(),
'mac' => strtolower($client->getMac()),
'lang' => 'es_ES.UTF_8',
'ip' => $client->getIp(),
'server_ip' => $this->ogBootApiUrl,
'router' => $client->getOrganizationalUnit()->getNetworkSettings()->getRouter(),
'netmask' => $client->getOrganizationalUnit()->getNetworkSettings() ? $client->getOrganizationalUnit()->getNetworkSettings()->getNetmask() : '255.255.255.0',
'computer_name' => $client->getName(),
'netiface' => $client->getNetiface(),
'group' => $client->getOrganizationalUnit()->getName(),
'ogrepo' => $client->getRepository()->getIp() ,
'ogcore' => $this->ogCoreIP,
'oglive' => $this->ogBootApiUrl,
'oglog' => $client->getOrganizationalUnit()->getNetworkSettings()?->getOgLog(),
'ogshare' => $client->getOrganizationalUnit()->getNetworkSettings()?->getOgShare()
? $client->getOrganizationalUnit()->getNetworkSettings()?->getOgShare(): $this->ogBootApiUrl,
'oglivedir' => $client->getOgLive()->getFilename(),
'ogprof' => 'false',
'hardprofile' => $client->getHardwareProfile() ? $client->getHardwareProfile()->getDescription() : 'default',
'ogntp' => $client->getOrganizationalUnit()->getNetworkSettings()?->getNtp(), //optional
'ogdns' => $client->getOrganizationalUnit()->getNetworkSettings()?->getDns(), //optional
'ogProxy' => $client->getOrganizationalUnit()->getNetworkSettings()?->getProxy(), //optional
// 'ogunit' => '', //eliminar
'resolution' => '788'
$params = [
'json' => [
'template_name' => $pxeTemplate->getName(),
'mac' => strtolower($client->getMac()),
'lang' => 'es_ES.UTF_8',
'ip' => $client->getIp(),
'server_ip' => $this->ogBootApiUrl,
'router' => $client->getOrganizationalUnit()->getNetworkSettings()->getRouter(),
'netmask' => $client->getOrganizationalUnit()->getNetworkSettings() ? $client->getOrganizationalUnit()->getNetworkSettings()->getNetmask() : '255.255.255.0',
'computer_name' => $client->getName(),
'netiface' => $client->getNetiface(),
'group' => $client->getOrganizationalUnit()->getName(),
'ogrepo' => $client->getRepository()?->getIp() ,
'ogcore' => $this->ogCoreIP,
'oglive' => $this->ogBootApiUrl,
'oglog' => $client->getOrganizationalUnit()->getNetworkSettings()?->getOgLog(),
'ogshare' => $client->getOrganizationalUnit()->getNetworkSettings()?->getOgShare()
? $client->getOrganizationalUnit()->getNetworkSettings()?->getOgShare(): $this->ogBootApiUrl,
'oglivedir' => $client->getOgLive()->getFilename(),
'ogprof' => 'false',
'hardprofile' => $client->getHardwareProfile() ? $client->getHardwareProfile()->getDescription() : 'default',
'ogntp' => $client->getOrganizationalUnit()->getNetworkSettings()?->getNtp(),
'ogdns' => $client->getOrganizationalUnit()->getNetworkSettings()?->getDns(),
'ogProxy' => $client->getOrganizationalUnit()->getNetworkSettings()?->getProxy(),
'resolution' => '788'
]
];
try {
$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);
}
$content = $this->createRequest('POST', 'http://'.$this->ogBootApiUrl.'/ogboot/v1/pxes', $params);
$client->setPxeSync(true);
$this->entityManager->persist($client);
$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;
use App\Controller\OgBoot\AbstractOgBootController;
use App\Controller\OgDhcp\AbstractOgDhcpController;
use App\Dto\Input\PxeTemplateAddClientsInput;
use App\Entity\Client;
use App\Entity\PxeTemplate;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\AsController;
@ -16,8 +19,15 @@ use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
#[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
@ -25,11 +35,13 @@ class AddClientAction extends AbstractOgDhcpController
* @throws RedirectionExceptionInterface
* @throws ClientExceptionInterface
*/
public function __invoke(PxeTemplateAddClientsInput $input, PxeTemplate $pxeTemplate, HttpClientInterface $httpClient): JsonResponse
public function __invoke(PxeTemplateAddClientsInput $input, PxeTemplate $pxeTemplate): JsonResponse
{
$clients = $input->clients;
foreach ($clients as $client) {
$this->postAction->__invoke($client->getEntity(), $pxeTemplate);
/** @var Client $clientEntity */
$clientEntity = $client->getEntity();
$clientEntity->setTemplate($pxeTemplate);

View File

@ -24,22 +24,12 @@ class DeleteAction extends AbstractOgBootController
* @throws RedirectionExceptionInterface
* @throws ClientExceptionInterface
*/
public function __invoke(PxeTemplate $data, HttpClientInterface $httpClient, EntityManagerInterface $entityManager): JsonResponse
public function __invoke(PxeTemplate $data): JsonResponse
{
try {
$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);
}
$content = $this->createRequest('DELETE', 'http://'.$this->ogBootApiUrl.'/ogboot/v1/pxe-templates/'.$data->getName());
if ($response->getStatusCode() === Response::HTTP_OK) {
$entityManager->remove($data);
$entityManager->flush();
}
$this->entityManager->remove($data);
$this->entityManager->flush();
return new JsonResponse(status: Response::HTTP_OK);
}

View File

@ -24,39 +24,21 @@ class PostAction extends AbstractOgBootController
* @throws RedirectionExceptionInterface
* @throws ClientExceptionInterface
*/
public function __invoke(PxeTemplate $data, HttpClientInterface $httpClient, EntityManagerInterface $entityManager): JsonResponse
public function __invoke(PxeTemplate $data): JsonResponse
{
try {
$response = $httpClient->request('POST', 'http://'.$this->ogBootApiUrl.'/ogboot/v1/pxe-templates', [
'headers' => [
'accept' => 'application/json',
'Content-Type' => 'application/json',
],
'json' => [
'name_template' => $data->getName(),
'content_template' => $data->getTemplateContent(),
],
]);
$params = [
'json' => [
'name_template' => $data->getName(),
'content_template' => $data->getTemplateContent(),
]
];
if ($response->getStatusCode() === Response::HTTP_OK) {
$data->setSynchronized(true);
$entityManager->persist($data);
$entityManager->flush();
}
$content = $this->createRequest('POST', 'http://'.$this->ogBootApiUrl.'/ogboot/v1/pxe-templates' , $params);
$data = json_decode($response->getContent(), true);
return new JsonResponse($data, Response::HTTP_OK);
$data->setSynchronized(true);
$this->entityManager->persist($data);
$this->entityManager->flush();
} catch (ClientExceptionInterface $e) {
$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);
}
return new JsonResponse(data: $content, status: Response::HTTP_OK);
}
}

View File

@ -26,19 +26,24 @@ class SyncAction extends AbstractOgBootController
* @throws RedirectionExceptionInterface
* @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) {
$templateEntity = $this->entityManager->getRepository(PxeTemplate::class)->findOneBy(['name' => $template]);
if ($templateEntity) {
$this->entityManager->persist($templateEntity);
} else {
$templateEntity = new PxeTemplate();
$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->flush();

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace App\Controller\OgDhcp;
use App\Controller\OgBoot\PxeBootFile\PostAction;
use App\Service\Utils\GetIpAddressAndNetmaskFromCIDRService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
@ -23,9 +24,10 @@ abstract class AbstractOgDhcpController extends AbstractController
{
public function __construct(
#[Autowire(env: 'OG_DHCP_API_URL')]
protected readonly string $ogDhcpApiUrl,
protected readonly EntityManagerInterface $entityManager,
protected readonly GetIpAddressAndNetmaskFromCIDRService $getIpAddressAndNetmaskFromCIDRService,
protected readonly string $ogDhcpApiUrl,
protected readonly EntityManagerInterface $entityManager,
protected readonly GetIpAddressAndNetmaskFromCIDRService $getIpAddressAndNetmaskFromCIDRService,
protected readonly HttpClientInterface $httpClient,
)
{
}

View File

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

View File

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