From 6b0391b3e3c9b086213ec88bc8c28c85f199ae6b Mon Sep 17 00:00:00 2001 From: Iustin Pop <iustin@google.com> Date: Fri, 5 Mar 2010 10:53:08 +0100 Subject: [PATCH] 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: Iustin Pop <iustin@google.com> Reviewed-by: Michael Hanselmann <hansmi@google.com> --- lib/hypervisor/hv_chroot.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/hypervisor/hv_chroot.py b/lib/hypervisor/hv_chroot.py index d207aa250..f033d5e78 100644 --- a/lib/hypervisor/hv_chroot.py +++ b/lib/hypervisor/hv_chroot.py @@ -105,6 +105,13 @@ class ChrootManager(hv_base.BaseHypervisor): data.sort(key=lambda x: x.count("/"), reverse=True) 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): """Get the list of running instances. @@ -121,7 +128,7 @@ class ChrootManager(hv_base.BaseHypervisor): @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): raise HypervisorError("Instance %s is not running" % instance_name) return (instance_name, 0, 0, 0, 0, 0) @@ -146,7 +153,7 @@ class ChrootManager(hv_base.BaseHypervisor): 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): try: os.mkdir(root_dir) @@ -179,7 +186,7 @@ class ChrootManager(hv_base.BaseHypervisor): - 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): return @@ -240,7 +247,7 @@ class ChrootManager(hv_base.BaseHypervisor): """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): raise HypervisorError("Instance %s is not running" % instance.name) -- GitLab