Skip to content
Snippets Groups Projects
Commit 6b0391b3 authored by Iustin Pop's avatar Iustin Pop
Browse files

hv_chroot: remove hard-coded path constructs


This patch abstract the computation of an instance's root directory into
a separate function (that uses PathJoin instead of "%s/%s").

Signed-off-by: default avatarIustin Pop <iustin@google.com>
Reviewed-by: default avatarMichael Hanselmann <hansmi@google.com>
parent 6136f8f0
No related branches found
No related tags found
No related merge requests found
...@@ -105,6 +105,13 @@ class ChrootManager(hv_base.BaseHypervisor): ...@@ -105,6 +105,13 @@ class ChrootManager(hv_base.BaseHypervisor):
data.sort(key=lambda x: x.count("/"), reverse=True) data.sort(key=lambda x: x.count("/"), reverse=True)
return data return data
@classmethod
def _InstanceDir(cls, instance_name):
"""Return the root directory for an instance.
"""
return utils.PathJoin(cls._ROOT_DIR, instance_name)
def ListInstances(self): def ListInstances(self):
"""Get the list of running instances. """Get the list of running instances.
...@@ -121,7 +128,7 @@ class ChrootManager(hv_base.BaseHypervisor): ...@@ -121,7 +128,7 @@ class ChrootManager(hv_base.BaseHypervisor):
@return: (name, id, memory, vcpus, stat, times) @return: (name, id, memory, vcpus, stat, times)
""" """
dir_name = "%s/%s" % (self._ROOT_DIR, instance_name) dir_name = self._InstanceDir(instance_name)
if not self._IsDirLive(dir_name): if not self._IsDirLive(dir_name):
raise HypervisorError("Instance %s is not running" % instance_name) raise HypervisorError("Instance %s is not running" % instance_name)
return (instance_name, 0, 0, 0, 0, 0) return (instance_name, 0, 0, 0, 0, 0)
...@@ -146,7 +153,7 @@ class ChrootManager(hv_base.BaseHypervisor): ...@@ -146,7 +153,7 @@ class ChrootManager(hv_base.BaseHypervisor):
execute '/ganeti-chroot start'. execute '/ganeti-chroot start'.
""" """
root_dir = "%s/%s" % (self._ROOT_DIR, instance.name) root_dir = self._InstanceDir(instance.name)
if not os.path.exists(root_dir): if not os.path.exists(root_dir):
try: try:
os.mkdir(root_dir) os.mkdir(root_dir)
...@@ -179,7 +186,7 @@ class ChrootManager(hv_base.BaseHypervisor): ...@@ -179,7 +186,7 @@ class ChrootManager(hv_base.BaseHypervisor):
- finally unmount the instance dir - finally unmount the instance dir
""" """
root_dir = "%s/%s" % (self._ROOT_DIR, instance.name) root_dir = self._InstanceDir(instance.name)
if not os.path.exists(root_dir) or not self._IsDirLive(root_dir): if not os.path.exists(root_dir) or not self._IsDirLive(root_dir):
return return
...@@ -240,7 +247,7 @@ class ChrootManager(hv_base.BaseHypervisor): ...@@ -240,7 +247,7 @@ class ChrootManager(hv_base.BaseHypervisor):
"""Return a command for connecting to the console of an instance. """Return a command for connecting to the console of an instance.
""" """
root_dir = "%s/%s" % (cls._ROOT_DIR, instance.name) root_dir = cls._InstanceDir(instance.name)
if not os.path.ismount(root_dir): if not os.path.ismount(root_dir):
raise HypervisorError("Instance %s is not running" % instance.name) raise HypervisorError("Instance %s is not running" % instance.name)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment