netplan_yaml
This commit is contained in:
parent
120f7e7c5c
commit
0803645bac
|
|
@ -138,13 +138,25 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||||
'version' => 2,
|
'version' => 2,
|
||||||
'renderer' => 'networkd',
|
'renderer' => 'networkd',
|
||||||
'ethernets' => [],
|
'ethernets' => [],
|
||||||
|
'vlans' => new stdClass()
|
||||||
]
|
]
|
||||||
];
|
];
|
||||||
|
|
||||||
if (!empty($netplan['network']['vlans'])) {
|
/* ---------- PRIMARY ---------- */
|
||||||
$netplan['network']['vlans'] = $netplan['network']['vlans'];
|
if (
|
||||||
} else {
|
$data['primary']['mode'] !== 'disabled' ||
|
||||||
$netplan['network']['vlans'] = new stdClass(); // forces {}
|
$data['primary']['modev6'] !== 'disabled'
|
||||||
|
) {
|
||||||
|
$netplan['network']['ethernets'][$iface] =
|
||||||
|
build_interface($data['primary'], 'primary');
|
||||||
|
|
||||||
|
/* ---------- SECONDARY ---------- */
|
||||||
|
} elseif (
|
||||||
|
$data['secondary']['mode'] !== 'disabled' ||
|
||||||
|
$data['secondary']['modev6'] !== 'disabled'
|
||||||
|
) {
|
||||||
|
$netplan['network']['ethernets'][$iface] =
|
||||||
|
build_interface($data['secondary'], 'secondary');
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (['primary', 'secondary'] as $type) {
|
foreach (['primary', 'secondary'] as $type) {
|
||||||
|
|
@ -172,7 +184,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
file_put_contents('/var/www/50-cloud-init.yaml', netplan_yaml($netplan));
|
file_put_contents('/var/www/50-cloud-init.yaml', netplan_yaml(generate_netplan($data, $iface)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -84,8 +84,13 @@ function build_interface(array $cfg, string $type): array
|
||||||
$out['dhcp4'] = true;
|
$out['dhcp4'] = true;
|
||||||
} elseif ($cfg['mode'] === 'static') {
|
} elseif ($cfg['mode'] === 'static') {
|
||||||
$out['dhcp4'] = false;
|
$out['dhcp4'] = false;
|
||||||
$out['addresses'][] = $cfg["network_{$type}_ip"];
|
$out['addresses'] = [
|
||||||
$out['gateway4'] = $cfg["network_{$type}_gateway"];
|
$cfg["network_{$type}_ip"] // already CIDR
|
||||||
|
];
|
||||||
|
|
||||||
|
if ($cfg["network_{$type}_gateway"] !== '') {
|
||||||
|
$out['gateway4'] = $cfg["network_{$type}_gateway"];
|
||||||
|
}
|
||||||
|
|
||||||
$dns = array_filter([
|
$dns = array_filter([
|
||||||
$cfg["network_{$type}_dns1"],
|
$cfg["network_{$type}_dns1"],
|
||||||
|
|
@ -110,11 +115,18 @@ function build_interface(array $cfg, string $type): array
|
||||||
$out['dhcp6'] = false;
|
$out['dhcp6'] = false;
|
||||||
$out['accept-ra'] = false;
|
$out['accept-ra'] = false;
|
||||||
|
|
||||||
$out['addresses'][] =
|
if ($cfg["network_{$type}_ipv6"] !== '' &&
|
||||||
$cfg["network_{$type}_ipv6"] . '/' .
|
$cfg["network_{$type}_ipv6_prefix"] !== '') {
|
||||||
$cfg["network_{$type}_ipv6_prefix"];
|
|
||||||
|
|
||||||
$out['gateway6'] = $cfg["network_{$type}_ipv6_gateway"];
|
$out['addresses'][] =
|
||||||
|
$cfg["network_{$type}_ipv6"] . '/' .
|
||||||
|
$cfg["network_{$type}_ipv6_prefix"];
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($cfg["network_{$type}_ipv6_gateway"] !== '') {
|
||||||
|
$out['gateway6'] =
|
||||||
|
$cfg["network_{$type}_ipv6_gateway"];
|
||||||
|
}
|
||||||
|
|
||||||
$dns6 = array_filter([
|
$dns6 = array_filter([
|
||||||
$cfg["network_{$type}_ipv6_dns1"],
|
$cfg["network_{$type}_ipv6_dns1"],
|
||||||
|
|
@ -132,6 +144,36 @@ function build_interface(array $cfg, string $type): array
|
||||||
|
|
||||||
return $out;
|
return $out;
|
||||||
}
|
}
|
||||||
|
function generate_netplan(array $data, string $iface): array
|
||||||
|
{
|
||||||
|
$netplan = [
|
||||||
|
'network' => [
|
||||||
|
'version' => 2,
|
||||||
|
'renderer' => 'networkd',
|
||||||
|
'ethernets' => [],
|
||||||
|
'vlans' => new stdClass()
|
||||||
|
]
|
||||||
|
];
|
||||||
|
|
||||||
|
/* PRIMARY HAS PRIORITY */
|
||||||
|
if (
|
||||||
|
$data['primary']['mode'] !== 'disabled' ||
|
||||||
|
$data['primary']['modev6'] !== 'disabled'
|
||||||
|
) {
|
||||||
|
$netplan['network']['ethernets'][$iface] =
|
||||||
|
build_interface($data['primary'], 'primary');
|
||||||
|
|
||||||
|
/* SECONDARY ONLY IF PRIMARY DISABLED */
|
||||||
|
} elseif (
|
||||||
|
$data['secondary']['mode'] !== 'disabled' ||
|
||||||
|
$data['secondary']['modev6'] !== 'disabled'
|
||||||
|
) {
|
||||||
|
$netplan['network']['ethernets'][$iface] =
|
||||||
|
build_interface($data['secondary'], 'secondary');
|
||||||
|
}
|
||||||
|
|
||||||
|
return $netplan;
|
||||||
|
}
|
||||||
|
|
||||||
function netplan_yaml(array $data, int $indent = 0): string
|
function netplan_yaml(array $data, int $indent = 0): string
|
||||||
{
|
{
|
||||||
|
|
@ -140,25 +182,21 @@ function netplan_yaml(array $data, int $indent = 0): string
|
||||||
|
|
||||||
foreach ($data as $key => $value) {
|
foreach ($data as $key => $value) {
|
||||||
|
|
||||||
/* Empty map */
|
|
||||||
if ($value instanceof stdClass) {
|
if ($value instanceof stdClass) {
|
||||||
$out .= "{$pad}{$key}: {}\n";
|
$out .= "{$pad}{$key}: {}\n";
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Boolean */
|
|
||||||
if (is_bool($value)) {
|
if (is_bool($value)) {
|
||||||
$out .= "{$pad}{$key}: " . ($value ? 'true' : 'false') . "\n";
|
$out .= "{$pad}{$key}: " . ($value ? 'true' : 'false') . "\n";
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Scalar */
|
|
||||||
if (!is_array($value)) {
|
if (!is_array($value)) {
|
||||||
$out .= "{$pad}{$key}: {$value}\n";
|
$out .= "{$pad}{$key}: {$value}\n";
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Numeric array (list) */
|
|
||||||
if (array_keys($value) === range(0, count($value) - 1)) {
|
if (array_keys($value) === range(0, count($value) - 1)) {
|
||||||
$out .= "{$pad}{$key}:\n";
|
$out .= "{$pad}{$key}:\n";
|
||||||
foreach ($value as $item) {
|
foreach ($value as $item) {
|
||||||
|
|
@ -169,7 +207,6 @@ function netplan_yaml(array $data, int $indent = 0): string
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Mapping */
|
|
||||||
$out .= "{$pad}{$key}:\n";
|
$out .= "{$pad}{$key}:\n";
|
||||||
$out .= netplan_yaml($value, $indent + 1);
|
$out .= netplan_yaml($value, $indent + 1);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue