From eccc0a3cf3c25fc7da6e7ddb1f9292d393a2eb8d Mon Sep 17 00:00:00 2001 From: Ganga Ram Date: Tue, 10 Dec 2024 00:07:26 +0400 Subject: [PATCH] Central database for internal IP addresses - Implements a common database for internal IP addresses - All VMs read this to get it's assigned IP - Resolves: https://jira.tii.ae/browse/SSRCSP-5674 Signed-off-by: Ganga Ram --- modules/common/networking/hosts-entries.nix | 63 +++++++++++++++++++ modules/common/networking/hosts.nix | 49 +-------------- .../virtualization/microvm/adminvm.nix | 3 +- .../microvm/virtualization/microvm/appvm.nix | 9 ++- .../virtualization/microvm/audiovm.nix | 3 +- .../microvm/virtualization/microvm/guivm.nix | 4 +- .../virtualization/microvm/idsvm/idsvm.nix | 3 +- .../microvm/virtualization/microvm/netvm.nix | 3 +- modules/reference/appvms/appflowy.nix | 8 ++- modules/reference/appvms/business.nix | 4 ++ modules/reference/appvms/chromium.nix | 3 + modules/reference/appvms/comms.nix | 4 ++ modules/reference/appvms/gala.nix | 8 ++- modules/reference/appvms/google-chrome.nix | 3 + modules/reference/appvms/zathura.nix | 8 ++- .../dendrite-pinecone/dendrite-config.nix | 6 +- 16 files changed, 122 insertions(+), 59 deletions(-) create mode 100644 modules/common/networking/hosts-entries.nix diff --git a/modules/common/networking/hosts-entries.nix b/modules/common/networking/hosts-entries.nix new file mode 100644 index 000000000..ad0f9820b --- /dev/null +++ b/modules/common/networking/hosts-entries.nix @@ -0,0 +1,63 @@ +let + hostsEntries = [ + { + ip = 1; + name = "net-vm"; + } + { + ip = 2; + name = "ghaf-host"; + } + { + ip = 3; + name = "gui-vm"; + } + { + ip = 4; + name = "ids-vm"; + } + { + ip = 5; + name = "audio-vm"; + } + { + ip = 10; + name = "admin-vm"; + } + { + ip = 100; + name = "chrome-vm"; + } + { + ip = 101; + name = "gala-vm"; + } + { + ip = 102; + name = "zathura-vm"; + } + { + ip = 103; + name = "comms-vm"; + } + { + ip = 104; + name = "business-vm"; + } + ]; + # Create a lookup map from entries + lookupMap = builtins.listToAttrs ( + map (entry: { + inherit (entry) name; + value = entry.ip; + }) hostsEntries + ); + + # Function to find the corresponding IP address by name + ipByName = name: lookupMap.${name}; + +in +{ + inherit ipByName; + inherit hostsEntries; +} diff --git a/modules/common/networking/hosts.nix b/modules/common/networking/hosts.nix index a8ad1c0d6..1f5414173 100644 --- a/modules/common/networking/hosts.nix +++ b/modules/common/networking/hosts.nix @@ -36,53 +36,8 @@ let # debug network hosts are post-fixed: -debug ipBase = "192.168.100"; debugBase = "192.168.101"; - hostsEntries = [ - { - ip = 1; - name = "net-vm"; - } - { - ip = 2; - name = "ghaf-host"; - } - { - ip = 3; - name = "gui-vm"; - } - { - ip = 4; - name = "ids-vm"; - } - { - ip = 5; - name = "audio-vm"; - } - { - ip = 10; - name = "admin-vm"; - } - { - ip = 100; - name = "chrome-vm"; - } - { - ip = 101; - name = "gala-vm"; - } - { - ip = 102; - name = "zathura-vm"; - } - { - ip = 103; - name = "comms-vm"; - } - { - ip = 104; - name = "business-vm"; - } - ]; - + hosts-entries = import ./hosts-entries.nix; + inherit (hosts-entries) hostsEntries; mkHostEntry = { ip, name }: { diff --git a/modules/microvm/virtualization/microvm/adminvm.nix b/modules/microvm/virtualization/microvm/adminvm.nix index 9e235a9ca..601427050 100644 --- a/modules/microvm/virtualization/microvm/adminvm.nix +++ b/modules/microvm/virtualization/microvm/adminvm.nix @@ -7,6 +7,7 @@ let vmName = "admin-vm"; macAddress = "02:00:00:AD:01:01"; isLoggingEnabled = config.ghaf.logging.client.enable; + hostsEntries = import ../../../common/networking/hosts-entries.nix; adminvmBaseConfiguration = { imports = [ @@ -19,7 +20,7 @@ let vmName macAddress ; - internalIP = 10; + internalIP = hostsEntries.ipByName vmName; }) # We need to retrieve mac address and start log aggregator ../../../common/logging/hw-mac-retrieve.nix diff --git a/modules/microvm/virtualization/microvm/appvm.nix b/modules/microvm/virtualization/microvm/appvm.nix index c0d40dc14..469e63ff1 100644 --- a/modules/microvm/virtualization/microvm/appvm.nix +++ b/modules/microvm/virtualization/microvm/appvm.nix @@ -29,8 +29,7 @@ let inputs.self.nixosModules.givc-appvm (import ./common/vm-networking.nix { inherit config lib vmName; - inherit (vm) macAddress; - internalIP = vmIndex + 100; + inherit (vm) macAddress internalIP; }) ./common/ghaf-audio.nix @@ -240,6 +239,12 @@ in ''; type = types.str; }; + internalIP = mkOption { + description = '' + Internal IP address for this AppVM + ''; + type = types.int; + }; ramMb = mkOption { description = '' Amount of RAM for this AppVM diff --git a/modules/microvm/virtualization/microvm/audiovm.nix b/modules/microvm/virtualization/microvm/audiovm.nix index 64c47cab1..35041acad 100644 --- a/modules/microvm/virtualization/microvm/audiovm.nix +++ b/modules/microvm/virtualization/microvm/audiovm.nix @@ -13,6 +13,7 @@ let macAddress = "02:00:00:03:03:03"; isGuiVmEnabled = config.ghaf.virtualization.microvm.guivm.enable; has_acpi_path = config.ghaf.hardware.definition.audio.acpiPath != null; + hostsEntries = import ../../../common/networking/hosts-entries.nix; sshKeysHelper = pkgs.callPackage ../../../../packages/ssh-keys-helper { inherit pkgs; @@ -30,7 +31,7 @@ let vmName macAddress ; - internalIP = 5; + internalIP = hostsEntries.ipByName vmName; }) ./common/storagevm.nix ../../../common/logging/client.nix diff --git a/modules/microvm/virtualization/microvm/guivm.nix b/modules/microvm/virtualization/microvm/guivm.nix index 7ddedd443..4f6879e64 100644 --- a/modules/microvm/virtualization/microvm/guivm.nix +++ b/modules/microvm/virtualization/microvm/guivm.nix @@ -11,6 +11,8 @@ let vmName = "gui-vm"; macAddress = "02:00:00:02:02:02"; inherit (import ../../../../lib/launcher.nix { inherit pkgs lib; }) rmDesktopEntries; + hostsEntries = import ../../../common/networking/hosts-entries.nix; + guivmBaseConfiguration = { imports = [ inputs.impermanence.nixosModules.impermanence @@ -22,7 +24,7 @@ let vmName macAddress ; - internalIP = 3; + internalIP = hostsEntries.ipByName vmName; }) ./common/storagevm.nix diff --git a/modules/microvm/virtualization/microvm/idsvm/idsvm.nix b/modules/microvm/virtualization/microvm/idsvm/idsvm.nix index 4354e26c0..726bb5684 100644 --- a/modules/microvm/virtualization/microvm/idsvm/idsvm.nix +++ b/modules/microvm/virtualization/microvm/idsvm/idsvm.nix @@ -9,6 +9,7 @@ let configHost = config; vmName = "ids-vm"; + hostsEntries = import ../../../../common/networking/hosts-entries.nix; macAddress = "02:00:00:01:01:02"; idsvmBaseConfiguration = { imports = [ @@ -19,7 +20,7 @@ let vmName macAddress ; - internalIP = 4; + internalIP = hostsEntries.ipByName vmName; }) ( { lib, ... }: diff --git a/modules/microvm/virtualization/microvm/netvm.nix b/modules/microvm/virtualization/microvm/netvm.nix index 9fa2830a5..271dda0b7 100644 --- a/modules/microvm/virtualization/microvm/netvm.nix +++ b/modules/microvm/virtualization/microvm/netvm.nix @@ -12,6 +12,7 @@ let macAddress = "02:00:00:01:01:01"; isGuiVmEnabled = config.ghaf.virtualization.microvm.guivm.enable; + hostsEntries = import ../../../common/networking/hosts-entries.nix; sshKeysHelper = pkgs.callPackage ../../../../packages/ssh-keys-helper { inherit pkgs; @@ -29,7 +30,7 @@ let vmName macAddress ; - internalIP = 1; + internalIP = hostsEntries.ipByName vmName; isGateway = true; }) diff --git a/modules/reference/appvms/appflowy.nix b/modules/reference/appvms/appflowy.nix index 7a7604abf..f6312409f 100644 --- a/modules/reference/appvms/appflowy.nix +++ b/modules/reference/appvms/appflowy.nix @@ -7,10 +7,16 @@ config, ... }: -{ +let + hostsEntries = import ../../common/networking/hosts-entries.nix; name = "appflowy"; + vmname = name + "-vm"; +in +rec { + inherit name; packages = [ pkgs.appflowy ]; macAddress = "02:00:00:03:08:01"; + internalIP = hostsEntries.ipByName vmname; ramMb = 768; cores = 1; extraModules = [ diff --git a/modules/reference/appvms/business.nix b/modules/reference/appvms/business.nix index 14f6064d9..9f4ccbfd4 100644 --- a/modules/reference/appvms/business.nix +++ b/modules/reference/appvms/business.nix @@ -11,6 +11,8 @@ let inherit (lib) mkIf optionalString; #TODO: Move this to a common place name = "business"; + hostsEntries = import ../../common/networking/hosts-entries.nix; + vmname = name + "-vm"; proxyUserName = "proxy-user"; proxyGroupName = "proxy-admin"; tiiVpnAddr = "151.253.154.18"; @@ -114,6 +116,8 @@ in # TODO create a repository of mac addresses to avoid conflicts macAddress = "02:00:00:03:10:01"; + internalIP = hostsEntries.ipByName vmname; + ramMb = 6144; cores = 4; extraModules = [ diff --git a/modules/reference/appvms/chromium.nix b/modules/reference/appvms/chromium.nix index 166ed6a7d..37990feff 100644 --- a/modules/reference/appvms/chromium.nix +++ b/modules/reference/appvms/chromium.nix @@ -9,6 +9,8 @@ }: let name = "chromium"; + hostsEntries = import ../../common/networking/hosts-entries.nix; + vmname = name + "-vm"; in { name = "${name}"; @@ -17,6 +19,7 @@ in ] ++ lib.optional config.ghaf.development.debug.tools.enable pkgs.alsa-utils; # TODO create a repository of mac addresses to avoid conflicts macAddress = "02:00:00:03:05:01"; + internalIP = hostsEntries.ipByName vmname; ramMb = 6144; cores = 4; extraModules = [ diff --git a/modules/reference/appvms/comms.nix b/modules/reference/appvms/comms.nix index 54c6f8dd8..3235ff2bc 100644 --- a/modules/reference/appvms/comms.nix +++ b/modules/reference/appvms/comms.nix @@ -9,6 +9,8 @@ }: let name = "comms"; + hostsEntries = import ../../common/networking/hosts-entries.nix; + vmname = name + "-vm"; inherit (lib) hasAttr optionals; dendrite-pinecone = pkgs.callPackage ../../../packages/dendrite-pinecone { }; isDendritePineconeEnabled = @@ -28,6 +30,8 @@ in pkgs.tcpdump ] ++ pkgs.lib.optionals isDendritePineconeEnabled [ dendrite-pinecone ]; macAddress = "02:00:00:03:09:01"; + internalIP = hostsEntries.ipByName vmname; + ramMb = 4096; cores = 4; extraModules = [ diff --git a/modules/reference/appvms/gala.nix b/modules/reference/appvms/gala.nix index 6ae25a08a..1f44c300d 100644 --- a/modules/reference/appvms/gala.nix +++ b/modules/reference/appvms/gala.nix @@ -7,10 +7,16 @@ config, ... }: -{ +let name = "gala"; + hostsEntries = import ../../common/networking/hosts-entries.nix; + vmname = name + "-vm"; +in +{ + inherit name; packages = [ pkgs.gala-app ]; macAddress = "02:00:00:03:06:01"; + internalIP = hostsEntries.ipByName vmname; ramMb = 1536; cores = 2; extraModules = [ diff --git a/modules/reference/appvms/google-chrome.nix b/modules/reference/appvms/google-chrome.nix index 4d6abaf4b..9e12f0c5d 100644 --- a/modules/reference/appvms/google-chrome.nix +++ b/modules/reference/appvms/google-chrome.nix @@ -9,6 +9,8 @@ }: let name = "chrome"; + hostsEntries = import ../../common/networking/hosts-entries.nix; + vmname = name + "-vm"; in { name = "${name}"; @@ -17,6 +19,7 @@ in ] ++ lib.optional config.ghaf.development.debug.tools.enable pkgs.alsa-utils; # TODO create a repository of mac addresses to avoid conflicts macAddress = "02:00:00:03:11:01"; + internalIP = hostsEntries.ipByName vmname; ramMb = 6144; cores = 4; extraModules = [ diff --git a/modules/reference/appvms/zathura.nix b/modules/reference/appvms/zathura.nix index a62e7ecd0..fd4e61897 100644 --- a/modules/reference/appvms/zathura.nix +++ b/modules/reference/appvms/zathura.nix @@ -7,13 +7,19 @@ config, ... }: -{ +let name = "zathura"; + hostsEntries = import ../../common/networking/hosts-entries.nix; + vmname = name + "-vm"; +in +{ + inherit name; packages = [ pkgs.zathura pkgs.pqiv ]; macAddress = "02:00:00:03:07:01"; + internalIP = hostsEntries.ipByName vmname; ramMb = 512; cores = 1; extraModules = [ diff --git a/modules/reference/services/dendrite-pinecone/dendrite-config.nix b/modules/reference/services/dendrite-pinecone/dendrite-config.nix index 4f8409015..92ca9cf74 100644 --- a/modules/reference/services/dendrite-pinecone/dendrite-config.nix +++ b/modules/reference/services/dendrite-pinecone/dendrite-config.nix @@ -4,6 +4,8 @@ { config.ghaf.reference.services.dendrite-pinecone = let + hostsEntries = import ../../../common/networking/hosts-entries.nix; + vmname = "net-vm"; externalNic = let firstPciWifiDevice = lib.head config.ghaf.hardware.definition.network.pciDevices; @@ -15,9 +17,9 @@ vmNetworking = import ../../../microvm/virtualization/microvm/common/vm-networking.nix { inherit config; inherit lib; - vmName = "net-vm"; + vmName = vmname; inherit (config.microvm.net-vm) macAddress; - internalIP = 1; + internalIP = hostsEntries.ipByName vmname; }; in "${lib.head vmNetworking.networking.nat.internalInterfaces}";