refs #601. Integration API pxe-boot-file

feature/integration-ogboot
Manuel Aranda Rosales 2024-08-21 09:17:48 +02:00
parent 8e9db51cbc
commit 750e36ec99
10 changed files with 146 additions and 22 deletions

View File

@ -23,6 +23,13 @@ resources:
ApiPlatform\Metadata\Post: ~
ApiPlatform\Metadata\Delete: ~
sync:
class: ApiPlatform\Metadata\Post
method: POST
input: false
uriTemplate: /og-lives/sync
controller: App\Controller\OgBoot\OgLive\SyncAction
get_collection_oglives:
shortName: OgLive Server
description: Get collection of OgLive
@ -80,9 +87,10 @@ resources:
uninstall:
shortName: OgLive Server
description: Uninstall OgLive
class: ApiPlatform\Metadata\Get
method: GET
class: ApiPlatform\Metadata\Post
method: POST
input: false
output: false
uriTemplate: /og-lives/server/{uuid}/uninstall
controller: App\Controller\OgBoot\OgLive\UninstallAction

View File

@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20240821065158 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}
public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE pxe_template CHANGE template_content template_content LONGTEXT NOT NULL');
}
public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE pxe_template CHANGE template_content template_content TINYTEXT NOT NULL');
}
}

View File

@ -2,7 +2,69 @@
namespace App\Controller\OgBoot\OgLive;
class SyncAction
{
use App\Controller\OgBoot\AbstractOgLiveController;
use App\Entity\OgLive;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\AsController;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
#[AsController]
class SyncAction extends AbstractOgLiveController
{
/**
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ClientExceptionInterface
*/
public function __invoke(HttpClientInterface $httpClient, EntityManagerInterface $entityManager): JsonResponse
{
try {
$response = $httpClient->request('GET', $this->ogBootApiUrl.'/ogboot/v1/oglives', [
'headers' => [
'accept' => 'application/json',
],
]);
} catch (TransportExceptionInterface $e) {
return new JsonResponse( data: 'An error occurred', status: Response::HTTP_INTERNAL_SERVER_ERROR);
}
$data = json_decode($response->getContent(), true);
foreach ($data['installed_ogLives'] as $ogLive) {
$ogLiveEntity = $this->entityManager->getRepository(OgLive::class)->findOneBy(['checksum' => $ogLive['id']]);
if ($ogLiveEntity) {
$ogLiveEntity->setName($ogLive['filename']);
$ogLiveEntity->setInstalled(true);
$ogLiveEntity->setArchitecture($ogLive['architecture']);
$ogLiveEntity->setKernel($ogLive['kernel']);
$ogLiveEntity->setRevision($ogLive['revision']);
$ogLiveEntity->setDirectory($ogLive['directory']);
$ogLiveEntity->setChecksum($ogLive['id']);
$this->entityManager->persist($ogLiveEntity);
} else {
$ogLiveEntity = new OgLive();
$ogLiveEntity->setName($ogLive['filename']);
$ogLiveEntity->setInstalled(true);
$ogLiveEntity->setArchitecture($ogLive['architecture']);
$ogLiveEntity->setKernel($ogLive['kernel']);
$ogLiveEntity->setRevision($ogLive['revision']);
$ogLiveEntity->setDirectory($ogLive['directory']);
$ogLiveEntity->setChecksum($ogLive['id']);
}
$this->entityManager->persist($ogLiveEntity);
}
$this->entityManager->flush();
return new JsonResponse( data: $data, status: Response::HTTP_OK);
}
}

View File

@ -4,6 +4,7 @@ namespace App\Controller\OgBoot\OgLive;
use App\Controller\OgBoot\AbstractOgLiveController;
use App\Entity\OgLive;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\AsController;
@ -17,12 +18,12 @@ use Symfony\Contracts\HttpClient\HttpClientInterface;
class UninstallAction extends AbstractOgLiveController
{
/**
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ClientExceptionInterface
* @throws TransportExceptionInterface
*/
public function __invoke(OgLive $data, HttpClientInterface $httpClient): JsonResponse
public function __invoke(OgLive $data, HttpClientInterface $httpClient, EntityManagerInterface $entityManager): JsonResponse
{
try {
$response = $httpClient->request('DELETE', $this->ogBootApiUrl.'/ogboot/v1/oglives/'.$data->getChecksum(), [
@ -30,12 +31,17 @@ class UninstallAction extends AbstractOgLiveController
'accept' => 'application/json',
],
]);
} catch (TransportExceptionInterface $e) {
return new JsonResponse( data: 'An error occurred', status: Response::HTTP_INTERNAL_SERVER_ERROR);
return new JsonResponse( data: $e->getMessage(), status: Response::HTTP_INTERNAL_SERVER_ERROR);
}
$data = json_decode($response->getContent(), true);
if ($response->getStatusCode() === Response::HTTP_OK) {
$data->setInstalled(false);
$entityManager->persist($data);
$entityManager->flush();
}
return new JsonResponse( data: $data, status: Response::HTTP_OK);
return new JsonResponse(status: Response::HTTP_OK);
}
}

