From ad7a91ce0535414e7fd69d9ba053441bdb8464a3 Mon Sep 17 00:00:00 2001 From: lgromero Date: Thu, 5 Sep 2024 06:57:23 +0200 Subject: [PATCH] refs #649 adds ipxe template example to swagger documentation and adds new comprobations to post template --- .../Controller/OgBootController.php | 41 ++++++++++--------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/src/OgBootBundle/Controller/OgBootController.php b/src/OgBootBundle/Controller/OgBootController.php index 32e6779..caa58a3 100644 --- a/src/OgBootBundle/Controller/OgBootController.php +++ b/src/OgBootBundle/Controller/OgBootController.php @@ -51,21 +51,6 @@ Crear plantilla - POST /ogboot/pxe-templates Regenerar plantilla - PUT /ogboot/pxe-templates */ - /** - * @Route("/ogboot/help", name="help", methods={"GET"}) - */ - public function help(): Response - { - - # $result = $this->curlRequestService->common_request(OG_REST_CMD_POWEROFF, POST, [OG_REST_PARAM_CLIENTS => $ips]); - $result = $this->curlRequestService->callOgLive("help"); - - if ($result) { - return new Response($result, Response::HTTP_OK); - } else { - return new Response('Failed', Response::HTTP_INTERNAL_SERVER_ERROR); - } - } /** * @Route("/ogboot/v1/status", name="getStatus", methods={"GET"}) @@ -981,8 +966,8 @@ public function getBootFile(string $mac): Response * required=true, * @OA\JsonContent( * type="object", - * @OA\Property(property="name_template", type="string", example=""), - * @OA\Property(property="content_template", type="string", example="") + * @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\nset ISODIR __OGLIVE__\nset default 0\nset kernelargs __INFOHOST__\n:try_iso\nkernel http://__SERVERIP__/tftpboot/${ISODIR}/ogvmlinuz ${kernelargs} || goto fallback\ninitrd http://__SERVERIP__/tftpboot/${ISODIR}/oginitrd.img\nboot\n\n:fallback\nset ISODIR ogLive\nkernel http://__SERVERIP__/tftpboot/${ISODIR}/ogvmlinuz ${kernelargs}\ninitrd http://__SERVERIP__/tftpboot/${ISODIR}/oginitrd.img\nboot\n") * ) * ), * @OA\Response( @@ -990,8 +975,8 @@ public function getBootFile(string $mac): Response * description="La plantilla de arranque se creó exitosamente.", * @OA\JsonContent( * type="object", - * @OA\Property(property="message", type="string", example=""), - * @OA\Property(property="template", type="string", example="") + * @OA\Property(property="message", type="string", example="Plantilla creada exitosamente"), + * @OA\Property(property="template", type="string", example="mi_plantilla.ipxe") * ) * ), * @OA\Response( @@ -1015,14 +1000,30 @@ public function createTemplate(Request $request) $nameTemplate = $data['name_template']; $contentTemplate = $data['content_template']; $templateDir = '/opt/ogboot/tftpboot/ipxe_scripts/templates'; + + // Validar el nombre de la plantilla if (!preg_match('/^[a-zA-Z0-9._-]+$/', $nameTemplate)) { return new Response('Nombre de la plantilla no válido.', 400); } $filePath = $templateDir . '/' . $nameTemplate; $contentTemplate = str_replace("\\n", "\n", $contentTemplate); + try { + // Intentar crear la plantilla file_put_contents($filePath, $contentTemplate); + + // Comprobar si el archivo se ha creado correctamente + if (!file_exists($filePath)) { + throw new \Exception('El archivo no se pudo crear.'); + } + + // Verificar si el contenido escrito coincide con el contenido esperado + $writtenContent = file_get_contents($filePath); + if ($writtenContent !== $contentTemplate) { + throw new \Exception('El contenido del archivo no coincide con el contenido esperado.'); + } + } catch (\Exception $e) { return new Response('Ocurrió un error al crear la plantilla de arranque. ' . $e->getMessage(), 500); } @@ -1035,6 +1036,8 @@ public function createTemplate(Request $request) + + /** * @Route("/ogboot/v1/pxe-templates/{name}", methods={"DELETE"}) *