refs #311 changes getpxe parameters of return and removes all ids to the name of the oglives

pull/3/head
Luis Gerardo Romero Garcia 2024-06-05 10:15:25 +02:00
parent 9546b0b83f
commit f2581bf340
1 changed files with 190 additions and 89 deletions

View File

@ -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( * @OA\Get(
* path="/ogboot/v1/oglives/{id}", * path="/ogboot/v1/oglives/{name}",
* summary="Get information of an installed ogLive client", * summary="Get information of an installed ogLive client",
* @OA\Parameter( * @OA\Parameter(
* name="id", * name="name",
* in="path", * in="path",
* description="Index or Directory of the installed ogLive client", * description="Name or Directory of the installed ogLive client",
* required=true, * required=true,
* @OA\Schema( * @OA\Schema(
* type="string" * type="string"
@ -346,12 +346,9 @@ Regenerar plantilla - PUT /ogboot/pxe-templates
* ) * )
* ) * )
*/ */
public function getOglive(Request $request, string $name): Response
public function getOglive(Request $request): Response
{ {
$id = (int) $request->get('id'); $result = $this->curlRequestService->callOgLive("show " . escapeshellarg($name));
# $result = $this->curlRequestService->common_request(OG_REST_CMD_POWEROFF, POST, [OG_REST_PARAM_CLIENTS => $ips]);
$result = $this->curlRequestService->callOgLive("show $id");
if ($result) { if ($result) {
return new Response($result, Response::HTTP_OK); return new Response($result, Response::HTTP_OK);
@ -361,14 +358,14 @@ Regenerar plantilla - PUT /ogboot/pxe-templates
} }
/** /**
* @Route("/ogboot/v1/oglives/default/{id}", name="setOgliveDefault", methods={"POST"}) * @Route("/ogboot/v1/oglives/default/{name}", name="setOgliveDefault", methods={"POST"})
* @OA\Post( * @OA\Post(
* path="/ogboot/v1/oglives/default/{id}", * path="/ogboot/v1/oglives/default/{name}",
* summary="Change default ogLive", * summary="Set default ogLive client",
* @OA\Parameter( * @OA\Parameter(
* name="id", * name="name",
* in="path", * in="path",
* description="Index of the ogLive client to set as default", * description="Name of the ogLive client to set as default",
* required=true, * required=true,
* @OA\Schema( * @OA\Schema(
* type="string" * type="string"
@ -376,29 +373,52 @@ Regenerar plantilla - PUT /ogboot/pxe-templates
* ), * ),
* @OA\Response( * @OA\Response(
* response=200, * response=200,
* description="Default ogLive changed successfully", * description="ogLive client set as default successfully",
* @OA\JsonContent( * @OA\JsonContent(
* type="object", * type="object",
* @OA\Property(property="success", type="string") * @OA\Property(property="message", type="string")
* ) * )
* ), * ),
* @OA\Response( * @OA\Response(
* response=404, * response=404,
* description="ogLive client not found" * description="ogLive client not found"
* ),
* @OA\Response(
* response=500,
* description="Failed to set default ogLive client"
* ) * )
* ) * )
*/ */
public function setOgliveDefault(Request $request, string $name): Response
public function setOgliveDefault(Request $request): Response
{ {
$id = (int) $request->get('id'); // Obtener la lista de ogLives
# $result = $this->curlRequestService->common_request(OG_REST_CMD_POWEROFF, POST, [OG_REST_PARAM_CLIENTS => $ips]); $listResult = $this->curlRequestService->callOgLive("list");
if (!$listResult) {
return new Response('Failed to retrieve ogLive list', 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"); $result = $this->curlRequestService->callOgLive("set-default $id");
if ($result) { if ($result) {
return new Response($result, Response::HTTP_OK); return new JsonResponse(['message' => 'ogLive client set as default successfully'], Response::HTTP_OK);
} else { } else {
return new Response('Failed', Response::HTTP_INTERNAL_SERVER_ERROR); 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( * @OA\Delete(
* path="/ogboot/v1/oglives/{id}", * path="/ogboot/v1/oglives/{name}",
* summary="Uninstall an ogLive client", * summary="Uninstall an ogLive client",
* @OA\Parameter( * @OA\Parameter(
* name="id", * name="name",
* in="path", * in="path",
* description="ID of the ogLive client to uninstall", * description="Name of the ogLive client to uninstall",
* required=true, * required=true,
* @OA\Schema( * @OA\Schema(
* type="integer" * type="string"
* ) * )
* ), * ),
* @OA\Response( * @OA\Response(
@ -544,6 +564,10 @@ Regenerar plantilla - PUT /ogboot/pxe-templates
* ) * )
* ), * ),
* @OA\Response( * @OA\Response(
* response=404,
* description="ogLive client not found"
* ),
* @OA\Response(
* response=500, * response=500,
* description="Failed to uninstall ogLive client", * description="Failed to uninstall ogLive client",
* @OA\JsonContent( * @OA\JsonContent(
@ -553,17 +577,36 @@ Regenerar plantilla - PUT /ogboot/pxe-templates
* ) * )
* ) * )
*/ */
public function uninstallOglive(Request $request, string $name): Response
public function uninstallOglive(Request $request): Response
{ {
$id = (int) $request->get('id'); // Obtener la lista de ogLives
# $result = $this->curlRequestService->common_request(OG_REST_CMD_POWEROFF, POST, [OG_REST_PARAM_CLIENTS => $ips]); $listResult = $this->curlRequestService->callOgLive("list");
if (!$listResult) {
return new Response('Failed to retrieve ogLive list', 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"); $result = $this->curlRequestService->callOgLive("uninstall $id");
if ($result) { if ($result) {
return new Response($result, Response::HTTP_OK); return new JsonResponse(['message' => 'ogLive client uninstalled successfully'], Response::HTTP_OK);
} else { } else {
return new Response('Failed', Response::HTTP_INTERNAL_SERVER_ERROR); return new JsonResponse(['error' => 'Failed to uninstall ogLive client'], Response::HTTP_INTERNAL_SERVER_ERROR);
} }
} }
@ -614,54 +657,112 @@ public function getBootFiles(): JsonResponse
} }
/** /**
* @Route("/ogboot/v1/pxes/{mac}", name="ogboot_boot_file", methods={"GET"}) * @Route("/ogboot/v1/pxes/{mac}", name="ogboot_get_boot_file_with_params", methods={"GET"})
* @OA\Get( * @OA\Get(
* path="/ogboot/v1/pxes/{mac}", * path="/ogboot/v1/pxes/{mac}",
* summary="Obtener archivo de arranque PXE", * summary="Get boot file with parameters",
* description="Obtiene el contenido del archivo de arranque PXE para una dirección MAC específica.",
* @OA\Parameter( * @OA\Parameter(
* name="mac", * name="mac",
* in="path", * in="path",
* description="MAC address",
* required=true, * required=true,
* description="Dirección MAC del cliente",
* @OA\Schema(type="string", example="00:50:56:22:11:12") * @OA\Schema(type="string", example="00:50:56:22:11:12")
* ), * ),
* @OA\Response( * @OA\Response(
* response=200, * response=200,
* description="Contenido del archivo de arranque PXE", * description="Successful operation",
* @OA\MediaType( * @OA\JsonContent(
* mediaType="text/plain", * type="object",
* @OA\Schema(type="string", example="Contenido del archivo PXE...") * @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( * @OA\Response(
* response=404, * response=500,
* description="Archivo no encontrado" * description="Failed to retrieve boot file"
* ) * )
* ) * )
*/ */
public function getBootFile(string $mac): Response 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; $fileName = "01-" . $mac;
$filePath = "$directory/$fileName"; $filePath = "$directory/$fileName";
if (!file_exists($filePath)) { 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);
return new Response(null, Response::HTTP_NOT_FOUND);
} }
$content = file_get_contents($filePath); $content = file_get_contents($filePath);
$templateName = '';
return new Response($content, Response::HTTP_OK, ['Content-Type' => 'text/plain']); $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"}) * @Route("/ogboot/v1/pxes", name="create_boot_file", methods={"POST"})