refs #311 #373 adds endpoints pxe and pxe-template to the ogbootcontroller

pull/3/head
Luis Gerardo Romero Garcia 2024-05-21 11:51:06 +02:00
parent 2fe1e64ef9
commit 518a00db59
1 changed files with 103 additions and 85 deletions

View File

@ -725,105 +725,123 @@ public function getBootFiles(): JsonResponse
return new JsonResponse(['message' => 'El archivo de arranque se eliminó correctamente'], Response::HTTP_OK);
}
/**
* @OA\Post(
* path="/poweroff",
* summary="Power off a client",
* @OA\RequestBody(
* description="JSON payload",
* required=true,
* @OA\JsonContent(
* type="object",
* @OA\Property(property="clients", type="array", @OA\Items(type="string", example="192.168.1.1"))
* )
* ),
* @OA\Response(
* response=200,
* description="Clients powered off successfully",
* @OA\JsonContent(
* type="object",
* @OA\Property(property="success", type="string")
* )
* ),
* @OA\Response(
* response=400,
* description="Error occurred",
* @OA\JsonContent(
* type="object",
* @OA\Property(property="error", type="string")
* )
* )
* )
* @Route("/poweroff", name="poweroff", methods={"POST"})
*/
public function poweroff(Request $request): Response
/**
* @Route("", methods={"POST"})
*
* @OA\Post(
* path="/ogboot/v1/pxe-templates",
* summary="Crear Plantilla",
* description="Crea una nueva plantilla de arranque utilizando los datos proporcionados.",
* @OA\RequestBody(
* required=true,
* @OA\JsonContent(
* type="object",
* @OA\Property(property="name_template", type="string", example="mi_plantilla.ipxe"),
* @OA\Property(property="content_template", type="string", example="#!ipxe\nset timeout 0\nset timeout-style hidden\n\nset ISODIR ogLive\nset default 0\nset kernelargs INFOHOST\ntime kernel tftp://172.17.8.71/ogLive/ogvmlinuz ${kernelargs}\ntime initrd tftp://172.17.8.71/ogLive/oginitrd.img\nboot")
* )
* ),
* @OA\Response(
* response=200,
* description="La plantilla de arranque se creó exitosamente.",
* @OA\JsonContent(
* type="object",
* @OA\Property(property="message", type="string", example="Plantilla creada exitosamente."),
* @OA\Property(property="template", type="string", example="#!ipxe\nset timeout 0\nset timeout-style hidden\n\nset ISODIR ogLive\nset default 0\nset kernelargs INFOHOST\ntime kernel tftp://172.17.8.71/ogLive/ogvmlinuz ${kernelargs}\ntime initrd tftp://172.17.8.71/ogLive/oginitrd.img\nboot")
* )
* ),
* @OA\Response(
* response=400,
* description="La solicitud no pudo ser procesada debido a un error en los datos proporcionados en el cuerpo de la solicitud."
* ),
* @OA\Response(
* response=500,
* description="Ocurrió un error al crear la plantilla de arranque."
* )
* )
*/
public function createTemplate(Request $request)
{
$data = json_decode($request->getContent(), true);
if (!isset($data['clients'])) {
return new Response('Invalid data', Response::HTTP_BAD_REQUEST);
if (!isset($data['name_template']) || !isset($data['content_template'])) {
return new Response('La solicitud no pudo ser procesada debido a un error en los datos proporcionados en el cuerpo de la solicitud.', 400);
}
$ips = $data['clients'];
$nameTemplate = $data['name_template'];
$contentTemplate = $data['content_template'];
$templateDir = '/opt/ogboot/tftpboot/ipxe_scripts/templates';
$result = $this->curlRequestService->common_request(OG_REST_CMD_POWEROFF, POST, [OG_REST_PARAM_CLIENTS => $ips]);
if ($result) {
return new Response('Success', Response::HTTP_OK);
} else {
return new Response('Failed', Response::HTTP_INTERNAL_SERVER_ERROR);
// Validar nombre del archivo
if (!preg_match('/^[a-zA-Z0-9._-]+$/', $nameTemplate)) {
return new Response('Nombre de la plantilla no válido.', 400);
}
// Crear la ruta completa del archivo
$filePath = $templateDir . '/' . $nameTemplate;
try {
file_put_contents($filePath, $contentTemplate);
} catch (\Exception $e) {
return new Response('Ocurrió un error al crear la plantilla de arranque.', 500);
}
return new Response(json_encode([
'message' => 'Plantilla creada exitosamente.',
'template' => $contentTemplate
]), 200, ['Content-Type' => 'application/json']);
}
/**
* @OA\Post(
* path="/reboot",
* summary="Reboot a client",
* @OA\RequestBody(
* description="JSON payload",
* required=true,
* @OA\JsonContent(
* type="object",
* @OA\Property(property="clients", type="array", @OA\Items(type="string", example="192.168.1.1"))
* )
* ),
* @OA\Response(
* response=200,
* description="Clients rebooted successfully",
* @OA\JsonContent(
* type="object",
* @OA\Property(property="success", type="string")
* )
* ),
* @OA\Response(
* response=400,
* description="Error occurred",
* @OA\JsonContent(
* type="object",
* @OA\Property(property="error", type="string")
* )
* )
* )
* @Route("/reboot", name="reboot", methods={"POST"})
*/
public function reboot(Request $request): Response
/**
* @Route("/{name}", methods={"DELETE"})
*
* @OA\Delete(
* path="/ogboot/v1/pxe-templates/{name}",
* summary="Eliminar Plantilla",
* description="Elimina una plantilla de arranque específica utilizando su nombre.",
* @OA\Parameter(
* name="name",
* in="path",
* required=true,
* description="El nombre de la plantilla de arranque que se desea eliminar.",
* @OA\Schema(type="string")
* ),
* @OA\Response(
* response=200,
* description="La plantilla de arranque se eliminó correctamente.",
* @OA\JsonContent(
* type="object",
* @OA\Property(property="message", type="string", example="Plantilla eliminada correctamente.")
* )
* ),
* @OA\Response(
* response=404,
* description="No se encontró ninguna plantilla de arranque con el nombre especificado."
* ),
* @OA\Response(
* response=500,
* description="Ocurrió un error al eliminar la plantilla de arranque."
* )
* )
*/
public function deleteTemplate($name)
{
$data = json_decode($request->getContent(), true);
$templateDir = '/opt/ogboot/tftpboot/ipxe_scripts/templates';
$filePath = $templateDir . '/' . $name;
if (!isset($data['clients'])) {
return new Response('Invalid data', Response::HTTP_BAD_REQUEST);
if (!file_exists($filePath)) {
return new Response('No se encontró ninguna plantilla de arranque con el nombre especificado.', 404);
}
$ips = $data['clients'];
$result = $this->curlRequestService->common_request(OG_REST_CMD_REBOOT, 'POST', [OG_REST_PARAM_CLIENTS => $ips]);
if ($result) {
return new Response('Success', Response::HTTP_OK);
} else {
return new Response('Failed', Response::HTTP_INTERNAL_SERVER_ERROR);
try {
unlink($filePath);
} catch (\Exception $e) {
return new Response('Ocurrió un error al eliminar la plantilla de arranque.', 500);
}
return new Response(json_encode([
'message' => 'Plantilla eliminada correctamente.'
]), 200, ['Content-Type' => 'application/json']);
}