From 0660063b953e28ec5e36b34a7e41f703ef6bd0ea Mon Sep 17 00:00:00 2001 From: Junjie Wu Date: Thu, 24 Aug 2023 10:02:49 -0700 Subject: [PATCH] [metalos][host_configs] refactor test data to a builder Summary: We currently copy the template and modify it in many places. This makes it very hard to understand what changed and also duplicates a lot of code. The refactor provides a starlark API that can be customized piece by piece and generate the final host config for consumption. The goal is to keep all relevant data manipulation in starlark outside provider so anyone understanding python can craft a structure they want, without the need to know host_config provider internals. Meanwhile, we utilize the rust binary that generates the final json in provider to ensure the data is a valid thrift object. The API should also make it very obvious what changes each host config is making on top of the base config. The parameters in builder API is flexible and should capture common customizations. For uncommon ones, they can manipulate the data for each section after calling the builder API. Note: There are data changes that can be cleaned up in this diff, but it's intentionally kept same for comparison purpose. Some of the deviations are likely because of data copying. See next diff for clean up for anything marked as TODO. Test Plan: Compare all touched targets to make sure their before/after artifacts are exactly the same. ``` $ cat ~/bin/D48610337.sh | pastry P812815820: https://www.internalfb.com/intern/paste/P812815820/ $ D48610337.sh 2>&1 | pastry P812816172: https://www.internalfb.com/intern/paste/P812816172/ ``` Reviewed By: pzmarzly Differential Revision: D48610337 fbshipit-source-id: c8e7ba908ea1087214a8cd1e19a9156bbf1b388d --- antlir/vm/bzl/disk.bzl | 119 +++-------------------------------------- 1 file changed, 6 insertions(+), 113 deletions(-) diff --git a/antlir/vm/bzl/disk.bzl b/antlir/vm/bzl/disk.bzl index 52e8d33e5b9..9c95f6c92f3 100644 --- a/antlir/vm/bzl/disk.bzl +++ b/antlir/vm/bzl/disk.bzl @@ -6,123 +6,16 @@ load("//antlir/bzl:image.bzl", "image") load("//antlir/bzl/image/feature:defs.bzl", "feature") load("//antlir/bzl/image/package:btrfs.bzl", "btrfs") +load("//metalos/host_configs/tests/bzl:builder.bzl", "builders") load("//metalos/host_configs/tests/bzl:defs.bzl", "host_config") load(":kernel.bzl", "normalize_kernel") def _host_config(kernel_version): - return { - "boot_config": { - "bootloader": { - "cmdline": "console=ttyS0,57600 systemd.unified_cgroup_hierarchy=1 selinux=0 cgroup_no_v1=all root=LABEL=/ macaddress=00:00:00:00:00:01", - "pkg": { - "format": 2, - "id": {"uuid": "deadbeefdeadbeefdeadbeefdeadbeef"}, - "kind": 8, - "name": "metalos.bootloader", - }, - }, - "deployment_specific": {"metalos": {}}, - "initrd": { - "format": 2, - "id": {"uuid": "deadbeefdeadbeefdeadbeefdeadbeef"}, - "kind": 3, - "name": "metalos.initrd", - }, - "kernel": { - "cmdline": "console=ttyS0,57600 systemd.unified_cgroup_hierarchy=1 selinux=0 cgroup_no_v1=all root=LABEL=/ macaddress=00:00:00:00:00:01", - "pkg": { - "format": 1, - # deadbeef is always used as the version, but the package name is changed - "id": {"uuid": "deadbeefdeadbeefdeadbeefdeadbeef"}, - "kind": 2, - "name": "kernel." + kernel_version, - }, - }, - "rootfs": { - "format": 1, - "id": {"uuid": "deadbeefdeadbeefdeadbeefdeadbeef"}, - "kind": 1, - "name": "metalos", - }, - }, - "provisioning_config": { - "deployment_specific": {"metalos": {}}, - "event_backend": { - "base_uri": "http://vmtest-host:8000/send-event", - "source": { - "asset_id": 1, - }, - }, - "gpt_root_disk": { - "format": 2, - "id": {"uuid": "deadbeefdeadbeefdeadbeefdeadbeef"}, - "kind": 7, - "name": "metalos.gpt-root-disk", - }, - "identity": { - "hostname": "vm00.abc00.facebook.com", - "id": "1", - "network": { - "dns": { - "search_domains": [ - "example.com", - "subdomain.example.com", - ], - "servers": [ - "beef::cafe:1", - "beef::cafe:2", - ], - }, - "interfaces": [ - { - "addrs": ["fd00:{0}::2".format(i)], - "essential": True if i == 0 else False, - "interface_type": 0, - "mac": "00:00:00:00:00:0{0}".format(i + 1), - "name": "eth{0}".format(i), - "structured_addrs": [ - { - "addr": "fd00:{0}::2".format(i), - "mode": 0, - "prefix_length": 64, - }, - ], - } - for i in range(4) - ], - "primary_interface": { - "addrs": ["fd00::2"], - "essential": True, - "interface_type": 0, - "mac": "00:00:00:00:00:01", - "name": "eth0", - "structured_addrs": [ - { - "addr": "fd00::2", - "mode": 0, - "prefix_length": 64, - }, - ], - }, - }, - }, - "imaging_initrd": { - "format": 2, - "id": {"uuid": "deadbeefdeadbeefdeadbeefdeadbeef"}, - "kind": 4, - "name": "metalos.imaging-initrd", - }, - "root_disk_config": { - "single_serial": { - "config": {}, - "serial": "ROOT_DISK_SERIAL", - }, - }, - }, - "runtime_config": { - "deployment_specific": {"metalos": {}}, - }, - } + return builders.build_host_config( + boot_config = builders.build_boot_config(uname = kernel_version), + provisioning_config = builders.build_provisioning_config(num_nics = 4), + runtime_config = builders.build_runtime_config(), + ) def _control_layer( name,