refs #503 #504 #639 removes oglive daemon execution and changes install oglive endpoint parameter to URL

ogboot_debian_installer
Luis Gerardo Romero Garcia 2024-08-13 14:35:37 +02:00
parent d8d883073a
commit 7293943c27
2 changed files with 20 additions and 124 deletions

View File

@ -512,10 +512,10 @@ public function setOgliveDefault(Request $request): Response
* @OA\JsonContent(
* type="object",
* @OA\Property(
* property="isoname",
* property="iso_url",
* type="string",
* example="ogLive-focal-5.13.0-27-beta-amd64-r20210706.5b4bf5f.iso",
* description="Name of the ogLive ISO to install"
* example="https://ognproject.evlt.uma.es/trac/downloads/ogLive-focal-5.13.0-27-beta-amd64-r20210706.5b4bf5f.iso",
* description="Full URL of the ogLive ISO to install"
* )
* )
* ),
@ -547,12 +547,12 @@ public function installOglive(Request $request): Response
{
$data = json_decode($request->getContent(), true);
if (!isset($data['isoname'])) {
if (!isset($data['iso_url'])) {
return new JsonResponse(['error' => 'Invalid input data'], Response::HTTP_BAD_REQUEST);
}
$isoname = $data['isoname'];
$result = $this->curlRequestService->callOgLive("download " . escapeshellarg($isoname));
$isoUrl = $data['iso_url'];
$result = $this->curlRequestService->callOgLive("download " . escapeshellarg($isoUrl));
if (is_array($result) && isset($result['success']) && $result['success'] === false) {
return new JsonResponse(['error' => 'Failed to install ogLive client', 'details' => $result['error']], Response::HTTP_INTERNAL_SERVER_ERROR);
@ -562,7 +562,6 @@ public function installOglive(Request $request): Response
}
/**
* @Route("/ogboot/v1/oglives/{checksum}", name="uninstallOglive", methods={"DELETE"})
* @OA\Delete(
@ -1068,6 +1067,7 @@ public function getBootFile(string $mac): Response
return new Response($content, Response::HTTP_OK, ['Content-Type' => 'text/plain']);
}
/**
* @Route("/ogboot/v1/pxe-templates", methods={"POST"})
*
@ -1080,7 +1080,7 @@ public function getBootFile(string $mac): Response
* @OA\JsonContent(
* type="object",
* @OA\Property(property="name_template", type="string", example="pxe"),
* @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\nkernel tftp://SERVERIP/ogLive/ogvmlinuz ${kernelargs}\ninitrd tftp://SERVERIP/ogLive/oginitrd.img\nboot")
* @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__\n# Menú de entrada para seleccionar OgLive\n:try_iso\nkernel tftp://__SERVERIP__/${ISODIR}/ogvmlinuz ${kernelargs} || goto fallback\ninitrd tftp://__SERVERIP__/${ISODIR}/oginitrd.img\nboot\n\n:fallback\necho \"OgLive default\"\nset ISODIR ogLive\nkernel tftp://__SERVERIP__/${ISODIR}/ogvmlinuz ${kernelargs}\ninitrd tftp://__SERVERIP__/${ISODIR}/oginitrd.img\nboot")
* )
* ),
* @OA\Response(
@ -1089,7 +1089,7 @@ public function getBootFile(string $mac): Response
* @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\nkernel tftp://SERVERIP/ogLive/ogvmlinuz ${kernelargs}\ninitrd tftp://SERVERIP/ogLive/oginitrd.img\nboot")
* @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__\n# Menú de entrada para seleccionar OgLive\n:try_iso\nkernel tftp://__SERVERIP__/${ISODIR}/ogvmlinuz ${kernelargs} || goto fallback\ninitrd tftp://__SERVERIP__/${ISODIR}/oginitrd.img\nboot\n\n:fallback\necho \"OgLive default\"\nset ISODIR ogLive\nkernel tftp://__SERVERIP__/${ISODIR}/ogvmlinuz ${kernelargs}\ninitrd tftp://__SERVERIP__/${ISODIR}/oginitrd.img\nboot")
* )
* ),
* @OA\Response(

View File

@ -19,128 +19,24 @@ class CurlRequestService
return $bits;
}
// src/Service/CurlRequestService.php
public function installOglive($isoname)
{
$socketPath = '/tmp/oglive_daemon.sock';
$socket = socket_create(AF_UNIX, SOCK_STREAM, 0);
if ($socket === false) {
syslog(LOG_ERR, 'Error al crear el socket: ' . socket_strerror(socket_last_error()));
return ['success' => false, 'output' => 'Error al crear el socket'];
}
$result = socket_connect($socket, $socketPath);
if ($result === false) {
syslog(LOG_ERR, 'Error al conectar con el socket: ' . socket_strerror(socket_last_error($socket)));
socket_close($socket);
return ['success' => false, 'output' => 'Error al conectar con el socket'];
}
$command = [
'action' => 'download',
'args' => [$isoname]
];
socket_write($socket, json_encode($command), strlen(json_encode($command)));
$response = '';
$status = [];
while ($buffer = socket_read($socket, 2048)) {
$response .= $buffer;
$status[] = json_decode($buffer, true);
}
socket_close($socket);
// Analiza el último estado recibido
$lastStatus = end($status);
if ($lastStatus && $lastStatus['status'] === 'completed') {
return ['success' => true, 'output' => $lastStatus];
} else {
return ['success' => false, 'output' => $status];
}
}
public function callOgLive($parameter)
{
$socketPath = '/var/run/oglive/oglive_daemon.sock';
// Ruta completa al script oglivecli
$ogLiveCliPath = sprintf("%s/bin/oglivecli", dirname(dirname(dirname(__DIR__))));
file_put_contents('/tmp/serviceOglive.log', 'callOgLive called with parameter: ' . $parameter . PHP_EOL, FILE_APPEND);
// Construir el comando sin sudo ni contraseña
$command = sprintf('%s %s', $ogLiveCliPath, escapeshellarg($parameter));
$socket = socket_create(AF_UNIX, SOCK_STREAM, 0);
if ($socket === false) {
$error = 'Error al crear el socket: ' . socket_strerror(socket_last_error());
file_put_contents('/tmp/serviceOglive.log', 'Socket creation error: ' . $error . PHP_EOL, FILE_APPEND);
return [
'success' => false,
'error' => $error
];
}
// Registrar el comando en syslog para depuración
syslog(LOG_INFO, 'command ' . $command);
$result = socket_connect($socket, $socketPath);
if ($result === false) {
$error = 'Error al conectar con el socket: ' . socket_strerror(socket_last_error($socket));
file_put_contents('/tmp/serviceOglive.log', 'Socket connection error: ' . $error . PHP_EOL, FILE_APPEND);
socket_close($socket);
return [
'success' => false,
'error' => $error
];
}
// Ejecutar el comando
$output = shell_exec($command);
$args = array_map('trim', explode(' ', $parameter));
$action = array_shift($args);
$command = [
'action' => $action,
'args' => $args
];
socket_write($socket, json_encode($command), strlen(json_encode($command)));
$response = '';
while ($buffer = socket_read($socket, 2048)) {
$response .= $buffer;
}
socket_close($socket);
file_put_contents('/tmp/serviceOglive.log', 'Raw response: ' . $response . PHP_EOL, FILE_APPEND);
if (empty($response)) {
$error = 'Respuesta vacía del demonio';
file_put_contents('/tmp/serviceOglive.log', 'Empty response error: ' . $error . PHP_EOL, FILE_APPEND);
return [
'success' => false,
'error' => $error
];
}
$decodedResponse = json_decode($response, true);
if (json_last_error() !== JSON_ERROR_NONE) {
$error = 'Error al decodificar JSON: ' . json_last_error_msg();
file_put_contents('/tmp/serviceOglive.log', 'JSON decode error: ' . $error . PHP_EOL, FILE_APPEND);
return [
'success' => false,
'error' => $error
];
}
if (isset($decodedResponse['success']) && $decodedResponse['success']) {
file_put_contents('/tmp/serviceOglive.log', 'Decoded successful response: ' . json_encode($decodedResponse['output']) . PHP_EOL, FILE_APPEND);
return $decodedResponse['output'];
} else {
$error = $decodedResponse['error'] ?? 'Unknown error';
file_put_contents('/tmp/serviceOglive.log', 'Error in response: ' . $error . PHP_EOL, FILE_APPEND);
return [
'success' => false,
'error' => $error
];
}
return $output;
}