diff options
author | Selvasundaram <selvam@gluster.com> | 2011-09-27 12:11:15 +0530 |
---|---|---|
committer | Selvasundaram <selvam@gluster.com> | 2011-09-27 12:11:15 +0530 |
commit | cdf343951cd9848ff502a1157f2db5146d11a491 (patch) | |
tree | 5d083d6dd5fbcae2dfe6c986dfbb5a451913d000 | |
parent | deae182e1269112486d752439717bea7fe3615b3 (diff) | |
parent | f3b959a9ece36c64a5a5fea19b91304105fa1023 (diff) |
Merge branch 'master' of github.com:gluster/console
47 files changed, 779 insertions, 2089 deletions
diff --git a/build/create-rpms.sh b/build/create-rpms.sh index b02ae91e..db7ea679 100755 --- a/build/create-rpms.sh +++ b/build/create-rpms.sh @@ -7,3 +7,4 @@ sudo rm -rf rpms || exit ${FILE_ERR} sudo mkdir rpms || exit ${FILE_ERR} sudo mv /usr/src/redhat/RPMS/x86_64/glustermg*.rpm rpms || exit ${FILE_ERR} sudo chown -R jenkins:jenkins rpms || exit ${FILE_ERR} +sudo rm -f /usr/src/redhat/SOURCES/glustermg-backend-${VERSION}.tar.gz /usr/src/redhat/SOURCES/glustermg-${VERSION}.war.tar.gz diff --git a/src/com.gluster.storage.management.core/.project b/src/com.gluster.storage.management.core/.project index 6c8fbb5c..c016cd3c 100644 --- a/src/com.gluster.storage.management.core/.project +++ b/src/com.gluster.storage.management.core/.project @@ -20,9 +20,15 @@ <arguments> </arguments> </buildCommand> + <buildCommand> + <name>com.instantiations.assist.eclipse.coverage.instrumentationBuilder</name> + <arguments> + </arguments> + </buildCommand> </buildSpec> <natures> <nature>org.eclipse.pde.PluginNature</nature> <nature>org.eclipse.jdt.core.javanature</nature> + <nature>com.instantiations.assist.eclipse.coverage.codeCoverageNature</nature> </natures> </projectDescription> diff --git a/src/com.gluster.storage.management.core/junit/com/gluster/storage/management/core/utils/StringUtilTest.java b/src/com.gluster.storage.management.core/junit/com/gluster/storage/management/core/utils/StringUtilTest.java new file mode 100644 index 00000000..dc8c6c80 --- /dev/null +++ b/src/com.gluster.storage.management.core/junit/com/gluster/storage/management/core/utils/StringUtilTest.java @@ -0,0 +1,309 @@ +package com.gluster.storage.management.core.utils; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + + +/** + * The class <code>StringUtilTest</code> contains tests for the class <code>{@link StringUtil}</code>. + * + * @generatedBy CodePro at 21/9/11 4:53 PM + * @author root + * @version $Revision: 1.0 $ + */ +public class StringUtilTest { + public enum Season { WINTER, SPRING, SUMMER, FALL }; + /** + * Run the String collectionToString(Collection<? extends Object>,String) method test. + * + * @throws Exception + * + * @generatedBy CodePro at 21/9/11 4:53 PM + */ + @Test + public void testCollectionToString_1() + throws Exception { + List<String> string = new ArrayList<String>(); + string.add("test string"); + String delimiter = ""; + + String result = StringUtil.collectionToString(string, delimiter); + assertEquals("test string", result); + } + + /** + * Run the String collectionToString(Collection<? extends Object>,String) method test. + * + * @throws Exception + * + * @generatedBy CodePro at 21/9/11 4:53 PM + */ + @Test + public void testCollectionToString_2() + throws Exception { + List<String> string = new ArrayList<String>(); + string.add("test string"); + string.add("welcome to world"); + String delimiter = "::"; + + String result = StringUtil.collectionToString(string, delimiter); + + assertEquals("test string::welcome to world", result); + } + + /** + * Run the String collectionToString(Collection<? extends Object>,String) method test. + * + * @throws Exception + * + * @generatedBy CodePro at 21/9/11 4:53 PM + */ + @Test + public void testCollectionToString_3() + throws Exception { + List<String> string = new ArrayList<String>(); + string.add("test ## string"); + string.add("java world"); + String delimiter = "##"; + + String result = StringUtil.collectionToString(string, delimiter); + assertEquals("test ## string##java world", result); + } + + /** + * Run the String collectionToString(Collection<? extends Object>,String) method test. + * + * @throws Exception + * + * @generatedBy CodePro at 21/9/11 4:53 PM + */ + @Test + public void testCollectionToString_4() + throws Exception { + List<String> string = new ArrayList<String>(); + String delimiter = ""; + + String result = StringUtil.collectionToString(string, delimiter); + assertEquals("", result); + } + + /** + * Run the List<String> enumToArray(T[]) method test. + * + * @throws Exception + * + * @generatedBy CodePro at 21/9/11 4:53 PM + */ + @Test + public void testEnumToArray_1() + throws Exception { + + String[] expected = new String[] {"WINTER", "SPRING", "SUMMER", "FALL"}; + List<String> result = StringUtil.enumToArray(Season.values()); + + assertNotNull(result); + assertEquals(4, result.size()); + } + + /** + * Run the List<String> extractList(String,String) method test. + * + * @throws Exception + * + * @generatedBy CodePro at 21/9/11 4:53 PM + */ + @Test + public void testExtractList_1() + throws Exception { + String input = "This is test message"; + String delim = " "; + + List<String> result = StringUtil.extractList(input, delim); + + assertNotNull(result); + assertEquals(4, result.size()); + } + + /** + * Run the List<String> extractList(String,String) method test. + * + * @throws Exception + * + * @generatedBy CodePro at 21/9/11 4:53 PM + */ + @Test + public void testExtractList_2() + throws Exception { + String input = "welcome#to#java#world"; + String delim = "#"; + + List<String> result = StringUtil.extractList(input, delim); + + assertNotNull(result); + assertEquals(4, result.size()); + } + + /** + * Run the List<String> extractList(String,String) method test. + * + * @throws Exception + * + * @generatedBy CodePro at 21/9/11 4:53 PM + */ + @Test + public void testExtractList_3() + throws Exception { + String input = "list$to%string"; + String delim = "%"; + + List<String> result = StringUtil.extractList(input, delim); + + assertNotNull(result); + assertEquals(2, result.size()); + } + + /** + * Run the Map<String, String> extractMap(String,String,String) method test. + * + * @throws Exception + * + * @generatedBy CodePro at 21/9/11 4:53 PM + */ + @Test + public void testExtractMap_1() + throws Exception { + String input = "k1=v1,k2=v2,k3=v3"; + String majorDelim = ","; + String minorDelim = "="; + + Map<String, String> result = StringUtil.extractMap(input, majorDelim, minorDelim); + + // add additional test code here + assertNotNull(result); + assertEquals(3, result.size()); + } + + /** + * Run the Map<String, String> extractMap(String,String,String) method test. + * + * @throws Exception + * + * @generatedBy CodePro at 21/9/11 4:53 PM + */ + @Test + public void testExtractMap_2() + throws Exception { + String input = "k1=>v1&k2=>v2&k3=>v3"; + String majorDelim = "&"; + String minorDelim = "=>"; + + Map<String, String> result = StringUtil.extractMap(input, majorDelim, minorDelim); + + // add additional test code here + assertNotNull(result); + assertEquals(3, result.size()); + } + + /** + * Run the boolean filterString(String,String,boolean) method test. + * + * @throws Exception + * + * @generatedBy CodePro at 21/9/11 4:53 PM + */ + @Test + public void testFilterString_1() + throws Exception { + String sourceString = "This is java program"; + String filterString = "Java"; + boolean caseSensitive = true; + + boolean result = StringUtil.filterString(sourceString, filterString, caseSensitive); + + assertEquals(false, result); + } + + /** + * Run the boolean filterString(String,String,boolean) method test. + * + * @throws Exception + * + * @generatedBy CodePro at 21/9/11 4:53 PM + */ + @Test + public void testFilterString_2() + throws Exception { + String sourceString = "This is java program"; + String filterString = "Java"; + boolean caseSensitive = false; + + boolean result = StringUtil.filterString(sourceString, filterString, caseSensitive); + + assertEquals(true, result); + } + + /** + * Run the String removeSpaces(String) method test. + * + * @throws Exception + * + * @generatedBy CodePro at 21/9/11 4:53 PM + */ + @Test + public void testRemoveSpaces_1() + throws Exception { + String str = "this is test string"; + + String result = StringUtil.removeSpaces(str); + + // add additional test code here + assertEquals("thisisteststring", result); + } + + /** + * Perform pre-test initialization. + * + * @throws Exception + * if the initialization fails for some reason + * + * @generatedBy CodePro at 21/9/11 4:53 PM + */ + @Before + public void setUp() + throws Exception { + // add additional set up code here + } + + /** + * Perform post-test clean-up. + * + * @throws Exception + * if the clean-up fails for some reason + * + * @generatedBy CodePro at 21/9/11 4:53 PM + */ + @After + public void tearDown() + throws Exception { + // Add additional tear down code here + } + + /** + * Launch the test. + * + * @param args the command line arguments + * + * @generatedBy CodePro at 21/9/11 4:53 PM + */ + public static void main(String[] args) { + new org.junit.runner.JUnitCore().run(StringUtilTest.class); + } +}
\ No newline at end of file diff --git a/src/com.gluster.storage.management.core/src/com/gluster/storage/management/core/model/Server.java b/src/com.gluster.storage.management.core/src/com/gluster/storage/management/core/model/Server.java index ad2599a6..3662188c 100644 --- a/src/com.gluster.storage.management.core/src/com/gluster/storage/management/core/model/Server.java +++ b/src/com.gluster.storage.management.core/src/com/gluster/storage/management/core/model/Server.java @@ -199,7 +199,6 @@ public class Server extends Entity { * * @param server */ - @SuppressWarnings("unchecked") public void copyFrom(Server server) { setName(server.getName()); setParent(server.getParent()); diff --git a/src/com.gluster.storage.management.core/src/com/gluster/storage/management/core/utils/StringUtil.java b/src/com.gluster.storage.management.core/src/com/gluster/storage/management/core/utils/StringUtil.java index cdf71dcc..7d9ec340 100644 --- a/src/com.gluster.storage.management.core/src/com/gluster/storage/management/core/utils/StringUtil.java +++ b/src/com.gluster.storage.management.core/src/com/gluster/storage/management/core/utils/StringUtil.java @@ -18,7 +18,6 @@ *******************************************************************************/ package com.gluster.storage.management.core.utils; -import java.text.NumberFormat; import java.util.ArrayList; import java.util.Collection; import java.util.LinkedHashMap; @@ -101,27 +100,4 @@ public class StringUtil { } return output; } - - public static String formatNumber(Double number, int dec) { - NumberFormat nf = NumberFormat.getInstance(); - nf.setMaximumFractionDigits(dec); - nf.setGroupingUsed(false); - // Setting this to true will give you xx,xxx,xxx type of formatting. - String formattedvalue = nf.format(number); - return formattedvalue; - } - - public static void main(String args[]) { - - //Test case for "ListToString" - List<String> string = new ArrayList<String>(); - // Empty list - System.out.println(StringUtil.collectionToString(string, ", ")); - // Only one - string.add("test"); - System.out.println(StringUtil.collectionToString(string, ",:")); - // Multiple - string.add("welcome to java"); - System.out.println(StringUtil.collectionToString(string, "")); - } } diff --git a/src/com.gluster.storage.management.gateway.scripts/src/backend/Disk.py b/src/com.gluster.storage.management.gateway.scripts/src/backend/Disk.py deleted file mode 100755 index 3b44e3a8..00000000 --- a/src/com.gluster.storage.management.gateway.scripts/src/backend/Disk.py +++ /dev/null @@ -1,127 +0,0 @@ -# Copyright (c) 2011 Gluster, Inc. <http://www.gluster.com> -# This file is part of GlusterSP. -# - -import os -import dbus - -class Disk: - def __init__(self): - """init""" - - self.volumes = [] - self.disks = [] - self.bus = dbus.SystemBus() - self.hal_obj = self.bus.get_object("org.freedesktop.Hal", - "/org/freedesktop/Hal/Manager") - self.hal = dbus.Interface(self.hal_obj, "org.freedesktop.Hal.Manager") - self.devices = [] - self.devices = self.hal.FindDeviceByCapability("storage") - - self.detect_disks() - self.detect_mountable_volumes() - - def getDiskList(self): - - return self.disks - - def getMountableDiskList(self): - - return self.volumes - - def detect_disks(self): - for device in self.devices: - dev = self._get_device(device) - if dev.GetProperty("storage.drive_type") != "cdrom": - if not dev.GetProperty("block.is_volume"): - self._add_disks(dev) - continue - - def _add_disks(self, dev): - disk = str(dev.GetProperty('block.device')) - disk_size = str(int(dev.GetProperty('storage.size')) / 1024**2) - - try: - if dev.GetProperty('storage.removable'): - disk_size = str(int(dev.GetProperty('storage.removable.media_size')) / 1024**2) - except: # TODO: Add appropriated exception on property error. - return - - self.disks.append({ - 'device': disk, - 'description': str(dev.GetProperty('storage.model')) + " " + str(dev.GetProperty('storage.vendor')), - 'interface': str(dev.GetProperty('storage.bus')), - 'size': disk_size, - 'drive_type': str(dev.GetProperty('storage.drive_type')) - }) - - def detect_mountable_volumes(self): - """ Detect all mountable volumes using HAL via D-Bus """ - for device in self.devices: - dev = self._get_device(device) - if dev.GetProperty("storage.drive_type") != "cdrom": - if dev.GetProperty("block.is_volume"): - self._add_volume(dev) - continue - else: # iterate over children looking for a volume - children = self.hal.FindDeviceStringMatch("info.parent", - device) - if not children and "disk" == dev.GetProperty("storage.drive_type"): - self._add_volume(dev) - for child in children: - child = self._get_device(child) - if child.GetProperty("block.is_volume"): - self._add_volume(child, parent=dev) - #break # don't break, allow all partitions - - def _add_volume(self, dev, parent=None): - volume = str(dev.GetProperty('block.device')) - if not parent: - self.volumes.append ({ - 'device' : volume, - 'label' : str(dev.GetProperty('block.device')), - 'fstype' : None, - 'fsversion': None, - 'uuid' : None, - 'interface': str(dev.GetProperty('storage.bus')), - 'parent' : None, - 'description': str(dev.GetProperty('storage.model')) + " " + str(dev.GetProperty('storage.vendor')), - 'size' : None, - 'totalsize' : str(int(dev.GetProperty('storage.size')) / 1024**2), - 'drive_type': str(dev.GetProperty('storage.drive_type')), - 'mount_point': "NA" - }) - return - - self.volumes.append ({ - 'device' : volume, - 'label' : str(dev.GetProperty('volume.label')), - 'fstype' : str(dev.GetProperty('volume.fstype')), - 'fsversion': str(dev.GetProperty('volume.fsversion')), - 'uuid' : str(dev.GetProperty('volume.uuid')), - 'interface': str(parent.GetProperty('storage.bus')), - 'parent' : str(parent.GetProperty('block.device')), - 'description': str(parent.GetProperty('storage.model')) + " " + str(parent.GetProperty('storage.vendor')), - 'size' : str(int(dev.GetProperty('volume.size')) / 1024**2), - 'totalsize' : str(int(parent.GetProperty('storage.size')) / 1024**2), - 'drive_type': str(parent.GetProperty('storage.drive_type')), - 'mount_point': str(dev.GetProperty('volume.mount_point')) - }) - return - - def _get_device(self, udi): - """ Return a dbus Interface to a specific HAL device UDI """ - dev_obj = self.bus.get_object("org.freedesktop.Hal", udi) - return dbus.Interface(dev_obj, "org.freedesktop.Hal.Device") - - def get_free_bytes(self, device=None): - """ Return the number of available bytes on our device """ - import statvfs - stat = os.statvfs(device) - return stat[statvfs.F_BSIZE] * stat[statvfs.F_BAVAIL] - - def get_used_bytes(self, device=None): - """ Return the number of used bytes on our device """ - import statvfs - stat = os.statvfs(device) - return ((stat[statvfs.F_BSIZE] * stat[statvfs.F_BLOCKS]) - (stat[statvfs.F_BSIZE] * stat[statvfs.F_BAVAIL])) diff --git a/src/com.gluster.storage.management.gateway.scripts/src/backend/DiskUtils.py b/src/com.gluster.storage.management.gateway.scripts/src/backend/DiskUtils.py index 5d7b0b4a..4d1b701a 100644 --- a/src/com.gluster.storage.management.gateway.scripts/src/backend/DiskUtils.py +++ b/src/com.gluster.storage.management.gateway.scripts/src/backend/DiskUtils.py @@ -12,18 +12,10 @@ if not p1 in sys.path: if not p2 in sys.path: sys.path.append(p2) import glob -from copy import deepcopy -import dbus import Globals -import time import Utils -import Disk -import Protocol import FsTabUtils -ONE_MB_SIZE = 1048576 - - def _stripDev(device): if Utils.isString(device) and device.startswith("/dev/"): return device[5:] @@ -70,11 +62,6 @@ def getUuidByDiskPartition(device): return None -def getDiskPartitionUuid(partition): - Utils.log("WARNING: getDiskPartitionUuid() is deprecated by getUuidByDiskPartition()") - return getUuidByDiskPartition(partition) - - def getDiskPartitionByLabel(label): ## TODO: Finding needs to be enhanced labelFile = "/dev/disk/by-label/%s" % label @@ -84,11 +71,6 @@ def getDiskPartitionByLabel(label): return None -def getDeviceByLabel(label): - Utils.log("WARNING: getDeviceByLabel() is deprecated by getDiskPartitionByLabel()") - return getDiskPartitionByLabel(label) - - def getDiskPartitionLabel(device): rv = Utils.runCommand("e2label %s" % device, output=True, root=True) if rv["Status"] == 0: @@ -96,78 +78,6 @@ def getDiskPartitionLabel(device): return False -def getRootPartition(fsTabFile=Globals.FSTAB_FILE): - fsTabEntryList = FsTabUtils.readFsTab(fsTabFile) - for fsTabEntry in fsTabEntryList: - if fsTabEntry["MountPoint"] == "/": - if fsTabEntry["Device"].startswith("UUID="): - return getDiskPartitionByUuid(fsTabEntry["Device"].split("UUID=")[-1]) - if fsTabEntry["Device"].startswith("LABEL="): - partitionName = getDiskPartitionByLabel(fsTabEntry["Device"].split("LABEL=")[-1]) - if partitionName: - return partitionName - return getDeviceName(fsTabEntry["Device"]) - return None - -def getRaidDisk(): - array = [] - arrayList = [] - mdFound = False - - try: - fp = open("/proc/mdstat") - for line in fp: - str = line.strip() - if str.startswith("md"): - array.append(str) - mdFound = True - continue - if mdFound: - if str: - array.append(str) - else: - arrayList.append(array) - array = [] - mdFound = False - fp.close() - except IOError, e: - return None - - raidList = {} - for array in arrayList: - raid = {} - tokens = array[0].split() - raid['Interface'] = tokens[3] - device = getDevice(tokens[0]) - raid['MountPoint'] = getDeviceMountPoint(device) - if raid['MountPoint']: - raid['Type'] = "DATA" - raid['SpaceInUse'] = getDeviceUsedSpace(device) - else: - raid['SpaceInUse'] = None - rv = Utils.runCommand("blkid -c /dev/null %s" % (device), output=True, root=True) - raid['Uuid'] = None - raid['FsType'] = None - raid['Status'] = "UNINITIALIZED" - if isDiskInFormatting(device): - raid['Status'] = "INITIALIZING" - if not rv["Stderr"]: - words = rv["Stdout"].strip().split() - if words: - raid['Status'] = "INITIALIZED" - if len(words) > 2: - raid['Uuid'] = words[1].split("UUID=")[-1].split('"')[1] - raid['FsType'] = words[2].split("TYPE=")[-1].split('"')[1] - raid['Disks'] = [x.split('[')[0] for x in tokens[4:]] - raid['Size'] = float(array[1].split()[0]) / 1024.0 - raidList[tokens[0]] = raid - return raidList - - -def getOsDisk(): - Utils.log("WARNING: getOsDisk() is deprecated by getRootPartition()") - return getRootPartition() - def getDiskInfo(diskNameList=None): procPartitionsDict = getProcPartitions() diskDict = {} @@ -239,127 +149,17 @@ def getDiskInfo(diskNameList=None): return outputDict -def getDiskList(diskDeviceList=None): - return diskInfo["disks"] - - -def checkDiskMountPoint(diskMountPoint): - try: - fstabEntries = open(Globals.FSTAB_FILE).readlines() - except IOError, e: - fstabEntries = [] - Utils.log("failed to read file %s: %s" % (Globals.FSTAB_FILE, str(e))) - found = False - for entry in fstabEntries: - entry = entry.strip() - if not entry: - continue - entries = entry.split() - if entries and len(entries) > 1 and entries[0].startswith("UUID=") and entries[1].upper() == diskMountPoint.upper(): - return True - return False - - -def getMountPointByUuid(partitionUuid): - # check uuid in etc/fstab - try: - fstabEntries = open(Globals.FSTAB_FILE).readlines() - except IOError, e: - fstabEntries = [] - Utils.log("failed to read file %s: %s" % (Globals.FSTAB_FILE, str(e))) - found = False - for entry in fstabEntries: - entry = entry.strip() - if not entry: - continue - if entry.split()[0] == "UUID=" + partitionUuid: - return entry.split()[1] - return None - -def getDeviceUsedSpace(device): - rv = Utils.runCommand("df -kl %s" % (device), output=True, root=True) - if rv["Status"] == 0: - try: - return long(rv["Stdout"].split("\n")[1].split()[2]) / 1024 - except IndexError, e: - pass - except ValueError, e: - pass - -def getDiskSizeInfo(partition): - # get values from df output - total = None - used = None - free = None - command = "df -kl -t ext3 -t ext4 -t xfs" - rv = Utils.runCommand(command, output=True, root=True) - message = Utils.stripEmptyLines(rv["Stdout"]) - if rv["Status"] != 0: - Utils.log("failed to get disk partition details") - return None, None, None - for line in rv["Stdout"].split("\n"): - tokens = line.split() - if len(tokens) < 4: - continue - if tokens[0] == partition: - total = int(tokens[1]) / 1024.0 - used = int(tokens[2]) / 1024.0 - free = int(tokens[3]) / 1024.0 - break - - if total: - return total, used, free - - # get total size from parted output - for i in range(len(partition), 0, -1): - pos = i - 1 - if not partition[pos].isdigit(): - break - disk = partition[:pos+1] - partitionNumber = partition[pos+1:] - if not partitionNumber.isdigit(): - return None, None, None - - number = int(partitionNumber) - command = "parted -ms %s unit kb print" % disk - rv = Utils.runCommand(command, output=True, root=True) - if rv["Status"] != 0: - Utils.log("failed to get disk partition details") - return None, None, None - - lines = rv["Stdout"].split(";\n") - if len(lines) < 3: - return None,None,None - - for line in lines[2:]: - tokens = line.split(':') - if len(tokens) < 4: - continue - if tokens[0] == str(number): - total = int(tokens[3].split('kB')[0]) / 1024.0 - break - return total, used, free - - def isDataDiskPartitionFormatted(device): - #if getDiskPartitionLabel(device) != Globals.DATA_PARTITION_LABEL: - # return False - device = getDeviceName(device) - diskObj = Disk.Disk() - for disk in diskObj.getMountableDiskList(): - if disk['device'].upper() == device.upper(): - mountPoint = disk['mount_point'] - if not mountPoint: - return False - if not os.path.exists(mountPoint): - return False + rv = Utils.runCommand("blkid -c /dev/null -o value %s" % device, output=True, root=True) + if rv["Status"] != 0: + return False uuid = getUuidByDiskPartition(device) if not uuid: return False for fsTabEntry in FsTabUtils.readFsTab(): - if fsTabEntry["Device"] == ("UUID=%s" % uuid) and fsTabEntry["MountPoint"] == mountPoint: + if fsTabEntry["Device"] == ("UUID=%s" % uuid) or fsTabEntry["Device"] == device: return True return False @@ -369,11 +169,6 @@ def isDiskInFormatting(device): return os.path.exists(DEVICE_FORMAT_LOCK_FILE) -def isDiskInFormat(device): - Utils.log("WARNING: isDiskInFormat() is deprecated by isDataDiskPartitionFormatted()") - return isDataDiskPartitionFormatted(device) - - def getDeviceMountPoint(device): lines = Utils.readFile("/proc/mounts", lines=True) uuid = getUuidByDiskPartition(device) diff --git a/src/com.gluster.storage.management.gateway.scripts/src/backend/FsTabUtils.py b/src/com.gluster.storage.management.gateway.scripts/src/backend/FsTabUtils.py index 368b7a15..653d0dda 100644 --- a/src/com.gluster.storage.management.gateway.scripts/src/backend/FsTabUtils.py +++ b/src/com.gluster.storage.management.gateway.scripts/src/backend/FsTabUtils.py @@ -11,17 +11,14 @@ if not p1 in sys.path: sys.path.append(p1) if not p2 in sys.path: sys.path.append(p2) +import Utils import Globals def readFsTab(fsTabFile=Globals.FSTAB_FILE): - try: - fsTabfp = open(fsTabFile) - except IOError, e: - log("readFsTab(): " + str(e)) - return None + lines = Utils.readFile(fsTabFile) fsTabEntryList = [] - for line in fsTabfp: + for line in lines: tokens = line.strip().split() if not tokens or tokens[0].startswith('#'): continue @@ -43,8 +40,6 @@ def readFsTab(fsTabFile=Globals.FSTAB_FILE): pass if fsTabEntry["Device"] and fsTabEntry["MountPoint"] and fsTabEntry["FsType"] and fsTabEntry["Options"]: fsTabEntryList.append(fsTabEntry) - - fsTabfp.close() return fsTabEntryList def writeFsTab(fsTabEntryList, fsTabFile=Globals.FSTAB_FILE): diff --git a/src/com.gluster.storage.management.gateway.scripts/src/backend/NetworkUtils.py b/src/com.gluster.storage.management.gateway.scripts/src/backend/NetworkUtils.py index 27a9c056..2b28a00c 100755 --- a/src/com.gluster.storage.management.gateway.scripts/src/backend/NetworkUtils.py +++ b/src/com.gluster.storage.management.gateway.scripts/src/backend/NetworkUtils.py @@ -10,284 +10,68 @@ if not p1 in sys.path: sys.path.append(p1) if not p2 in sys.path: sys.path.append(p2) - -if not "/usr/share/system-config-network/" in sys.path: - sys.path.append("/usr/share/system-config-network") - -import os -import tempfile import Globals +import Utils -from Utils import * -#from netconfpkg.NCHardwareList import getHardwareList - -def readHostFile(fileName=None): - hostEntryList = [] - if not fileName: - fileName = "/etc/hosts" - try: - for line in open(fileName): - tokens = line.split("#")[0].strip().split() - if len(tokens) < 2: - continue - hostEntryList.append({tokens[0] : tokens[1:]}) - return hostEntryList - except IOError, e: - log("failed to read %s file: %s" % (fileName, str(e))) - return None - - -def writeHostFile(hostEntryList, fileName=None): - if fileName: - hostFile = fileName - else: - hostFile = tempfile.mktemp(prefix="GSPSA") - try: - fp = open(hostFile, "w") - for host in hostEntryList: - fp.write("%s\t%s\n" % (host.keys()[0], " ".join(host.values()[0]))) - fp.close() - if hostFile == fileName: - return True - except IOError, e: - log("failed to write %s file: %s" % (hostFile, str(e))) - return False - if runCommand("mv -f %s /etc/hosts" % hostFile, root=True) != 0: - log("failed to rename file %s to /etc/hosts" % hostFile) - return False - return True - - -def readResolvConfFile(fileName=None, includeLocalHost=False): +def readResolvConfFile(fileName=None): nameServerList = [] domain = None searchDomain = None if not fileName: fileName = Globals.RESOLV_CONF_FILE - try: - for line in open(fileName): - tokens = line.split("#")[0].strip().split() - if len(tokens) < 2: - continue - if tokens[0].upper() == "NAMESERVER": - if includeLocalHost == False and tokens[1] == "127.0.0.1": - continue - nameServerList.append(tokens[1]) - continue - if tokens[0].upper() == "DOMAIN": - domain = tokens[1:] - continue - if tokens[0].upper() == "SEARCH": - searchDomain = tokens[1:] - continue - return nameServerList, domain, searchDomain - except IOError, e: - log("failed to read %s file: %s" % (fileName, str(e))) - return None, None, None - - -def writeResolvConfFile(nameServerList, domain, searchDomain, fileName=None, appendLocalHost=True): - if fileName: - resolvConfFile = fileName - else: - resolvConfFile = tempfile.mktemp(prefix="GSPSA") - try: - fp = open(resolvConfFile, "w") - if appendLocalHost: - fp.write("nameserver 127.0.0.1\n") - for nameServer in nameServerList: - fp.write("nameserver %s\n" % nameServer) - if domain: - fp.write("domain %s\n" % " ".join(domain)) - if searchDomain: - fp.write("search %s\n" % " ".join(searchDomain)) - fp.close() - if resolvConfFile == fileName: - return True - except IOError, e: - log("failed to write %s file: %s" % (resolvConfFile, str(e))) - return False - if runCommand("mv -f %s %s" % (resolvConfFile, Globals.RESOLV_CONF_FILE), root=True) != 0: - log("failed to rename file %s to %s" % (resolvConfFile, Globals.RESOLV_CONF_FILE)) - return False - return True + lines = Utils.readFile(fileName, lines=True) + for line in lines: + tokens = line.split("#")[0].strip().split() + if len(tokens) < 2: + continue + if tokens[0].upper() == "NAMESERVER": + nameServerList.append(tokens[1]) + continue + if tokens[0].upper() == "DOMAIN": + domain = tokens[1:] + continue + if tokens[0].upper() == "SEARCH": + searchDomain = tokens[1:] + continue + return nameServerList, domain, searchDomain def readIfcfgConfFile(deviceName, root=""): conf = {} fileName = "%s%s/ifcfg-%s" % (root, Globals.SYSCONFIG_NETWORK_DIR, deviceName) - try: - for line in open(fileName): - tokens = line.split("#")[0].split("=") - if len(tokens) != 2: - continue - conf[tokens[0].strip().lower()] = tokens[1].strip() - return conf - except IOError, e: - log("failed to read %s file: %s" % (fileName, str(e))) - return None - - -def writeIfcfgConfFile(deviceName, conf, root="", deviceFile=None): - if not deviceFile: - deviceFile = "%s%s/ifcfg-%s" % (root, Globals.SYSCONFIG_NETWORK_DIR, deviceName) - if root: - ifcfgConfFile = deviceFile - else: - ifcfgConfFile = tempfile.mktemp(prefix="GSPSA") - try: - fp = open(ifcfgConfFile, "w") - for key in conf.keys(): - if key == "description": - fp.write("#%s=%s\n" % (key.upper(), conf[key])) - continue - if key in ['link', 'mode']: - continue - if conf["device"].startswith("bond") and key in ['hwaddr', 'master', 'slave']: - continue - if key == "slave" and conf['master']: - fp.write("SLAVE=yes\n") - continue - if key == "onboot": - if conf[key] == True: - fp.write("ONBOOT=yes\n") - elif isString(conf[key]) and conf[key].upper() == "YES": - fp.write("ONBOOT=yes\n") - else: - fp.write("ONBOOT=no\n") - continue - if not conf[key]: - continue - fp.write("%s=%s\n" % (key.upper(), conf[key])) - fp.close() - if ifcfgConfFile == deviceFile: - return True - except IOError, e: - log("failed to write %s file" % (ifcfgConfFile, str(e))) - return False - if runCommand("mv -f %s %s" % (ifcfgConfFile, deviceFile), root=True) != 0: - log("failed to rename file %s to %s" % (ifcfgConfFile, deviceFile)) - return False - return True - -def getNetDeviceDetail(deviceName): - deviceDetail = {} - deviceDetail['Name'] = deviceName - rv = runCommand("ifconfig %s" % deviceName, output=True, root=True) - if rv["Status"] != 0: - return False - for line in rv["Stdout"].split(): - tokens = line.strip().split(":") - if tokens[0].upper() == "ENCAP": - deviceDetail['Model'] = tokens[1].strip().upper() - break - - for line in rv["Stdout"].split("\n"): - if line.strip().startswith("inet addr:"): - tokens = line.strip().split(":") - if tokens[0].upper() == "INET ADDR": - try: - deviceDetail['Ip'] = tokens[1].strip().split()[0] - deviceDetail['Mask'] = tokens[-1].strip() - except IndexError, e: - pass - break - return deviceDetail - -def getNetDeviceGateway(deviceName): - rv = runCommand("route -n", output=True, root=True) - if rv["Status"] != 0: - return None - if not rv["Stdout"]: - return None - lines = [line for line in rv["Stdout"].split("\n") if line.find("UG") != -1 and line.find(deviceName)] - if not lines: - return None - line = lines[-1].split() - if line and len(line) > 1: - return line[1] - return None - -def getNetSpeed(deviceName): - rv = runCommand("ethtool %s" % deviceName, output=True, root=True) - if rv["Status"] != 0: - return False - for line in rv["Stdout"].split("\n"): - tokens = line.strip().split(":") - if tokens[0].upper() == "SPEED": - return tokens[1].strip().upper().split("MB")[0] - return None - -def getLinkStatus(deviceName): - return True - ## ethtool takes very long time to respond. So its disabled now - rv = runCommand("ethtool %s" % deviceName, output=True, root=True) - if rv["Status"] != 0: - return False - for line in rv["Stdout"].split("\n"): - tokens = line.strip().split(":") - if tokens[0].upper() == "LINK DETECTED": - if tokens[1].strip().upper() == "YES": - return True - else: - return False - return False + lines = Utils.readFile(fileName, lines=True) + for line in lines: + tokens = line.split("#")[0].split("=") + if len(tokens) != 2: + continue + conf[tokens[0].strip().lower()] = tokens[1].strip() + return conf def getBondMode(deviceName, fileName=None): if not fileName: fileName = Globals.MODPROBE_CONF_FILE - try: - for line in open(fileName): - tokens = line.split("#")[0].split() - if len(tokens) < 4: - continue - if tokens[0].upper() == "OPTIONS" and tokens[1] == deviceName: - if tokens[2].startswith("mode="): - return tokens[2].split("=")[1] - if tokens[3].startswith("mode="): - return tokens[3].split("=")[1] - if tokens[4].startswith("mode="): - return tokens[4].split("=")[1] - if tokens[5].startswith("mode="): - return tokens[5].split("=")[1] - return None - except IOError, e: - log("failed to read %s file: %s" % (fileName, str(e))) - return None - - -def setBondMode(deviceName, mode, fileName=None): - if not fileName: - fileName = Globals.MODPROBE_CONF_FILE - tempFileName = getTempFileName() - try: - fp = open(tempFileName, "w") - lines = open(fileName).readlines() - except IOError, e: - log("unable to open file %s: %s" % (Globals.MODPROBE_CONF_FILE, str(e))) - return False + lines = Utils.readFile(fileName, lines=True) for line in lines: - tokens = line.split() - if len(tokens) > 1 and "OPTIONS" == tokens[0].upper() and "BOND" in tokens[1].upper() and deviceName == tokens[1]: - fp.write("options %s max_bonds=2 mode=%s miimon=100\n" % (deviceName, mode)) - deviceName = None + tokens = line.split("#")[0].split() + if len(tokens) < 4: continue - fp.write(line) - if deviceName: - fp.write("alias %s bonding\n" % deviceName) - fp.write("options %s max_bonds=2 mode=%s miimon=100\n" % (deviceName, mode)) - fp.close() - if runCommand(["mv", "-f", tempFileName, fileName], root=True) != 0: - log("unable to move file from %s to %s" % (tempFileName, fileName)) - return False - return True + if tokens[0].upper() == "OPTIONS" and tokens[1] == deviceName: + if tokens[2].startswith("mode="): + return tokens[2].split("=")[1] + if tokens[3].startswith("mode="): + return tokens[3].split("=")[1] + if tokens[4].startswith("mode="): + return tokens[4].split("=")[1] + if tokens[5].startswith("mode="): + return tokens[5].split("=")[1] + return None + def getNetDeviceList(root=""): - netDeviceList = [] + netDeviceList = {} for deviceName in os.listdir("/sys/class/net/"): netDevice = {} - netDevice["device"] = None netDevice["description"] = None netDevice["hwaddr"] = None netDevice["type"] = None @@ -305,173 +89,62 @@ def getNetDeviceList(root=""): netDevice["link"] = None netDevice["mode"] = None - #netDevice["device"] = device.Name netDevice["device"] = deviceName - #netDevice["description"] = device.Description netDevice["description"] = deviceName - #netDevice["type"] = device.Type - netDevice["type"] = None - netDevice["link"] = getLinkStatus(deviceName) + netDevice["hwaddr"] = Utils.readFile("/sys/class/net/%s/address" % deviceName).strip() + + rv = Utils.runCommand("ifconfig %s" % deviceName, output=True) + if rv["Status"] == 0: + for line in rv["Stdout"].split("\n"): + if line.find("Link encap:") != -1: + netDevice["type"] = line.split("Link encap:")[1].split()[0] + continue + if line.find("inet addr:") != -1: + tokens = line.split("inet addr:")[1].split() + netDevice["ipaddr"] = tokens[0] + if line.find("Mask:") != -1: + netDevice["netmask"] = line.split("Mask:")[1].split()[0] + #print tokens[1].split(":")[1] + + rv = Utils.runCommand("ethtool %s" % deviceName, output=True, root=True) + if rv["Status"] == 0: + for line in rv["Stdout"].split("\n"): + if line.find("Speed: ") != -1: + netDevice["speed"] = line.split("Speed: ")[1].upper().split("MB")[0] + elif line.find("Link detected: ") != -1: + netDevice["link"] = line.split("Link detected: ")[1] + + rv = Utils.runCommand("route -n", output=True, root=True) + if rv["Status"] == 0: + for line in rv["Stdout"].split("\n"): + tokens = line.split() + if len(tokens) == 8 and tokens[-1] == deviceName and tokens[3] == "UG": + netDevice["gateway"] = tokens[1] + netDevice["mode"] = getBondMode(deviceName, root + Globals.MODPROBE_CONF_FILE) - deviceDetail = getNetDeviceDetail(deviceName) - if deviceDetail.has_key('Model'): - netDevice["model"] = deviceDetail['Model'] - else: - netDevice["model"] = None - if deviceDetail.has_key('Ip'): - netDevice["ipaddr"] = deviceDetail['Ip'] - else: - netDevice["ipaddr"] = None - if deviceDetail.has_key('Mask'): - netDevice["netmask"] = deviceDetail['Mask'] - else: - netDevice["netmask"] = None - netDevice["speed"] = getNetSpeed(deviceName) - try: - netDevice["hwaddr"] = open("/sys/class/net/%s/address" % deviceName).read().strip() - except IOError, e: - pass - - netDeviceList.append(netDevice) + + netDeviceList[deviceName] = netDevice conf = readIfcfgConfFile(deviceName, root) if not conf: continue try: + if not netDevice["ipaddr"]: + netDevice["ipaddr"] = conf["ipaddr"] + if not netDevice["netmask"]: + netDevice["netmask"] = conf["netmask"] + if not netDevice["gateway"]: + netDevice["gateway"] = conf["gateway"] netDevice["onboot"] = conf["onboot"] - except KeyError, e: - pass - try: netDevice["bootproto"] = conf["bootproto"] - except KeyError, e: - pass - if conf.has_key("ipaddr") and conf["ipaddr"]: - netDevice["ipaddr"] = conf["ipaddr"] - try: - netDevice["netmask"] = conf["netmask"] - except KeyError, e: - pass - if conf.has_key("gateway") and conf["gateway"]: - netDevice["gateway"] = conf["gateway"] - else: - netDevice["gateway"] = getNetDeviceGateway(deviceName) - try: netDevice["peerdns"] = conf["peerdns"] - except KeyError, e: - pass - try: netDevice["autodns"] = conf["autodns"] - except KeyError, e: - pass - try: netDevice["dns1"] = conf["dns1"] - except KeyError, e: - pass - try: netDevice["dns2"] = conf["dns2"] - except KeyError, e: - pass - try: netDevice["dns3"] = conf["dns3"] - except KeyError, e: - pass - try: netDevice["master"] = conf["master"] - except KeyError, e: - pass - try: netDevice["slave"] = conf["slave"] - except KeyError, e: - pass - try: netDevice["nmcontrolled"] = conf["nmcontrolled"] except KeyError, e: pass - return netDeviceList - - ## bondDevices = [os.path.basename(device) for device in glob.glob("/sys/class/net/bond*")] - - ## bondDevices = [os.path.basename(device) for device in glob.glob("/sys/class/net/bond*")] - ## for deviceName in bondDevices: - ## if deviceName in linkedBondList: - ## if deviceName in sysConfigDeviceList: - ## deviceList[deviceName] = sysConfigDeviceList[deviceName] - ## else: - ## deviceList[deviceName] = {'device':deviceName, 'onboot':'no', 'bootproto':'none'} - ## continue - ## if len(ethDevices) > 2: - ## deviceList[deviceName] = {'device':deviceName, 'onboot':'no', 'bootproto':'none'} - - -def configureDhcpServer(serverIpAddress, dhcpIpAddress): - tmpDhcpConfFile = tempfile.mktemp(prefix="GSPSA") - - serverPortString = "68" - try: - for arg in open("/proc/cmdline").read().strip().split(): - token = arg.split("=") - if token[0] == "dhcp": - serverPortString = token[1] - break - except IOError, e: - log(syslog.LOG_ERR, "Failed to read /proc/cmdline. Continuing with default port 68: %s" % str(e)) - try: - serverPort = int(serverPortString) - except ValueError, e: - log(syslog.LOG_ERR, "Invalid dhcp port '%s' in /proc/cmdline. Continuing with default port 68: %s" % (serverPortString, str(e))) - serverPort = 68 - - try: - fp = open(tmpDhcpConfFile, "w") - fp.write("bind-interfaces\n") - fp.write("except-interface=lo\n") - fp.write("dhcp-range=%s,%s\n" % (dhcpIpAddress, dhcpIpAddress)) - fp.write("dhcp-lease-max=1\n") - fp.write("dhcp-alternate-port=%s\n" % serverPort) - fp.write("dhcp-leasefile=%s\n" % Globals.DNSMASQ_LEASE_FILE) - #fp.write("server=%s\n" % serverIpAddress) - #fp.write("dhcp-script=/usr/sbin/server-info\n") - fp.close() - except IOError, e: - log(syslog.LOG_ERR, "unable to write dnsmasq dhcp configuration %s: %s" % (tmpDhcpConfFile, str(e))) - return False - if runCommand("mv -f %s %s" % (tmpDhcpConfFile, Globals.DNSMASQ_DHCP_CONF_FILE), root=True) != 0: - log(syslog.LOG_ERR, "unable to copy dnsmasq dhcp configuration to %s" % Globals.DNSMASQ_DHCP_CONF_FILE) - return False - return True - - -def isDhcpServer(): - return os.path.exists(Globals.DNSMASQ_DHCP_CONF_FILE) - - -def getDhcpServerStatus(): - if runCommand("service dnsmasq status", root=True) == 0: - return True - return False - - -def startDhcpServer(): - if runCommand("service dnsmasq start", root=True) == 0: - return True - return False - - -def stopDhcpServer(): - if runCommand("service dnsmasq stop", root=True) == 0: - runCommand("rm -f %s" % Globals.DNSMASQ_LEASE_FILE, root=True) - return True - return False - - -def restartDhcpServer(): - stopDhcpServer() - runCommand("rm -f %s" % Globals.DNSMASQ_LEASE_FILE, root=True) - return startDhcpServer() - - -def reloadDhcpServer(): - if runCommand("service dnsmasq reload", root=True) == 0: - return True - return False 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 e5256178..5476e090 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 @@ -16,17 +16,13 @@ import Utils def readVolumeSmbConfFile(fileName=Globals.VOLUME_SMBCONF_FILE): entryList = [] - try: - fp = open(fileName) - for line in fp: - tokens = line.split("#")[0].strip().split(";")[0].strip().split("=") - if len(tokens) != 2: - continue - if tokens[0].strip().upper() == "INCLUDE": - entryList.append(tokens[1].strip()) - fp.close() - except IOError, e: - Utils.log("Failed to open file %s: %s" % (fileName, str(e))) + lines = Utils.readFile(fileName, lines=True) + for line in lines: + tokens = line.split("#")[0].strip().split(";")[0].strip().split("=") + if len(tokens) != 2: + continue + if tokens[0].strip().upper() == "INCLUDE": + entryList.append(tokens[1].strip()) return entryList diff --git a/src/com.gluster.storage.management.gateway.scripts/src/backend/clear_volume_directory.py b/src/com.gluster.storage.management.gateway.scripts/src/backend/clear_volume_directory.py index a9da783b..374a7e9c 100755 --- a/src/com.gluster.storage.management.gateway.scripts/src/backend/clear_volume_directory.py +++ b/src/com.gluster.storage.management.gateway.scripts/src/backend/clear_volume_directory.py @@ -11,10 +11,7 @@ if not p1 in sys.path: sys.path.append(p1) if not p2 in sys.path: sys.path.append(p2) -import syslog import time -from XmlHandler import ResponseXml -import DiskUtils import Utils from optparse import OptionParser @@ -24,7 +21,7 @@ def main(): (options, args) = parser.parse_args() if len(args) != 1: - sys.stderr.write("usage: %s VOLUME_PATH [-d/--delete]\n" % os.path.basename(sys.argv[0])) + sys.stderr.write("usage: %s [-d | --delete] VOLUME_PATH\n" % os.path.basename(sys.argv[0])) sys.exit(-1) volumeDirectory = args[0] @@ -32,18 +29,20 @@ def main(): sys.stderr.write("Given volume directory path:%s does not exists\n" % volumeDirectory) sys.exit(1) - # trim '/' at the end if '/' == volumeDirectory[-1]: volumeDirectory = volumeDirectory[:-1] + newVolumeDirectoryName = "%s_%s" % (volumeDirectory, time.time()) if Utils.runCommand("mv -f %s %s" % (volumeDirectory, newVolumeDirectoryName), root=True) != 0: sys.stderr.write("Failed to rename volume directory\n") sys.exit(2) - if not options.todelete: - sys.exit(0) + if options.todelete: + process = Utils.runCommandBG("rm -fr %s" % newVolumeDirectoryName, root=True) + if not process: + sys.exit(3) + sys.exit(0) - sys.exit(Utils.runCommand("rm -fr %s" % newVolumeDirectoryName, root=True)) if __name__ == "__main__": main() diff --git a/src/com.gluster.storage.management.gateway.scripts/src/backend/create_volume_directory.py b/src/com.gluster.storage.management.gateway.scripts/src/backend/create_volume_directory.py deleted file mode 100755 index a4eb2627..00000000 --- a/src/com.gluster.storage.management.gateway.scripts/src/backend/create_volume_directory.py +++ /dev/null @@ -1,54 +0,0 @@ -#!/usr/bin/python -# Copyright (C) 2011 Gluster, Inc. <http://www.gluster.com> -# This file is part of Gluster Management Gateway. -# - -import os -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 DiskUtils -import Utils - - -def main(): - if len(sys.argv) != 3: - sys.stderr.write("usage: %s <disk name> <volume name>\n" % os.path.basename(sys.argv[0])) - sys.exit(-1) - - disk = sys.argv[1] - volumeName = sys.argv[2] - - # Retrieving disk uuid - diskUuid = DiskUtils.getUuidByDiskPartition(DiskUtils.getDevice(disk)) - - if not diskUuid: - Utils.log("failed to find disk:%s uuid" % disk) - sys.stderr.write("failed to find disk:%s uuid\n" % disk) - sys.exit(1) - - # Retrieving disk mount point using disk uuid - diskMountPoint = DiskUtils.getMountPointByUuid(diskUuid) - if not os.path.exists(diskMountPoint): - Utils.log("failed to retrieve disk:%s mount point" % disk) - sys.stderr.write("failed to retrieve disk:%s mount point\n" % disk) - sys.exit(2) - - # creating volume directory under disk mount point - volumeDirectory = "%s/%s" % (diskMountPoint, volumeName) - if os.path.exists(volumeDirectory): - Utils.log("Volume directory:%s already exists" % (volumeDirectory)) - sys.stderr.write("Volume directory:%s already exists\n" % (volumeDirectory)) - sys.exit(3) - - rv = Utils.runCommand("mkdir %s" % volumeDirectory, root=True) - if rv != 0: - sys.stderr.write("Failed to create volume directory\n") - sys.exit(rv) - -if __name__ == "__main__": - main() diff --git a/src/com.gluster.storage.management.gateway.scripts/src/backend/disable-ssh-password-auth.sh b/src/com.gluster.storage.management.gateway.scripts/src/backend/disable-ssh-password-auth.sh deleted file mode 100755 index 07ee1a3a..00000000 --- a/src/com.gluster.storage.management.gateway.scripts/src/backend/disable-ssh-password-auth.sh +++ /dev/null @@ -1,30 +0,0 @@ -#!/bin/bash - -#----------------------------------------------------------------------------- -# disable-ssh-password-auth.sh -# Script for disabling SSH password authentication. This is used by the -# management gateway after installing the public key, so that the gluster -# node can be accessed (using ssh) only from the management gateway. -#----------------------------------------------------------------------------- - -CONFIG_FILE="/etc/ssh/sshd_config" -TIMESTAMP=`date +%d%m%Y%H%M%S` -BACKUP_FILE="${CONFIG_FILE}_${TIMESTAMP}" -TEMP_FILE="/tmp/new_sshd_config_${TIMESTAMP}" - -# Modify config file to disable password authentication, redirect to a temp file -# TODO: disable only if enabled! -sed "s/^PasswordAuthentication yes$/PasswordAuthentication no/g" ${CONFIG_FILE} > ${TEMP_FILE} - -# Secure the file by changing permissions (600) -chmod 600 ${TEMP_FILE} - -# Take backup of config file -cp ${CONFIG_FILE} ${BACKUP_FILE} - -# Overwrite config file with the modified one -mv ${TEMP_FILE} ${CONFIG_FILE} - -# Re-start ssh daemon -/etc/init.d/sshd restart - diff --git a/src/com.gluster.storage.management.gateway.scripts/src/backend/format_device.py b/src/com.gluster.storage.management.gateway.scripts/src/backend/format_device.py index c3ee2146..8ae00260 100755 --- a/src/com.gluster.storage.management.gateway.scripts/src/backend/format_device.py +++ b/src/com.gluster.storage.management.gateway.scripts/src/backend/format_device.py @@ -41,20 +41,16 @@ def main(): if os.path.exists(deviceFormatStatusFile): Utils.log("format status file %s exists" % deviceFormatStatusFile) - try: - fp = open(deviceFormatStatusFile) - line = fp.read() - fp.close() - if line.strip().upper() == "COMPLETED": - sys.stderr.write("Device already formatted\n") - sys.exit(3) - else: - sys.stderr.write("Device format already running\n") - sys.exit(4) - except IOError, e: - Utils.log("failed to read format status file %s: %s" % (deviceFormatStatusFile, str(e))) - sys.stderr.write("%s\n" % str(e)) + line = Utils.readFile(deviceFormatStatusFile) + if not line: + sys.stderr.write("failed to read format status file %s\n" % deviceFormatStatusFile) sys.exit(-2) + if line.strip().upper() == "COMPLETED": + sys.stderr.write("Device already formatted\n") + sys.exit(3) + else: + sys.stderr.write("Device format already running\n") + sys.exit(4) if os.path.exists(deviceFormatLockFile): Utils.log("lock file %s exists" % deviceFormatLockFile) diff --git a/src/com.gluster.storage.management.gateway.scripts/src/backend/get_brick_status.py b/src/com.gluster.storage.management.gateway.scripts/src/backend/get_brick_status.py index afc15f3a..b72321d7 100755 --- a/src/com.gluster.storage.management.gateway.scripts/src/backend/get_brick_status.py +++ b/src/com.gluster.storage.management.gateway.scripts/src/backend/get_brick_status.py @@ -24,22 +24,22 @@ def main(): if not os.path.exists(pidFile): print "OFFLINE" - else: - try: - fp = open(pidFile) - pidString = fp.readline() - fp.close() - os.getpgid(int(pidString)) - print "ONLINE" - except IOError, e: - Utils.log("failed to open file %s: %s" % (pidFile, str(e))) - print "UNKNOWN" - except ValueError, e: - Utils.log("invalid pid %s in file %s: %s" % (pidString, pidFile, str(e))) - print "UNKNOWN" - except OSError, e: - #Utils.log("failed to get process detail of pid %s: %s" % (pidString, str(e))) - print "OFFLINE" + sys.exit(0) + + lines = Utils.readFile(pidFile) + if not lines: + print "UNKNOWN" + sys.exit(0) + try: + pidString = lines[0] + os.getpgid(int(pidString)) + print "ONLINE" + except ValueError, e: + Utils.log("invalid pid %s in file %s: %s" % (pidString, pidFile, str(e))) + print "UNKNOWN" + except OSError, e: + #Utils.log("failed to get process detail of pid %s: %s" % (pidString, str(e))) + print "OFFLINE" sys.exit(0) if __name__ == "__main__": diff --git a/src/com.gluster.storage.management.gateway.scripts/src/backend/get_disk_mount_point.py b/src/com.gluster.storage.management.gateway.scripts/src/backend/get_disk_mount_point.py deleted file mode 100755 index cf966fec..00000000 --- a/src/com.gluster.storage.management.gateway.scripts/src/backend/get_disk_mount_point.py +++ /dev/null @@ -1,56 +0,0 @@ -#!/usr/bin/python -# Copyright (C) 2011 Gluster, Inc. <http://www.gluster.com> -# This file is part of Gluster Management Gateway. -# - -import os -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 Utils -import FsTabUtils -from XmlHandler import ResponseXml - - -def getmountpoint(path): - if not path: - Utils.log("Not a valid path:%s" % path) - rs.appendTagRoute("status.code", "-1") - rs.appendTagRoute("status.message", "Error: given path name is empty") - return rs.toprettyxml() - - rs = ResponseXml() - mountPoint = None - - for line in FsTabUtils.readFsTab(): - if path.startswith(line['MountPoint']): - if not mountPoint: - mountPoint = line['MountPoint'] - if len(line['MountPoint']) > len(mountPoint): - mountPoint = line['MountPoint'] - - if "/" == mountPoint or not mountPoint: - Utils.log("failed to find mount point of the given path:%s" % path) - rs.appendTagRoute("status.code", "-1") - rs.appendTagRoute("status.message", "Error: Unable to find disk mount point") - return rs.toprettyxml() - - rs.appendTagRoute("status.code", "0") - rs.appendTagRoute("status.message", mountPoint) - return rs.toprettyxml() - -def main(): - if len(sys.argv) != 2: - sys.stderr.write("usage: %s <path>\n" % os.path.basename(sys.argv[0])) - sys.exit(-1) - - path = sys.argv[1] - print getmountpoint(path) - sys.exit(0) - -if __name__ == "__main__": - main() diff --git a/src/com.gluster.storage.management.gateway.scripts/src/backend/get_disk_name_by_path.py b/src/com.gluster.storage.management.gateway.scripts/src/backend/get_disk_name_by_path.py deleted file mode 100755 index e9955e21..00000000 --- a/src/com.gluster.storage.management.gateway.scripts/src/backend/get_disk_name_by_path.py +++ /dev/null @@ -1,55 +0,0 @@ -#!/usr/bin/python -# Copyright (C) 2011 Gluster, Inc. <http://www.gluster.com> -# This file is part of Gluster Storage Platform. -# - -import os -import Utils -from DiskUtils import * -from XmlHandler import ResponseXml - - -def getmountpoint(path): - if not path: - Utils.log("Not a valid path:%s" % path) - rs.appendTagRoute("status.code", "-1") - rs.appendTagRoute("status.message", "Error: given path name is empty") - return rs.toprettyxml() - - rs = ResponseXml() - mountPoint = None - fsTabEntry = None - for line in readFsTab(): - if path.startswith(line['MountPoint']): - if not mountPoint: - mountPoint = line['MountPoint'] - fsTabEntry = line - if len(line['MountPoint']) > len(mountPoint): - mountPoint = line['MountPoint'] - fsTabEntry = line - - if "/" == mountPoint or not mountPoint: - Utils.log("failed to find mount point of the given path:%s" % path) - rs.appendTagRoute("status.code", "-1") - rs.appendTagRoute("status.message", "Error: Unable to find disk mount point") - return rs.toprettyxml() - - rs.appendTagRoute("status.code", "0") - if fsTabEntry["Device"].startswith("UUID="): - rs.appendTagRoute("status.message", getDiskPartitionByUuid(fsTabEntry["Device"].split("UUID=")[-1])) - else: - rs.appendTagRoute("status.message", "Unable to find disk name") - return rs.toprettyxml() - -def main(): - if len(sys.argv) != 2: - sys.stderr.write("usage: %s <path>\n" % os.path.basename(sys.argv[0])) - sys.exit(-1) - - path = sys.argv[1] - print getmountpoint(path) - sys.exit(0) - -if __name__ == "__main__": - main() - diff --git a/src/com.gluster.storage.management.gateway.scripts/src/backend/get_format_device_status.py b/src/com.gluster.storage.management.gateway.scripts/src/backend/get_format_device_status.py index 3bc63db0..532f1585 100755 --- a/src/com.gluster.storage.management.gateway.scripts/src/backend/get_format_device_status.py +++ b/src/com.gluster.storage.management.gateway.scripts/src/backend/get_format_device_status.py @@ -34,47 +34,26 @@ def main(): sys.exit(1) if os.path.exists(deviceFormatStatusFile): - try: - fp = open(deviceFormatStatusFile) - line = fp.read() - fp.close() - line = line.strip() + line = Utils.readFile(deviceFormatStatusFile) + line = line.strip() - Utils.removeFile(deviceFormatOutputFile) - Utils.removeFile(deviceFormatStatusFile) + Utils.removeFile(deviceFormatOutputFile) + Utils.removeFile(deviceFormatStatusFile) - responseDom = ResponseXml() - responseDom.appendTagRoute("device", sys.argv[1]) - responseDom.appendTagRoute("completedBlocks", "0") - responseDom.appendTagRoute("totalBlocks", "0") - responseDom.appendTagRoute("message", line) - if line.upper() == "COMPLETED": - responseDom.appendTagRoute("formatStatus", "COMPLETED") - else: - responseDom.appendTagRoute("formatStatus", "NOT_RUNNING") - print responseDom.toxml() - sys.exit(0) - except IOError, e: - Utils.log("failed to read format status file %s: %s" % (deviceFormatStatusFile, str(e))) - sys.stderr.write("%s\n" % str(e)) - sys.exit(-2) - - if not os.path.exists(deviceFormatOutputFile): responseDom = ResponseXml() responseDom.appendTagRoute("device", sys.argv[1]) responseDom.appendTagRoute("completedBlocks", "0") responseDom.appendTagRoute("totalBlocks", "0") - responseDom.appendTagRoute("message", None) - responseDom.appendTagRoute("formatStatus", "IN_PROGRESS") + responseDom.appendTagRoute("message", line) + if line.upper() == "COMPLETED": + responseDom.appendTagRoute("formatStatus", "COMPLETED") + else: + responseDom.appendTagRoute("formatStatus", "NOT_RUNNING") print responseDom.toxml() sys.exit(0) - try: - fp = open(deviceFormatOutputFile) - content = fp.read() - fp.close() - except IOError, e: - Utils.log("failed to read format output file %s: %s" % (deviceFormatOutputFile, str(e))) + content = Utils.readFile(deviceFormatOutputFile, lines=True) + if not content: responseDom = ResponseXml() responseDom.appendTagRoute("device", sys.argv[1]) responseDom.appendTagRoute("completedBlocks", "0") @@ -91,10 +70,7 @@ def main(): responseDom.appendTagRoute("device", sys.argv[1]) responseDom.appendTagRoute("completedBlocks", "0") responseDom.appendTagRoute("totalBlocks", "0") - if content: - responseDom.appendTagRoute("message", content[-1]) - else: - responseDom.appendTagRoute("message") + responseDom.appendTagRoute("message", content[-1]) responseDom.appendTagRoute("formatStatus", "IN_PROGRESS") print responseDom.toxml() sys.exit(0) diff --git a/src/com.gluster.storage.management.gateway.scripts/src/backend/get_rrd_cpu_details.py b/src/com.gluster.storage.management.gateway.scripts/src/backend/get_rrd_cpu_details.py index 0a05a4d3..da08fde1 100755 --- a/src/com.gluster.storage.management.gateway.scripts/src/backend/get_rrd_cpu_details.py +++ b/src/com.gluster.storage.management.gateway.scripts/src/backend/get_rrd_cpu_details.py @@ -17,7 +17,7 @@ CPU_RRD_FILE = "/var/lib/rrd/cpu.rrd" def main(): if len(sys.argv) != 2: - sys.stderr.write("usage: %s <PERIOD>\n" % os.path.basename(sys.argv[0])) + sys.stderr.write("usage: %s DURATION\n" % os.path.basename(sys.argv[0])) sys.exit(-1) period = sys.argv[1] diff --git a/src/com.gluster.storage.management.gateway.scripts/src/backend/get_rrd_memory_details.py b/src/com.gluster.storage.management.gateway.scripts/src/backend/get_rrd_memory_details.py index 1e3c24f6..07a9d7d0 100755 --- a/src/com.gluster.storage.management.gateway.scripts/src/backend/get_rrd_memory_details.py +++ b/src/com.gluster.storage.management.gateway.scripts/src/backend/get_rrd_memory_details.py @@ -3,32 +3,6 @@ # This file is part of Gluster Management Gateway. # -# Input command: get_rrd_memory_details.py 1hour -# OUTPUT as bellow: -# <?xml version="1.0" encoding="ISO-8859-1"?> -# -# <xport> -# <meta> -# <start>1310455500</start> -# <step>300</step> -# <end>1310459100</end> -# <rows>13</rows> -# <columns>5</columns> -# <legend> -# <entry>memoryUsed</entry> -# <entry>memoryFree</entry> -# <entry>memoryCache</entry> -# <entry>memoryBuffer</entry> -# <entry>totalMemory</entry> -# </legend> -# </meta> -# <data> -# <row><t>1310455500</t><v>1.9181091707e+06</v><v>1.5819754974e+06</v><v>1.2528146351e+06</v><v>1.2528146351e+06</v><v>3.5000846681e+06</v></row> -# --- -# --- -# </data> -# </xport> - import os import sys p1 = os.path.abspath(os.path.dirname(sys.argv[0])) @@ -37,14 +11,13 @@ if not p1 in sys.path: sys.path.append(p1) if not p2 in sys.path: sys.path.append(p2) -import syslog import Utils MEMORY_RRD_FILE = "/var/lib/rrd/mem.rrd" def main(): if len(sys.argv) != 2: - sys.stderr.write("usage: %s <PERIOD>\n" % os.path.basename(sys.argv[0])) + sys.stderr.write("usage: %s DURATION\n" % os.path.basename(sys.argv[0])) sys.exit(-1) period = sys.argv[1] diff --git a/src/com.gluster.storage.management.gateway.scripts/src/backend/get_rrd_net_details.py b/src/com.gluster.storage.management.gateway.scripts/src/backend/get_rrd_net_details.py index 41674ef9..ee28ca13 100755 --- a/src/com.gluster.storage.management.gateway.scripts/src/backend/get_rrd_net_details.py +++ b/src/com.gluster.storage.management.gateway.scripts/src/backend/get_rrd_net_details.py @@ -15,7 +15,7 @@ import Utils def main(): if len(sys.argv) != 3: - sys.stderr.write("usage: %s <DEVICE> <PERIOD>\n" % os.path.basename(sys.argv[0])) + sys.stderr.write("usage: %s DEVICE DURATION\n" % os.path.basename(sys.argv[0])) sys.exit(-1) device = sys.argv[1] diff --git a/src/com.gluster.storage.management.gateway.scripts/src/backend/get_server_details.py b/src/com.gluster.storage.management.gateway.scripts/src/backend/get_server_details.py index 5c5b4c4a..f446b99f 100755 --- a/src/com.gluster.storage.management.gateway.scripts/src/backend/get_server_details.py +++ b/src/com.gluster.storage.management.gateway.scripts/src/backend/get_server_details.py @@ -11,14 +11,12 @@ if not p1 in sys.path: sys.path.append(p1) if not p2 in sys.path: sys.path.append(p2) -import dbus import socket import re import Utils import Protocol import DiskUtils -from NetworkUtils import * -from Disk import * +import NetworkUtils from XmlHandler import ResponseXml from optparse import OptionParser @@ -31,7 +29,6 @@ def getDiskDom(): diskDom = Protocol.XDOM() disksTag = diskDom.createTag("disks", None) diskTagDict = {} - raidDisksTag = diskDom.createTag("raidDisks", None) for raidDiskName, raidDisk in procMdstat.iteritems(): raidDiskTag = diskDom.createTag("disk", None) raidDiskTag.appendChild(diskDom.createTag("name", raidDiskName)) @@ -48,7 +45,9 @@ def getDiskDom(): raidDiskTag.appendChild(diskDom.createTag("fsVersion")) raidDiskTag.appendChild(diskDom.createTag("size", diskInfo[raidDiskName]['Size'] / 1024.0)) raidDiskTag.appendChild(diskDom.createTag("spaceInUse", diskInfo[raidDiskName]['SpaceInUse'])) - raidDisksTag.appendChild(raidDiskTag) + raidDisksTag = diskDom.createTag("raidDisks", None) # raid members tag + raidDiskTag.appendChild(raidDisksTag) + disksTag.appendChild(raidDiskTag) for raidMember in raidDisk['Member']: # Case1: Raid array member is a disk. The following code will add the disk details under a disk tag if diskInfo.has_key(raidMember): @@ -73,7 +72,7 @@ def getDiskDom(): diskTag.appendChild(diskDom.createTag("fsVersion", diskInfo[raidMember]["FsVersion"])) diskTag.appendChild(diskDom.createTag("size", diskInfo[raidMember]["Size"] / 1024.0)) diskTag.appendChild(diskDom.createTag("spaceInUse", diskInfo[raidMember]["SpaceInUse"])) - raidDiskTag.appendChild(diskTag) + raidDisksTag.appendChild(diskTag) del diskInfo[raidMember] continue # Case2: Raid array member is a partition. The following code will add the partition and its corresponding disk its belong to. @@ -95,7 +94,7 @@ def getDiskDom(): diskTag.appendChild(diskDom.createTag("spaceInUse", item["SpaceInUse"])) partitionsTag = diskDom.createTag("partitions", None) diskTag.appendChild(partitionsTag) - raidDiskTag.appendChild(diskTag) + raidDisksTag.appendChild(diskTag) # Constructed disk tag will be added to the dictonary. # This will be used to keep add all the corresponding partitions tags of the disk to the disk tag. diskTagDict[disk] = {'diskTag': diskTag, 'partitionsTag': partitionsTag} @@ -170,7 +169,7 @@ def getServerDetails(listall): serverName = socket.getfqdn() meminfo = Utils.getMeminfo() cpu = Utils.getCpuUsageAvg() - nameServerList, domain, searchDomain = readResolvConfFile() + nameServerList, domain, searchDomain = NetworkUtils.readResolvConfFile() if not domain: domain = [None] @@ -184,8 +183,8 @@ def getServerDetails(listall): serverTag.appendChild(responseDom.createTag("status", "OFFLINE")) serverTag.appendChild(responseDom.createTag("glusterFsVersion", Utils.getGlusterVersion())) serverTag.appendChild(responseDom.createTag("cpuUsage", str(cpu))) - serverTag.appendChild(responseDom.createTag("totalMemory", str(convertKbToMb(meminfo['MemTotal'])))) - serverTag.appendChild(responseDom.createTag("memoryInUse", str(convertKbToMb(meminfo['MemUsed'])))) + serverTag.appendChild(responseDom.createTag("totalMemory", str(Utils.convertKbToMb(meminfo['MemTotal'])))) + serverTag.appendChild(responseDom.createTag("memoryInUse", str(Utils.convertKbToMb(meminfo['MemUsed'])))) serverTag.appendChild(responseDom.createTag("uuid", None)) for dns in nameServerList: @@ -193,39 +192,29 @@ def getServerDetails(listall): #TODO: probe and retrieve timezone, ntp-server details and update the tags - deviceList = {} interfaces = responseDom.createTag("networkInterfaces", None) - for device in getNetDeviceList(): - if device["model"] in ['LOCAL', 'IPV6-IN-IPV4']: - continue - deviceList[device["device"]] = device - try: - macAddress = open("/sys/class/net/%s/address" % device["device"]).read().strip() - except IOError, e: + for deviceName, values in NetworkUtils.getNetDeviceList().iteritems(): + if values["type"].upper() in ['LOCAL', 'IPV6-IN-IPV4']: continue interfaceTag = responseDom.createTag("networkInterface", None) - interfaceTag.appendChild(responseDom.createTag("name", device["device"])) - interfaceTag.appendChild(responseDom.createTag("hwAddr",macAddress)) - interfaceTag.appendChild(responseDom.createTag("speed", device["speed"])) - interfaceTag.appendChild(responseDom.createTag("model", device["model"])) - if deviceList[device["device"]]: - if deviceList[device["device"]]["onboot"]: - interfaceTag.appendChild(responseDom.createTag("onboot", "yes")) - else: - interfaceTag.appendChild(responseDom.createTag("onBoot", "no")) - interfaceTag.appendChild(responseDom.createTag("bootProto", deviceList[device["device"]]["bootproto"])) - interfaceTag.appendChild(responseDom.createTag("ipAddress", deviceList[device["device"]]["ipaddr"])) - interfaceTag.appendChild(responseDom.createTag("netMask", deviceList[device["device"]]["netmask"])) - interfaceTag.appendChild(responseDom.createTag("defaultGateway", deviceList[device["device"]]["gateway"])) - if deviceList[device["device"]]["mode"]: - interfaceTag.appendChild(responseDom.createTag("mode", deviceList[device["device"]]["mode"])) - if deviceList[device["device"]]["master"]: - interfaceTag.appendChild(responseDom.createTag("bonding", "yes")) - spliter = re.compile(r'[\D]') - interfaceTag.appendChild(responseDom.createTag("bondid", spliter.split(device["master"])[-1])) + interfaceTag.appendChild(responseDom.createTag("name", deviceName)) + interfaceTag.appendChild(responseDom.createTag("hwAddr", values["hwaddr"])) + interfaceTag.appendChild(responseDom.createTag("speed", values["speed"])) + interfaceTag.appendChild(responseDom.createTag("model", values["type"])) + if values["onboot"]: + interfaceTag.appendChild(responseDom.createTag("onBoot", "yes")) else: - interfaceTag.appendChild(responseDom.createTag("onBoot", "no")) - interfaceTag.appendChild(responseDom.createTag("bootProto", "none")) + interfaceTag.appendChild(responseDom.createTag("onBoot", "no")) + interfaceTag.appendChild(responseDom.createTag("bootProto", values["bootproto"])) + interfaceTag.appendChild(responseDom.createTag("ipAddress", values["ipaddr"])) + interfaceTag.appendChild(responseDom.createTag("netMask", values["netmask"])) + interfaceTag.appendChild(responseDom.createTag("defaultGateway", values["gateway"])) + if values["mode"]: + interfaceTag.appendChild(responseDom.createTag("mode", values["mode"])) + if values["master"]: + interfaceTag.appendChild(responseDom.createTag("bonding", "yes")) + spliter = re.compile(r'[\D]') + interfaceTag.appendChild(responseDom.createTag("bondid", spliter.split(values["master"])[-1])) interfaces.appendChild(interfaceTag) serverTag.appendChild(interfaces) diff --git a/src/com.gluster.storage.management.gateway.scripts/src/backend/get_volume_brick_log.py b/src/com.gluster.storage.management.gateway.scripts/src/backend/get_volume_brick_log.py index fffbc3ba..026c3c00 100755 --- a/src/com.gluster.storage.management.gateway.scripts/src/backend/get_volume_brick_log.py +++ b/src/com.gluster.storage.management.gateway.scripts/src/backend/get_volume_brick_log.py @@ -13,6 +13,7 @@ if not p1 in sys.path: if not p2 in sys.path: sys.path.append(p2) from XmlHandler import XDOM +import Utils def enumLogType(logCode): if "M" == logCode.upper(): @@ -56,21 +57,21 @@ def logSplit(log): def getVolumeLog(logFilePath, tailCount): rs = XDOM() if not logFilePath: - print >> sys.stderr, "No log file path given" - sys.exit(-1); + sys.stderr.write("No log file path given\n") + sys.exit(-1) if not tailCount: - print >> sys.stderr, "No tail count given" - sys.exit(-1); + sys.stderr.write("No tail count given\n") + sys.exit(-1) pattern = '\[\d{4}-\d{2}-\d{2}\s{1}\d{2}:\d{2}:\d{2}.\d+\]\s{1}([MACEWNIDT]){1}\s+' - if not os.path.exists(logFilePath): - print >> sys.stderr, "volume log file [%s] not found!" % logFilePath - sys.exit(-1); - fp = open(logFilePath) - lines = [line for line in fp if re.match(pattern, line)] - fp.close() + content = Utils.readFile(logFilePath, lines=True) + if not content: + sys.stderr.write("volume log not found in file %s\n" % logFilePath) + sys.exit(-1) + + lines = [line for line in content if re.match(pattern, line)] i = len(lines) - int(tailCount) if i < 0: i = 0 @@ -84,7 +85,7 @@ def getVolumeLog(logFilePath, tailCount): def main(): if len(sys.argv) != 3: - print >> sys.stderr, "usage: %s <Log File Path> <Line Count>" % sys.argv[0] + sys.stderr.write("usage: %s LOG-FILE LINE-COUNT\n" % sys.argv[0]) sys.exit(-1) logFilePath = sys.argv[1] diff --git a/src/com.gluster.storage.management.gateway.scripts/src/backend/gluster_cifs_volume_startup.py b/src/com.gluster.storage.management.gateway.scripts/src/backend/gluster_cifs_volume_startup.py index cc4c394d..9ea7e021 100644 --- a/src/com.gluster.storage.management.gateway.scripts/src/backend/gluster_cifs_volume_startup.py +++ b/src/com.gluster.storage.management.gateway.scripts/src/backend/gluster_cifs_volume_startup.py @@ -79,15 +79,7 @@ def main(): Utils.runCommand("rm -fr %s/*" % Globals.CIFS_EXPORT_DIR, root=True, shell=True) sys.exit(0) - try: - fp = open(Globals.VOLUME_SMBCONF_FILE) - lines = fp.readlines() - fp.close() - except IOError, e: - Utils.log("Failed to samba volume configuration file %s: %s" % (Globals.VOLUME_SMBCONF_FILE, str(e))) - sys.stderr.write("Failed to samba volume configuration file %s: %s\n" % (Globals.VOLUME_SMBCONF_FILE, str(e))) - sys.exit(1) - + lines = Utils.readFile(Globals.VOLUME_SMBCONF_FILE) volumeSmbConfList = [line.strip() for line in lines] for volumeName in volumeInfo.keys(): if not "include = %s/%s.smbconf" % (Globals.VOLUME_CONF_DIR, volumeName) in volumeSmbConfList: diff --git a/src/com.gluster.storage.management.gateway.scripts/src/backend/gluster_provision_block_wrapper.py b/src/com.gluster.storage.management.gateway.scripts/src/backend/gluster_provision_block_wrapper.py index e7aeeb5f..a2827ea2 100755 --- a/src/com.gluster.storage.management.gateway.scripts/src/backend/gluster_provision_block_wrapper.py +++ b/src/com.gluster.storage.management.gateway.scripts/src/backend/gluster_provision_block_wrapper.py @@ -13,7 +13,6 @@ if not p2 in sys.path: sys.path.append(p2) import subprocess import Utils -import DiskUtils from optparse import OptionParser def writeStatus(deviceFormatStatusFile, message): diff --git a/src/com.gluster.storage.management.gateway.scripts/src/backend/multicast-discoverd.py b/src/com.gluster.storage.management.gateway.scripts/src/backend/multicast-discoverd.py index fbadd048..cb5de70c 100755 --- a/src/com.gluster.storage.management.gateway.scripts/src/backend/multicast-discoverd.py +++ b/src/com.gluster.storage.management.gateway.scripts/src/backend/multicast-discoverd.py @@ -31,17 +31,12 @@ def exitHandler(signum, frame): def updateGlusterdUuid(signum, frame): - try: - fp = open("/etc/glusterd/glusterd.info") - content = fp.read() - fp.close() - for line in content.strip().split(): - if line.startswith("UUID="): - GLUSTERD_UUID = line.split("=")[1] - break - except IOError, e: - Utils.log("failed to read file /etc/glusterd/glusterd.info: %s" % str(e)) - GLUSTERD_UUID = "NA" + lines = Utils.readFile("/etc/glusterd/glusterd.info", lines=True) + for line in lines: + if line.strip().startswith("UUID="): + GLUSTERD_UUID = line.strip().split("=")[1] + return + GLUSTERD_UUID = "NA" def isInPeer(): diff --git a/src/com.gluster.storage.management.gateway.scripts/src/common/Globals.py b/src/com.gluster.storage.management.gateway.scripts/src/common/Globals.py index 26a74bfd..49a12b69 100644 --- a/src/com.gluster.storage.management.gateway.scripts/src/common/Globals.py +++ b/src/com.gluster.storage.management.gateway.scripts/src/common/Globals.py @@ -20,38 +20,14 @@ SAMBA_CONF_FILE = "/etc/samba/smb.conf" REAL_SAMBA_CONF_FILE = "/etc/samba/real.smb.conf" MODPROBE_CONF_FILE = "/etc/modprobe.d/bonding.conf" RESOLV_CONF_FILE = "/etc/resolv.conf" -DNSMASQ_LEASE_FILE = "/var/tmp/dnsmasq.leases" -LIVE_MODE_FILE = "/etc/live" -DNSMASQ_CONF_DIR = "/etc/dnsmasq.d" -DNSMASQ_DHCP_CONF_FILE = DNSMASQ_CONF_DIR + "/dhcp.conf" -DATA_PARTITION_LABEL = "GLUSTERDATA" VOLUME_USER_DESCRIPTION = "Gluster Volume User" GLUSTER_BASE_DIR = "/etc/glustermg" REEXPORT_DIR = "/reexport" CIFS_EXPORT_DIR = "/cifs" -GLUSTER_UPDATES_FILE = "updates.xml" -INSTALLER_SERVER_NAME = "$installer$" ## Derived constants -GLUSTER_CONF_DIR = GLUSTER_BASE_DIR + "/conf" -GLUSTER_TMP_DIR = GLUSTER_BASE_DIR + "/tmp" VOLUME_CONF_DIR = GLUSTER_BASE_DIR + "/volumes" -SERVER_CONF_DIR = GLUSTER_BASE_DIR + "/servers" -DNS_RECORDS_DIR = GLUSTER_BASE_DIR + "/dns-records" -GSN_USER_INFO_FILE = GLUSTER_BASE_DIR + "/gsn-user.info" -GLUSTER_VERSION_FILE = GLUSTER_BASE_DIR + "/version" -GLUSTER_UPDATE_SITE_FILE = GLUSTER_BASE_DIR + "/update-site" -GLUSTER_DIRECTORY_SERVICE_CONF_FILE = GLUSTER_BASE_DIR + "/directory.xml" -GLUSTER_TIME_CONF_FILE = GLUSTER_BASE_DIR + "/timeconfig.xml" -TRANSACTION_KEY_FILE = GLUSTER_BASE_DIR + "/transaction.key" -SERVER_COUNT_FILE = GLUSTER_BASE_DIR + "/server-count" -SIGNATURE_FILE = GLUSTER_BASE_DIR + "/.signature" -GLUSTER_SERVER_POOL_FILE = GLUSTER_BASE_DIR + "/pool" -GLUSTER_ADMIN_FILE = GLUSTER_BASE_DIR + "/.password" -INSTALLER_CONF_DIR = SERVER_CONF_DIR + "/" + INSTALLER_SERVER_NAME VOLUME_SMBCONF_FILE = VOLUME_CONF_DIR + "/volumes.smbconf.list" -GLOBAL_NETWORK_FILE = INSTALLER_CONF_DIR + "/network.xml" -INSTALLED_SERVER_COUNT_FILE = INSTALLER_CONF_DIR + "/installed-server-count" AWS_WEB_SERVICE_URL = "http://169.254.169.254/latest" DEFAULT_UID = 1024000 diff --git a/src/com.gluster.storage.management.gateway.scripts/src/common/Protocol.py b/src/com.gluster.storage.management.gateway.scripts/src/common/Protocol.py index 9649e534..e078be1a 100644 --- a/src/com.gluster.storage.management.gateway.scripts/src/common/Protocol.py +++ b/src/com.gluster.storage.management.gateway.scripts/src/common/Protocol.py @@ -7,7 +7,6 @@ import xml.parsers.expat import xml.dom.minidom as MDOM import os import Globals -import copy import Utils XML_STRING = 0 diff --git a/src/com.gluster.storage.management.gateway.scripts/src/common/Utils.py b/src/com.gluster.storage.management.gateway.scripts/src/common/Utils.py index 874acaa8..fc9bac5d 100644 --- a/src/com.gluster.storage.management.gateway.scripts/src/common/Utils.py +++ b/src/com.gluster.storage.management.gateway.scripts/src/common/Utils.py @@ -11,16 +11,10 @@ if not p1 in sys.path: if not p2 in sys.path: sys.path.append(p2) import re -import socket -import struct import syslog import subprocess -#import spwd import time -#import uuid import tempfile -import grp -import pwd import Globals @@ -30,6 +24,31 @@ SYSLOG_REQUIRED = False LOG_FILE_NAME = None LOG_FILE_OBJ = None logOpened = False +sshCommandPrefix = "ssh -l root -q -i /opt/glustermg/keys/gluster.pem -o BatchMode=yes -o GSSAPIAuthentication=no -o PasswordAuthentication=no -o StrictHostKeyChecking=no".split() +try: + commandPath = "/opt/glustermg/%s/backend" % os.environ['GMG_VERSION'] +except KeyError, e: + commandPath = "/opt/glustermg/1.0.0/backend" + +def log(priority, message=None): + global logOpened + if not logOpened: + syslog.openlog(os.path.basename(sys.argv[0])) + logOpened = True + + if type(priority) == type(""): + logPriority = syslog.LOG_INFO + logMessage = priority + else: + logPriority = priority + logMessage = message + if not logMessage: + return + #if Globals.DEBUG: + # sys.stderr.write(logMessage) + else: + syslog.syslog(logPriority, logMessage) + return def isString(value): @@ -42,10 +61,53 @@ def getTempFileName(): return filename +def readFile(fileName, lines=False): + content = None + try: + fp = open(fileName) + if lines: + content = fp.readlines() + else: + content = fp.read() + fp.close() + return content + except IOError, e: + log("failed to read file %s: %s" % (fileName, str(e))) + if lines: + return [] + else: + return "" + + +def writeFile(fileName, content): + try: + fp = open(fileName, "w") + if isString(content): + fp.write(content) + elif type(content) == type([]): + fp.writelines(content) + fp.close() + return True + except IOError, e: + log("failed to write file %s: %s" % (fileName, str(e))) + return False + + +def removeFile(fileName, root=False): + if root: + if runCommand("rm %s" % fileName, root=True) == 0: + return True + return False + try: + os.remove(fileName) + return True + except OSError, e: + log("Failed to remove file %s: %s" % (fileName, str(e))) + return False + + def runCommandBG(command, stdinFileObj=None, stdoutFileObj=None, stderrFileObj=None, shell=False, root=None): - log("runCommandBG(): Trying to execute command [%s]" % command) - if shell: if not isString(command): return None @@ -120,70 +182,18 @@ def runCommand(command, shell=shell, root=root) if process: rv['Status'] = process.wait() - rv['Stdout'] = open(stdoutFileName).read() - rv['Stderr'] = open(stderrFileName).read() + rv['Stdout'] = readFile(stdoutFileName) + rv['Stderr'] = readFile(stderrFileName) os.remove(stdinFileName) os.remove(stdoutFileName) os.remove(stderrFileName) - log("runCommand(): execution status of command [%s] = [%s]" % (command, rv)) - if output: return rv return rv["Status"] -def runCommandFG(command, stdout=False, stderr=False, - shell=False, root=None): - if stdout or stderr: - output = True - else: - output = False - return runCommand(command, output=output, shell=shell, root=root) - - -def IP2Number(ipString): - try: - return socket.htonl(struct.unpack("I", socket.inet_aton(ipString))[0]) - except socket.error, e: - return None - except TypeError, e: - return None - except struct.error, e: - return None - - -def Number2IP(number): - try: - return socket.inet_ntoa(struct.pack("I", socket.ntohl(number))) - except socket.error, e: - return None - except AttributeError, e: - return None - except ValueError, e: - return None - - -def computeHostName(hostName): - if not hostName: - return False - - hostPrefix = "" - for i in range(len(hostName), 0, -1): - pos = i - 1 - if hostName[pos].isdigit(): - continue - break - hostPrefix = hostName[:pos+1] - try: - hostIndex = int(hostName[pos+1:]) - except ValueError, e: - hostIndex = 0 - # TODO: Check the availablity of the (server) name - return "%s%s" % (hostPrefix, hostIndex + 1) - - def daemonize(): try: pid = os.fork() @@ -221,27 +231,11 @@ def daemonize(): return True -def getDownloadStatus(fileName): - try: - lines = [line for line in open(fileName) - if "saved" in line or "%" in line] - except IOError, e: - return 0 - if not lines: - return 0 - if "saved" in lines[-1]: - return 100 - return lines[-1].split("%")[0].split()[-1] - - def getMeminfo(): - """-> dict of data from meminfo (str:int). - Values are in kilobytes. - """ - import re + lines = readFile("/proc/meminfo", lines=True) re_parser = re.compile(r'^(?P<key>\S*):\s*(?P<value>\d*)\s*kB' ) result = {} - for line in open('/proc/meminfo'): + for line in lines: match = re_parser.match(line) if not match: continue # skip lines that don't parse @@ -251,29 +245,12 @@ def getMeminfo(): return result -def getCpuUsage(): - """-> dict of cpuid : (usertime, nicetime, systemtime, idletime) - cpuid "cpu" means the total for all CPUs. - cpuid "cpuN" means the value for CPU N. - """ - wanted_records = [line for line in open('/proc/stat') if - line.startswith('cpu')] - result = {} - for cpuline in wanted_records: - fields = cpuline.split()[:5] - data = map(int, fields[1:]) - result[fields[0]] = tuple(data) - return result - def _getCpuStatList(): - try: - fp = open("/proc/stat") - line = fp.readline() - fp.close() - return map(float, line.split()[1:5]) - except IOError, e: - log("Failed to open /proc/stat: %s" % str(e)) - return None + lines = readFile("/proc/stat", lines=True) + if not lines: + return None + return map(float, lines[0].split()[1:5]) + def getCpuUsageAvg(): st1 = _getCpuStatList() @@ -289,398 +266,23 @@ def getCpuUsageAvg(): except ZeroDivisionError, e: return 0 -def getLoadavg(): - try: - loadavgstr = open('/proc/loadavg', 'r').readline().strip() - except IOError, e: - syslog.syslog(syslog.LOG_ERR, "failed to find cpu load: %s" % str(e)) - return None - - data = map(float, loadavgstr.split()[1:]) - # returns 1 minute load average - return data[0] - - -def getInfinibandPortStatus(): - - """ Check for availability of infiniband port - and return which port is active in a key pair value - """ - - # Check for existence of infiniband ports - value = os.popen ("ls /sys/class/infiniband").readline().strip() - - if not value: - return None - - portlist = os.popen ("echo /sys/class/infiniband/*/ports/*").readline().split() - - portkeys = {} - - for port in portlist: - value = os.popen ("cat %s/state" % - port.strip()).readline().split(':')[1].strip() - portkeys[port.strip()] = value - - return portkeys - - -def getPasswordHash(userName): - try: - #return spwd.getspnam(userName).sp_pwd - return "Not implimented" - except KeyError, e: - return None - - -def generateSignature(): - #return str(uuid.uuid4()) + ('--%f' % time.time()) - return ('--%f' % time.time()) - - -def isUserExist(userName): - try: - grp.getgrnam(userName).gr_gid - return True - except KeyError, e: - pass - try: - pwd.getpwnam(userName).pw_uid - return True - except KeyError, e: - pass - return False - - -def getPlatformVersion(fileName=Globals.GLUSTER_VERSION_FILE): - versionInfo = {} - versionInfo["Version"] = None - versionInfo["Update"] = None - try: - lines = open(Globals.GLUSTER_VERSION_FILE).readlines() - for line in open(fileName): - line = line.strip() - k = line[:line.index("=")] - v = line[line.index("=") + 1:] - if v[0] == "'" or v[0] == '"': - v = v[1:] - if v[-1] == "'" or v[-1] == '"': - v = v[:-1] - if k.upper() == "VERSION": - versionInfo["Version"] = v - if k.upper() == "UPDATE": - versionInfo["Update"] = v - except IOError, e: - log("Failed to read file %s: %s" % (fileName, str(e))) - return versionInfo - - -def setPlatformVersion(versionInfo, fileName=Globals.GLUSTER_VERSION_FILE): - if isString(versionInfo): - tokens = versionInfo.strip().split(".") - if len(tokens) < 2: - log("Invalid version format %s. Expecting <MAJOR>.<MINOR>.<PATCHLEVEL>" % versionInfo) - return False - version = ".".join(tokens[:2]) - update = ".".join(tokens[2:]) - if not update: - update = "0" - else: - version = versionInfo["Version"] - update = versionInfo["Update"] - try: - fp = open(fileName, "w") - fp.write("VERSION=%s\n" % version) - fp.write("UPDATE=%s\n" % update) - fp.close() - return True - except IOError, e: - log("Failed to write file %s: %s" % (fileName, str(e))) - return False - - -def removeFile(fileName, root=False): - if root: - if runCommand("rm %s" % fileName, root=True) == 0: - return True - return False - try: - os.remove(fileName) - return True - except OSError, e: - log("Failed to remove file %s: %s" % (fileName, str(e))) - return False - - -def isLiveMode(): - return os.path.exists(Globals.LIVE_MODE_FILE) def convertKbToMb(kb): return kb / 1024.0 -def getIPIndex(indexFile): - try: - fp = open(indexFile) - line = fp.readline() - fp.close() - index = int(line) - except IOError, e: - index = 0 - except ValueError, e: - index = False - return index - -def setIPIndex(index, indexFile): - try: - fp = open(indexFile, "w") - fp.write(str(index)) - fp.close() - except IOError, e: - return False - return True - -def IP2Number(ipString): - try: - return socket.htonl(struct.unpack("I", socket.inet_aton(ipString))[0]) - except socket.error, e: - return None - except TypeError, e: - return None - except struct.error, e: - return None - -def Number2IP(number): - try: - return socket.inet_ntoa(struct.pack("I", socket.ntohl(number))) - except socket.error, e: - return None - except AttributeError, e: - return None - except ValueError, e: - return None - -def hasEntryFoundInFile(searchString, dnsEntryFileName): - try: - addServerEntryList = open(dnsEntryFileName).read().split() - except IOError, e: - return None - if searchString in addServerEntryList: - return True - return False - - -def computeIpAddress(ipAddress, startIp, endIp): - startIpNumber = IP2Number(startIp) - endIpNumber = IP2Number(endIp) - if not ipAddress: - return startIp - nextIpNumber = IP2Number(ipAddress) - while True: - nextIpNumber = nextIpNumber + 1 - ipAddress = Number2IP(nextIpNumber) - rv = runCommand("ping -qnc 1 %s" % ipAddress, output=True) - if rv["Status"] != 0: - return False - if rv != 0: - break - - if nextIpNumber >= startIpNumber and nextIpNumber <= endIpNumber: - return ipAddress - - nextIpNumber = IP2Number(startIp) - while True: - ipAddress = Number2IP(nextIpNumber) - nextIpNumber = nextIpNumber + 1 - rv = runCommand("ping -qnc 1 %s" % ipAddress, output=True) - if rv["Status"] != 0: - return False - if rv != 0: - break - - if IP2Number(ipAddress) >= startIpNumber and IP2Number(ipAddress) <= endIpNumber: - return ipAddress - return False - - -def setHostNameAndIp(hostName, ipAddress, lastAddServerDetailFile): - try: - fp = open(lastAddServerDetailFile, "w") - fp.write("HOSTNAME=" + hostName + "\n") - fp.write("IPADDRESS=" + ipAddress); - fp.close() - except IOError, e: - return False - return True - -def getPort(): - try: - fd = open(Globals.PORT_FILE, "r") - portString = fd.readline() - fd.close() - port = int(portString) - except IOError, e: - port = Globals.DEFAULT_PORT - 2 - except ValueError, e: - port = Globals.DEFAULT_PORT - 2 - return port - -def setPort(port): - try: - fd = open(Globals.PORT_FILE, "w") - fd.write(str(port)) - fd.close() - except IOError, e: - return False - return True - - -def daemonize(): - try: - pid = os.fork() - if pid > 0: - # exit first parent - sys.exit(0) - except OSError, e: - #sys.stderr.write("fork #1 failed: %d (%s)\n" % (e.errno, e.strerror)) - return False - - # decouple from parent environment - os.chdir("/") - os.setsid() - os.umask(0) - - # do second fork - try: - pid = os.fork() - if pid > 0: - # exit from second parent - sys.exit(0) - except OSError, e: - #sys.stderr.write("fork #2 failed: %d (%s)\n" % (e.errno, e.strerror)) - return False - - # redirect standard file descriptors - sys.stdout.flush() - sys.stderr.flush() - si = file("/dev/null", 'r') - so = file("/dev/null", 'a+') - se = file("/dev/null", 'a+', 0) - os.dup2(si.fileno(), sys.stdin.fileno()) - os.dup2(so.fileno(), sys.stdout.fileno()) - os.dup2(se.fileno(), sys.stderr.fileno()) - return True - - -def getDhcpServerStatus(): - if runCommand("service dnsmasq status", root=True) != 0: - return False - return True - -def startDhcpServer(): - if runCommand("service dnsmasq start", root=True) != 0: - return False - return True - -def stopDhcpServer(): - if runCommand("service dnsmasq stop", root=True) != 0: - return False - return True - -def getStoragePoolInfo(): - startRange = None - endRange = None - try: - for line in open(Globals.GLUSTER_SERVER_POOL_FILE): - tokens = line.split("=") - if tokens[0] == "STARTRANGE": - startRange = tokens[1].strip() - if tokens[0] == "ENDRANGE": - endRange = tokens[1].strip() - except IOError, e: - log(syslog.LOG_ERR, "unable to read %s file: %s" % (Globals.GLUSTER_SERVER_POOL_FILE, str(e))) - return startRange, endRange - -def configureDnsmasq(serverIpAddress, dhcpIpAddress): - dnsmasqConfFile = Globals.GLUSTER_CONF_CONF_DIR + "/dnsmasq.conf" - serverPortString = "68" - try: - for arg in open("/proc/cmdline").read().strip().split(): - token = arg.split("=") - if token[0] == "dhcp": - serverPortString = token[1] - break - except IOError, e: - log(syslog.LOG_ERR, "Failed to read /proc/cmdline. Continuing with default port 68: %s" % str(e)) - try: - serverPort = int(serverPortString) - except ValueError, e: - log(syslog.LOG_ERR, "Invalid dhcp port '%s' in /proc/cmdline. Continuing with default port 68: %s" % (serverPortString, str(e))) - serverPort = 68 - - try: - fp = open(dnsmasqConfFile, "w") - fp.write("no-hosts\n") - #fp.write("addn-hosts=%s\n" % Globals.GLUSTER_DNS_ENTRIES) - fp.write("bind-interfaces\n") - fp.write("except-interface=lo\n") - fp.write("dhcp-range=%s,%s\n" % (dhcpIpAddress, dhcpIpAddress)) - fp.write("dhcp-lease-max=1\n") - #fp.write("dhcp-option=option:router,%s\n" % serverIp) - #fp.write("dhcp-option=option:ntp-server,%s\n" % serverIp) - fp.write("dhcp-alternate-port=%s\n" % serverPort) - fp.write("server=%s\n" % serverIpAddress) - fp.write("dhcp-script=/usr/sbin/server-info\n") - fp.close() - except IOError, e: - log(syslog.LOG_ERR, "unable to write dnsmasq configuration %s: %s" % (dnsmasqConfFile, str(e))) - return False - if runCommand(["cp", "-f", Globals.GLUSTER_CONF_CONF_DIR + "/dnsmasq.conf", Globals.DNSMASQ_CONF_FILE], root=True) != 0: - log(syslog.LOG_ERR, "unable to copy dnsmasq configuration to " + Globals.DNSMASQ_CONF_FILE) - return False - return True - -def configureDhcpServer(serverIpAddress, dhcpIpAddress): - return configureDnsmasq(serverIpAddress, dhcpIpAddress) - -def log(priority, message=None): - global logOpened - if not logOpened: - syslog.openlog(os.path.basename(sys.argv[0])) - logOpened = True - - if type(priority) == type(""): - logPriority = syslog.LOG_INFO - logMessage = priority - else: - logPriority = priority - logMessage = message - if not logMessage: - return - #if Globals.DEBUG: - # sys.stderr.write(logMessage) - else: - syslog.syslog(logPriority, logMessage) - return - - -def stripEmptyLines(content): - ret = "" - for line in content.split("\n"): - if line.strip() != "": - ret += line - return ret - - def getDeviceFormatStatusFile(device): return "/var/tmp/format_%s.status" % device.replace('/', '_') + def getDeviceFormatLockFile(device): return "/var/lock/format_%s.lock" % device.replace('/', '_') + def getDeviceFormatOutputFile(device): return "/var/tmp/format_%s.out" % device.replace('/', '_') + def getGlusterVersion(): rv = runCommand("/usr/sbin/gluster --version", output=True) if rv["Stderr"]: @@ -691,34 +293,33 @@ def getGlusterVersion(): return None return rv["Stdout"].strip().split()[1] -def getCifsUserUid(userName): - try: - fp = open(Globals.CIFS_USER_FILE) - content = fp.read() - fp.close() - except IOError, e: - log("failed to read file %s: %s" % (Globals.CIFS_USER_FILE, str(e))) - return False - for line in content.strip().split(): - tokens = line.split(":") +def getCifsUserUid(userName): + lines = readFile(Globals.CIFS_USER_FILE, lines=True) + for line in lines: + if not line.strip(): + continue + tokens = line.strip().split(":") if tokens[1] == userName: return int(tokens[0]) return None -def readFile(fileName, lines=False): - content = None - try: - fp = open(fileName) - if lines: - content = fp.readlines() - else: - content = fp.read() - fp.close() - return content - except IOError, e: - log("failed to read file %s: %s" % (fileName, str(e))) - if lines: - return [] +def grun(serverFile, command, argumentList=[]): + commandList = ["%s/%s" % (commandPath, command)] + argumentList + serverNameList = readFile(serverFile, lines=True) + if not serverNameList: + return 1 + status = True + for serverName in serverNameList: + rv = runCommand(sshCommandPrefix + [serverName.strip()] + commandList, output=True) + if rv["Status"] != 0: + sys.stderr.write("%s: %s\n" % (serverName.strip(), rv["Status"])) + sys.stderr.write("Stdout:\n%s\n" % rv["Stdout"]) + sys.stderr.write("Stderr:\n%s\n" % rv["Stderr"]) + sys.stderr.write("---\n") + status = False + + if status: + return 0 else: - return "" + return 2 diff --git a/src/com.gluster.storage.management.gateway.scripts/src/common/XmlHandler.py b/src/com.gluster.storage.management.gateway.scripts/src/common/XmlHandler.py index 22c023cc..d55ef07a 100644 --- a/src/com.gluster.storage.management.gateway.scripts/src/common/XmlHandler.py +++ b/src/com.gluster.storage.management.gateway.scripts/src/common/XmlHandler.py @@ -7,7 +7,6 @@ import xml.parsers.expat import xml.dom.minidom as MDOM import os import Globals -import copy import Utils XML_STRING = 0 diff --git a/src/com.gluster.storage.management.gateway.scripts/src/gateway/add_user_cifs_all.py b/src/com.gluster.storage.management.gateway.scripts/src/gateway/add_user_cifs_all.py index 33adea0b..adfd031c 100755 --- a/src/com.gluster.storage.management.gateway.scripts/src/gateway/add_user_cifs_all.py +++ b/src/com.gluster.storage.management.gateway.scripts/src/gateway/add_user_cifs_all.py @@ -16,36 +16,19 @@ import Utils def getUid(userName): - try: - fp = open(Globals.CIFS_USER_FILE) - content = fp.read() - fp.close() - except IOError, e: - Utils.log("failed to read file %s: %s" % (Globals.CIFS_USER_FILE, str(e))) - return False - - for line in content.strip().split(): - tokens = line.split(":") + lines = Utils.readFile(Globals.CIFS_USER_FILE, lines=True) + for line in lines: + tokens = line.strip().split(":") if tokens[1] == userName: return int(tokens[0]) return None def getLastUid(): - if not os.path.exists(Globals.CIFS_USER_FILE): - return Globals.DEFAULT_UID - try: - fp = open(Globals.CIFS_USER_FILE) - content = fp.read() - fp.close() - except IOError, e: - Utils.log("failed to read file %s: %s" % (Globals.CIFS_USER_FILE, str(e))) - return False - - lines = content.strip().split() + lines = Utils.readFile(Globals.CIFS_USER_FILE, lines=True) if not lines: return Globals.DEFAULT_UID - return int(lines[-1].split(":")[0]) + return int([line.strip().split(':')[0] for line in lines if line.strip()][-1]) def setUid(uid, userName): @@ -80,7 +63,7 @@ def main(): existingUser = True print (serverFile, uid, userName, password) - rv = Utils.runCommand("grun.py %s add_user_cifs.py %s %s %s" % (serverFile, uid, userName, password)) + rv = Utils.grun(serverFile, "add_user_cifs.py", [uid, userName, password]) if existingUser: sys.exit(rv) diff --git a/src/com.gluster.storage.management.gateway.scripts/src/gateway/create_volume_cifs_all.py b/src/com.gluster.storage.management.gateway.scripts/src/gateway/create_volume_cifs_all.py index 59e74bed..8a43e7dc 100755 --- a/src/com.gluster.storage.management.gateway.scripts/src/gateway/create_volume_cifs_all.py +++ b/src/com.gluster.storage.management.gateway.scripts/src/gateway/create_volume_cifs_all.py @@ -16,18 +16,13 @@ import Utils def addVolumeCifsConf(volumeName, userList): - try: - fp = open(Globals.CIFS_VOLUME_FILE) - content = fp.read() - fp.close() - except IOError, e: - Utils.log("failed to read file %s: %s" % (Globals.CIFS_VOLUME_FILE, str(e))) - content = "" - + lines = Utils.readFile(Globals.CIFS_VOLUME_FILE, lines=True) try: fp = open(Globals.CIFS_VOLUME_FILE, "w") - for line in content.split(): - if line.split(":")[0] != volumeName: + for line in lines: + if not line.strip(): + continue + if line.strip().split(":")[0] != volumeName: fp.write("%s\n" % line) fp.write("%s:%s\n" % (volumeName, ":".join(userList))) fp.close() @@ -55,7 +50,7 @@ def main(): sys.stderr.write("User %s does not exists\n" % missingUserList) sys.exit(1) - rv = Utils.runCommand(["grun.py", serverFile, "create_volume_cifs.py", volumeName] + userList) + rv = Utils.grun(serverFile, "create_volume_cifs.py", [volumeName] + userList) if rv == 0: if not addVolumeCifsConf(volumeName, userList): sys.stderr.write("Failed to add volume %s and user-list %s in cifs volume configuration\n" % (volumeName, userList)) diff --git a/src/com.gluster.storage.management.gateway.scripts/src/gateway/delete_user_cifs_all.py b/src/com.gluster.storage.management.gateway.scripts/src/gateway/delete_user_cifs_all.py index 3c68891c..a86e7264 100755 --- a/src/com.gluster.storage.management.gateway.scripts/src/gateway/delete_user_cifs_all.py +++ b/src/com.gluster.storage.management.gateway.scripts/src/gateway/delete_user_cifs_all.py @@ -16,18 +16,12 @@ import Utils def removeUser(userName): - try: - fp = open(Globals.CIFS_USER_FILE) - content = fp.read() - fp.close() - except IOError, e: - Utils.log("failed to read file %s: %s" % (Globals.CIFS_USER_FILE, str(e))) - return False - + lines = Utils.readFile(Globals.CIFS_USER_FILE, lines=True) try: fp = open(Globals.CIFS_USER_FILE, "w") - lines = content.strip().split() for line in lines: + if not line.strip(): + continue if line.split(":")[1] == userName: continue fp.write("%s\n" % line) @@ -40,13 +34,13 @@ def removeUser(userName): def main(): if len(sys.argv) < 3: - sys.stderr.write("usage: %s SERVER_LIST USERNAME\n" % os.path.basename(sys.argv[0])) + sys.stderr.write("usage: %s SERVER_FILE USERNAME\n" % os.path.basename(sys.argv[0])) sys.exit(-1) - serverList = sys.argv[1] + serverFile = sys.argv[1] userName = sys.argv[2] - rv = Utils.runCommand("grun.py %s delete_user_cifs.py %s" % (serverList, userName)) + rv = Utils.grun(serverFile, "delete_user_cifs.py", [userName]) if rv == 0: if not removeUser(userName): Utils.log("Failed to remove the user:%s on gateway server\n" % userName) diff --git a/src/com.gluster.storage.management.gateway.scripts/src/gateway/delete_volume_cifs_all.py b/src/com.gluster.storage.management.gateway.scripts/src/gateway/delete_volume_cifs_all.py index 3456b92d..925a3548 100755 --- a/src/com.gluster.storage.management.gateway.scripts/src/gateway/delete_volume_cifs_all.py +++ b/src/com.gluster.storage.management.gateway.scripts/src/gateway/delete_volume_cifs_all.py @@ -16,18 +16,13 @@ import Utils def removeVolumeCifsConf(volumeName): - try: - fp = open(Globals.CIFS_VOLUME_FILE) - content = fp.read() - fp.close() - except IOError, e: - Utils.log("failed to read file %s: %s" % (Globals.CIFS_VOLUME_FILE, str(e))) - content = "" - + lines = Utils.readFile(Globals.CIFS_VOLUME_FILE, lines=True) try: fp = open(Globals.CIFS_VOLUME_FILE, "w") - for line in content.split(): - if line.split(":")[0] != volumeName: + for line in lines: + if not line.strip(): + continue + if line.strip().split(":")[0] != volumeName: fp.write("%s\n" % line) fp.close() except IOError, e: @@ -44,7 +39,7 @@ def main(): serverFile = sys.argv[1] volumeName = sys.argv[2] - rv = Utils.runCommand(["grun.py", serverFile, "delete_volume_cifs.py", volumeName]) + rv = Utils.grun(serverFile, "delete_volume_cifs.py", [volumeName]) if rv == 0: if not removeVolumeCifsConf(volumeName): sys.stderr.write("Failed to remove volume %s and user-list in cifs volume configuration\n" % volumeName) diff --git a/src/com.gluster.storage.management.gateway.scripts/src/gateway/get_volume_user_cifs.py b/src/com.gluster.storage.management.gateway.scripts/src/gateway/get_volume_user_cifs.py index c385633e..c072a556 100755 --- a/src/com.gluster.storage.management.gateway.scripts/src/gateway/get_volume_user_cifs.py +++ b/src/com.gluster.storage.management.gateway.scripts/src/gateway/get_volume_user_cifs.py @@ -22,24 +22,16 @@ def main(): volumeName = sys.argv[1] - if not os.path.exists(Globals.CIFS_VOLUME_FILE): - sys.exit(0) - - try: - fp = open(Globals.CIFS_VOLUME_FILE) - content = fp.read() - fp.close() - for line in content.split(): - tokens = line.split(":") - if tokens[0] == volumeName: - print "\n".join(tokens[1:]) - sys.exit(0) - # given volume is not configured for cifs export - sys.exit(0) - except IOError, e: - Utils.log("failed to read file %s: %s" % (Globals.CIFS_VOLUME_FILE, str(e))) - sys.stderr.write("Failed to read cifs-volume-file %s: %s\n" % (Globals.CIFS_VOLUME_FILE, str(e))) - sys.exit(2) + lines = Utils.readFile(Globals.CIFS_VOLUME_FILE, lines=True) + for line in lines: + if not line.strip(): + continue + tokens = line.strip().split(":") + if tokens[0] == volumeName: + print "\n".join(tokens[1:]) + sys.exit(0) + # given volume is not configured for cifs export + sys.exit(0) if __name__ == "__main__": diff --git a/src/com.gluster.storage.management.gateway.scripts/src/gateway/grun.py b/src/com.gluster.storage.management.gateway.scripts/src/gateway/grun.py index ac91d0c8..6519d726 100755 --- a/src/com.gluster.storage.management.gateway.scripts/src/gateway/grun.py +++ b/src/com.gluster.storage.management.gateway.scripts/src/gateway/grun.py @@ -14,43 +14,8 @@ if not p2 in sys.path: import Utils -def main(): - sshCommandPrefix = "ssh -l root -q -i /opt/glustermg/keys/gluster.pem -o BatchMode=yes -o GSSAPIAuthentication=no -o PasswordAuthentication=no -o StrictHostKeyChecking=no".split() +if len(sys.argv) < 3: + sys.stderr.write("usage: %s SERVER_FILE COMMAND [ARGUMENTS]\n" % os.path.basename(sys.argv[0])) + sys.exit(-1) - if len(sys.argv) < 3: - sys.stderr.write("usage: %s SERVER_FILE COMMAND [ARGUMENTS]\n" % os.path.basename(sys.argv[0])) - sys.exit(-1) - serverFile = sys.argv[1] - try: - command = ["/opt/glustermg/%s/backend/%s" % (os.environ['GMG_VERSION'], sys.argv[2])] - except KeyError, e: - command = ["/opt/glustermg/1.0.0/backend/%s" % sys.argv[2]] - command += sys.argv[3:] - - try: - fp = open(serverFile) - serverNameList = fp.readlines() - fp.close() - except IOError, e: - Utils.log("Failed to read server file %s: %s\n" % (serverFile, str(e))) - sys.stderr.write("Failed to read server file %s: %s\n" % (serverFile, str(e))) - sys.exit(1) - - status = True - for serverName in serverNameList: - rv = Utils.runCommand(sshCommandPrefix + [serverName.strip()] + command, output=True) - if rv["Status"] != 0: - sys.stderr.write("%s: %s\n" % (serverName, rv["Status"])) - sys.stderr.write("Stdout:\n%s\n" % rv["Stdout"]) - sys.stderr.write("Stderr:\n%s\n" % rv["Stderr"]) - sys.stderr.write("---\n") - status = False - - if status: - sys.exit(0) - else: - sys.exit(2) - - -if __name__ == "__main__": - main() +sys.exit(Utils.grun(sys.argv[1], sys.argv[2], sys.argv[3:])) diff --git a/src/com.gluster.storage.management.gateway.scripts/src/gateway/remove_server_volume_cifs_config.py b/src/com.gluster.storage.management.gateway.scripts/src/gateway/remove_server_volume_cifs_config.py index e90b6a57..27fb9b92 100755 --- a/src/com.gluster.storage.management.gateway.scripts/src/gateway/remove_server_volume_cifs_config.py +++ b/src/com.gluster.storage.management.gateway.scripts/src/gateway/remove_server_volume_cifs_config.py @@ -23,29 +23,13 @@ def main(): serverName = sys.argv[1] volumeFile = sys.argv[2] - try: - fp = open(volumeFile) - lines = fp.readlines() - fp.close() - except IOError, e: - Utils.log("Failed to read volume file %s: %s" % (volumeFile, str(e))) - sys.stderr.write("Failed to read volume file %s: %s\n" % (volumeFile, str(e))) - sys.exit(1) - + lines = Utils.readFile(volumeFile, lines=True) volumeNameList = [line.strip() for line in lines] if not volumeNameList: sys.exit(0) - try: - fp = open(Globals.CIFS_VOLUME_FILE) - content = fp.read() - fp.close() - except IOError, e: - Utils.log("failed to read file %s: %s" % (Globals.CIFS_VOLUME_FILE, str(e))) - sys.stderr.write("failed to read file %s: %s\n" % (Globals.CIFS_VOLUME_FILE, str(e))) - sys.exit(2) - - cifsVolumeList = [line.split(":")[0] for line in content.split()] + lines = Utils.readFile(Globals.CIFS_VOLUME_FILE, lines=True) + cifsVolumeList = [line.strip().split(":")[0] for line in lines if line.strip()] runningCifsVolumeList = set(cifsVolumeList).intersection(set(volumeNameList)) if not runningCifsVolumeList: @@ -63,9 +47,9 @@ def main(): status = True for volumeName in runningCifsVolumeList: - if Utils.runCommand(["grun.py", tempFileName, "stop_volume_cifs.py", volumeName.strip()]) != 0: + if Utils.grun(tempFileName, "stop_volume_cifs.py", [volumeName.strip()]) != 0: status = False - if Utils.runCommand(["grun.py", tempFileName, "delete_volume_cifs.py", volumeName.strip()]) != 0: + if Utils.grun(tempFileName, "delete_volume_cifs.py", [volumeName.strip()]) != 0: status = False try: diff --git a/src/com.gluster.storage.management.gateway.scripts/src/gateway/setup_cifs_config_all.py b/src/com.gluster.storage.management.gateway.scripts/src/gateway/setup_cifs_config_all.py index 8dd59c8c..e7e0a4a0 100755 --- a/src/com.gluster.storage.management.gateway.scripts/src/gateway/setup_cifs_config_all.py +++ b/src/com.gluster.storage.management.gateway.scripts/src/gateway/setup_cifs_config_all.py @@ -21,7 +21,7 @@ def main(): serverFile = sys.argv[1] - rv = Utils.runCommand(["grun.py", serverFile, "setup_cifs_config.py"]) + rv = Utils.grun(serverFile, "setup_cifs_config.py") sys.exit(rv) diff --git a/src/com.gluster.storage.management.gateway.scripts/src/gateway/update_volume_cifs_all.py b/src/com.gluster.storage.management.gateway.scripts/src/gateway/update_volume_cifs_all.py index c5c9d1ef..e5576c45 100755 --- a/src/com.gluster.storage.management.gateway.scripts/src/gateway/update_volume_cifs_all.py +++ b/src/com.gluster.storage.management.gateway.scripts/src/gateway/update_volume_cifs_all.py @@ -16,21 +16,16 @@ import Utils def updateVolumeCifsConf(volumeName, userList): - try: - fp = open(Globals.CIFS_VOLUME_FILE) - content = fp.read() - fp.close() - except IOError, e: - Utils.log("failed to read file %s: %s" % (Globals.CIFS_VOLUME_FILE, str(e))) - return False - + lines = Utils.readFile(Globals.CIFS_VOLUME_FILE, lines=True) try: fp = open(Globals.CIFS_VOLUME_FILE, "w") - for line in content.split(): - if line.split(":")[0] == volumeName: + for line in lines: + if not line.strip(): + continue + if line.strip().split(":")[0] == volumeName: fp.write("%s:%s\n" % (volumeName, ":".join(userList))) else: - fp.write("%s\n" % line) + fp.write("%s\n" % line.strip()) fp.close() except IOError, e: Utils.log("failed to write file %s: %s" % (Globals.CIFS_VOLUME_FILE, str(e))) @@ -56,8 +51,7 @@ def main(): sys.stderr.write("User %s does not exists\n" % missingUserList) sys.exit(1) - - rv = Utils.runCommand(["grun.py", serverFile, "update_volume_cifs.py", volumeName] + userList) + rv = Utils.grun(serverFile, "update_volume_cifs.py", [volumeName] + userList) if rv == 0: if not updateVolumeCifsConf(volumeName, userList): sys.stderr.write("Failed to update volume %s and user-list %s in cifs volume configuration\n" % (volumeName, userList)) diff --git a/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/services/VolumeService.java b/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/services/VolumeService.java index 9b6af355..eb585b98 100644 --- a/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/services/VolumeService.java +++ b/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/services/VolumeService.java @@ -477,7 +477,7 @@ public class VolumeService { } String gzipPath = FileUtil.getTempDirName() + CoreConstants.FILE_SEPARATOR + volume.getName() + "-logs.tar.gz"; - new ProcessUtil().executeCommand("tar", "czvf", gzipPath, "-C", tempDir.getParent(), tempDir.getName()); + ProcessUtil.executeCommand("tar", "czvf", gzipPath, "-C", tempDir.getParent(), tempDir.getName()); // delete the temp directory FileUtil.recursiveDelete(tempDir); @@ -589,7 +589,7 @@ public class VolumeService { String logFilePath = logDir + CoreConstants.FILE_SEPARATOR + logFileName; // Usage: get_volume_disk_log.py <volumeName> <diskName> <lineCount> - LogMessageListResponse response = serverUtil.executeScriptOnServer(true, brick.getServerName(), + LogMessageListResponse response = serverUtil.executeScriptOnServer(brick.getServerName(), VOLUME_BRICK_LOG_SCRIPT + " " + logFilePath + " " + lineCount, LogMessageListResponse.class); // populate disk and trim other fields @@ -782,8 +782,8 @@ public class VolumeService { String brickDirectory = brickInfo[1]; try { - serverUtil.executeScriptOnServer(true, serverName, VOLUME_DIRECTORY_CLEANUP_SCRIPT + " " - + brickDirectory + " " + (deleteFlag ? "-d" : ""), String.class); + serverUtil.executeScriptOnServer(serverName, VOLUME_DIRECTORY_CLEANUP_SCRIPT + " " + + brickDirectory + " " + (deleteFlag ? "-d" : "")); } catch(Exception e) { logger.error("Error while cleaning brick [" + serverName + ":" + brickDirectory + "] of volume [" + volumeName + "] : " + e.getMessage(), e); @@ -842,9 +842,8 @@ public class VolumeService { String brickDirectory = brick.getBrickDirectory(); // String mountPoint = brickDirectory.substring(0, brickDirectory.lastIndexOf("/")); - serverUtil.executeScriptOnServer(true, brick.getServerName(), - VOLUME_DIRECTORY_CLEANUP_SCRIPT + " " + brickDirectory + " " + (deleteFlag ? "-d" : ""), - String.class); + serverUtil.executeScriptOnServer(brick.getServerName(), VOLUME_DIRECTORY_CLEANUP_SCRIPT + " " + + brickDirectory + " " + (deleteFlag ? "-d" : "")); } } @@ -947,14 +946,13 @@ public class VolumeService { throw new GlusterRuntimeException("No online servers found in cluster [" + clusterName + "]"); } - String command = "gluster volume set help-xml"; try { - return serverUtil.executeOnServer(true, onlineServer.getName(), command, VolumeOptionInfoListResponse.class); + return glusterUtil.getVolumeOptionsInfo(onlineServer.getName()); } catch (Exception e) { // check if online server has gone offline. If yes, try again one more time. if (e instanceof ConnectionException || serverUtil.isServerOnline(onlineServer) == false) { onlineServer = clusterService.getNewOnlineServer(clusterName); - return serverUtil.executeOnServer(true, onlineServer.getName(), command, VolumeOptionInfoListResponse.class); + return glusterUtil.getVolumeOptionsInfo(onlineServer.getName()); } else { throw new GlusterRuntimeException("Fetching volume options info failed! [" + e.getMessage() + "]"); } diff --git a/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/tasks/InitializeDiskTask.java b/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/tasks/InitializeDiskTask.java index 591ddef0..7586795a 100644 --- a/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/tasks/InitializeDiskTask.java +++ b/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/tasks/InitializeDiskTask.java @@ -119,8 +119,8 @@ public class InitializeDiskTask extends Task { private void startInitializeDisk(String serverName) { String fsTypeCommand = (getFsType().equals(GlusterConstants.FSTYPE_DEFAULT)) ? "" : " -t " + getFsType(); - String output = serverUtil.executeScriptOnServer(true, serverName, INITIALIZE_DISK_SCRIPT - + fsTypeCommand + " " + getDiskName(), String.class); + String output = serverUtil.executeScriptOnServer(serverName, INITIALIZE_DISK_SCRIPT + fsTypeCommand + " " + + getDiskName()); TaskStatus taskStatus = new TaskStatus(new Status(Status.STATUS_CODE_RUNNING, output)); taskStatus.setPercentageSupported((getFsType().equals(GlusterConstants.FSTYPE_XFS)) ? false : true); getTaskInfo().setStatus(taskStatus); diff --git a/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/tasks/MigrateBrickTask.java b/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/tasks/MigrateBrickTask.java index 35f9ba29..5d321d71 100644 --- a/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/tasks/MigrateBrickTask.java +++ b/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/tasks/MigrateBrickTask.java @@ -106,12 +106,11 @@ public class MigrateBrickTask extends Task { private void startMigration(String onlineServerName) { String volumeName = getTaskInfo().getReference().split("#")[0]; - ProcessResult processResult = glusterUtil.executeBrickMigration(onlineServerName, volumeName, + String output = glusterUtil.executeBrickMigration(onlineServerName, volumeName, getFromBrick(), getToBrick(), "start"); - if (processResult.getOutput().trim().matches(".*started successfully$")) { + if (output.matches(".*started successfully$")) { getTaskInfo().setStatus( - new TaskStatus(new Status(Status.STATUS_CODE_RUNNING, processResult.getOutput().trim()))); - return; + new TaskStatus(new Status(Status.STATUS_CODE_RUNNING, output))); } } @@ -132,12 +131,12 @@ public class MigrateBrickTask extends Task { private void pauseMigration(String onlineServer) { String volumeName = getTaskInfo().getReference().split("#")[0]; - ProcessResult processResult = glusterUtil.executeBrickMigration(onlineServer, volumeName, + String output = glusterUtil.executeBrickMigration(onlineServer, volumeName, getFromBrick(), getToBrick(), "pause"); TaskStatus taskStatus = new TaskStatus(); - if (processResult.getOutput().trim().matches(".*paused successfully$")) { + if (output.matches(".*paused successfully$")) { taskStatus.setCode(Status.STATUS_CODE_PAUSE); - taskStatus.setMessage(processResult.getOutput()); + taskStatus.setMessage(output); getTaskInfo().setStatus(taskStatus); return; } @@ -165,15 +164,13 @@ public class MigrateBrickTask extends Task { private void commitMigration(String serverName) { String volumeName = getTaskInfo().getReference().split("#")[0]; - ProcessResult processResult = glusterUtil.executeBrickMigration(serverName, volumeName, - getFromBrick(), getToBrick(), "commit"); + String output = glusterUtil.executeBrickMigration(serverName, volumeName, getFromBrick(), getToBrick(), + "commit"); TaskStatus taskStatus = new TaskStatus(); - if (processResult.isSuccess()) { - if (processResult.getOutput().trim().matches(".*commit successful$")) { - taskStatus.setCode(Status.STATUS_CODE_SUCCESS); - taskStatus.setMessage(processResult.getOutput()); - getTaskInfo().setStatus(taskStatus); - } + if (output.matches(".*commit successful$")) { + taskStatus.setCode(Status.STATUS_CODE_SUCCESS); + taskStatus.setMessage(output); + getTaskInfo().setStatus(taskStatus); } } @@ -194,12 +191,12 @@ public class MigrateBrickTask extends Task { private void stopMigration(String serverName) { String volumeName = getTaskInfo().getReference().split("#")[0]; - ProcessResult processResult = glusterUtil.executeBrickMigration(serverName, volumeName, getFromBrick(), + String output = glusterUtil.executeBrickMigration(serverName, volumeName, getFromBrick(), getToBrick(), "abort"); TaskStatus taskStatus = new TaskStatus(); - if (processResult.getOutput().trim().matches(".*aborted successfully$")) { + if (output.matches(".*aborted successfully$")) { taskStatus.setCode(Status.STATUS_CODE_SUCCESS); - taskStatus.setMessage(processResult.getOutput()); + taskStatus.setMessage(output); getTaskInfo().setStatus(taskStatus); } } @@ -225,34 +222,29 @@ public class MigrateBrickTask extends Task { } TaskStatus taskStatus = new TaskStatus(); - try { - String volumeName = getTaskInfo().getReference().split("#")[0]; - ProcessResult processResult = glusterUtil.executeBrickMigration(serverName, volumeName, - getFromBrick(), getToBrick(), "status"); - String output = processResult.getOutput().trim(); - if (output.matches("^Number of files migrated.*Migration complete$") - || output.matches("^Number of files migrated = 0 .*Current file=")) { - // Note: Workaround - if no file in the volume brick to migrate, - // Gluster CLI is not giving proper (complete) status - taskStatus.setCode(Status.STATUS_CODE_COMMIT_PENDING); - if (autoCommit) { - commitMigration(serverName); - return getTaskInfo().getStatus(); // return the committed status - } else { - taskStatus.setMessage(output.replaceAll("Migration complete", "Commit pending")); - } - } else if (output.matches("^Number of files migrated.*Current file=.*")) { - taskStatus.setCode(Status.STATUS_CODE_RUNNING); - } else if (output.matches("^replace brick has been paused.*")) { - taskStatus.setCode(Status.STATUS_CODE_PAUSE); + String volumeName = getTaskInfo().getReference().split("#")[0]; + String output = glusterUtil.executeBrickMigration(serverName, volumeName, getFromBrick(), + getToBrick(), "status"); + + if (output.matches("^Number of files migrated.*Migration complete$") + || output.matches("^Number of files migrated = 0 .*Current file=")) { + // Note: Workaround - if no file in the volume brick to migrate, + // Gluster CLI is not giving proper (complete) status + taskStatus.setCode(Status.STATUS_CODE_COMMIT_PENDING); + if (autoCommit) { + commitMigration(serverName); + return getTaskInfo().getStatus(); // return the committed status } else { - taskStatus.setCode(Status.STATUS_CODE_FAILURE); + taskStatus.setMessage(output.replaceAll("Migration complete", "Commit pending")); } - taskStatus.setMessage(output); - } catch (Exception e) { + } else if (output.matches("^Number of files migrated.*Current file=.*")) { + taskStatus.setCode(Status.STATUS_CODE_RUNNING); + } else if (output.matches("^replace brick has been paused.*")) { + taskStatus.setCode(Status.STATUS_CODE_PAUSE); + } else { taskStatus.setCode(Status.STATUS_CODE_FAILURE); - taskStatus.setMessage(e.getMessage()); } + taskStatus.setMessage(output); taskInfo.setStatus(taskStatus); // Update the task status return taskStatus; } diff --git a/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/tasks/RebalanceVolumeTask.java b/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/tasks/RebalanceVolumeTask.java index 19ae0383..d81ea4bf 100644 --- a/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/tasks/RebalanceVolumeTask.java +++ b/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/tasks/RebalanceVolumeTask.java @@ -78,7 +78,7 @@ public class RebalanceVolumeTask extends Task { private void startRebalance(String serverName) { String command = "gluster volume rebalance " + getTaskInfo().getReference() + " " + getLayout() + " start"; - String output = serverUtil.executeOnServer(true, serverName, command, String.class); + String output = serverUtil.executeOnServer(serverName, command); getTaskInfo().setStatus(new TaskStatus(new Status(Status.STATUS_CODE_RUNNING, output))); } diff --git a/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/utils/AbstractStatsFactory.java b/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/utils/AbstractStatsFactory.java index d5167e21..7f3045ba 100644 --- a/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/utils/AbstractStatsFactory.java +++ b/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/utils/AbstractStatsFactory.java @@ -154,8 +154,8 @@ public abstract class AbstractStatsFactory implements StatsFactory { argsStr += " " + arg; } } - return serverUtil.executeScriptOnServer(true, serverName, getStatsScriptName() + argsStr + " " - + period, ServerStats.class); + return serverUtil.executeScriptOnServer(serverName, getStatsScriptName() + argsStr + " " + period, + ServerStats.class); } public abstract String getStatsScriptName(); diff --git a/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/utils/GlusterUtil.java b/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/utils/GlusterUtil.java index f41dd989..3bcd5826 100644 --- a/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/utils/GlusterUtil.java +++ b/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/utils/GlusterUtil.java @@ -20,12 +20,12 @@ */ package com.gluster.storage.management.gateway.utils; -import java.io.File; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Map.Entry; +import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -44,13 +44,11 @@ import com.gluster.storage.management.core.model.Volume; import com.gluster.storage.management.core.model.Volume.TRANSPORT_TYPE; import com.gluster.storage.management.core.model.Volume.VOLUME_STATUS; import com.gluster.storage.management.core.model.Volume.VOLUME_TYPE; -import com.gluster.storage.management.core.utils.ProcessResult; +import com.gluster.storage.management.core.response.VolumeOptionInfoListResponse; import com.gluster.storage.management.core.utils.StringUtil; @Component public class GlusterUtil { - private static final String glusterFSminVersion = "3.1"; - private static final String HOSTNAME_PFX = "Hostname:"; private static final String UUID_PFX = "Uuid:"; private static final String STATE_PFX = "State:"; @@ -75,20 +73,11 @@ public class GlusterUtil { private static final String INITIALIZE_DISK_STATUS_SCRIPT = "get_format_device_status.py"; private static final String BRICK_STATUS_SCRIPT = "get_brick_status.py"; - @Autowired - private SshUtil sshUtil; + private static final Logger logger = Logger.getLogger(GlusterUtil.class); @Autowired private ServerUtil serverUtil; - public void setSshUtil(SshUtil sshUtil) { - this.sshUtil = sshUtil; - } - - public SshUtil getSshUtil() { - return sshUtil; - } - /** * Extract value of given token from given line. It is assumed that the token, if present, will be of the following * form: <code>token: value</code> @@ -117,12 +106,7 @@ public class GlusterUtil { } private String getUuid(String serverName) { - ProcessResult result = getSshUtil().executeRemote(serverName, "cat " + GLUSTERD_INFO_FILE); - if (!result.isSuccess()) { - throw new GlusterRuntimeException("Couldn't read file [" + GLUSTERD_INFO_FILE + "]. Error: " - + result.toString()); - } - return result.getOutput().split("=")[1]; + return serverUtil.executeOnServer(serverName, "cat " + GLUSTERD_INFO_FILE, String.class).split("=")[1]; } public List<GlusterServer> getGlusterServers(GlusterServer knownServer) { @@ -174,71 +158,31 @@ public class GlusterUtil { return glusterServers; } - public List<String> getGlusterServerNames(String knownServer) { - String output = getPeerStatus(knownServer); - if (output == null) { - return null; - } - - List<String> glusterServerNames = new ArrayList<String>(); - for (String line : output.split(CoreConstants.NEWLINE)) { - String hostName = extractToken(line, HOSTNAME_PFX); - if (hostName != null) { - glusterServerNames.add(hostName); - } - } - return glusterServerNames; - } - /** * @param knownServer * A known server on which the gluster command will be executed to fetch peer status * @return Outout of the "gluster peer status" command */ private String getPeerStatus(String knownServer) { - ProcessResult result = getSshUtil().executeRemote(knownServer, "gluster peer status"); - if (!result.isSuccess()) { - throw new GlusterRuntimeException("Couldn't get peer status on server [" + knownServer + "]. Error: " - + result); - } - return result.getOutput(); + return serverUtil.executeOnServer(knownServer, "gluster peer status", String.class); } public void addServer(String existingServer, String newServer) { - ProcessResult result = sshUtil.executeRemote(existingServer, "gluster peer probe " + newServer); - if(!result.isSuccess()) { - throw new GlusterRuntimeException("Couldn't probe server [" + newServer + "] from [" + existingServer - + "]. Error: " + result); - } - + serverUtil.executeOnServer(existingServer, "gluster peer probe " + newServer); // reverse peer probe to ensure that host names appear in peer status on both sides - result = sshUtil.executeRemote(newServer, "gluster peer probe " + existingServer); - if(!result.isSuccess()) { - throw new GlusterRuntimeException("Couldn't _reverse_ probe server [" + existingServer + "] from [" - + newServer + "]. Error: " + result); - } + serverUtil.executeOnServer(newServer, "gluster peer probe " + existingServer); } public void startVolume(String volumeName, String knownServer) { - ProcessResult result = sshUtil.executeRemote(knownServer, "gluster volume start " + volumeName); - if(!result.isSuccess()) { - throw new GlusterRuntimeException("Couldn't start volume [" + volumeName + "]! Error: " + result.toString()); - } + serverUtil.executeOnServer(knownServer, "gluster volume start " + volumeName); } public void stopVolume(String volumeName, String knownServer) { - ProcessResult result = sshUtil.executeRemote(knownServer, "gluster --mode=script volume stop " + volumeName); - if(!result.isSuccess()) { - throw new GlusterRuntimeException("Couldn't stop volume [" + volumeName + "]! Error: " + result.toString()); - } + serverUtil.executeOnServer(knownServer, "gluster --mode=script volume stop " + volumeName); } public void resetOptions(String volumeName, String knownServer) { - ProcessResult result = sshUtil.executeRemote(knownServer, "gluster volume reset " + volumeName); - if(!result.isSuccess()) { - throw new GlusterRuntimeException("Couldn't reset options for volume [" + volumeName + "]! Error: " - + result); - } + serverUtil.executeOnServer(knownServer, "gluster volume reset " + volumeName); } public void createVolume(String knownServer, String volumeName, String volumeTypeStr, String transportTypeStr, @@ -259,10 +203,7 @@ public class GlusterUtil { String command = prepareVolumeCreateCommand(volumeName, StringUtil.extractList(bricks, ","), count, volTypeArg, transportTypeArg); - ProcessResult result = sshUtil.executeRemote(knownServer, command); - if (!result.isSuccess()) { - throw new GlusterRuntimeException("Error in creating volume [" + volumeName + "]: " + result); - } + serverUtil.executeOnServer(knownServer, command); try { createOptions(volumeName, StringUtil.extractMap(options, ",", "="), knownServer); @@ -308,37 +249,20 @@ public class GlusterUtil { } public void setOption(String volumeName, String key, String value, String knownServer) { - ProcessResult result = sshUtil.executeRemote(knownServer, "gluster volume set " + volumeName + " " + key + " " - + "\"" + value + "\""); - if (!result.isSuccess()) { - throw new GlusterRuntimeException("Volume [" + volumeName + "] set [" + key + "=" + value + "] => " - + result); - } + serverUtil.executeOnServer(knownServer, "gluster volume set " + volumeName + " " + key + " " + "\"" + + value + "\""); } public void deleteVolume(String volumeName, String knownServer) { - ProcessResult result = sshUtil.executeRemote(knownServer, "gluster --mode=script volume delete " + volumeName); - if (!result.isSuccess()) { - throw new GlusterRuntimeException("Couldn't delete volume [" + volumeName + "]! Error: " + result); - } + serverUtil.executeOnServer(knownServer, "gluster --mode=script volume delete " + volumeName); } private String getVolumeInfo(String volumeName, String knownServer) { - ProcessResult result = sshUtil.executeRemote(knownServer, "gluster volume info " + volumeName); - if (!result.isSuccess()) { - throw new GlusterRuntimeException("Command [gluster volume info " + volumeName + "] failed on [" - + knownServer + "] with error: " + result); - } - return result.getOutput(); + return serverUtil.executeOnServer(knownServer, "gluster volume info " + volumeName, String.class); } private String getVolumeInfo(String knownServer) { - ProcessResult result = sshUtil.executeRemote(knownServer, "gluster volume info "); - if (!result.isSuccess()) { - throw new GlusterRuntimeException("Command [gluster volume info] failed on [" + knownServer - + "] with error: " + result); - } - return result.getOutput(); + return serverUtil.executeOnServer(knownServer, "gluster volume info", String.class); } private boolean readVolumeType(Volume volume, String line) { @@ -426,7 +350,7 @@ public class GlusterUtil { // Do not throw exception, Gracefully handle as Offline brick. private BRICK_STATUS getBrickStatus(String serverName, String volumeName, String brick){ try { - String output = serverUtil.executeScriptOnServer(true, serverName, BRICK_STATUS_SCRIPT + " " + volumeName + String output = serverUtil.executeScriptOnServer(serverName, BRICK_STATUS_SCRIPT + " " + volumeName + " " + brick, String.class); if (output.equals(CoreConstants.ONLINE)) { return BRICK_STATUS.ONLINE; @@ -434,6 +358,8 @@ public class GlusterUtil { return BRICK_STATUS.OFFLINE; } } catch(Exception e) { // Particularly interested on ConnectionExecption, if the server is offline + logger.warn("Exception while fetching brick status for [" + volumeName + "][" + brick + + "]. Marking it as offline!", e); return BRICK_STATUS.OFFLINE; } } @@ -542,21 +468,12 @@ public class GlusterUtil { command.append(" " + brickDir); } - ProcessResult result = sshUtil.executeRemote(knownServer, command.toString()); - if(!result.isSuccess()) { - throw new GlusterRuntimeException("Error in volume [" + volumeName + "] add-brick [" + bricks + "]: " - + result); - } + serverUtil.executeOnServer(knownServer, command.toString()); } public String getLogLocation(String volumeName, String brickName, String knownServer) { String command = "gluster volume log locate " + volumeName + " " + brickName; - ProcessResult result = sshUtil.executeRemote(knownServer, command); - if (!result.isSuccess()) { - throw new GlusterRuntimeException("Command [" + command + "] failed with error: [" + result.getExitValue() - + "][" + result.getOutput() + "]"); - } - String output = result.getOutput(); + String output = serverUtil.executeOnServer(knownServer, command, String.class); if (output.startsWith(VOLUME_LOG_LOCATION_PFX)) { return output.substring(VOLUME_LOG_LOCATION_PFX.length()).trim(); } @@ -565,13 +482,12 @@ public class GlusterUtil { + "] doesn't start with prefix [" + VOLUME_LOG_LOCATION_PFX + "]"); } - //TODO File.separator should be changed if gateway runs on windows/mac public String getLogFileNameForBrickDir(String brickDir) { String logFileName = brickDir; - if (logFileName.startsWith(File.separator)) { - logFileName = logFileName.replaceFirst(File.separator, ""); + if (logFileName.length() > 0 && logFileName.charAt(0) == '/') { + logFileName = logFileName.replaceFirst("/", ""); } - logFileName = logFileName.replaceAll(File.separator, "-") + ".log"; + logFileName = logFileName.replaceAll("/", "-") + ".log"; return logFileName; } @@ -580,46 +496,31 @@ public class GlusterUtil { for (String brickDir : bricks) { command.append(" " + brickDir); } - ProcessResult result = sshUtil.executeRemote(knownServer, command.toString()); - if(!result.isSuccess()) { - throw new GlusterRuntimeException(result.toString()); - } + serverUtil.executeOnServer(knownServer, command.toString()); } public void removeServer(String existingServer, String serverName) { - ProcessResult result = sshUtil.executeRemote(existingServer, "gluster --mode=script peer detach " + serverName); - if(!result.isSuccess()) { - throw new GlusterRuntimeException("Couldn't remove server [" + serverName + "]! Error: " + result); - } + serverUtil.executeOnServer(existingServer, "gluster --mode=script peer detach " + serverName); } public TaskStatus checkRebalanceStatus(String serverName, String volumeName) { String command = "gluster volume rebalance " + volumeName + " status"; - ProcessResult processResult = sshUtil.executeRemote(serverName, command); + String output = serverUtil.executeOnServer(serverName, command, String.class).trim(); TaskStatus taskStatus = new TaskStatus(); - if (processResult.isSuccess()) { - if (processResult.getOutput().trim().matches("^rebalance completed.*")) { - taskStatus.setCode(Status.STATUS_CODE_SUCCESS); - } else if(processResult.getOutput().trim().matches(".*in progress.*")) { - taskStatus.setCode(Status.STATUS_CODE_RUNNING); - } else { - taskStatus.setCode(Status.STATUS_CODE_FAILURE); - } + if (output.matches("^rebalance completed.*")) { + taskStatus.setCode(Status.STATUS_CODE_SUCCESS); + } else if (output.matches(".*in progress.*")) { + taskStatus.setCode(Status.STATUS_CODE_RUNNING); } else { taskStatus.setCode(Status.STATUS_CODE_FAILURE); } - taskStatus.setMessage(processResult.getOutput()); // Common + taskStatus.setMessage(output); return taskStatus; } public void stopRebalance(String serverName, String volumeName) { String command = "gluster volume rebalance " + volumeName + " stop"; - ProcessResult processResult = sshUtil.executeRemote(serverName, command); - TaskStatus taskStatus = new TaskStatus(); - if (processResult.isSuccess()) { - taskStatus.setCode(Status.STATUS_CODE_SUCCESS); - taskStatus.setMessage(processResult.getOutput()); - } + serverUtil.executeOnServer(serverName, command); } public TaskStatus getInitializingDeviceStatus(String serverName, String diskName) { @@ -627,7 +528,7 @@ public class GlusterUtil { TaskStatus taskStatus = new TaskStatus(); try { - initDiskStatusResponse = serverUtil.executeScriptOnServer(true, serverName, INITIALIZE_DISK_STATUS_SCRIPT + " " + initDiskStatusResponse = serverUtil.executeScriptOnServer(serverName, INITIALIZE_DISK_STATUS_SCRIPT + " " + diskName, InitDiskStatusResponse.class); } catch(RuntimeException e) { taskStatus.setCode(Status.STATUS_CODE_FAILURE); @@ -649,22 +550,13 @@ public class GlusterUtil { return taskStatus; } - public ProcessResult executeBrickMigration(String onlineServerName, String volumeName, String fromBrick, + public String executeBrickMigration(String onlineServerName, String volumeName, String fromBrick, String toBrick, String operation) { String command = "gluster volume replace-brick " + volumeName + " " + fromBrick + " " + toBrick + " " + operation; - ProcessResult processResult = sshUtil.executeRemote(onlineServerName, command); - if (!processResult.isSuccess()) { - throw new GlusterRuntimeException(processResult.toString()); - } - return processResult; + return serverUtil.executeOnServer(onlineServerName, command, String.class).trim(); } - public static void main(String args[]) { - // List<String> names = new GlusterUtil().getGlusterServerNames(); - // System.out.println(names); - List<String> disks = new ArrayList<String>(); - disks.add("server1:sda"); - disks.add("server1:sdb"); - new GlusterUtil().addBricks("Volume3", disks, "localhost"); + public VolumeOptionInfoListResponse getVolumeOptionsInfo(String serverName) { + return serverUtil.executeOnServer(serverName, "gluster volume set help-xml", VolumeOptionInfoListResponse.class); } } diff --git a/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/utils/ServerUtil.java b/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/utils/ServerUtil.java index e491a1e7..7211a086 100644 --- a/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/utils/ServerUtil.java +++ b/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/utils/ServerUtil.java @@ -98,22 +98,19 @@ public class ServerUtil { */ public void fetchServerDetails(Server server) { try { - Server serverDetails = (Server)fetchServerDetails(server.getName()); + Server serverDetails = fetchServerDetails(server.getName()); server.copyFrom(serverDetails); // Update the details in <Server> object server.setDisks(serverDetails.getDisks()); } catch (ConnectionException e) { + logger.warn("Couldn't connect to server [" + server.getName() + "]. Marking it offline!", e); server.setStatus(SERVER_STATUS.OFFLINE); } } public boolean isServerOnline(Server server) { // fetch latest details and check if server is still online - try { - fetchServerDetails(server); - return server.isOnline(); - } catch (Exception e) { - return false; - } + fetchServerDetails(server); + return server.isOnline(); } public String fetchHostName(String serverName) { @@ -123,7 +120,7 @@ public class ServerUtil { private Server fetchServerDetails(String serverName) { // fetch standard server details like cpu, disk, memory details - return executeScriptOnServer(true, serverName, REMOTE_SCRIPT_GET_SERVER_DETAILS, Server.class); + return executeScriptOnServer(serverName, REMOTE_SCRIPT_GET_SERVER_DETAILS, Server.class); } /** @@ -202,7 +199,7 @@ public class ServerUtil { @Override public void run() { try { - result.add(executeOnServer(true, serverName, commandWithArgs, expectedClass)); + result.add(executeOnServer(serverName, commandWithArgs, expectedClass)); } catch(Exception e) { String errMsg = "Couldn't execute command [" + commandWithArgs + "] on [" + serverName + "]!"; logger.error(errMsg, e); @@ -218,7 +215,23 @@ public class ServerUtil { * Executes given script on given server. Since the remote server may contain multiple versions of backend, this * method will invoke the script present in directory of same version as the gateway. * - * @param runInForeground + * @param serverName + * @param scriptWithArgs + * The script name followed by arguments to be passed. Note that the script name should not contain path + * as it will be automatically identified by the method. + * @param expectedClass + * Class of the object expected from script execution + * @return Output (console/error) from the script execution + * @throws GlusterRuntimeException in case the remote execution fails. + */ + public String executeScriptOnServer(String serverName, String scriptWithArgs) { + return executeOnServer(serverName, getRemoteScriptDir() + File.separator + scriptWithArgs, String.class); + } + + /** + * Executes given script on given server. Since the remote server may contain multiple versions of backend, this + * method will invoke the script present in directory of same version as the gateway. + * * @param serverName * @param scriptWithArgs * The script name followed by arguments to be passed. Note that the script name should not contain path @@ -228,16 +241,15 @@ public class ServerUtil { * @return Object of the expected class from remote execution of the command. * @throws GlusterRuntimeException in case the remote execution fails. */ - public <T> T executeScriptOnServer(boolean runInForeground, String serverName, String scriptWithArgs, + public <T> T executeScriptOnServer(String serverName, String scriptWithArgs, Class<T> expectedClass) { - return executeOnServer(runInForeground, serverName, getRemoteScriptDir() + File.separator + scriptWithArgs, + return executeOnServer(serverName, getRemoteScriptDir() + File.separator + scriptWithArgs, expectedClass); } /** * Executes given command on given server * - * @param runInForeground * @param serverName * @param commandWithArgs * @param expectedClass @@ -246,7 +258,7 @@ public class ServerUtil { * ungracefully, an object of class {@link Status} will be returned. */ @SuppressWarnings("unchecked") - public <T> T executeOnServer(boolean runInForeground, String serverName, String commandWithArgs, + public <T> T executeOnServer(String serverName, String commandWithArgs, Class<T> expectedClass) { String output = executeOnServer(serverName, commandWithArgs); if (expectedClass == String.class) { @@ -256,7 +268,7 @@ public class ServerUtil { return unmarshal(expectedClass, output); } - private String executeOnServer(String serverName, String commandWithArgs) { + public String executeOnServer(String serverName, String commandWithArgs) { ProcessResult result = sshUtil.executeRemote(serverName, commandWithArgs); if (!result.isSuccess()) { @@ -316,7 +328,6 @@ public class ServerUtil { * Input string * @return Object of given expected class */ - @SuppressWarnings("unchecked") public <T> T unmarshal(Class<T> expectedClass, String input) { try { // create JAXB context and instantiate marshaller @@ -339,26 +350,6 @@ public class ServerUtil { * @return Status object containing the disk name, or error message in case the remote script fails. */ public Status getDiskForDir(String serverName, String brickDir) { - return executeScriptOnServer(true, serverName, REMOTE_SCRIPT_GET_DISK_FOR_DIR + " " + brickDir, Status.class); - } - - public static void main(String[] args) { -// ServerStats stats = new ServerUtil().fetchCPUUsageData("s1", "1d"); -// for(ServerStatsRow row : stats.getRows()) { -// System.out.println(row.getUsageData().get(2)); -// } -// JAXBContext context; -// try { -// context = JAXBContext.newInstance(ServerStats.class); -// Marshaller m = context.createMarshaller(); -// ByteArrayOutputStream out = new ByteArrayOutputStream(); -// m.marshal(stats, out); -// ServerStats stats1 = (ServerStats)new ServerUtil().unmarshal(ServerStats.class, out.toString(), false); -// for(ServerStatsRow row : stats1.getRows()) { -// System.out.println(row.getUsageData().get(2)); -// } -// } catch (JAXBException e) { -// e.printStackTrace(); -// } + return executeScriptOnServer(serverName, REMOTE_SCRIPT_GET_DISK_FOR_DIR + " " + brickDir, Status.class); } } diff --git a/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/utils/SshUtil.java b/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/utils/SshUtil.java index b871e376..706fef31 100644 --- a/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/utils/SshUtil.java +++ b/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/utils/SshUtil.java @@ -54,7 +54,6 @@ public class SshUtil { private static final String SSH_AUTHORIZED_KEYS_PATH_REMOTE = SSH_AUTHORIZED_KEYS_DIR_REMOTE + SSH_AUTHORIZED_KEYS_FILE; public static final File PRIVATE_KEY_FILE = new File(SSH_AUTHORIZED_KEYS_DIR_LOCAL + "gluster.pem"); public static final File PUBLIC_KEY_FILE = new File(SSH_AUTHORIZED_KEYS_DIR_LOCAL + "gluster.pub"); -// private static final String SCRIPT_DISABLE_SSH_PASSWORD_AUTH = "disable-ssh-password-auth.sh"; private LRUCache<String, Connection> sshConnCache = new LRUCache<String, Connection>(10); // TODO: Make user name configurable @@ -76,15 +75,23 @@ public class SshUtil { getConnectionWithPassword(serverName).close(); return true; } catch(Exception e) { + logger.warn("Couldn't connect to [" + serverName + "] with default password!", e); return false; } } + /** + * Checks if public key of management gateway is configured on given server + * + * @param serverName + * @return true if public key is configured, else false + */ public boolean isPublicKeyInstalled(String serverName) { try { getConnectionWithPubKey(serverName).close(); return true; } catch(ConnectionException e) { + logger.warn("Couldn't connect to [" + serverName + "] with public key!", e); return false; } } @@ -424,21 +431,6 @@ public class SshUtil { return executeCommand(getConnection(serverName), command); } - /** - * Checks if public key of management gateway is configured on given server - * - * @param serverName - * @return true if public key is configured, else false - */ - public boolean isPublicKeySetup(String serverName) { - try { - getConnectionWithPubKey(serverName); - return true; - } catch (Exception e) { - return false; - } - } - public void cleanup() { for (Connection conn : sshConnCache.values()) { conn.close(); |