From 79af5f86f2a9cc0527c894be50aca371f830c8fb Mon Sep 17 00:00:00 2001 From: peppi-lotta Date: Thu, 7 Dec 2023 13:09:55 +0000 Subject: [PATCH] Tilt improved for easier baremetalhost creation Signed-off-by: peppi-lotta --- Tiltfile | 27 ++++++ docs/dev-setup.md | 26 ++++++ hack/ci-e2e.sh | 25 +----- tools/bmh_test/clean_local_bmh_test_setup.sh | 25 ++++++ tools/bmh_test/create_bmh.sh | 87 ++++++++++++++++++++ tools/bmh_test/create_vm.sh | 26 ++++++ tools/bmh_test/run_local_bmh_test_setup.sh | 28 +++++++ tools/bmh_test/vm2vbmc.sh | 11 +++ 8 files changed, 233 insertions(+), 22 deletions(-) create mode 100755 tools/bmh_test/clean_local_bmh_test_setup.sh create mode 100755 tools/bmh_test/create_bmh.sh create mode 100755 tools/bmh_test/create_vm.sh create mode 100755 tools/bmh_test/run_local_bmh_test_setup.sh create mode 100755 tools/bmh_test/vm2vbmc.sh diff --git a/Tiltfile b/Tiltfile index 7f46e05507..1e94d2bf01 100644 --- a/Tiltfile +++ b/Tiltfile @@ -2,6 +2,7 @@ update_settings(k8s_upsert_timeout_secs=60) # on first tilt up, often can take longer than 30 seconds +load("ext://uibutton", "cmd_button", "location", "text_input") # set defaults settings = { "allowed_contexts": [ @@ -169,6 +170,30 @@ def include_user_tilt_files(): for f in user_tiltfiles: include(f) +def include_custom_buttons(): + + local_resource( + name = "BareMetalHosts", + cmd = ["bash", "-c", "echo This is a local resource for BareMetalHosts"], + auto_init = False, + trigger_mode = TRIGGER_MODE_MANUAL, + ) + + cmd_button( + 'BareMetalHosts:add_new_bmh', + argv=['sh', '-c', 'tools/bmh_test/create_bmh.sh $NAME $VBMC_PORT $CONSUMER $CONSUMER_NAMESPACE' ], + resource = "BareMetalHosts", + icon_name='add_box', + text='Add New baremetalhost', + inputs=[ + text_input('NAME', '"bmh-test-" is automatically added to the begining of the name. This naming convention is later used to clean the local testing environment.'), + text_input('VBMC_PORT'), + text_input('CONSUMER'), + text_input('CONSUMER_NAMESPACE'), + ], + ) + + ############################## # Actual work happens here ############################## @@ -177,6 +202,8 @@ validate_auth() include_user_tilt_files() +include_custom_buttons() + load_provider_tiltfiles(["."]) local("make tools/bin/kustomize") enable_provider("metal3-bmo") diff --git a/docs/dev-setup.md b/docs/dev-setup.md index 0b638511c7..80751039de 100644 --- a/docs/dev-setup.md +++ b/docs/dev-setup.md @@ -174,6 +174,32 @@ refer to [the development setup guide of CAPM3](https://github.com/metal3-io/cluster-api-provider-metal3/blob/main/docs/dev-setup.md#tilt-for-dev-in-capm3) and specially the [Baremetal Operator Integration](https://github.com/metal3-io/cluster-api-provider-metal3/blob/main/docs/dev-setup.md#including-baremetal-operator-and-ip-address-manager) +### Making (virtual) BareMetalHosts with Tilt interface + +Virtinst, libvirt-clients, libvirt-daemon-system, and [Virtualbmc](https://pypi.org/project/virtualbmc/) are required to to create BareMetalHosts this way. The network and VBMC needed for making a BareMetalHosts can be initialized with + +```sh +tools/bmh_test/run_local_bmh_test_setup.sh +``` + +When Tilt is up, it is possible to make BareMetalHosts by pressing a button in the Tilt localhost interface. This is currently only supported for Unix based systems. This button runs the content of file + +```sh +tools/bmh_test/create_bmh.sh +``` + +and adds the values given to the button as arguments. Controlplane host can be created with + +```sh +tools/bmh_test/create_bmh.sh +``` + +The network, VBMC, and virtual machines can be cleaned with + +```sh +tools/bmh_test/clean_local_bmh_test_setup.sh +``` + ## Using libvirt VMs with Ironic In order to use VMs as hosts, they need to be connected to diff --git a/hack/ci-e2e.sh b/hack/ci-e2e.sh index d30156fc77..f63713fd0f 100755 --- a/hack/ci-e2e.sh +++ b/hack/ci-e2e.sh @@ -48,25 +48,8 @@ minikube image load quay.io/metal3-io/baremetal-operator:e2e # Create libvirt domain VM_NAME="bmo-e2e-0" export BOOT_MAC_ADDRESS="00:60:2f:31:81:01" -SERIAL_LOG_PATH="/var/log/libvirt/qemu/${VM_NAME}-serial0.log" - -virt-install \ - --connect qemu:///system \ - --name "${VM_NAME}" \ - --description "Virtualized BareMetalHost" \ - --osinfo=ubuntu-lts-latest \ - --ram=4096 \ - --vcpus=2 \ - --disk size=20 \ - --graphics=none \ - --console pty,target_type=serial \ - --serial file,path="${SERIAL_LOG_PATH}" \ - --xml "./devices/serial/@type=pty" \ - --xml "./devices/serial/log/@file=${SERIAL_LOG_PATH}" \ - --xml "./devices/serial/log/@append=on" \ - --pxe \ - --network network=baremetal-e2e,mac="${BOOT_MAC_ADDRESS}" \ - --noautoconsole + +"${REPO_ROOT}/tools/bmh_test/create_vm.sh" "${VM_NAME}" "${BOOT_MAC_ADDRESS}" # This IP is defined by the network we created above. IP_ADDRESS="192.168.222.1" @@ -88,9 +71,7 @@ if [[ "${BMO_E2E_EMULATOR}" == "vbmc" ]]; then quay.io/metal3-io/vbmc # Add BMH VM to VBMC - docker exec vbmc vbmc add "${VM_NAME}" --port "${VBMC_PORT}" - docker exec vbmc vbmc start "${VM_NAME}" - docker exec vbmc vbmc list + "${REPO_ROOT}/tools/bmh_test/vm2vbmc.sh" "${VM_NAME}" "${VBMC_PORT}" elif [[ "${BMO_E2E_EMULATOR}" == "sushy-tools" ]]; then # Sushy-tools variables diff --git a/tools/bmh_test/clean_local_bmh_test_setup.sh b/tools/bmh_test/clean_local_bmh_test_setup.sh new file mode 100755 index 0000000000..c0bbc88b29 --- /dev/null +++ b/tools/bmh_test/clean_local_bmh_test_setup.sh @@ -0,0 +1,25 @@ +#!/usr/bin/env bash + +set -eux + +# Get a list of all virtual machines +VM_LIST=$(virsh -c qemu:///system list --all --name | grep '^bmh-test-') || true + +if [[ -n "${VM_LIST}" ]]; then + # Loop through the list and delete each virtual machine + for vm_name in ${VM_LIST}; do + virsh -c qemu:///system destroy --domain "${vm_name}" + virsh -c qemu:///system undefine --domain "${vm_name}" --remove-all-storage + kubectl delete baremetalhost "${vm_name}" || true + done +else + echo "No virtual machines found. Skipping..." +fi + +# Clear vbmc +docker stop vbmc +docker rm vbmc + +# Clear network +virsh -c qemu:///system net-destroy baremetal-e2e +virsh -c qemu:///system net-undefine baremetal-e2e diff --git a/tools/bmh_test/create_bmh.sh b/tools/bmh_test/create_bmh.sh new file mode 100755 index 0000000000..b2e2b9b970 --- /dev/null +++ b/tools/bmh_test/create_bmh.sh @@ -0,0 +1,87 @@ +#!/usr/bin/env bash + +# ------------------------------------------------------------------------------------------- +# Description: This script creates a virtual machine using virt-install, +# adds the virtual machine to VBMC (Virtual BMC) for out-of-band management, +# and applies the configuration to Kubernetes +# +# Usage: make tilt-up -> press button in the right upper corner to create bmhs +# /tools/bmh_test/create_bmh.sh +# +# Prerequecites: a network with ip address of 192.168.222.1 named baremetal-e2e and +# VBMC runing +# ------------------------------------------------------------------------------------------- + +set -euxo pipefail + +REPO_ROOT=$(realpath "$(dirname "${BASH_SOURCE[0]}")/../..") + +cd "${REPO_ROOT}" || exit 1 + +# Set default values +NAME="bmh-test-${1:?}" +VBMC_PORT="${2:?}" +CONSUMER="${3:-}" +CONSUMER_NAMESPACE="${4:-}" + +# Generate a random MAC address for the VM's network interface +MAC_ADDRESS="$(printf '00:60:2F:%02X:%02X:%02X\n' $((RANDOM%256)) $((RANDOM%256)) $((RANDOM%256)))" + +# Create a virtual machine and connect it to vbmc +"${REPO_ROOT}/tools/bmh_test/create_vm.sh" "${NAME}" "${MAC_ADDRESS}" +"${REPO_ROOT}/tools/bmh_test/vm2vbmc.sh" "${NAME}" "${VBMC_PORT}" + +# Create a YAML file to generate Kubernetes configuration for the VM +# Apply the generated YAML file to the cluster +if [[ -n "${CONSUMER}" ]] && [[ -n "${CONSUMER_NAMESPACE}" ]]; then + echo "Applying YAML for controlplane host..." + cat < /dev/null; then + echo "ERROR: ${cmd} not found. Please install it." + exit 1 + fi +done + +# Define and start a virtual network +virsh -c qemu:///system net-define "${REPO_ROOT}/hack/e2e/net.xml" +virsh -c qemu:///system net-start baremetal-e2e + +# Start VBMC +docker run --name vbmc --network host -d \ + -v /var/run/libvirt/libvirt-sock:/var/run/libvirt/libvirt-sock \ + -v /var/run/libvirt/libvirt-sock-ro:/var/run/libvirt/libvirt-sock-ro \ + quay.io/metal3-io/vbmc diff --git a/tools/bmh_test/vm2vbmc.sh b/tools/bmh_test/vm2vbmc.sh new file mode 100755 index 0000000000..bfbfbc5c26 --- /dev/null +++ b/tools/bmh_test/vm2vbmc.sh @@ -0,0 +1,11 @@ +#!/usr/bin/env bash + +set -eux + +NAME="${1:?}" +VBMC_PORT="${2:?}" + +# Add the BareMetalHost VM to VBMC +docker exec vbmc vbmc add "${NAME}" --port "${VBMC_PORT}" --libvirt-uri "qemu:///system" +docker exec vbmc vbmc start "${NAME}" +docker exec vbmc vbmc list