diff --git a/carthage/modeling/base.py b/carthage/modeling/base.py index 5c3ef5fe..555d4c5b 100644 --- a/carthage/modeling/base.py +++ b/carthage/modeling/base.py @@ -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 @@ -24,6 +25,7 @@ from .utils import * logger = logging.getLogger(__name__) +LAYOUT_UUID = uuid.UUID('040e488f-016b-43fe-b52d-5217f43fdf1c') __all__ = [] @@ -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'] diff --git a/carthage/resources/templates/vm-config.mako b/carthage/resources/templates/vm-config.mako index 34c36b3c..86a6800b 100644 --- a/carthage/resources/templates/vm-config.mako +++ b/carthage/resources/templates/vm-config.mako @@ -1,5 +1,4 @@ <% -import uuid model = model_in or object() boot_order = 1 memory_mb = getattr(model, 'memory_mb', 8192) @@ -9,7 +8,7 @@ disk_cache = getattr(model, 'disk_cache', 'writethrough') %> ${name} - ${uuid.uuid4()} + ${uuid} ${memory_mb*1024} ${cpus} diff --git a/carthage/vm.py b/carthage/vm.py index d678d4b1..4cb2bbe1 100644 --- a/carthage/vm.py +++ b/carthage/vm.py @@ -13,6 +13,7 @@ import os.path import shutil import types +import uuid import mako import mako.lookup import mako.template @@ -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 @@ -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: