From c5eb8042916c64ff63e29d6d993c5f2372969547 Mon Sep 17 00:00:00 2001 From: lgromero Date: Tue, 22 Oct 2024 19:30:37 +0000 Subject: [PATCH] refs #1026 #1027 adds new ogboot ip parameter to installer and adds it to the .env file. Changes url from oglives list to point to ognproject artefactos --- bin/oglivecli | 62 ++++++++++--------- config/services.yaml | 1 + installer/config.json | 1 + installer/ogboot_installer.py | 6 ++ .../Controller/OgBootController.php | 40 +++++++----- 5 files changed, 68 insertions(+), 42 deletions(-) diff --git a/bin/oglivecli b/bin/oglivecli index 903a432..ebd14e8 100755 --- a/bin/oglivecli +++ b/bin/oglivecli @@ -30,7 +30,8 @@ PROG=$(basename "$(realpath "$0")") # Program name. PROGDIR=$(dirname "$(realpath "$0")") # Dir where program is OPENGNSYS=$(realpath $PROGDIR/../) # ogboot main directory. DOWNLOADDIR=$OPENGNSYS/lib # Directory to store ogLive images. -DOWNLOADURL="https://ognproject.evlt.uma.es/trac/downloads" # Download URL. +#@DOWNLOADURL="https://ognproject.evlt.uma.es/trac/downloads" # Download URL. +DOWNLOADURL="https://ognproject.evlt.uma.es/oglive/" TFTPDIR=$OPENGNSYS/tftpboot/ # TFTP directory. DEFOGLIVE="ogLive" # Default ogLive directory. MINREL=20190601 # Mininum ogLive compatibility release. @@ -166,49 +167,53 @@ function download() { fi } - # Muestra un menú para seleccionar y descargar un archivo ogLive ISO del sitio web de OpenGnsys. function downloadMenu() { - local OGLIVE NISOS i HTTPCODE ISOREL - OGLIVE=( $(curl -k --silent $DOWNLOADURL | grep "$DEFOGLIVE.*iso") ) - NISOS=${#OGLIVE[@]} - - local downloads=() - - for i in $(seq 1 $NISOS); do + local OGLIVE NISOS i HTTPCODE ISOREL + # Usamos grep para filtrar solo los enlaces que contienen archivos ISO, pero eliminamos etiquetas HTML con sed + OGLIVE=( $(curl -k --silent "$DOWNLOADURL" | grep -oP '(?<=href=")[^"]*\.iso' | sed 's/^.*\///') ) + NISOS=${#OGLIVE[@]} + + local downloads=() + + for i in $(seq 1 $NISOS); do installed=false compatible=false - + # Obtener el nombre completo del archivo ISO - OGLIVEFILE=$(realpath "$DOWNLOADDIR/${OGLIVE[i-1]}") - + OGLIVEFILE="${OGLIVE[i-1]}" + # Extraer la distribución, revisión y arquitectura OGLIVEDIST="$(echo $OGLIVEFILE | cut -f2 -d-)" OGLIVEREV="${OGLIVEFILE##*-}"; OGLIVEREV="${OGLIVEREV%%.*}" OGLIVEKRNL="$(echo $OGLIVEFILE | cut -f3- -d-)"; OGLIVEKRNL="${OGLIVEKRNL%-$OGLIVEREV.*}" OGLIVEARCH="$(echo $OGLIVEFILE | awk -F- '{print $(NF-1)}')" + case "$OGLIVEARCH" in - i386|amd64) - OGLIVEKRNL="${OGLIVEKRNL%-$OGLIVEARCH}" ;; - *) - OGLIVEARCH="i386" ;; - esac - OGLIVEDIR="$TFTPDIR/$DEFOGLIVE-${OGLIVEKRNL%%-*}-$OGLIVEARCH-$OGLIVEREV" - OGLIVEDIR="${OGLIVEDIR/amd64-/}" - - + i386|amd64) + OGLIVEKRNL="${OGLIVEKRNL%-$OGLIVEARCH}" ;; + *) + OGLIVEARCH="i386" ;; + esac + + OGLIVEDIR="$TFTPDIR/$DEFOGLIVE-${OGLIVEKRNL%%-*}-$OGLIVEARCH-$OGLIVEREV" + OGLIVEDIR="${OGLIVEDIR/amd64-/}" + # Verificar si el ogLive está instalado y no es un directorio .old if [ -d "$OGLIVEDIR" ] && [[ ! "$OGLIVEDIR" =~ \.old$ ]]; then - installed=true - else - installed=false + installed=true + else + installed=false fi + ISOREL=${OGLIVE[i-1]##*-r}; ISOREL=${ISOREL%%.*} - [ $ISOREL -ge $MINREL ] && compatible=true + [ "$ISOREL" -ge "$MINREL" ] && compatible=true + url="$DOWNLOADURL/${OGLIVE[i-1]}" + local DATA=$(jq -n \ --arg id "$i" \ - --arg filename "${OGLIVE[i-1]}" \ + --arg filename "$OGLIVEFILE" \ --arg url "$url" \ --argjson installed "$installed" \ --argjson compatible "$compatible" \ @@ -221,6 +226,7 @@ function downloadMenu() { '{downloads: $downloads}' } + # Show a menu to select and download an ogLive ISO image from the OpenGnsys website. function download_old() { local OGLIVE NISOS i HTTPCODE TARGETFILE @@ -334,8 +340,8 @@ function install() { if [ -n "$SAMBAPASS" ]; then echo -ne "$SAMBAPASS\n$SAMBAPASS\n" | $OPENGNSYS/bin/setsmbpass "$(basename "$OGLIVEDIR")" >/dev/null 2>&1 || { echo "{\"error\": \"SET_SMBPASS_FAILED\", \"message\": \"Failed to set Samba password with SAMBAPASS.\"}"; exit 500; } - else - $OPENGNSYS/bin/setsmbpass "$(basename "$OGLIVEDIR")" >/dev/null 2>&1 || { echo "{\"error\": \"SET_SMBPASS_FAILED\", \"message\": \"Failed to set Samba password without SAMBAPASS.\"}"; exit 500; } +# else +# $OPENGNSYS/bin/setsmbpass "$(basename "$OGLIVEDIR")" >/dev/null 2>&1 || { echo "{\"error\": \"SET_SMBPASS_FAILED\", \"message\": \"Failed to set Samba password without SAMBAPASS.\"}"; exit 500; } fi find -L "$OGLIVEDIR" -type d -exec chmod 755 {} \; >/dev/null 2>&1 || { echo "{\"error\": \"CHMOD_FAILED\", \"message\": \"Failed to change permissions for directories in $OGLIVEDIR.\"}"; exit 500; } diff --git a/config/services.yaml b/config/services.yaml index 07cf001..e407b0c 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -12,6 +12,7 @@ services: autoconfigure: true # Automatically registers your services as commands, event subscribers, etc. bind: $ogCoreApiUrl: '%env(OGCORE_API_URL)%' + $ogBootIP: '%env(OGBOOT_IP)%' # makes classes in src/ available to be used as services # this creates a service per class whose id is the fully-qualified class name diff --git a/installer/config.json b/installer/config.json index ad52b3d..57e5a70 100644 --- a/installer/config.json +++ b/installer/config.json @@ -1,5 +1,6 @@ { "ogCore_ServerIP": "172.17.8.82", + "ogBoot_ServerIP": "172.17.8.37", "ogBoot_Dir": "/opt/opengnsys/ogboot", "ogBootSambaUser": "opengnsys", "ogBootSambaPass": "og" diff --git a/installer/ogboot_installer.py b/installer/ogboot_installer.py index 7c7367f..9fe6a87 100755 --- a/installer/ogboot_installer.py +++ b/installer/ogboot_installer.py @@ -24,6 +24,7 @@ with open(config_file, 'r') as f: config = json.load(f) OGCORE_IP = config["ogCore_ServerIP"] +OGBOOT_IP = config['ogBoot_ServerIP'] INSTALL_OGBOOT_TARGET = config["ogBoot_Dir"] OPENGNSYS_CLIENT_USER = config["ogBootSambaUser"] OPENGNSYS_CLIENT_PASSWD = config["ogBootSambaPass"] @@ -244,6 +245,11 @@ def og_boot_symfony_install(): with open(env_dest, 'a') as env_file: env_file.write(f"\n{ogcore_api_url}\n") logger.info(f"Added OGCORE_API_URL to {env_dest} with IP: {OGCORE_IP}") + + ogboot_ip = f'OGBOOT_IP="{OGBOOT_IP}"' + with open(env_dest, 'a') as env_file: + env_file.write(f"\n{ogboot_ip}\n") + logger.info(f"Added OGBOOT_IP to {env_dest} with IP: {OGBOOT_IP}") except Exception as e: logger.error(f"An error occurred while copying files or modifying .env: {e}") diff --git a/src/OgBootBundle/Controller/OgBootController.php b/src/OgBootBundle/Controller/OgBootController.php index 9bb3218..93766f1 100644 --- a/src/OgBootBundle/Controller/OgBootController.php +++ b/src/OgBootBundle/Controller/OgBootController.php @@ -28,7 +28,7 @@ class OgBootController private $httpClient; private $params; private $tftpbootDir; - public function __construct(CurlRequestService $curlRequestService, LoggerInterface $logger, HttpClientInterface $httpClient, ParameterBagInterface $params) + public function __construct(CurlRequestService $curlRequestService, LoggerInterface $logger, HttpClientInterface $httpClient, ParameterBagInterface $params, protected readonly string $ogBootIP) { $this->curlRequestService = $curlRequestService; $this->logger = $logger; @@ -874,8 +874,8 @@ public function getBootFiles(): JsonResponse public function getBootFile(string $mac): Response { // Ruta donde están alojados los archivos de arranque - #$directory = '/opt/ogboot/tftpboot/ipxe_scripts'; - $directory = $this->tftpbootDir . '/ipxe_scripts'; + $directory = '/opt/ogboot/tftpboot/ipxe_scripts'; + // Generar el nombre del archivo basado en la dirección MAC $mac = $this->validateAndFormatMac($mac ?? null); $fileName = "01-" . $mac; @@ -1081,7 +1081,7 @@ public function createBootFile(Request $request): JsonResponse 'resolution' => $data['resolution'] ?? '788' ]; - #$templateDir = '/opt/ogboot/tftpboot/ipxe_scripts/templates'; +# $templateDir = '/opt/ogboot/tftpboot/ipxe_scripts/templates'; $templateDir = $this->tftpbootDir . '/ipxe_scripts/templates'; $templatePath = $templateDir . '/' . $templateName; @@ -1097,14 +1097,14 @@ public function createBootFile(Request $request): JsonResponse // Construcción de los argumentos del kernel $kernelArgs = 'ro boot=oginit quiet splash irqpoll acpi=on og2nd=sqfs ogprotocol=smb ogactiveadmin=true ogdebug=true ogtmpfs=15 ' . - 'oglivedir=' . $parameters['oglivedir'] . ' ' . + 'oglivedir=${ISODIR}' . ' ' . 'LANG=' . $parameters['LANG'] . ' ' . 'ip=' . $parameters['ip'] . ':' . $serverIp . ':' . $parameters['router'] . ':' . $parameters['netmask'] . ':' . $parameters['computer_name'] . ':' . $parameters['netiface'] . ':none ' . 'group=' . str_replace(' ', '_', trim($parameters['group'])) . ' ' . 'ogrepo=' . $parameters['ogrepo'] . ' ' . - 'oglive=' . $serverIp . ' ' . - 'oglog=' . $serverIp . ' ' . - 'ogshare=' . $serverIp . ' ' . + 'oglive=' . $parameters['oglive'] . ' ' . + 'oglog=' . $parameters['oglog'] . ' ' . + 'ogshare=' . $parameters['ogshare'] . ' ' . 'ogprof=' . ($parameters['ogprof'] === 'true' ? 'true' : 'false') . ' ' . (!empty($parameters['hardprofile']) ? 'hardprofile=' . str_replace(' ', '_', trim($parameters['hardprofile'])) . ' ' : '') . (!empty($parameters['ogntp']) ? 'ogntp=' . $parameters['ogntp'] . ' ' : '') . @@ -1114,12 +1114,24 @@ public function createBootFile(Request $request): JsonResponse (is_numeric($parameters['resolution']) && $parameters['resolution'] <= 999 ? 'vga=' . $parameters['resolution'] : (strpos($parameters['resolution'], ':') !== false ? 'video=' . $parameters['resolution'] : ' ' . $parameters['resolution'])); - // Insertar un comentario al principio del archivo con el nombre de la plantilla usada - $pxeContent = "#Template: $templateName\n" . - str_replace(['__INFOHOST__', '__SERVERIP__', '__OGLIVE__'], [$kernelArgs, $serverIp, $ogLiveDir], $templateContent); + // Reemplazar las variables en el contenido de la plantilla + $pxeContent = str_replace( + ['__INFOHOST__', '__SERVERIP__', '__OGLIVE__'], + [$kernelArgs, $this->ogBootIP, $ogLiveDir], + $templateContent + ); + + // Insertar el comentario con el nombre de la plantilla después de #!ipxe + if (strpos($pxeContent, '#!ipxe') === 0) { + // Añadir el comentario después de #!ipxe + $pxeContent = "#!ipxe\n#Template: $templateName\n" . substr($pxeContent, 6); + } else { + // Si no encuentra #!ipxe, agregarlo al principio seguido del comentario + $pxeContent = "#!ipxe\n#Template: $templateName\n" . $pxeContent; + } // Nombre del archivo PXE basado en la MAC - $pxeFileName = '01-' . $mac; + $pxeFileName = '01-' . $mac; // Ruta para guardar el archivo PXE #$pxeFilePath = '/opt/ogboot/tftpboot/ipxe_scripts/' . $pxeFileName; @@ -1131,7 +1143,7 @@ public function createBootFile(Request $request): JsonResponse // Retornar la plantilla creada en el formato solicitado return new JsonResponse([ - 'success' => 'Plantilla creada con exito', + 'success' => 'Plantilla creada con éxito', 'message' => $pxeContent ], Response::HTTP_OK); } @@ -1261,7 +1273,7 @@ public function deleteBootFile(string $mac): Response */ public function getAllTemplates(): JsonResponse { - #$templateDir = '/opt/ogboot/tftpboot/ipxe_scripts/templates'; +# $templateDir = '/opt/ogboot/tftpboot/ipxe_scripts/templates'; $templateDir = $this->tftpbootDir . '/ipxe_scripts/templates'; // Verificar si el directorio existe if (!is_dir($templateDir)) {