Skip to content

Commit

Permalink
Consistent uuids for VMs in layouts
Browse files Browse the repository at this point in the history
* Generate a uuid from layout_name (unless woverridden) in CarthageLayout

* Use that to generate a VM level uuid assuming there is a layout.
  • Loading branch information
hartmans committed Oct 21, 2024
1 parent 432e01c commit 6bc9872
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
10 changes: 10 additions & 0 deletions carthage/modeling/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import types
import typing
from pathlib import Path
import uuid
from .implementation import *
from .decorators import *
from carthage.dependency_injection import * # type: ignore
Expand All @@ -24,6 +25,7 @@
from .utils import *

logger = logging.getLogger(__name__)
LAYOUT_UUID = uuid.UUID('040e488f-016b-43fe-b52d-5217f43fdf1c')

__all__ = []

Expand Down Expand Up @@ -563,6 +565,14 @@ def __init__(self, **kwargs):
if not kvstore.persistent_seed_path:
kvstore.load(str(seed_path))

@memoproperty
def layout_uuid(self):
'''
Returns a uuid for this layout; based on layout_name or the class's qualname.
'''
name = self.layout_name or self.__class__.__qualname__
return uuid.uuid5(LAYOUT_UUID, name)


__all__ += ['CarthageLayout']

Expand Down
3 changes: 1 addition & 2 deletions carthage/resources/templates/vm-config.mako
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<%
import uuid
model = model_in or object()
boot_order = 1
memory_mb = getattr(model, 'memory_mb', 8192)
Expand All @@ -9,7 +8,7 @@ disk_cache = getattr(model, 'disk_cache', 'writethrough')
%>
<domain type='kvm' xmlns:qemu='http://libvirt.org/schemas/domain/qemu/1.0'>
<name>${name}</name>
<uuid>${uuid.uuid4()}</uuid>
<uuid>${uuid}</uuid>
<memory unit='KiB'>${memory_mb*1024}</memory>

<vcpu placement='static' >${cpus}</vcpu>
Expand Down
12 changes: 12 additions & 0 deletions carthage/vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import os.path
import shutil
import types
import uuid
import mako
import mako.lookup
import mako.template
Expand Down Expand Up @@ -62,6 +63,16 @@ def __init__(self, name, *, console_needed=None,
self.vm_running = self.machine_running
self._operation_lock = asyncio.Lock()

@memoproperty
def uuid(self):
from .modeling import CarthageLayout
layout = self.injector.get_instance(InjectionKey(CarthageLayout, _optional=True))
if layout:
layout_uuid = layout.layout_uuid
return uuid.uuid5(layout_uuid, 'vm:'+self.full_name)
return uuid.uuid4()


async def gen_volume(self):
if self.volume is not None:
return
Expand Down Expand Up @@ -97,6 +108,7 @@ async def write_config(self):
disk_config=disk_config,
if_name=lambda n: carthage.network.base.if_name(
"vn", self.config_layout.container_prefix, n.name, self.name),
uuid=self.uuid,
volume=self.volume))
if self.console_needed:
with open(self.console_json_path, "wt") as f:
Expand Down

0 comments on commit 6bc9872

Please sign in to comment.