Example #1
0
def _atomic_copytree(srcpath, dstpath, remove_src=False):
    """
    Copy srcpath to dstpatch in an atomic manner.

    It applies atomic directory copy by using the atomicity of overwriting a
    link (rename syscall).

    In case the remove_src flag argument is True, the srcpath is deleted.

    Note: In case of an interruption, it is assured that the destination is
    intact, pointing to the previous data or the new one. Intermediate
    temporary files  or the srcpath may still exists on the filesystem.
    """
    rand_suffix = random_iface_name(max_length=8)
    rand_dstpath = dstpath + '.' + rand_suffix
    rand_dstpath_symlink = rand_dstpath + '.ln'

    shutil.copytree(srcpath, rand_dstpath)
    os.symlink(rand_dstpath, rand_dstpath_symlink)

    old_realdstpath = os.path.realpath(dstpath)
    old_realdstpath_existed = old_realdstpath != dstpath

    _fsynctree(rand_dstpath)

    os.rename(rand_dstpath_symlink, dstpath)
    if old_realdstpath_existed:
        fileutils.rm_tree(old_realdstpath)
    if remove_src:
        fileutils.rm_tree(srcpath)
Example #2
0
def _atomic_copytree(srcpath, dstpath, remove_src=False):
    """
    Copy srcpath to dstpatch in an atomic manner.

    It applies atomic directory copy by using the atomicity of overwriting a
    link (rename syscall).

    In case the remove_src flag argument is True, the srcpath is deleted.

    Note: In case of an interruption, it is assured that the destination is
    intact, pointing to the previous data or the new one. Intermediate
    temporary files  or the srcpath may still exists on the filesystem.
    """
    rand_suffix = random_iface_name(max_length=8)
    rand_dstpath = dstpath + '.' + rand_suffix
    rand_dstpath_symlink = rand_dstpath + '.ln'

    shutil.copytree(srcpath, rand_dstpath)
    os.symlink(rand_dstpath, rand_dstpath_symlink)

    old_realdstpath = os.path.realpath(dstpath)
    old_realdstpath_existed = old_realdstpath != dstpath

    _fsynctree(rand_dstpath)

    os.rename(rand_dstpath_symlink, dstpath)
    if old_realdstpath_existed:
        fileutils.rm_tree(old_realdstpath)
    if remove_src:
        fileutils.rm_tree(srcpath)
Example #3
0
def netconf_dir():
    tempdir = tempfile.mkdtemp()
    try:
        os.mkdir(os.path.join(tempdir, NETCONF_NETS))
        os.mkdir(os.path.join(tempdir, NETCONF_BONDS))
        os.mkdir(os.path.join(tempdir, NETCONF_DEVS))
        yield tempdir
    finally:
        fileutils.rm_tree(tempdir)
Example #4
0
 def _clear_config(confpath):
     try:
         real_confpath = os.path.realpath(confpath)
         fileutils.rm_file(confpath)
         fileutils.rm_tree(real_confpath)
     except OSError as e:
         # Support the "old" non-symlink config path.
         if e.errno == errno.EISDIR:
             fileutils.rm_tree(confpath)
         else:
             raise
Example #5
0
def _create_hook_dir(dir_name):
    dir_path = os.path.join(constants.P_VDSM_HOOKS, dir_name)
    dir_existed = os.path.isdir(dir_path)

    if not dir_existed:
        os.mkdir(dir_path)

    yield dir_path

    if not dir_existed:
        fileutils.rm_tree(dir_path)
Example #6
0
 def _clear_config(confpath):
     try:
         real_confpath = os.path.realpath(confpath)
         fileutils.rm_file(confpath)
         fileutils.rm_tree(real_confpath)
     except OSError as e:
         # Support the "old" non-symlink config path.
         if e.errno == errno.EISDIR:
             fileutils.rm_tree(confpath)
         else:
             raise
Example #7
0
def create_hooks_dir():
    created_root_dir = None
    dir_path = constants.P_VDSM_HOOKS
    while not os.path.isdir(dir_path):
        created_root_dir = dir_path
        dir_path, _ = os.path.split(dir_path)

    if created_root_dir:
        os.makedirs(constants.P_VDSM_HOOKS)

    yield

    if created_root_dir:
        fileutils.rm_tree(created_root_dir)
