From 9b8974afa8d86091962c6ae6f491df9306d761b2 Mon Sep 17 00:00:00 2001 From: "Andres D. Molins" Date: Tue, 10 Oct 2023 16:16:33 +0200 Subject: [PATCH] Fix: Use the same hostname on metadata and cloud-init configuration. --- vm_supervisor/vm/firecracker/instance.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/vm_supervisor/vm/firecracker/instance.py b/vm_supervisor/vm/firecracker/instance.py index 4589efcf4..a50432941 100644 --- a/vm_supervisor/vm/firecracker/instance.py +++ b/vm_supervisor/vm/firecracker/instance.py @@ -181,13 +181,17 @@ async def create_snapshot(self) -> CompressedDiskVolumeSnapshot: self.latest_snapshot = snapshot return compressed_snapshot + def _get_vm_hostname(self) -> str: + item_hash_binary: bytes = base64.b16decode(self.vm_hash.encode().upper()) + return base64.b32encode(item_hash_binary).decode().strip("=").lower() + def _encode_user_data(self) -> bytes: """Creates user data configuration file for cloud-init tool""" ssh_authorized_keys = self.resources.message_content.authorized_keys or [] config: Dict[str, Union[str, bool, List[str]]] = { - "hostname": str(self.vm_hash), + "hostname": self._get_vm_hostname(), "disable_root": False, "ssh_pwauth": False, "ssh_authorized_keys": ssh_authorized_keys, @@ -237,13 +241,9 @@ def _create_network_file(self) -> bytes: def _create_metadata_file(self) -> bytes: """Creates metadata configuration file for cloud-init tool""" - hostname = ( - base64.b32encode(str.encode(self.vm_hash)).decode().strip("=").lower() - ) - metadata = { "instance-id": f"iid-instance-{self.vm_id}", - "local-hostname": hostname, + "local-hostname": self._get_vm_hostname(), } return json.dumps(metadata).encode()