diff options
author | Tim <timothyasir@gluster.com> | 2011-08-30 13:01:34 +0530 |
---|---|---|
committer | Tim <timothyasir@gluster.com> | 2011-09-16 15:37:08 +0530 |
commit | 32b435cb634297fe8328a3a0d1b211fe95552333 (patch) | |
tree | 5003a54509d22b95ac0087493f71d0ee694d7143 /src | |
parent | 0cd2d5bfe5758e79632345e41fe8944298bb250b (diff) |
Code cleanup done for VolumeUtils.py
Signed-off-by: Tim <timothyasir@gluster.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/com.gluster.storage.management.gateway.scripts/src/backend/VolumeUtils.py | 526 |
1 files changed, 13 insertions, 513 deletions
diff --git a/src/com.gluster.storage.management.gateway.scripts/src/backend/VolumeUtils.py b/src/com.gluster.storage.management.gateway.scripts/src/backend/VolumeUtils.py index 020a1900..e5256178 100644 --- a/src/com.gluster.storage.management.gateway.scripts/src/backend/VolumeUtils.py +++ b/src/com.gluster.storage.management.gateway.scripts/src/backend/VolumeUtils.py @@ -3,30 +3,15 @@ # import os -import glob -import tempfile -from operator import itemgetter +import sys +p1 = os.path.abspath(os.path.dirname(sys.argv[0])) +p2 = "%s/common" % os.path.dirname(p1) +if not p1 in sys.path: + sys.path.append(p1) +if not p2 in sys.path: + sys.path.append(p2) import Globals -from Protocol import * -from Utils import * -from DiskUtils import * -from ServerUtils import * -import GlusterdUtils as Glusterd - - -def isVolumeExist(volumeName): - volumeDom = XDOM() - return volumeDom.parseFile("%s/%s.xml" % (Globals.VOLUME_CONF_DIR, volumeName)) and \ - Glusterd.isVolumeExist(volumeName) - - -def getVolumeUuid(volumeName): - fileName = "%s/%s.xml" % (Globals.VOLUME_CONF_DIR, volumeName) - volumeDom = XDOM() - if not volumeDom.parseFile(fileName): - log("Failed to parse volume configuration file %s of %s" % (fileName, volumeName)) - return None - return volumeDom.getTextByTagRoute("uuid") +import Utils def readVolumeSmbConfFile(fileName=Globals.VOLUME_SMBCONF_FILE): @@ -41,7 +26,7 @@ def readVolumeSmbConfFile(fileName=Globals.VOLUME_SMBCONF_FILE): entryList.append(tokens[1].strip()) fp.close() except IOError, e: - log("Failed to open file %s: %s" % (fileName, str(e))) + Utils.log("Failed to open file %s: %s" % (fileName, str(e))) return entryList @@ -53,7 +38,7 @@ def writeVolumeSmbConfFile(entryList, fileName=Globals.VOLUME_SMBCONF_FILE): fp.close() return True except IOError, e: - log("Failed to write file %s: %s" % (fileName, str(e))) + Utils.log("Failed to write file %s: %s" % (fileName, str(e))) return False @@ -76,7 +61,7 @@ def excludeVolume(volumeName, fileName=Globals.VOLUME_SMBCONF_FILE): if volumeFile not in entryList: return True entryList.remove(volumeFile) - log("entryList = %s" % entryList) + Utils.log("entryList = %s" % entryList) return writeVolumeSmbConfFile(entryList, fileName) @@ -99,7 +84,7 @@ def writeVolumeCifsConfiguration(volumeName, userList, adminUser=None): fp.close() return True except IOError, e: - log("Failed to write file %s: %s" % (volumeFile, str(e))) + Utils.log("Failed to write file %s: %s" % (volumeFile, str(e))) return False @@ -109,491 +94,6 @@ def removeVolumeCifsConfiguration(volumeName): os.remove(volumeFile) return True except OSError, e: - log("Failed to remove file %s: %s" % (volumeFile, str(e))) - return False - - -def getVolumeListByPartitionName(partitionName): - volumeConfigFileList = glob.glob(Globals.VOLUME_CONF_DIR + "/*.xml") - if not volumeConfigFileList: - return None - - volumeList = [] - for volumeXmlFile in volumeConfigFileList: - volumeDom = XDOM() - volumeDom.parseFile(volumeXmlFile) - serverTopology = volumeDom.getElementsByTagRoute("volume.topology.group") - serverPartitionFound = False - for topology in serverTopology: - partitionDom = XDOM() - for partition in topology.getElementsByTagName("partition"): - partitionDom.setDomObj(partition) - if partitionDom.getTextByTagRoute("name") == partitionName: - serverPartitionFound = True - break - if serverPartitionFound: - volumeList.append(volumeDom.getElementsByTagRoute("volume")[0]) - break - return volumeList - - -def addServerPartitionConfig(inputDom, groupOrder, partitionTag): - if not(inputDom and groupOrder and partitionTag): - return False - groupDom = XDOM() - for group in inputDom.getElementsByTagRoute("topology.group"): - groupDom.setDomObj(group) - order = groupDom.getTextByTagRoute("order") - if order and int(order) == groupOrder: - group.appendChild(partitionTag) - return inputDom - return False - - -def removeServerPartitionConfig(inputDom, partitionName): - if not(inputDom and partitionName): - return False - for group in inputDom.getElementsByTagRoute("topology.group"): - partitionDom = XDOM() - for partition in group.getElementsByTagName("partition"): - partitionDom.setDomObj(partition) - if partitionDom.getTextByTagRoute("name") == partitionName: - group.removeChild(partition) - return inputDom - return False - - -def updateServerPartitionConfig(inputDom, partitionName, partitionTag): - if not(inputDom and partitionName and partitionTag): - return False - for group in inputDom.getElementsByTagRoute("topology.group"): - partitionDom = XDOM() - for partition in group.getElementsByTagName("partition"): - partitionDom.setDomObj(partition) - if partitionDom.getTextByTagRoute("name") == partitionName: - try: - group.replaceChild(partitionTag, partition) - return inputDom - except AttributeError: - return False - return False - - -def getServerPartitionConfigUuid(serverGroupList, serverPartition): - for group in serverGroupList: - if not group: - continue - partitionDom = XDOM() - for partition in group.getElementsByTagName("partition"): - partitionDom.setDomObj(partition) - partitionName = partition.getTextByTagName("name") - if not partitionName: - continue - if partitionName == serverPartition: - return partitionDom.getTextByTagName("uuid") - return False - - -def setServerPartitionConfigProperty(inputDom, partitionName, propertyDict): - if not(inputDom and partitionName and propertyDict): - return False - for group in inputDom.getElementsByTagRoute("topology.group"): - partitionDom = XDOM() - for partition in group.getElementsByTagName("partition"): - partitionDom.setDomObj(partition) - if partitionDom.getTextByTagRoute("name") == partitionName: - for part in propertyDict.keys(): - x = partition.getElementsByTagName(part) - if x: - x[0].childNodes[0].nodeValue = propertyDict[part] - return inputDom - return False - - -def getSortedServerPartitionConfigProperty(inputDom): - groupDict = {} - if not inputDom: - return None - groupDom = XDOM() - for group in inputDom.getElementsByTagRoute("topology.group"): - groupDom.setDomObj(group) - groupOrder = groupDom.getTextByTagRoute("order") - if not groupOrder: - return None - groupOrder = int(groupOrder) - if groupOrder < 1: - return None - partitionDom = XDOM() - partitionDict = {} - for partition in group.getElementsByTagName("partition"): - partitionDom.setDomObj(partition) - partitionName = partitionDom.getTextByTagRoute("name") - if not partitionName: - return None - partitionOrder = partitionDom.getTextByTagRoute("order") - if not partitionOrder: - return None - partitionUuid = partitionDom.getTextByTagRoute("uuid") - partitionOrder = int(partitionOrder) - if partitionOrder < 1: - return None - partitionDetails = partitionName.split(":") - if not partitionDetails or len(partitionDetails) < 1: - return None - partitionDict[partitionOrder] = { "order":partitionOrder, - "servername":partitionDetails[0], - "name":partitionDetails[1], - "uuid":partitionUuid} - groupDict[groupOrder] = partitionDict - - serverList = [] - groupOrderList = groupDict.keys() - groupOrderList.sort() - for groupOrder in groupOrderList: - partitionOrderList = groupDict[groupOrder].keys() - partitionOrderList.sort() - for partitionOrder in partitionOrderList: - serverList.append(groupDict[groupOrder][partitionOrder]) - - return serverList - - -def getSortedServerPartitionList(serverGroupElements): - serverPartitionDict = {} - groupOrderList = [] - serverList = [] - partitionDom = XDOM() - for group in serverGroupElements: - if not group: - continue - groupOrderE = group.getElementsByTagName("order") - if not (groupOrderE and groupOrderE[0].childNodes): - return None - value = int(XDOM.getText(groupOrderE[0].childNodes)) - if value > 0: - groupOrderList.append(value) - partitionDict = {} - for partition in group.getElementsByTagName("partition"): - partitionDom.setDomObj(partition) - - partitionName = partitionDom.getTextByTagRoute("name") - if not partitionName: - return None - partitionOrder = partitionDom.getTextByTagRoute("order") - if not partitionOrder: - return None - partitionUuid = partitionDom.getTextByTagRoute("uuid") - partitionDict[int(partitionOrder)] = [partitionName, partitionUuid] - serverPartitionDict[value] = partitionDict - groupOrderList.sort() - - for groupOrder in groupOrderList: - items = serverPartitionDict[groupOrder].items() - items.sort(key = itemgetter(0)) - serverList = serverList + [ items[i][1] for i in range(0,len(items))] - return serverList - - -def clearExportDirectory(serverList, volumeName, volumeUuid): - thisServerName = getCurrentServerName() - for exportServer in serverList: - serverName, partition = exportServer[0].split(":") - if thisServerName != serverName: - continue - partitionUuid = getUuidByDiskPartition(getDevice(partition)) - if not partitionUuid: - log("unable to find uuid of partition %s" % partition) - return False - volumeDirName = "%s/%s/%s" % (Globals.GLUSTER_LUN_DIR, partitionUuid, volumeUuid) - if os.path.exists(volumeDirName): - ## Removing /data/PARTITION-UUID/VOLUME-UUID/ - ## TODO: Get an option to remove it at this time - if runCommandFG("mv -f %s %s.delete" % (volumeDirName, volumeDirName), root=True) != 0: - return False - if runCommandFG("rm -f %s/%s/volumes/%s" % (Globals.GLUSTER_LUN_DIR, partitionUuid, volumeName), root=True) != 0: - return False - return True - - -def createExportDirectory(serverList, volumeName, volumeUuid): - thisServerName = getCurrentServerName() - tempVolumeNameFile = getTempFileName() - - try: - fp = open(tempVolumeNameFile, "w") - fp.write("VOLUME_NAME=%s\n" % volumeName) - fp.write("VOLUME_UUID=%s\n" % volumeUuid) - fp.close() - except IOError, e: - log("failed to create temporary file for volume-name: %s" % (volumeName, str(e))) + Utils.log("Failed to remove file %s: %s" % (volumeFile, str(e))) return False - for exportServer in serverList: - serverName, partition = exportServer[0].split(":") - if thisServerName != serverName: - continue - partitionUuid = getUuidByDiskPartition(getDevice(partition)) - if not partitionUuid: - log("unable to find uuid of partition %s" % partition) - return False - - volumeDirName = "%s/%s/%s" % (Globals.GLUSTER_LUN_DIR, partitionUuid, volumeUuid) - ## Creating /data/PARTITION-UUID/VOLUME-UUID/ - if runCommandFG("mkdir %s" % volumeDirName, root=True) != 0: - return False - - ## Creating /data/PARTITION-UUID/VOLUME-UUID/exports/ - ## Creating /data/PARTITION-UUID/VOLUME-UUID/exports/brick1/ - if runCommandFG("mkdir -p %s/exports/brick1" % volumeDirName, root=True) != 0: - return False - - ## Creating /data/PARTITION-UUID/VOLUME-UUID/log/ - if runCommandFG("mkdir %s/log" % volumeDirName, root=True) != 0: - return False - - ## Creating /data/PARTITION-UUID/VOLUME-UUID/config/ - if runCommandFG("mkdir %s/config" % volumeDirName, root=True) != 0: - return False - - volumeLinkDirName = "%s/%s/volumes" % (Globals.GLUSTER_LUN_DIR, partitionUuid) - if not os.path.exists(volumeLinkDirName): - if runCommandFG("mkdir %s" % volumeLinkDirName, root=True) != 0: - return False - - ## Creating symlink - ## /data/PARTITION-UUID/volumes/VOLUME-NAME -> /data/PARTITION-UUID/VOLUME-UUID/ - command = "ln -fTs %s %s/%s" % (volumeDirName, - volumeLinkDirName, volumeName) - if runCommandFG(command, root=True) != 0: - return False - - if runCommandFG("cp -f %s %s/config/volume-name" % (tempVolumeNameFile, volumeDirName), root=True) != 0: - return False - - try: - os.remove(tempVolumeNameFile) - except OSError, e: - log("Failed to remove file %s: %s" % (tempVolumeNameFile, str(e))) - - return True - - -def getPartitionListByServerName(volumeDom, serverName, serverPartitionList=None): - partitionList = {} - if serverPartitionList: - for partitionName in serverPartitionList: - partitionUuid = getServerDiskPartitionUuid(serverName, partitionName) - if not partitionUuid: - log(syslog.LOG_ERR, "failed to get disk partition %s uuid of server %s" % (partitionName, serverName)) - return None - partitionList[partitionName] = partitionUuid - return partitionList - for group in volumeDom.getElementsByTagRoute("topology.group"): - for partitionTag in group.getElementsByTagName("partition"): - nameE = partitionTag.getElementsByTagName("name") - if not nameE: - continue - partition = XDOM.getText(nameE[0].childNodes) - if not partition: - continue - server, partitionName = partition.split(":") - if server != serverName: - continue - partitionUuid = getServerDiskPartitionUuid(serverName, partitionName) - if not partitionUuid: - log(syslog.LOG_ERR, "failed to get disk partition %s uuid of server %s" % (partitionName, serverName)) - return None - partitionList[partitionName] = partitionUuid - return partitionList - - -def isVolumeRunning(volumeName): - return Glusterd.isVolumeRunning(volumeName) - -def addVolumeMigrationDetails(sourcePartition, destinationPartition, volumeName): - migrationDom = XDOM() - if not os.path.exists(Globals.VOLUME_MIGRATION_LIST_FILE): - migrationDom.appendTagRoute("volume-migration") - else: - if not migrationDom.parseFile(Globals.VOLUME_MIGRATION_LIST_FILE): - log("Failed to load volume-migration.xml file") - return None - migrationList = migrationDom.getElementsByTagRoute("volume-migration.migration") - for tagE in migrationList: - dom = XDOM() - dom.setDomObj(tagE) - if dom.getTextByTagRoute("source-partition") == sourcePartition and \ - dom.getTextByTagRoute("destination-partition") == destinationPartition and \ - dom.getTextByTagRoute("volume-name") == volumeName: - return False - migrationTag = migrationDom.getElementsByTagRoute("volume-migration") - if not migrationTag: - return None - partitionTag = migrationDom.createTag("migration") - partitionTag.appendChild(migrationDom.createTag("source-partition", sourcePartition)) - partitionTag.appendChild(migrationDom.createTag("destination-partition", destinationPartition)) - partitionTag.appendChild(migrationDom.createTag("volume-name", volumeName)) - migrationTag[0].appendChild(partitionTag) - if not migrationDom.writexml(Globals.VOLUME_MIGRATION_LIST_FILE): - log("Unable to write disk migration details into %s/volume-migration.xml" % Globals.GLUSTER_BASE_DIR) - return False - return True - - -def removeVolumeMigrationDetails(sourcePartition, destinationPartition, volumeName): - migrationDom = XDOM() - if not os.path.exists(Globals.VOLUME_MIGRATION_LIST_FILE): - return None - if not migrationDom.parseFile(Globals.VOLUME_MIGRATION_LIST_FILE): - log("Failed to load volume-migration.xml file") - return None - migrationList = migrationDom.getElementsByTagRoute("volume-migration.migration") - for tagE in migrationList: - dom = XDOM() - dom.setDomObj(tagE) - if dom.getTextByTagRoute("source-partition") == sourcePartition and \ - dom.getTextByTagRoute("destination-partition") == destinationPartition and \ - dom.getTextByTagRoute("volume-name") == volumeName: - migrationDom.getElementsByTagRoute("volume-migration")[0].removeChild(tagE) - if not migrationDom.writexml(Globals.VOLUME_MIGRATION_LIST_FILE): - log("Unable to write disk migration details into %s/volume-migration.xml" % Globals.GLUSTER_BASE_DIR) - return False - return True - - -def addPartitionMigrationDetails(sourcePartition, destinationPartition, volumeList=None): - migrationDom = XDOM() - if not os.path.exists(Globals.MIGRATE_PARTITION_LIST_FILE): - migrationDom.appendTagRoute("partition-migration") - else: - if not migrationDom.parseFile(Globals.MIGRATE_PARTITION_LIST_FILE): - log("Failed to load migration.xml file") - return None - migrationList = migrationDom.getElementsByTagRoute("partition-migration.migration") - for tagE in migrationList: - dom = XDOM() - dom.setDomObj(tagE) - if dom.getTextByTagRoute("source-partition") == sourcePartition: - return False - if dom.getTextByTagRoute("destination-partition") == destinationPartition: - return False - migrationTag = migrationDom.getElementsByTagRoute("partition-migration") - if not migrationTag: - return None - partitionTag = migrationDom.createTag("migration") - partitionTag.appendChild(migrationDom.createTag("source-partition", sourcePartition)) - partitionTag.appendChild(migrationDom.createTag("destination-partition", destinationPartition)) - migrationTag[0].appendChild(partitionTag) - if not migrationDom.writexml(Globals.MIGRATE_PARTITION_LIST_FILE): - log("Unable to write disk migration details into %s/migration.xml" % Globals.GLUSTER_BASE_DIR) - return False - if volumeList: - for volumeName in volumeList: - addVolumeMigrationDetails(sourcePartition, destinationPartition, volumeName) - return True - - -def removePartitionMigrationDetails(sourcePartition, destinationPartition, volumeList=None): - migrationDom = XDOM() - if not os.path.exists(Globals.MIGRATE_PARTITION_LIST_FILE): - return None - if not migrationDom.parseFile(Globals.MIGRATE_PARTITION_LIST_FILE): - log("Failed to load migration.xml file") - return None - migrationList = migrationDom.getElementsByTagRoute("partition-migration.migration") - for tagE in migrationList: - dom = XDOM() - dom.setDomObj(tagE) - if dom.getTextByTagRoute("source-partition") == sourcePartition and \ - dom.getTextByTagRoute("destination-partition") == destinationPartition: - migrationDom.getElementsByTagRoute("partition-migration")[0].removeChild(tagE) - if not migrationDom.writexml(Globals.MIGRATE_PARTITION_LIST_FILE): - log("Unable to write disk migration details into %s/migration.xml" % Globals.GLUSTER_BASE_DIR) - return False - if volumeList: - for volumeName in volumeList: - removeVolumeMigrationDetails(sourcePartition, destinationPartition, volumeName) - return True - - -def isMigrationInProgress(partition): - migrationDom = XDOM() - if not os.path.exists(Globals.MIGRATE_PARTITION_LIST_FILE): - return None - if not migrationDom.parseFile(Globals.MIGRATE_PARTITION_LIST_FILE): - log("Failed to load migration.xml file") - return None - migrationList = migrationDom.getElementsByTagRoute("partition-migration.migration") - for tagE in migrationList: - dom = XDOM() - dom.setDomObj(tagE) - if migrationDom.getTextByTagRoute("source-partition") == partition or \ - migrationDom.getTextByTagRoute("destination-partition") == partition: - return True - return False - - -def getServerDiskPartitionUuid(serverName, partition): - diskConfigDom = XDOM() - if not diskConfigDom.parseFile("%s/%s/disk.xml" % (Globals.SERVER_CONF_DIR, serverName)): - return None - for disk in diskConfigDom.getElementsByTagRoute("disks.disk"): - diskDom = XDOM() - diskDom.setDomObj(disk) - partitionList = diskDom.getElementsByTagRoute("partition") - for tagE in partitionList: - partitionDom = XDOM() - partitionDom.setDomObj(tagE) - if partitionDom.getTextByTagRoute("device") == partition: - return partitionDom.getTextByTagRoute("uuid") - - -def getVolumeServerList(requestDom, requestFlag=True): - if requestFlag: - serverGroupElementList = requestDom.getElementsByTagRoute("command.volume.topology.group") - else: - serverGroupElementList = requestDom.getElementsByTagRoute("volume.topology.group") - if not serverGroupElementList: - return None - serverList = [] - partitionDom = XDOM() - for group in serverGroupElementList: - for partition in group.getElementsByTagName("partition"): - partitionDom.setDomObj(partition) - partitionName = partitionDom.getTextByTagRoute("name") - if not partitionName: - continue - serverPartition = partitionName.split(":") - if not(len(serverPartition) > 1 and serverPartition[1]): - return None - if serverPartition[0] not in serverList: - serverList.append(serverPartition[0]) - return serverList - - -def getVolumeServerListByName(volumeName): - serverList = [] - serverDom = XDOM() - volumeDom = XDOM() - if not os.path.exists("%s/%s.xml" % (Globals.VOLUME_CONF_DIR, volumeName)): - return False - if not volumeDom.parseFile("%s/%s.xml" % (Globals.VOLUME_CONF_DIR, volumeName)): - return False - return getVolumeServerList(volumeDom, False) - - -def getMigrateVolumeServerPartitionInfo(volumeName): - volumeMigrationDom = XDOM() - if not volumeMigrationDom.parseFile(Globals.VOLUME_MIGRATION_LIST_FILE): - Utils.log("Failed to parse file %s" % Globals.VOLUME_MIGRATION_LIST_FILE) - return None - volumeInfo = {} - dom = XDOM() - for tagE in volumeMigrationDom.getElementsByTagRoute("volume-migration.migration"): - dom.setDomObj(tagE) - if dom.getTextByTagRoute("volume-name") == volumeName: - volumeInfo['Name'] = volumeName - volumeInfo['SourcePartition'] = dom.getTextByTagRoute("source-partition") - volumeInfo['DestinationPartition'] = dom.getTextByTagRoute("destination-partition") - return volumeInfo - return None |