|
|
|
@ -12,11 +12,11 @@ import weakref
@@ -12,11 +12,11 @@ import weakref
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class OverlayDirectory: |
|
|
|
|
""" Allocates an overlayfs with a given lower dir and a temporary upper dir. The |
|
|
|
|
overlayfs is destroyed when this object is deallocated """ |
|
|
|
|
"""Allocates an overlayfs with a given lower dir and a temporary upper dir. The |
|
|
|
|
overlayfs is destroyed when this object is deallocated""" |
|
|
|
|
|
|
|
|
|
class DoesNotExist(Exception): |
|
|
|
|
""" Raised if `lower` does not exist """ |
|
|
|
|
"""Raised if `lower` does not exist""" |
|
|
|
|
|
|
|
|
|
def __init__(self, path): |
|
|
|
|
self.path = path |
|
|
|
@ -38,7 +38,8 @@ class OverlayDirectory:
@@ -38,7 +38,8 @@ class OverlayDirectory:
|
|
|
|
|
root_temp_dir.mkdir() |
|
|
|
|
|
|
|
|
|
self.overlay_temp_dir = tempfile.TemporaryDirectory( |
|
|
|
|
prefix=name + ("_" if name else ""), dir=root_temp_dir.resolve(), |
|
|
|
|
prefix=name + ("_" if name else ""), |
|
|
|
|
dir=root_temp_dir.resolve(), |
|
|
|
|
) |
|
|
|
|
self.temp_dir = Path(self.overlay_temp_dir.name) |
|
|
|
|
self.temp_dir_cleaner = None |
|
|
|
@ -95,7 +96,7 @@ class OverlayDirectory:
@@ -95,7 +96,7 @@ class OverlayDirectory:
|
|
|
|
|
self.mounted = False |
|
|
|
|
|
|
|
|
|
def _set_perms_for_cleanup(self): |
|
|
|
|
""" Sets the permissions to allow cleanup """ |
|
|
|
|
"""Sets the permissions to allow cleanup""" |
|
|
|
|
|
|
|
|
|
if not self.temp_dir.exists(): |
|
|
|
|
return # Already deleted |
|
|
|
@ -131,7 +132,7 @@ class Container(util.LibvirtObject):
@@ -131,7 +132,7 @@ class Container(util.LibvirtObject):
|
|
|
|
|
return "This container is already instanciated" |
|
|
|
|
|
|
|
|
|
def __init__(self, conn, networks, name=None, mem=int(1e6), enable_v4=True): |
|
|
|
|
""" Parameters: |
|
|
|
|
"""Parameters: |
|
|
|
|
* conn: connection to libvirt, |
|
|
|
|
* networks: iterable of Network instances this container is connected to, |
|
|
|
|
* name: name of the container. Defaults to something id-based. |
|
|
|
@ -142,9 +143,9 @@ class Container(util.LibvirtObject):
@@ -142,9 +143,9 @@ class Container(util.LibvirtObject):
|
|
|
|
|
|
|
|
|
|
super().__init__(conn) |
|
|
|
|
|
|
|
|
|
if not name: |
|
|
|
|
name = str(self.id) |
|
|
|
|
self.name = settings.PREFIX + "_dom_" + name |
|
|
|
|
self.name = f"{settings.PREFIX}_dom{self.id:02d}" |
|
|
|
|
if name: |
|
|
|
|
self.name += "_" + name |
|
|
|
|
|
|
|
|
|
self.networks = networks |
|
|
|
|
self.mem = mem |
|
|
|
@ -159,14 +160,14 @@ class Container(util.LibvirtObject):
@@ -159,14 +160,14 @@ class Container(util.LibvirtObject):
|
|
|
|
|
return self.lxc_container is not None |
|
|
|
|
|
|
|
|
|
def get_jinja_networks(self): |
|
|
|
|
""" Get a jinja2 environment-compatible network list """ |
|
|
|
|
"""Get a jinja2 environment-compatible network list""" |
|
|
|
|
return [ |
|
|
|
|
{"mac": util.MACAddress(net.id, self.id), "name": net.name, "net": net} |
|
|
|
|
for net in self.networks |
|
|
|
|
] |
|
|
|
|
|
|
|
|
|
def _create_network_files(self): |
|
|
|
|
""" Creates systemd-networkd .network files to set IP addresses """ |
|
|
|
|
"""Creates systemd-networkd .network files to set IP addresses""" |
|
|
|
|
if not self.overlay_root: |
|
|
|
|
raise Exception("No root directory specified yet") |
|
|
|
|
net_config_templ = JinjaTemplate("nic.network") |
|
|
|
@ -204,8 +205,8 @@ class Container(util.LibvirtObject):
@@ -204,8 +205,8 @@ class Container(util.LibvirtObject):
|
|
|
|
|
self.lxc_container = self.conn.createXML(xml) |
|
|
|
|
|
|
|
|
|
def notify_cleanup(self): |
|
|
|
|
""" This method can be called before `cleanup` to notify the host that a |
|
|
|
|
cleanup will be performed soon, speeding the process up. """ |
|
|
|
|
"""This method can be called before `cleanup` to notify the host that a |
|
|
|
|
cleanup will be performed soon, speeding the process up.""" |
|
|
|
|
if self.lxc_container: |
|
|
|
|
try: |
|
|
|
|
self.lxc_container.shutdown() |
|
|
|
|