Skip to content

Commit

Permalink
Fix: Set hostname encoding as string bytes (#427)
Browse files Browse the repository at this point in the history
Fix: Set Item hash of the VM as string and enconding as bytes to be able to use it on b32encode function.

Co-authored-by: Andres D. Molins <[email protected]>
  • Loading branch information
nesitor and Andres D. Molins authored Oct 10, 2023
1 parent eab7262 commit a440f0b
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions vm_supervisor/vm/firecracker/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,17 @@ async def create_snapshot(self) -> CompressedDiskVolumeSnapshot:
self.latest_snapshot = snapshot
return compressed_snapshot

def _get_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_hostname(),
"disable_root": False,
"ssh_pwauth": False,
"ssh_authorized_keys": ssh_authorized_keys,
Expand Down Expand Up @@ -237,11 +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(self.vm_hash).decode().strip("=").lower()

metadata = {
"instance-id": f"iid-instance-{self.vm_id}",
"local-hostname": hostname,
"local-hostname": self._get_hostname(),
}

return json.dumps(metadata).encode()
Expand Down

0 comments on commit a440f0b

Please sign in to comment.