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)
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)
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)
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
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)
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
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)
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)
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)
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
def tearDown(self): self.patch.revert() fileutils.rm_tree(self._test_dir)
def tearDown(self): fileutils.rm_tree(self.tempdir)
def _clearDisk(self): logging.info('Clearing %s and %s', self.networksPath, self.bondingsPath) fileutils.rm_tree(self.networksPath) fileutils.rm_tree(self.bondingsPath)
def tearDown(self): fileutils.rm_tree(self.tempdir)
def tearDown(self): self.patch.revert() fileutils.rm_tree(self._test_dir)
def _clearDisk(self): logging.info('Clearing %s and %s', self.networksPath, self.bondingsPath) fileutils.rm_tree(self.networksPath) fileutils.rm_tree(self.bondingsPath)