refs #981 #984 adds a minimal kea configuration to the installer, adds a json with the interfaces and the ogbootIp, adds gateway configuration when a subnet is create o modified in the controller
testing/og-dhcp-API/pipeline/head There was a failure building this commit Details

nginx_conf
Luis Gerardo Romero Garcia 2024-10-18 12:49:42 +02:00
parent 0f8870e003
commit 2946fc1d19
3 changed files with 211 additions and 108 deletions

View File

@ -0,0 +1,4 @@
{
"interfaces": ["eth0", "eth1"],
"ogbootIP": "172.17.8.37"
}

View File

@ -450,6 +450,73 @@ modify_php_fpm_config() {
} }
configure_kea() {
# Ruta del archivo config_ogdhcp.json proporcionado por el usuario
CONFIG_FILE="config_ogdhcp.json"
# Verificar si jq está instalado
if ! command -v jq &> /dev/null; then
echo "jq no está instalado. Por favor, instala jq para continuar."
exit 1
fi
# Verificar si el archivo de configuración existe
if [ ! -f "$CONFIG_FILE" ]; then
echo "El archivo $CONFIG_FILE no se encuentra. Asegúrate de que esté disponible antes de la instalación."
exit 1
fi
# Leer los parámetros del archivo JSON usando jq
INTERFACES=$(jq -r '.interfaces[]' "$CONFIG_FILE")
OGBOOT_IP=$(jq -r '.ogbootIP' "$CONFIG_FILE")
# Crear la configuración mínima de Kea DHCP
KEA_CONFIG="/etc/kea/kea-dhcp4.conf"
# Hacer una copia de seguridad del archivo kea-dhcp4.conf si ya existe
if [ -f "$KEA_CONFIG" ]; then
cp "$KEA_CONFIG" "$KEA_CONFIG.backup"
echo "Se ha creado una copia de seguridad del archivo de configuración actual en $KEA_CONFIG.backup"
fi
# Generar la configuración mínima para Kea DHCP
cat > "$KEA_CONFIG" << EOL
{
"Dhcp4": {
"interfaces-config": {
"interfaces": [ $(
for interface in $INTERFACES; do
echo "\"$interface\""
done | paste -sd "," -
) ]
},
"client-classes": [
{
"name": "UEFI-64",
"test": "not substring(option[60].hex,0,20) == 'PXEClient:Arch:00000'",
"boot-file-name": "ipxe.efi",
"next-server": "$OGBOOT_IP"
},
{
"name": "Legacy",
"test": "substring(option[60].hex,0,20) == 'PXEClient:Arch:00000'",
"boot-file-name": "undionly.kpxe",
"next-server": "$OGBOOT_IP"
}
],
"control-socket": {
"socket-name": "/run/kea/kea4-ctrl-socket",
"socket-type": "unix"
}
}
}
EOL
echo "Se ha generado la configuración mínima de Kea DHCP en $KEA_CONFIG"
}
##################################################################### #####################################################################
####### Algunas funciones útiles de propósito general: ####### Algunas funciones útiles de propósito general:
##################################################################### #####################################################################
@ -555,6 +622,13 @@ if [ $? -ne 0 ]; then
exit 1 exit 1
fi fi
configure_kea
if [ $? -ne 0 ]; then
errorAndLog "Error configuring Kea DHCP initial configuration"
exit 1
fi
# install_kea # install_kea
# install_php # install_php
# install_composer # install_composer

View File

@ -377,12 +377,23 @@ public function addDhcpSubnet(Request $request): JsonResponse
try { try {
$response = $this->curlKeaService->executeCurlCommand('config-get'); $response = $this->curlKeaService->executeCurlCommand('config-get');
$subnetName = $address . '/' . $this->curlKeaService->convertMaskToCIDR($mask); $subnetName = $address . '/' . $this->curlKeaService->convertMaskToCIDR($mask);
// Crear el campo option-data para la puerta de enlace (gateway)
$gatewayIP = substr($address, 0, strrpos($address, '.')) . '.1';
$newSubnet = [ $newSubnet = [
"id" => $subnetId, "id" => $subnetId,
"subnet" => $subnetName, "subnet" => $subnetName,
"next-server" => $nextServer, "next-server" => $nextServer,
"boot-file-name" => $bootFileName, "boot-file-name" => $bootFileName,
"reservations" => [] "reservations" => [],
"option-data" => [
[
"name" => "routers",
"code" => 3,
"data" => $gatewayIP
]
]
]; ];
if (!isset($response[0]['arguments']['Dhcp4']['subnet4'])) { if (!isset($response[0]['arguments']['Dhcp4']['subnet4'])) {
@ -454,6 +465,7 @@ public function addDhcpSubnet(Request $request): JsonResponse
} }
} }
/** /**
* @OA\Delete( * @OA\Delete(
* path="/ogdhcp/v1/subnets/{subnetId}", * path="/ogdhcp/v1/subnets/{subnetId}",
@ -724,17 +736,30 @@ public function addDhcpSubnet(Request $request): JsonResponse
$responseError = "La subred con el id '$subnetId' no existe"; $responseError = "La subred con el id '$subnetId' no existe";
return new JsonResponse(['error' => $responseError], 400); return new JsonResponse(['error' => $responseError], 400);
} else { } else {
// Calcular el gateway añadiendo .1 al final de la subred
$gateway = preg_replace('/\d+$/', '1', $address);
// Modificar la subred existente
$response[0]['arguments']['Dhcp4']['subnet4'][$subnetIndex] = [ $response[0]['arguments']['Dhcp4']['subnet4'][$subnetIndex] = [
"id" => $subnetId, "id" => $subnetId,
"subnet" => $subnetName, "subnet" => $subnetName,
"next-server" => $nextServer, "next-server" => $nextServer,
"boot-file-name" => $bootFileName, "boot-file-name" => $bootFileName,
"reservations" => [] "reservations" => [],
"option-data" => [
[
"name" => "routers",
"code" => 3,
"data" => $gateway
]
]
]; ];
// Eliminar el campo 'hash' si existe // Eliminar el campo 'hash' si existe
if (isset($response[0]['arguments']['hash'])) { if (isset($response[0]['arguments']['hash'])) {
unset($response[0]['arguments']['hash']); unset($response[0]['arguments']['hash']);
} }
$array_encoded = json_encode($response[0]['arguments']); $array_encoded = json_encode($response[0]['arguments']);
$configurationParsed = str_replace('\\', '', $array_encoded); $configurationParsed = str_replace('\\', '', $array_encoded);
$configuration = json_decode($configurationParsed); $configuration = json_decode($configurationParsed);
@ -1035,7 +1060,7 @@ public function addDhcpHost(Request $request, $subnetId): JsonResponse
} }
} }
if ($createdHost) { if ($createdHost) {
return new JsonResponse(['success' => "Host agregado correctamente", 'newHost' => $createdHost], 200); return new JsonResponse(['success' => "Host agregado correctamente", 'message' => $createdHost], 200);
} else { } else {
return new JsonResponse(['error' => "No se pudo encontrar el host recién creado"], 400); return new JsonResponse(['error' => "No se pudo encontrar el host recién creado"], 400);
} }