Merge branch 'devel-2.7'
[ganeti-local] / lib / vcluster.py
index 40b6777..ed28cab 100644 (file)
@@ -21,6 +21,9 @@
 
 """Module containing utilities for virtual clusters.
 
+Most functions manipulate file system paths and are no-ops when the environment
+variables C{GANETI_ROOTDIR} and C{GANETI_HOSTNAME} are not set. See the
+functions' docstrings for details.
 
 """
 
@@ -29,10 +32,17 @@ import os
 from ganeti import compat
 
 
+ETC_HOSTS = "/etc/hosts"
+
 _VIRT_PATH_PREFIX = "/###-VIRTUAL-PATH-###,"
 _ROOTDIR_ENVNAME = "GANETI_ROOTDIR"
 _HOSTNAME_ENVNAME = "GANETI_HOSTNAME"
 
+#: List of paths which shouldn't be virtualized
+_VPATH_WHITELIST = compat.UniqueFrozenset([
+  ETC_HOSTS,
+  ])
+
 
 def _GetRootDirectory(envname):
   """Retrieves root directory from an environment variable.
@@ -81,6 +91,11 @@ def _CheckHostname(hostname):
 def _PreparePaths(rootdir, hostname):
   """Checks if the root directory and hostname are acceptable.
 
+  The (node-specific) root directory must have the hostname as its last
+  component. The parent directory then becomes the cluster-wide root directory.
+  This is necessary as some components must be able to predict the root path on
+  a remote node (e.g. copying files via scp).
+
   @type rootdir: string
   @param rootdir: Root directory (from environment)
   @type hostname: string
@@ -139,7 +154,10 @@ def ExchangeNodeRoot(node_name, filename,
                      _basedir=_VIRT_BASEDIR, _noderoot=_VIRT_NODEROOT):
   """Replaces the node-specific root directory in a path.
 
-  Replaces it with the root directory for another node.
+  Replaces it with the root directory for another node. Assuming
+  C{/tmp/vcluster/node1} is the root directory for C{node1}, the result will be
+  C{/tmp/vcluster/node3} for C{node3} (as long as a root directory is specified
+  in the environment).
 
   """
   if _basedir:
@@ -168,7 +186,9 @@ def AddNodePrefix(path, _noderoot=_VIRT_NODEROOT):
   """Adds a node-specific prefix to a path in a virtual cluster.
 
   Returned path includes user-specified root directory if specified in
-  environment.
+  environment. As an example, the path C{/var/lib/ganeti} becomes
+  C{/tmp/vcluster/node1/var/lib/ganeti} if C{/tmp/vcluster/node1} is the root
+  directory specified in the environment.
 
   """
   assert os.path.isabs(path)
@@ -186,6 +206,9 @@ def AddNodePrefix(path, _noderoot=_VIRT_NODEROOT):
 def _RemoveNodePrefix(path, _noderoot=_VIRT_NODEROOT):
   """Removes the node-specific prefix from a path.
 
+  This is the opposite of L{AddNodePrefix} and removes a node-local prefix
+  path.
+
   """
   assert os.path.isabs(path)
 
@@ -215,12 +238,12 @@ def MakeVirtualPath(path, _noderoot=_VIRT_NODEROOT):
 
   A path is "virtualized" by stripping it of its node-specific directory and
   prepending a prefix (L{_VIRT_PATH_PREFIX}). Use L{LocalizeVirtualPath} to
-  undo the process.
+  undo the process. Virtual paths are meant to be transported via RPC.
 
   """
   assert os.path.isabs(path)
 
-  if _noderoot:
+  if _noderoot and path not in _VPATH_WHITELIST:
     return _VIRT_PATH_PREFIX + _RemoveNodePrefix(path, _noderoot=_noderoot)
   else:
     return path
@@ -231,11 +254,12 @@ def LocalizeVirtualPath(path, _noderoot=_VIRT_NODEROOT):
 
   A "virtualized" path consists of a prefix (L{LocalizeVirtualPath}) and a
   local path. This function adds the node-specific directory to the local path.
+  Virtual paths are meant to be transported via RPC.
 
   """
   assert os.path.isabs(path)
 
-  if _noderoot:
+  if _noderoot and path not in _VPATH_WHITELIST:
     if path.startswith(_VIRT_PATH_PREFIX):
       return AddNodePrefix(path[len(_VIRT_PATH_PREFIX):], _noderoot=_noderoot)
     else: