Skip to content

Commit

Permalink
Initial Deployable support for Libvirt
Browse files Browse the repository at this point in the history
* Include the layout_name and orphan_policy in  metadata associated with VMs so we can build an orhpan finder.

* Add find, find_or_create to Vm.

Currently the find method has the wrong semantics. We want to consider a VM existing when its volume exists, but that requires reworking ImageVolume. So for now only running VMs exist as Deployables.
  • Loading branch information
hartmans committed Nov 6, 2024
1 parent ab2cd4d commit 409c864
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion carthage/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ class ImageVolume(AsyncInjectable, SetupTaskMixin):

'''
Represents a raw disk image.
Can be unpacked from a compressed base image, or created by a callback (or eros if *unpack* is False).
Can be unpacked from a compressed base image, or created by a callback (or zeros if *unpack* is False).
If *base_image* is set, then ``cp --reflink`` is used to clone an image.
'''
Expand Down
3 changes: 3 additions & 0 deletions carthage/resources/templates/vm-config.mako
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ def is_vnc():
<domain type='kvm' xmlns:qemu='http://libvirt.org/schemas/domain/qemu/1.0'>
<name>${name}</name>
<uuid>${uuid}</uuid>
<metadata>
<carthage:layout xmlns:carthage='https://github.com/hadron/carthage' layout='${layout_name}' orphan_policy='${orphan_policy}'/>
</metadata>
<memory unit='KiB'>${memory_mb*1024}</memory>

<vcpu placement='static' >${cpus}</vcpu>
Expand Down
27 changes: 27 additions & 0 deletions carthage/vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import mako.template
from pathlib import Path
from .dependency_injection import *
from . import deployment
from .utils import when_needed, memoproperty
from .setup_tasks import SetupTaskMixin, setup_task
from .image import ImageVolume
Expand Down Expand Up @@ -81,14 +82,36 @@ async def gen_volume(self):
self.ssh_rekeyed()
os.makedirs(self.stamp_path, exist_ok=True)

async def find(self):
if self.domid():
return True
return False

async def find_or_create(self):
if await self.find():
return
await self.start_machine()

async def write_config(self):
from .modeling import CarthageLayout
template = _templates.get_template("vm-config.mako")
await self.resolve_networking()
for i, link in self.network_links.items():
await link.instantiate(carthage.network.BridgeNetwork)
if self.image:
await self.image.async_become_ready()
await self.gen_volume()
layout = await self.ainjector.get_instance_async(InjectionKey(CarthageLayout, _ready=False, _optional=True))
if layout:
layout_name = layout.layout_name
try:
orphan_policy = layout.injector.get_instance(deployment.orphan_policy)
except KeyError:
orphan_policy = deployment.DeletionPolicy.delete
else:
layout_name = ''
orphan_policy = deployment.DeletionPolicy.retain

ci_data = None
if self.model and getattr(self.model, 'cloud_init', False):
ci_data = await self.ainjector(carthage.cloud_init.generate_cloud_init_cidata)
Expand All @@ -103,6 +126,8 @@ async def write_config(self):
console_needed=console_needed,
console_port=self.console_port.port if self.console_needed else None,
name=self.full_name,
layout_name=layout_name,
orphan_policy=orphan_policy,
links=self.network_links,
model_in=self.model,
disk_config=disk_config,
Expand Down Expand Up @@ -440,3 +465,5 @@ async def unpack(self):


__all__ = ('VM', 'Vm', 'InstallQemuAgent')


0 comments on commit 409c864

Please sign in to comment.