View File

@ -24,7 +24,7 @@ class GetCollectionAction extends AbstractOgLiveController
* @throws RedirectionExceptionInterface
* @throws ClientExceptionInterface
*/
public function __invoke(PxeBootFile $data, HttpClientInterface $httpClient): JsonResponse
public function __invoke(HttpClientInterface $httpClient): JsonResponse
{
try {
$response = $httpClient->request('GET', $this->ogBootApiUrl.'/ogboot/v1/pxes', [

View File

@ -38,9 +38,11 @@ class PostAction extends AbstractOgLiveController
],
]);
$data->setSynchronized(true);
$entityManager->persist($data);
$entityManager->flush();
if ($response->getStatusCode() === Response::HTTP_OK) {
$data->setSynchronized(true);
$entityManager->persist($data);
$entityManager->flush();
}
$data = json_decode($response->getContent(), true);
return new JsonResponse($data, Response::HTTP_OK);

View File

@ -10,7 +10,7 @@ use Symfony\Component\Serializer\Annotation\Groups;
#[Get(shortName: 'PxeTemplate')]
final class PxeTemplateOutput extends AbstractOutput
{
#[Groups(['pxe-template:read'])]
#[Groups(['pxe-template:read', 'pxe-boot-file:read'])]
public string $name;
#[Groups(['pxe-template:read'])]

View File

@ -15,7 +15,7 @@ class PxeTemplate extends AbstractEntity
use NameableTrait;
use SynchronizedTrait;
#[ORM\Column(type: Types::TEXT, length: 255)]
#[ORM\Column(type: Types::TEXT)]
private ?string $templateContent = null;
public function getTemplateContent(): ?string

View File

@ -35,31 +35,40 @@ readonly class PostService
foreach ($bootFile->getClients() as $client) {
$data = [
'template_name' => $bootFile->getTemplate()->getName(),
'template_name' => 'pxe_default',
'mac' => $client->getMac(),
'lang' => 'es_ES.UTF_8',
'ip' => $client->getIp(),
'server_ip' => '',
'server_ip' => '92.168.2.1',
'router' => $client->getOrganizationalUnit()->getNetworkSettings()->getRouter(),
'netmask' => $client->getOrganizationalUnit()->getNetworkSettings()->getNetmask(),
'computer_name' => $client->getName(),
'netiface' => $client->getNetiface(),
'group' => $client->getOrganizationalUnit()->getName(),
'ogrepo' => $client->getRepository() ? $client->getRepository()->getIpAddress() : $client->getOrganizationalUnit()->getNetworkSettings()->getRepository()->getIpAddress(),
'oglive' => '',
'ogrepo' => $client->getRepository() ? $client->getRepository()->getIpAddress() : '192.168.2.1',
'oglive' => '127.0.0.1',
'oglog' => '192.168.2.1',
'ogshare' => '192.168.2.1',
'oglivedir' => 'ogLive',
'ogprof' => 'false',
'hardprofile' => '',
'ogntp' => $client->getOrganizationalUnit()->getNetworkSettings()->getNtp(),
'ogdns' => $client->getOrganizationalUnit()->getNetworkSettings()->getDns(),
'ogProxy' => $client->getOrganizationalUnit()->getNetworkSettings()->getProxy(),
'ogunit' => ''
'ogunit' => '',
'resolution' => '768'
];
try {
$response = $httpClient->request('POST', $this->ogBootApiUrl.'/ogboot/v1/pxes', [
'headers' => [
'accept' => 'application/json',
'Content-Type' => 'application/json',
],
'json' => $data
]);
} catch (TransportExceptionInterface $e) {
return new JsonResponse( data: 'An error occurred', status: Response::HTTP_INTERNAL_SERVER_ERROR);
return new JsonResponse( data: $e->getMessage(), status: Response::HTTP_INTERNAL_SERVER_ERROR);
}
return json_decode($response->getContent(), true);

View File

@ -13,6 +13,7 @@ use App\Dto\Input\PxeBootFileInput;
use App\Dto\Output\PxeBootFileOutput;
use App\Repository\PxeBootFileRepository;
use App\Service\OgBoot\PxeBootFile\PostService;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
readonly class PxeBootFileProcessor implements ProcessorInterface
{
@ -41,6 +42,7 @@ readonly class PxeBootFileProcessor implements ProcessorInterface
/**
* @throws \Exception
* @throws TransportExceptionInterface
*/
private function processCreateOrUpdate($data, Operation $operation, array $uriVariables = [], array $context = []): PxeBootFileOutput
{
@ -57,7 +59,11 @@ readonly class PxeBootFileProcessor implements ProcessorInterface
$this->validator->validate($pxeTemplate);
$this->bootFileRepository->save($pxeTemplate);
$this->postService->__invoke($pxeTemplate);
try {
$this->postService->__invoke($pxeTemplate);
} catch (\Exception $e) {
}
return new PxeBootFileOutput($pxeTemplate);
}