diff --git a/src/OgBootBundle/Controller/OgBootController.php b/src/OgBootBundle/Controller/OgBootController.php index adf9225..33a4d24 100644 --- a/src/OgBootBundle/Controller/OgBootController.php +++ b/src/OgBootBundle/Controller/OgBootController.php @@ -290,14 +290,14 @@ Regenerar plantilla - PUT /ogboot/pxe-templates } /** - * @Route("/ogboot/v1/oglives/{id}", name="getOglive", methods={"GET"}) + * @Route("/ogboot/v1/oglives/{name}", name="getOglive", methods={"GET"}) * @OA\Get( - * path="/ogboot/v1/oglives/{id}", + * path="/ogboot/v1/oglives/{name}", * summary="Get information of an installed ogLive client", * @OA\Parameter( - * name="id", + * name="name", * in="path", - * description="Index or Directory of the installed ogLive client", + * description="Name or Directory of the installed ogLive client", * required=true, * @OA\Schema( * type="string" @@ -346,29 +346,26 @@ Regenerar plantilla - PUT /ogboot/pxe-templates * ) * ) */ +public function getOglive(Request $request, string $name): Response +{ + $result = $this->curlRequestService->callOgLive("show " . escapeshellarg($name)); - public function getOglive(Request $request): Response - { - $id = (int) $request->get('id'); - # $result = $this->curlRequestService->common_request(OG_REST_CMD_POWEROFF, POST, [OG_REST_PARAM_CLIENTS => $ips]); - $result = $this->curlRequestService->callOgLive("show $id"); - - if ($result) { - return new Response($result, Response::HTTP_OK); - } else { - return new Response('Failed', Response::HTTP_INTERNAL_SERVER_ERROR); - } + if ($result) { + return new Response($result, Response::HTTP_OK); + } else { + return new Response('Failed', Response::HTTP_INTERNAL_SERVER_ERROR); } +} /** - * @Route("/ogboot/v1/oglives/default/{id}", name="setOgliveDefault", methods={"POST"}) + * @Route("/ogboot/v1/oglives/default/{name}", name="setOgliveDefault", methods={"POST"}) * @OA\Post( - * path="/ogboot/v1/oglives/default/{id}", - * summary="Change default ogLive", + * path="/ogboot/v1/oglives/default/{name}", + * summary="Set default ogLive client", * @OA\Parameter( - * name="id", + * name="name", * in="path", - * description="Index of the ogLive client to set as default", + * description="Name of the ogLive client to set as default", * required=true, * @OA\Schema( * type="string" @@ -376,32 +373,55 @@ Regenerar plantilla - PUT /ogboot/pxe-templates * ), * @OA\Response( * response=200, - * description="Default ogLive changed successfully", + * description="ogLive client set as default successfully", * @OA\JsonContent( * type="object", - * @OA\Property(property="success", type="string") + * @OA\Property(property="message", type="string") * ) * ), * @OA\Response( * response=404, * description="ogLive client not found" + * ), + * @OA\Response( + * response=500, + * description="Failed to set default ogLive client" * ) * ) */ +public function setOgliveDefault(Request $request, string $name): Response +{ + // Obtener la lista de ogLives + $listResult = $this->curlRequestService->callOgLive("list"); - public function setOgliveDefault(Request $request): Response - { - $id = (int) $request->get('id'); - # $result = $this->curlRequestService->common_request(OG_REST_CMD_POWEROFF, POST, [OG_REST_PARAM_CLIENTS => $ips]); - $result = $this->curlRequestService->callOgLive("set-default $id"); + if (!$listResult) { + return new Response('Failed to retrieve ogLive list', Response::HTTP_INTERNAL_SERVER_ERROR); + } - if ($result) { - return new Response($result, Response::HTTP_OK); - } else { - return new Response('Failed', Response::HTTP_INTERNAL_SERVER_ERROR); + // Buscar el ID correspondiente al nombre proporcionado + $lines = explode("\n", trim($listResult)); + $id = null; + foreach ($lines as $line) { + if (strpos($line, $name) !== false) { + $id = explode(" ", trim($line))[0]; + break; } } + if ($id === null) { + return new Response('ogLive client not found', Response::HTTP_NOT_FOUND); + } + + // Establecer el ogLive como predeterminado utilizando el ID encontrado + $result = $this->curlRequestService->callOgLive("set-default $id"); + + if ($result) { + return new JsonResponse(['message' => 'ogLive client set as default successfully'], Response::HTTP_OK); + } else { + return new JsonResponse(['error' => 'Failed to set default ogLive client'], Response::HTTP_INTERNAL_SERVER_ERROR); + } +} + @@ -522,17 +542,17 @@ Regenerar plantilla - PUT /ogboot/pxe-templates /** - * @Route("/ogboot/v1/oglives/{id}", name="uninstallOglive", methods={"DELETE"}) + * @Route("/ogboot/v1/oglives/{name}", name="uninstallOglive", methods={"DELETE"}) * @OA\Delete( - * path="/ogboot/v1/oglives/{id}", + * path="/ogboot/v1/oglives/{name}", * summary="Uninstall an ogLive client", * @OA\Parameter( - * name="id", + * name="name", * in="path", - * description="ID of the ogLive client to uninstall", + * description="Name of the ogLive client to uninstall", * required=true, * @OA\Schema( - * type="integer" + * type="string" * ) * ), * @OA\Response( @@ -544,6 +564,10 @@ Regenerar plantilla - PUT /ogboot/pxe-templates * ) * ), * @OA\Response( + * response=404, + * description="ogLive client not found" + * ), + * @OA\Response( * response=500, * description="Failed to uninstall ogLive client", * @OA\JsonContent( @@ -553,20 +577,39 @@ Regenerar plantilla - PUT /ogboot/pxe-templates * ) * ) */ +public function uninstallOglive(Request $request, string $name): Response +{ + // Obtener la lista de ogLives + $listResult = $this->curlRequestService->callOgLive("list"); - public function uninstallOglive(Request $request): Response - { - $id = (int) $request->get('id'); - # $result = $this->curlRequestService->common_request(OG_REST_CMD_POWEROFF, POST, [OG_REST_PARAM_CLIENTS => $ips]); - $result = $this->curlRequestService->callOgLive("uninstall $id"); + if (!$listResult) { + return new Response('Failed to retrieve ogLive list', Response::HTTP_INTERNAL_SERVER_ERROR); + } - if ($result) { - return new Response($result, Response::HTTP_OK); - } else { - return new Response('Failed', Response::HTTP_INTERNAL_SERVER_ERROR); + // Buscar el ID correspondiente al nombre proporcionado + $lines = explode("\n", trim($listResult)); + $id = null; + foreach ($lines as $line) { + if (strpos($line, $name) !== false) { + $id = explode(" ", trim($line))[0]; + break; } } + if ($id === null) { + return new Response('ogLive client not found', Response::HTTP_NOT_FOUND); + } + + // Desinstalar el ogLive utilizando el ID encontrado + $result = $this->curlRequestService->callOgLive("uninstall $id"); + + if ($result) { + return new JsonResponse(['message' => 'ogLive client uninstalled successfully'], Response::HTTP_OK); + } else { + return new JsonResponse(['error' => 'Failed to uninstall ogLive client'], Response::HTTP_INTERNAL_SERVER_ERROR); + } +} + /** * @Route("/ogboot/v1/pxes", name="get_boot_files", methods={"GET"}) * @OA\Get( @@ -613,55 +656,113 @@ public function getBootFiles(): JsonResponse return new JsonResponse($response, Response::HTTP_OK); } - /** - * @Route("/ogboot/v1/pxes/{mac}", name="ogboot_boot_file", methods={"GET"}) - * @OA\Get( - * path="/ogboot/v1/pxes/{mac}", - * summary="Obtener archivo de arranque PXE", - * description="Obtiene el contenido del archivo de arranque PXE para una dirección MAC específica.", - * @OA\Parameter( - * name="mac", - * in="path", - * required=true, - * description="Dirección MAC del cliente", - * @OA\Schema(type="string", example="00:50:56:22:11:12") - * ), - * @OA\Response( - * response=200, - * description="Contenido del archivo de arranque PXE", - * @OA\MediaType( - * mediaType="text/plain", - * @OA\Schema(type="string", example="Contenido del archivo PXE...") - * ) - * ), - * @OA\Response( - * response=404, - * description="Archivo no encontrado" - * ) - * ) - */ - public function getBootFile(string $mac): Response - { +/** + * @Route("/ogboot/v1/pxes/{mac}", name="ogboot_get_boot_file_with_params", methods={"GET"}) + * @OA\Get( + * path="/ogboot/v1/pxes/{mac}", + * summary="Get boot file with parameters", + * @OA\Parameter( + * name="mac", + * in="path", + * description="MAC address", + * required=true, + * @OA\Schema(type="string", example="00:50:56:22:11:12") + * ), + * @OA\Response( + * response=200, + * description="Successful operation", + * @OA\JsonContent( + * type="object", + * @OA\Property(property="template_name", type="string", description="Template name", example="pxe"), + * @OA\Property(property="mac", type="string", description="MAC address", example="00:50:56:22:11:12"), + * @OA\Property(property="lang", type="string", description="Language", example="es_ES.UTF-8"), + * @OA\Property(property="ip", type="string", description="IP address", example="192.168.2.11"), + * @OA\Property(property="server_ip", type="string", description="Server IP address", example="192.168.2.1"), + * @OA\Property(property="router", type="string", description="Router", example="192.168.2.1"), + * @OA\Property(property="netmask", type="string", description="Netmask", example="255.255.255.0"), + * @OA\Property(property="computer_name", type="string", description="Computer name", example="pc11"), + * @OA\Property(property="netiface", type="string", description="Network interface", example="eth0"), + * @OA\Property(property="group", type="string", description="Group", example="Aula_virtual"), + * @OA\Property(property="ogrepo", type="string", description="Repository IP", example="192.168.2.1"), + * @OA\Property(property="oglive", type="string", description="Live server IP", example="192.168.2.1"), + * @OA\Property(property="oglog", type="string", description="Log server IP", example="192.168.2.1"), + * @OA\Property(property="ogshare", type="string", description="Share server IP", example="192.168.2.1"), + * @OA\Property(property="oglivedir", type="string", description="Live directory", example="ogLive"), + * @OA\Property(property="ogprof", type="string", description="Is professor", example="false"), + * @OA\Property(property="hardprofile", type="string", description="Hardware profile", example=""), + * @OA\Property(property="ogntp", type="string", description="NTP server", example=""), + * @OA\Property(property="ogdns", type="string", description="DNS server", example=""), + * @OA\Property(property="ogproxy", type="string", description="Proxy server", example=""), + * @OA\Property(property="ogunit", type="string", description="Unit directory", example=""), + * @OA\Property(property="resolution", type="string", description="Screen resolution", example="788") + * ) + * ), + * @OA\Response( + * response=500, + * description="Failed to retrieve boot file" + * ) + * ) + */ +public function getBootFile(string $mac): Response +{ + // Ruta donde están alojados los archivos de arranque + $directory = '/opt/ogboot/tftpboot/ipxe_scripts'; - $directory = '/opt/ogboot/tftpboot/ipxe_scripts'; + // Genera el nombre del archivo basado en la dirección MAC + $fileName = "01-" . $mac; + $filePath = "$directory/$fileName"; - $fileName = "01-" . $mac; - - - $filePath = "$directory/$fileName"; - - - if (!file_exists($filePath)) { - - return new Response(null, Response::HTTP_NOT_FOUND); - } - - $content = file_get_contents($filePath); - - return new Response($content, Response::HTTP_OK, ['Content-Type' => 'text/plain']); + if (!file_exists($filePath)) { + return new JsonResponse(['error' => 'No se encontró ningún archivo de arranque con la dirección MAC especificada'], Response::HTTP_NOT_FOUND); } + $content = file_get_contents($filePath); + $templateName = ''; + $contentLines = explode("\n", $content); + foreach ($contentLines as $line) { + if (strpos($line, '#template_name:') !== false) { + $templateName = trim(str_replace('#template_name:', '', $line)); + break; + } + } + + // Extraer los parámetros del contenido del archivo iPXE + preg_match('/set kernelargs (.*)/', $content, $matches); + if (isset($matches[1])) { + $kernelargs = $matches[1]; + parse_str(str_replace(' ', '&', $kernelargs), $params); + + $result = [ + 'template_name' => $templateName, + 'mac' => $mac, + 'lang' => $params['LANG'] ?? '', + 'ip' => explode(':', $params['ip'])[0] ?? '', + 'server_ip' => explode(':', $params['ip'])[1] ?? '', + 'router' => explode(':', $params['ip'])[2] ?? '', + 'netmask' => explode(':', $params['ip'])[3] ?? '', + 'computer_name' => explode(':', $params['ip'])[4] ?? '', + 'netiface' => explode(':', $params['ip'])[5] ?? '', + 'group' => $params['group'] ?? '', + 'ogrepo' => $params['ogrepo'] ?? '', + 'oglive' => $params['oglive'] ?? '', + 'oglog' => $params['oglog'] ?? '', + 'ogshare' => $params['ogshare'] ?? '', + 'oglivedir' => $params['oglivedir'] ?? '', + 'ogprof' => $params['ogprof'] ?? '', + 'hardprofile' => $params['hardprofile'] ?? '', + 'ogntp' => $params['ogntp'] ?? '', + 'ogdns' => $params['ogdns'] ?? '', + 'ogproxy' => $params['ogproxy'] ?? '', + 'ogunit' => $params['ogunit'] ?? '', + 'resolution' => $params['vga'] ?? '', + ]; + + return new JsonResponse($result, Response::HTTP_OK); + } + + return new JsonResponse(['error' => 'Failed to parse kernel arguments from the boot file'], Response::HTTP_INTERNAL_SERVER_ERROR); +} /** * @Route("/ogboot/v1/pxes", name="create_boot_file", methods={"POST"})