Some improvements. Sync multi clients ogBoot. Added base64 encode run script
testing/ogcore-api/pipeline/head This commit looks good Details

pull/27/head
Manuel Aranda Rosales 2025-04-11 08:14:07 +02:00
parent 290cbb0ae0
commit 0cce7ff52d
4 changed files with 89 additions and 7 deletions

View File

@ -39,15 +39,14 @@ class RunScriptAction extends AbstractController
throw new ValidatorException('IP is required');
}
//TODO: base64 script content
$data = [
'nfn' => 'EjecutarScript',
'scp' => $input->script,
'scp' => base64_encode($input->script),
'ids' => '0'
];
try {
$response = $this->httpClient->request('POST', 'https://' . $client->getIp() . ':8000/ogAdmClient/EjecutarScript', [
$response = $this->httpClient->request('POST', 'https://' . $client->getIp() . ':8000/opengnsys/EjecutarScript', [
'verify_peer' => false,
'verify_host' => false,
'headers' => [
@ -74,7 +73,6 @@ class RunScriptAction extends AbstractController
'script' => $input->script,
];
$this->createService->__invoke($client, CommandTypes::RUN_SCRIPT, TraceStatus::SUCCESS, $jobId, $inputData);
}

View File

@ -7,6 +7,7 @@ namespace App\Controller\OgAgent\Webhook;
use App\Entity\Client;
use App\Model\ClientStatus;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
@ -18,7 +19,8 @@ use Symfony\Component\Routing\Annotation\Route;
class AgentSessionController extends AbstractController
{
public function __construct(
protected readonly EntityManagerInterface $entityManager
protected readonly EntityManagerInterface $entityManager,
protected readonly LoggerInterface $logger
)
{
}
@ -58,6 +60,14 @@ class AgentSessionController extends AbstractController
$this->entityManager->persist($client);
$this->entityManager->flush();
$this->logger->info('Client started', [
'mac' => $data['mac'],
'ip' => $data['ip'],
'ostype' => $data['ostype'],
'osversion' => $data['osversion'],
'agent_version' => $data['agent_version'],
]);
return new JsonResponse([], Response::HTTP_OK);
}
@ -82,6 +92,13 @@ class AgentSessionController extends AbstractController
$this->entityManager->persist($client);
$this->entityManager->flush();
$this->logger->info('Client stopped', [
'mac' => $data['mac'],
'ip' => $data['ip'],
'ostype' => $data['ostype'],
'osversion' => $data['osversion'],
]);
return new JsonResponse([], Response::HTTP_OK);
}
@ -117,6 +134,18 @@ class AgentSessionController extends AbstractController
return new JsonResponse(['message' => 'Invalid status'], Response::HTTP_BAD_REQUEST);
}
$this->entityManager->persist($client);
$this->entityManager->flush();
$this->logger->info('Client logged in', [
'ip' => $data['ip'],
'user' => $data['user'],
'language' => $data['language'],
'session' => $data['session'],
'ostype' => $data['ostype'],
'osversion' => $data['osversion'],
]);
return new JsonResponse([], Response::HTTP_OK);
}
@ -152,6 +181,15 @@ class AgentSessionController extends AbstractController
return new JsonResponse(['message' => 'Invalid status'], Response::HTTP_BAD_REQUEST);
}
$this->entityManager->persist($client);
$this->entityManager->flush();
$this->logger->info('Client logged out', [
'ip' => $data['ip'],
'user' => $data['user'],
'ostype' => $data['ostype'],
]);
return new JsonResponse([], Response::HTTP_OK);
}
}

View File

@ -25,6 +25,14 @@ class ClientStatusNotifier
public function postUpdate(Client $client, PostUpdateEventArgs $event): void
{
$em = $event->getObjectManager();
$uow = $em->getUnitOfWork();
$changeSet = $uow->getEntityChangeSet($client);
if (!array_key_exists('status', $changeSet)) {
return;
}
try {
$this->notifyClientStatusChange($client);
} catch (\Exception $e) {
@ -38,9 +46,14 @@ class ClientStatusNotifier
private function notifyClientStatusChange(Client $client): void
{
$data[] = [
'@id' => '/clients/' . $client->getUuid(),
'status' => $client->getStatus(),
];
$update = new Update(
'clients',
json_encode(['@id' => '/clients/'.$client->getUuid(), 'status' => $client->getStatus()])
json_encode($data)
);
$this->hub->publish($update);

View File

@ -5,6 +5,7 @@ namespace App\EventSubscriber;
use ApiPlatform\Symfony\EventListener\EventPriorities;
use App\Controller\OgBoot\PxeBootFile\PostAction;
use App\Dto\Output\OrganizationalUnitOutput;
use App\Entity\Client;
use App\Entity\NetworkSettings;
use App\Entity\OrganizationalUnit;
use Doctrine\ORM\EntityManagerInterface;
@ -12,11 +13,16 @@ use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
final readonly class OrganizationalUnitSubscriber implements EventSubscriberInterface
{
public function __construct(
private EntityManagerInterface $entityManager,
private readonly PostAction $postAction,
)
{
@ -29,6 +35,12 @@ final readonly class OrganizationalUnitSubscriber implements EventSubscriberInte
];
}
/**
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ClientExceptionInterface
*/
public function updateNetworkSettings(ViewEvent $event): void
{
$organizationalUnitOutput = $event->getControllerResult();
@ -49,8 +61,9 @@ final readonly class OrganizationalUnitSubscriber implements EventSubscriberInte
$newNetworkSettings = $this->buildNetworkSettings($organizationalUnitEntity);
$this->updateChildrenNetworkSettings($organizationalUnitEntity, $newNetworkSettings);
$this->entityManager->flush();
$this->syncOgBoot($organizationalUnitEntity);
}
@ -103,4 +116,24 @@ final readonly class OrganizationalUnitSubscriber implements EventSubscriberInte
return $newNetworkSettings;
}
/**
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ClientExceptionInterface
*/
private function syncOgBoot (OrganizationalUnit $organizationalUnitEntity): void
{
$clients = $this->entityManager->getRepository(Client::class)->findClientsByOrganizationalUnitAndDescendants($organizationalUnitEntity->getId(), []);
/** @var Client $client */
foreach ($clients as $client) {
if ($client->getTemplate() === null) {
continue;
}
$this->postAction->__invoke($client, $client->getTemplate());
}
}
}