hv_chroot: remove hard-coded path constructs
authorIustin Pop <iustin@google.com>
Fri, 5 Mar 2010 09:53:08 +0000 (10:53 +0100)
committerIustin Pop <iustin@google.com>
Mon, 8 Mar 2010 13:51:35 +0000 (14:51 +0100)
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

index d207aa2..f033d5e 100644 (file)
@@ -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)