Yeela Kaplan has uploaded a new change for review. Change subject: utils: Move functionality from storage.misc ......................................................................
utils: Move functionality from storage.misc move rotateFiles and persistFile into utils Change-Id: I2c547e3ac2ecf19359c32f7e717ad7b098bfbca4 Signed-off-by: Yeela Kaplan <[email protected]> --- M lib/vdsm/utils.py M tests/miscTests.py M tests/utilsTests.py M vdsm/storage/misc.py M vdsm/storage/multipath.py M vdsm/storage/sd.py 6 files changed, 115 insertions(+), 114 deletions(-) git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/83/31583/1 diff --git a/lib/vdsm/utils.py b/lib/vdsm/utils.py index 84b7883..e88007c 100644 --- a/lib/vdsm/utils.py +++ b/lib/vdsm/utils.py @@ -143,6 +143,67 @@ raise +def rotateFiles(directory, prefixName, gen, cp=False, persist=False): + logging.debug("dir: %s, prefixName: %s, versions: %s" % + (directory, prefixName, gen)) + gen = int(gen) + files = os.listdir(directory) + files = glob.glob("%s*" % prefixName) + fd = {} + for fname in files: + name = fname.rsplit('.', 1) + try: + ind = int(name[1]) + except ValueError: + name[0] = fname + ind = 0 + except IndexError: + ind = 0 + except: + continue + if ind < gen: + fd[ind] = {'old': fname, 'new': name[0] + '.' + str(ind + 1)} + + keys = fd.keys() + keys.sort(reverse=True) + logging.debug("versions found: %s" % (keys)) + + for key in keys: + oldName = os.path.join(directory, fd[key]['old']) + newName = os.path.join(directory, fd[key]['new']) + if isOvirtNode() and persist and not cp: + try: + execCmd([constants.EXT_UNPERSIST, oldName], + sudo=True) + execCmd([constants.EXT_UNPERSIST, newName], + sudo=True) + except: + pass + try: + if cp: + execCmd([constants.EXT_CP, oldName, newName], sudo=True) + if (isOvirtNode() and + persist and not os.path.exists(newName)): + execCmd([constants.EXT_PERSIST, newName], + sudo=True) + + else: + os.rename(oldName, newName) + except: + pass + if isOvirtNode() and persist and not cp: + try: + execCmd([constants.EXT_PERSIST, newName], + sudo=True) + except: + pass + + +def persistFile(name): + if isOvirtNode(): + execCmd([constants.EXT_PERSIST, name], sudo=True) + + def _parseMemInfo(lines): """ Parse the content of ``/proc/meminfo`` as list of strings diff --git a/tests/miscTests.py b/tests/miscTests.py index a40afb0..e25135a 100644 --- a/tests/miscTests.py +++ b/tests/miscTests.py @@ -203,54 +203,6 @@ self.assertRaises(ValueError, misc.itmap(int, data, 0).next) -class RotateFiles(TestCaseBase): - def testNonExistingDir(self, persist=False): - """ - Tests that the method fails correctly when given a non existing dir. - """ - self.assertRaises(OSError, misc.rotateFiles, "/I/DONT/EXIST", "prefix", - 2, persist=persist) - - def testEmptyDir(self, persist=False): - """ - Test that when given an empty dir the rotator works correctly. - """ - prefix = "prefix" - with namedTemporaryDir() as dir: - misc.rotateFiles(dir, prefix, 0, persist=persist) - - def testFullDir(self, persist=False): - """ - Test that rotator does it's basic functionality. - """ - # Prepare - prefix = "prefix" - stubContent = ('"Multiple exclamation marks", ' - 'he went on, shaking his head, ' - '"are a sure sign of a diseased mind."') - # (C) Terry Pratchet - Small Gods - with namedTemporaryDir() as dir: - gen = 10 - - expectedDirContent = [] - for i in range(gen): - fname = "%s.txt.%d" % (prefix, i + 1) - expectedDirContent.append("%s.txt.%d" % (prefix, i + 1)) - f = open(os.path.join(dir, fname), "wb") - f.write(stubContent) - f.flush() - f.close() - - # Rotate - misc.rotateFiles(dir, prefix, gen, persist=persist) - - # Test result - currentDirContent = os.listdir(dir) - expectedDirContent.sort() - currentDirContent.sort() - self.assertEquals(currentDirContent, expectedDirContent) - - class ParseHumanReadableSize(TestCaseBase): def testValidInput(self): """ diff --git a/tests/utilsTests.py b/tests/utilsTests.py index 6e66c02..3875d59 100644 --- a/tests/utilsTests.py +++ b/tests/utilsTests.py @@ -18,13 +18,14 @@ # Refer to the README and COPYING files for full details of the license # -import os.path +import os import contextlib import errno import logging import sys import threading +from testlib import namedTemporaryDir from testlib import VdsmTestCase as TestCaseBase from testlib import permutations, expandPermutations from testValidation import checkSudo @@ -450,6 +451,54 @@ self.fail("Exception was not raised") +class RotateFiles(TestCaseBase): + def testNonExistingDir(self, persist=False): + """ + Tests that the method fails correctly when given a non existing dir. + """ + self.assertRaises(OSError, utils.rotateFiles, "/I/DONT/EXIST", + "prefix", 2, persist=persist) + + def testEmptyDir(self, persist=False): + """ + Test that when given an empty dir the rotator works correctly. + """ + prefix = "prefix" + with namedTemporaryDir() as dir: + utils.rotateFiles(dir, prefix, 0, persist=persist) + + def testFullDir(self, persist=False): + """ + Test that rotator does it's basic functionality. + """ + # Prepare + prefix = "prefix" + stubContent = ('"Multiple exclamation marks", ' + 'he went on, shaking his head, ' + '"are a sure sign of a diseased mind."') + # (C) Terry Pratchet - Small Gods + with namedTemporaryDir() as dir: + gen = 10 + + expectedDirContent = [] + for i in range(gen): + fname = "%s.txt.%d" % (prefix, i + 1) + expectedDirContent.append("%s.txt.%d" % (prefix, i + 1)) + f = open(os.path.join(dir, fname), "wb") + f.write(stubContent) + f.flush() + f.close() + + # Rotate + utils.rotateFiles(dir, prefix, gen, persist=persist) + + # Test result + currentDirContent = os.listdir(dir) + expectedDirContent.sort() + currentDirContent.sort() + self.assertEquals(currentDirContent, expectedDirContent) + + @expandPermutations class ExecCmdTest(TestCaseBase): CMD_TYPES = ((tuple,), (list,), (iter,)) diff --git a/vdsm/storage/misc.py b/vdsm/storage/misc.py index d0869ee..fa4fe1e 100644 --- a/vdsm/storage/misc.py +++ b/vdsm/storage/misc.py @@ -33,7 +33,6 @@ from itertools import chain, imap import contextlib import errno -import glob import logging import os import Queue @@ -449,67 +448,6 @@ if n < 0: raise se.InvalidParameterException(name, number) return n - - -def rotateFiles(directory, prefixName, gen, cp=False, persist=False): - log.debug("dir: %s, prefixName: %s, versions: %s" % - (directory, prefixName, gen)) - gen = int(gen) - files = os.listdir(directory) - files = glob.glob("%s*" % prefixName) - fd = {} - for fname in files: - name = fname.rsplit('.', 1) - try: - ind = int(name[1]) - except ValueError: - name[0] = fname - ind = 0 - except IndexError: - ind = 0 - except: - continue - if ind < gen: - fd[ind] = {'old': fname, 'new': name[0] + '.' + str(ind + 1)} - - keys = fd.keys() - keys.sort(reverse=True) - log.debug("versions found: %s" % (keys)) - - for key in keys: - oldName = os.path.join(directory, fd[key]['old']) - newName = os.path.join(directory, fd[key]['new']) - if utils.isOvirtNode() and persist and not cp: - try: - execCmd([constants.EXT_UNPERSIST, oldName], - sudo=True) - execCmd([constants.EXT_UNPERSIST, newName], - sudo=True) - except: - pass - try: - if cp: - execCmd([constants.EXT_CP, oldName, newName], sudo=True) - if (utils.isOvirtNode() and - persist and not os.path.exists(newName)): - execCmd([constants.EXT_PERSIST, newName], - sudo=True) - - else: - os.rename(oldName, newName) - except: - pass - if utils.isOvirtNode() and persist and not cp: - try: - execCmd([constants.EXT_PERSIST, newName], - sudo=True) - except: - pass - - -def persistFile(name): - if utils.isOvirtNode(): - execCmd([constants.EXT_PERSIST, name], sudo=True) def parseHumanReadableSize(size): diff --git a/vdsm/storage/multipath.py b/vdsm/storage/multipath.py index ba98866..d626305 100644 --- a/vdsm/storage/multipath.py +++ b/vdsm/storage/multipath.py @@ -158,7 +158,7 @@ supported state. The original configuration, if any, is saved """ if os.path.exists(MPATH_CONF): - misc.rotateFiles( + utils.rotateFiles( os.path.dirname(MPATH_CONF), os.path.basename(MPATH_CONF), MAX_CONF_COPIES, cp=True, persist=True) @@ -169,7 +169,7 @@ rc = misc.execCmd(cmd, sudo=True)[0] if rc != 0: raise se.MultipathSetupError() - misc.persistFile(MPATH_CONF) + utils.persistFile(MPATH_CONF) # Flush all unused multipath device maps misc.execCmd([constants.EXT_MULTIPATH, "-F"], sudo=True) diff --git a/vdsm/storage/sd.py b/vdsm/storage/sd.py index b34cc79..198586d 100644 --- a/vdsm/storage/sd.py +++ b/vdsm/storage/sd.py @@ -32,6 +32,7 @@ from resourceFactories import IMAGE_NAMESPACE, VOLUME_NAMESPACE import resourceManager as rm from vdsm import constants +from vdsm import utils import clusterlock import outOfProcess as oop from persistentDict import unicodeEncoder, unicodeDecoder @@ -733,7 +734,7 @@ def setMetadata(self, newMetadata): # Backup old md (rotate old backup files) - misc.rotateFiles(self.mdBackupDir, self.sdUUID, self.mdBackupVersions) + utils.rotateFiles(self.mdBackupDir, self.sdUUID, self.mdBackupVersions) oldMd = ["%s=%s\n" % (key, value) for key, value in self.getMetadata().copy().iteritems()] open(os.path.join(self.mdBackupDir, self.sdUUID), -- To view, visit http://gerrit.ovirt.org/31583 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I2c547e3ac2ecf19359c32f7e717ad7b098bfbca4 Gerrit-PatchSet: 1 Gerrit-Project: vdsm Gerrit-Branch: master Gerrit-Owner: Yeela Kaplan <[email protected]> _______________________________________________ vdsm-patches mailing list [email protected] https://lists.fedorahosted.org/mailman/listinfo/vdsm-patches
