Commit 778b75bb authored by Manuel Franceschini's avatar Manuel Franceschini
Browse files

Backend directory functions for file backend

Add _[Create,Remove,Rename]FileStorageDir function which are needed for
file-based instance management. These function check whether the given
directory to operate on is under the cluster-wide defined default file
storage dir. If this is not the case the won't do anything and return
False. This is to prevent cluster manipulation or damage.

Reviewed-by: ultrotter
parent 2d4011cd
......@@ -1417,6 +1417,130 @@ def RenameBlockDevices(devlist):
return result
def _TransformFileStorageDir(file_storage_dir):
"""Checks whether given file_storage_dir is valid.
Checks wheter the given file_storage_dir is within the cluster-wide
default file_storage_dir stored in SimpleStore. Only paths under that
directory are allowed.
Args:
file_storage_dir: string with path
Returns:
normalized file_storage_dir (string) if valid, None otherwise
"""
file_storage_dir = os.path.normpath(file_storage_dir)
base_file_storage_dir = ssconf.SimpleStore().GetFileStorageDir()
if (not os.path.commonprefix([file_storage_dir, base_file_storage_dir]) ==
base_file_storage_dir):
logger.Error("file storage directory '%s' is not under base file"
" storage directory '%s'" %
(file_storage_dir, base_file_storage_dir))
return None
return file_storage_dir
def CreateFileStorageDir(file_storage_dir):
"""Create file storage directory.
Args:
file_storage_dir: string containing the path
Returns:
tuple with first element a boolean indicating wheter dir
creation was successful or not
"""
file_storage_dir = _TransformFileStorageDir(file_storage_dir)
result = True,
if not file_storage_dir:
result = False,
else:
if os.path.exists(file_storage_dir):
if not os.path.isdir(file_storage_dir):
logger.Error("'%s' is not a directory" % file_storage_dir)
result = False,
else:
try:
os.makedirs(file_storage_dir, 0750)
except OSError, err:
logger.Error("Cannot create file storage directory '%s': %s" %
(file_storage_dir, err))
result = False,
return result
def RemoveFileStorageDir(file_storage_dir):
"""Remove file storage directory.
Remove it only if it's empty. If not log an error and return.
Args:
file_storage_dir: string containing the path
Returns:
tuple with first element a boolean indicating wheter dir
removal was successful or not
"""
file_storage_dir = _TransformFileStorageDir(file_storage_dir)
result = True,
if not file_storage_dir:
result = False,
else:
if os.path.exists(file_storage_dir):
if not os.path.isdir(file_storage_dir):
logger.Error("'%s' is not a directory" % file_storage_dir)
result = False,
# deletes dir only if empty, otherwise we want to return False
try:
os.rmdir(file_storage_dir)
except OSError, err:
logger.Error("Cannot remove file storage directory '%s': %s" %
(file_storage_dir, err))
result = False,
return result
def RenameFileStorageDir(old_file_storage_dir, new_file_storage_dir):
"""Rename the file storage directory.
Args:
old_file_storage_dir: string containing the old path
new_file_storage_dir: string containing the new path
Returns:
tuple with first element a boolean indicating wheter dir
rename was successful or not
"""
old_file_storage_dir = _TransformFileStorageDir(old_file_storage_dir)
new_file_storage_dir = _TransformFileStorageDir(new_file_storage_dir)
result = True,
if not old_file_storage_dir or not new_file_storage_dir:
result = False,
else:
if not os.path.exists(new_file_storage_dir):
if os.path.isdir(old_file_storage_dir):
try:
os.rename(old_file_storage_dir, new_file_storage_dir)
except OSError, err:
logger.Error("Cannot rename '%s' to '%s': %s"
% (old_file_storage_dir, new_file_storage_dir, err))
result = False,
else:
logger.Error("'%s' is not a directory" % old_file_storage_dir)
result = False,
else:
if os.path.exists(old_file_storage_dir):
logger.Error("Cannot rename '%s' to '%s'. Both locations exist." %
old_file_storage_dir, new_file_storage_dir)
result = False,
return result
class HooksRunner(object):
"""Hook runner.
......
Markdown is supported
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