Skip to content
Snippets Groups Projects
Verified Commit fb277725 authored by Karel Koci's avatar Karel Koci :metal:
Browse files

nsfarm/lxd/container: fix invalid name of container

The '-' is invalid so this replaces it with 'x'. For consistency this
also changes it in such a way that it is always appended so the hidden
issue like this won't happen again. It also makes the naming scheme
consistent.
parent eee7b93c
Branches
1 merge request!33Draft: nsfarm/lxd/container: fix invalid name of container
......@@ -74,12 +74,13 @@ class Container:
logger.debug("Container prepared: %s", self.lxd_container.name)
def _container_name(self, prefix="nsfarm"):
# Warning: the other parts of this project rely on this naming convention to identify containers (such as
# cleanup algorithm). Make sure that you update them when you do changes in this code.
name = f"{prefix}-{self._image.name}-{os.getpid()}"
if self._lxd.local.containers.exists(name):
i = 1
while self._lxd.local.containers.exists(f"{name}.{i}"):
i += 1
name = f"{name}.{i}"
i = 1
while self._lxd.local.containers.exists(f"{name}x{i}"):
i += 1
name = f"{name}x{i}"
return name
def cleanup(self):
......
......@@ -73,7 +73,7 @@ def clean_containers(dry_run=False):
else:
# Container have PID of process they are spawned by in the name. We can't safely remove any container
# without running owner process.
pid = int(cont.name.split('-')[-1].split('.')[0])
pid = int(cont.name.split('-')[-1].split('x')[0])
try:
os.kill(pid, 0)
except OSError as err:
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment