From 14bfc44a38f84006cdd6a612c482c49003c4afbb Mon Sep 17 00:00:00 2001 From: jmbardallo Date: Tue, 12 May 2026 14:53:50 +0200 Subject: [PATCH] =?UTF-8?q?A=C3=B1adida=20nueva=20ruta=20para=20cambiar=20?= =?UTF-8?q?la=20plantilla=20de=20un=20cliente=20/clients/set-boot-mode,=20?= =?UTF-8?q?recibe=20el=20nombre=20de=20la=20plantilla,=20el=20modo=200=3Dt?= =?UTF-8?q?emporal,=201=3Dpermanente=20y=20puede=20enviarse=20mac,=20ip=20?= =?UTF-8?q?o=20nombre=20del=20cliente=20o=20nombre=20del=20aula.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 6 + .../Migration/ImportLegacyDbCommand.php | 2 +- src/Command/PxeSyncCommand.php | 66 +++++++++ .../OgBoot/SetClientBootModeAction.php | 127 ++++++++++++++++++ 4 files changed, 200 insertions(+), 1 deletion(-) create mode 100644 src/Command/PxeSyncCommand.php create mode 100644 src/Controller/OgBoot/SetClientBootModeAction.php diff --git a/CHANGELOG.md b/CHANGELOG.md index c9067e8..cd92c35 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## [1.7.9] - 2026-05-12 +### Added +- Añadida nueva ruta para cambiar la plantilla de un cliente /clients/set-boot-mode, recibe el nombre de la plantilla, el modo 0=temporal, 1=permanente y puede enviarse mac, ip o nombre del cliente o nombre del aula. +### Fixed +- Modificado script de importación de base de datos OG 1.1 para añadir las tablas grupos y gruposordenadores como obligatorias junto con centros y aulas. + ## [1.7.8] - 2026-05-11 ### Fixed - En la importación de clientes, se inicializan con estado "off" diff --git a/src/Command/Migration/ImportLegacyDbCommand.php b/src/Command/Migration/ImportLegacyDbCommand.php index 5aab694..8dfc16e 100644 --- a/src/Command/Migration/ImportLegacyDbCommand.php +++ b/src/Command/Migration/ImportLegacyDbCommand.php @@ -153,7 +153,7 @@ class ImportLegacyDbCommand extends Command $commands = [ 'opengnsys:migration:organizational-unit' => [ 'desc' => 'Migrating Organizational Units', - 'tables' => ['centros', 'aulas'] + 'tables' => ['centros', 'aulas', 'grupos', 'gruposordenadores'] ], 'opengnsys:migration:os' => [ 'desc' => 'Migrating Operative Systems', diff --git a/src/Command/PxeSyncCommand.php b/src/Command/PxeSyncCommand.php new file mode 100644 index 0000000..039c675 --- /dev/null +++ b/src/Command/PxeSyncCommand.php @@ -0,0 +1,66 @@ +addArgument('mac', InputArgument::REQUIRED, 'Dirección MAC del cliente') + ->addArgument('delay', InputArgument::OPTIONAL, 'Tiempo de espera en segundos antes de sincronizar', 0); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $mac = $input->getArgument('mac'); + $delay = (int) $input->getArgument('delay'); + + if ($delay > 0) { + $output->writeln("Esperando $delay segundos..."); + sleep($delay); + } + + $client = $this->clientRepository->findOneBy(['mac' => $mac]); + if (!$client) { + $output->writeln("Cliente con MAC $mac no encontrado"); + return Command::FAILURE; + } + + $template = $client->getTemplate(); + if (!$template) { + $output->writeln("El cliente no tiene una plantilla asignada"); + return Command::FAILURE; + } + + $output->writeln("Sincronizando PXE para el cliente {$client->getName()} ({$mac}) con la plantilla {$template->getName()}..."); + + try { + $this->postPxeAction->__invoke($client, $template); + $output->writeln("Sincronización completada con éxito"); + return Command::SUCCESS; + } catch (\Exception $e) { + $output->writeln("Error durante la sincronización: " . $e->getMessage() . ""); + return Command::FAILURE; + } + } +} diff --git a/src/Controller/OgBoot/SetClientBootModeAction.php b/src/Controller/OgBoot/SetClientBootModeAction.php new file mode 100644 index 0000000..7186c7c --- /dev/null +++ b/src/Controller/OgBoot/SetClientBootModeAction.php @@ -0,0 +1,127 @@ +getContent(), true); + $clientData = $data['client'] ?? null; + $ouData = $data['ou'] ?? null; + $templateName = $data['template'] ?? null; + $mode = $data['mode'] ?? 0; // 0: TEMP, 1: PERM + + if ((!$clientData && !$ouData) || !$templateName) { + return new JsonResponse(['error' => 'Faltan parámetros: se requiere "client" u "ou", y "template"'], Response::HTTP_BAD_REQUEST); + } + + // 1. Obtener la lista de clientes afectados de forma explícita + $clients = []; + + if ($clientData) { + // Búsqueda específica de cliente (Prioridad: MAC > Nombre > IP) + if (isset($clientData['mac'])) { + $c = $this->clientRepository->findOneBy(['mac' => $clientData['mac']]); + if ($c) $clients[] = $c; + } elseif (isset($clientData['name'])) { + $c = $this->clientRepository->findOneBy(['name' => $clientData['name']]); + if ($c) $clients[] = $c; + } elseif (isset($clientData['ip'])) { + $c = $this->clientRepository->findOneBy(['ip' => $clientData['ip']]); + if ($c) $clients[] = $c; + } + } elseif ($ouData) { + // Búsqueda específica de Aula (OU) + if (isset($ouData['name'])) { + $ou = $this->ouRepository->findOneBy(['name' => $ouData['name']]); + if ($ou) { + $clients = $this->clientRepository->findBy(['organizationalUnit' => $ou]); + } + } elseif (isset($ouData['id'])) { + $ou = $this->ouRepository->find($ouData['id']); + if ($ou) { + $clients = $this->clientRepository->findBy(['organizationalUnit' => $ou]); + } + } + } + + if (empty($clients)) { + return new JsonResponse(['error' => "No se encontró ningún cliente que coincida con los criterios."], Response::HTTP_NOT_FOUND); + } + + $count = 0; + $errors = []; + + foreach ($clients as $client) { + /** @var Client $client */ + + $bootServer = $client->getBootServer() ?? $client->getOrganizationalUnit()?->getNetworkSettings()?->getBootServer(); + + if (!$bootServer) { + $errors[] = "El cliente {$client->getName()} no tiene un servidor de arranque asignado."; + continue; + } + + /** @var PxeTemplate|null $template */ + $template = $this->pxeTemplateRepository->findOneBy([ + 'name' => $templateName, + 'bootServer' => $bootServer + ]); + + if (!$template) { + $errors[] = "No se encontró la plantilla '$templateName' en el servidor {$bootServer->getName()} para el cliente {$client->getName()}."; + continue; + } + + if ($mode == 1) { + // MODO PERMANENTE: Actualizamos el objeto (PostAction hará el flush) + $client->setTemplate($template); + } + + // Generar el fichero PXE + try { + $this->postPxeAction->__invoke($client, $template); + $count++; + + if ($mode == 0) { + $projectDir = $this->getParameter('kernel.project_dir'); + $mac = $client->getMac(); + $cmd = sprintf('php %s/bin/console opengnsys:pxe:sync %s 60 > /dev/null 2>&1 &', $projectDir,escapeshellarg($mac)); + exec($cmd); + } + } catch (\Exception $e) { + $errors[] = "Error en {$client->getName()}: " . $e->getMessage(); + } + } + + return new JsonResponse([ + 'status' => count($errors) > 0 && $count == 0 ? 'error' : 'success', + 'clients_processed' => $count, + 'errors' => $errors, + 'mode' => ($mode == 1 ? 'PERMANENT' : 'TEMPORARY') + ]); + } +} -- 2.40.1