Example #8
0
def _store_net_config():
    """
    Declare the current running config as 'safe' and persist this safe config.

    It is implemented by copying the running config to the persistent (safe)
    config in an atomic manner.
    It applies atomic directory copy by using the atomicity of overwriting a
    link (rename syscall).
    """
    safeconf_dir = CONF_PERSIST_DIR[:-1]
    rand_suffix = random_iface_name(max_length=8)
    new_safeconf_dir = safeconf_dir + '.' + rand_suffix
    new_safeconf_symlink = new_safeconf_dir + '.ln'

    shutil.copytree(CONF_RUN_DIR[:-1], new_safeconf_dir)
    os.symlink(new_safeconf_dir, new_safeconf_symlink)

    real_old_safeconf_dir = os.path.realpath(safeconf_dir)
    os.rename(new_safeconf_symlink, safeconf_dir)
    real_old_safeconf_dir_existed = real_old_safeconf_dir != safeconf_dir
    if real_old_safeconf_dir_existed:
        fileutils.rm_tree(real_old_safeconf_dir)
Example #9
0
def _store_net_config():
    """
    Declare the current running config as 'safe' and persist this safe config.

    It is implemented by copying the running config to the persistent (safe)
    config in an atomic manner.
    It applies atomic directory copy by using the atomicity of overwriting a
    link (rename syscall).
    """
    safeconf_dir = CONF_PERSIST_DIR[:-1]
    rand_suffix = random_iface_name(max_length=8)
    new_safeconf_dir = safeconf_dir + '.' + rand_suffix
    new_safeconf_symlink = new_safeconf_dir + '.ln'

    shutil.copytree(CONF_RUN_DIR[:-1], new_safeconf_dir)
    os.symlink(new_safeconf_dir, new_safeconf_symlink)

    real_old_safeconf_dir = os.path.realpath(safeconf_dir)
    os.rename(new_safeconf_symlink, safeconf_dir)
    real_old_safeconf_dir_existed = real_old_safeconf_dir != safeconf_dir
    if real_old_safeconf_dir_existed:
        fileutils.rm_tree(real_old_safeconf_dir)
Example #10
0
        def wrapper(*args, **kwargs):

            directory_existed = False

            if not functional:
                old_vdsm_hooks = constants.P_VDSM_HOOKS
                constants.P_VDSM_HOOKS = tempfile.mkdtemp()

            hook_path = constants.P_VDSM_HOOKS + '/' + hook_dir

            try:
                os.mkdir(hook_path)
            except OSError as mkdir_error:
                if mkdir_error.errno == errno.EEXIST:
                    directory_existed = True
                else:
                    raise

            cookie_file = _createHookScript(hook_path, hook_name, hook_script)

            output = None

            try:
                kwargs['hook_cookiefile'] = cookie_file
                output = test_function(*args, **kwargs)
            finally:
                if directory_existed:
                    fileutils.rm_file(hook_path + '/' + hook_name)
                else:
                    fileutils.rm_tree(hook_path)

                fileutils.rm_file(cookie_file)

                if not functional:
                    constants.P_VDSM_HOOKS = old_vdsm_hooks

            return output
Example #11
0
 def tearDown(self):
     self.patch.revert()
     fileutils.rm_tree(self._test_dir)
Example #12
0
 def tearDown(self):
     fileutils.rm_tree(self.tempdir)
Example #13
0
 def _clearDisk(self):
     logging.info('Clearing %s and %s', self.networksPath,
                  self.bondingsPath)
     fileutils.rm_tree(self.networksPath)
     fileutils.rm_tree(self.bondingsPath)
Example #14
0
 def tearDown(self):
     fileutils.rm_tree(self.tempdir)
Example #15
0
 def tearDown(self):
     self.patch.revert()
     fileutils.rm_tree(self._test_dir)
Example #16
0
 def _clearDisk(self):
     logging.info('Clearing %s and %s',
                  self.networksPath, self.bondingsPath)
     fileutils.rm_tree(self.networksPath)
     fileutils.rm_tree(self.bondingsPath)