diff options
author | Shireesh Anjal <anjalshireesh@gmail.com> | 2011-09-23 04:56:48 -0700 |
---|---|---|
committer | Shireesh Anjal <anjalshireesh@gmail.com> | 2011-09-23 04:56:48 -0700 |
commit | b6b83d20cbd1701cf9ad82a1cae00dcfb077738a (patch) | |
tree | a3e13e98bbda5288445cc23f495c846cb1a71879 /src | |
parent | b8ebf1e37b12fc8e147ddcb7b8c151e6048cf0e9 (diff) | |
parent | 7ab7bab50b36dd883662fbd009cd18780b4397a3 (diff) |
Merge pull request #282 from TimothyAsir/master
Cleanup and bug fixes
Diffstat (limited to 'src')
34 files changed, 342 insertions, 1808 deletions
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)) |