Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 40 additions & 23 deletions XSConsoleData.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,23 @@
from XSConsoleState import *
from XSConsoleUtils import *

# PR #22: Add py3-compatible wrapper for commands.getstatusoutput() to keep the code base functional:
if sys.version_info >= (3, 0):
getoutput = subprocess.getoutput
getstatusoutput = subprocess.getstatusoutput

else:
import commands

getoutput = commands.getoutput

def getstatusoutput(command):
"""getstatusoutput with status = exit code, compatible with Python3 getstatusoutput()"""

status, output = commands.getstatusoutput(command)
return os.WEXITSTATUS(status), output # https://github.com/benjaminp/six/issues/207


class DataMethod:
def __init__(self, inSend, inName):
self.send = inSend
Expand Down Expand Up @@ -98,31 +115,31 @@ def Create(self):
self.ReadTimezones()
self.ReadKeymaps()

(status, output) = subprocess.getstatusoutput("dmidecode")
(status, output) = getstatusoutput("dmidecode")
if status != 0:
# Use test dmidecode file if there's no real output
(status, output) = subprocess.getstatusoutput("/bin/cat ./dmidecode.txt")
(status, output) = getstatusoutput("/bin/cat ./dmidecode.txt")

if status == 0:
self.ScanDmiDecode(output.split("\n"))

(status, output) = subprocess.getstatusoutput("/sbin/lspci -m")
(status, output) = getstatusoutput("/sbin/lspci -m")
if status != 0:
(status, output) = subprocess.getstatusoutput("/usr/bin/lspci -m")
(status, output) = getstatusoutput("/usr/bin/lspci -m")

if status == 0:
self.ScanLspci(output.split("\n"))

if os.path.isfile("/usr/bin/ipmitool"):
(status, output) = subprocess.getstatusoutput("/usr/bin/ipmitool mc info")
(status, output) = getstatusoutput("/usr/bin/ipmitool mc info")
if status == 0:
self.ScanIpmiMcInfo(output.split("\n"))

(status, output) = subprocess.getstatusoutput("/bin/cat /etc/xensource-inventory")
(status, output) = getstatusoutput("/bin/cat /etc/xensource-inventory")
if status == 0:
self.ScanInventory(output.split("\n"))

(status, output) = subprocess.getstatusoutput("/usr/bin/openssl x509 -in %s/xapi-ssl.pem -fingerprint -noout" % (Config.Inst().XCPConfigDir()))
(status, output) = getstatusoutput("/usr/bin/openssl x509 -in %s/xapi-ssl.pem -fingerprint -noout" % (Config.Inst().XCPConfigDir()))
if status == 0:
fp = output.split("=")
if len(fp) >= 2:
Expand Down Expand Up @@ -454,30 +471,30 @@ def LoggingDestinationSet(self, inDestination):
self.session.xenapi.host.syslog_reconfigure(self.host.opaqueref())

def UpdateFromResolveConf(self):
(status, output) = subprocess.getstatusoutput("/usr/bin/grep -v \"^;\" /etc/resolv.conf")
(status, output) = getstatusoutput("/usr/bin/grep -v \"^;\" /etc/resolv.conf")
if status == 0:
self.ScanResolvConf(output.split("\n"))

def UpdateFromSysconfig(self):
(status, output) = subprocess.getstatusoutput("/bin/cat /etc/sysconfig/network")
(status, output) = getstatusoutput("/bin/cat /etc/sysconfig/network")
if status == 0:
self.ScanSysconfigNetwork(output.split("\n"))

def UpdateFromHostname(self):
(status, output) = subprocess.getstatusoutput("/bin/cat /etc/hostname")
(status, output) = getstatusoutput("/bin/cat /etc/hostname")
if status == 0:
self.ScanHostname(output.split("\n"))

def UpdateFromNTPConf(self):
(status, output) = subprocess.getstatusoutput("/bin/cat /etc/chrony.conf")
(status, output) = getstatusoutput("/bin/cat /etc/chrony.conf")
if status == 0:
self.ScanNTPConf(output.split("\n"))

def StringToBool(self, inString):
return inString.lower().startswith('true')

def RootLabel(self):
output = subprocess.getoutput('/bin/cat /proc/cmdline')
output = getoutput('/bin/cat /proc/cmdline')
match = re.search(r'root=\s*LABEL\s*=\s*(\S+)', output)
if match:
retVal = match.group(1)
Expand Down Expand Up @@ -675,7 +692,7 @@ def ScanIpmiMcInfo(self, inLines):
self.data['bmc']['version'] = match.group(1)

def ScanService(self, service):
(status, output) = subprocess.getstatusoutput("systemctl is-enabled %s" % (service,))
(status, output) = getstatusoutput("systemctl is-enabled %s" % (service,))
self.data['chkconfig'][service] = status == 0

def ScanResolvConf(self, inLines):
Expand Down Expand Up @@ -779,7 +796,7 @@ def TimezoneSet(self, inTimezone):
cfg.write('/etc/sysconfig/clock')

def CurrentTimeString(self):
return subprocess.getoutput('/bin/date -R')
return getoutput('/bin/date -R')

def ReadKeymaps(self):
self.data['keyboard'] = {
Expand Down Expand Up @@ -811,7 +828,7 @@ def KeymapSet(self, inKeymap):

keymapParam = ShellUtils.MakeSafeParam(inKeymap)
# Load the keymap now
status, output = subprocess.getstatusoutput('/bin/loadkeys "'+keymapParam+'"')
status, output = getstatusoutput('/bin/loadkeys "'+keymapParam+'"')
if status != 0:
raise Exception(output)

Expand Down Expand Up @@ -914,7 +931,7 @@ def ReconfigureManagement(self, inPIF, inMode, inIP, inNetmask, inGateway, in
self.RequireSession()
self.session.xenapi.PIF.reconfigure_ip(inPIF['opaqueref'], inMode, inIP, inNetmask, inGateway, FirstValue(inDNS, ''))
self.session.xenapi.host.management_reconfigure(inPIF['opaqueref'])
status, output = subprocess.getstatusoutput('%s host-signal-networking-change' % (Config.Inst().XECLIPath()))
status, output = getstatusoutput('%s host-signal-networking-change' % (Config.Inst().XECLIPath()))
if status != 0:
raise Exception(output)
finally:
Expand Down Expand Up @@ -1090,32 +1107,32 @@ def StartXAPI(self):
State.Inst().SaveIfRequired()

def EnableService(self, service):
status, output = subprocess.getstatusoutput("systemctl enable %s" % (service,))
status, output = getstatusoutput("systemctl enable %s" % (service,))
if status != 0:
raise Exception(output)

def DisableService(self, service):
status, output = subprocess.getstatusoutput("systemctl disable %s" % (service,))
status, output = getstatusoutput("systemctl disable %s" % (service,))
if status != 0:
raise Exception(output)

def RestartService(self, service):
status, output = subprocess.getstatusoutput("systemctl restart %s" % (service,))
status, output = getstatusoutput("systemctl restart %s" % (service,))
if status != 0:
raise Exception(output)

def StartService(self, service):
status, output = subprocess.getstatusoutput("systemctl start %s" % (service,))
status, output = getstatusoutput("systemctl start %s" % (service,))
if status != 0:
raise Exception(output)

def StopService(self, service):
status, output = subprocess.getstatusoutput("systemctl stop %s" % (service,))
status, output = getstatusoutput("systemctl stop %s" % (service,))
if status != 0:
raise Exception(output)

def NTPStatus(self):
status, output = subprocess.getstatusoutput("/usr/bin/ntpstat")
status, output = getstatusoutput("/usr/bin/ntpstat")
return output

def SetVerboseBoot(self, inVerbose):
Expand All @@ -1124,7 +1141,7 @@ def SetVerboseBoot(self, inVerbose):
else:
name = 'quiet'

status, output = subprocess.getstatusoutput(
status, output = getstatusoutput(
"(export TERM=xterm && /opt/xensource/libexec/set-boot " + name + ")")
if status != 0:
raise Exception(output)
Expand Down
22 changes: 11 additions & 11 deletions XSConsoleDataUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def DeviceList(cls, inWritableOnly):
@classmethod
def SRDeviceList(self):
retVal= []
status, output = subprocess.getstatusoutput("/opt/xensource/libexec/list_local_disks")
status, output = getstatusoutput("/opt/xensource/libexec/list_local_disks")
if status == 0:
regExp = re.compile(r"\s*\(\s*'([^']*)'\s*,\s*'([^']*)'\s*,\s*'([^']*)'\s*,\s*'([^']*)'\s*,\s*'([^']*)'\s*\)")
for line in output.split("\n"):
Expand Down Expand Up @@ -168,18 +168,18 @@ def USBFormat(self, inVDI):
if e.errno != errno.EINTR: # Loop if EINTR
raise

status, output = subprocess.getstatusoutput('/bin/sync')
status, output = getstatusoutput('/bin/sync')

if status != 0:
raise Exception(output)

# Format the new partition with VFAT
status, output = subprocess.getstatusoutput("/sbin/mkfs.vfat -n 'XenServer Backup' -F 32 '" +partitionName + "' 2>&1")
status, output = getstatusoutput("/sbin/mkfs.vfat -n 'XenServer Backup' -F 32 '" +partitionName + "' 2>&1")

if status != 0:
raise Exception(output)

status, output = subprocess.getstatusoutput('/bin/sync')
status, output = getstatusoutput('/bin/sync')

if status != 0:
raise Exception(output)
Expand Down Expand Up @@ -237,7 +237,7 @@ def __init__(self, inVDI, inMode = None):
FileUtils.AssertSafePath(self.mountDev)
self.mountPoint = tempfile.mkdtemp(".xsconsole")

status, output = subprocess.getstatusoutput("/bin/mount -t auto -o " + self.mode + ' ' +self.mountDev+" "+self.mountPoint + " 2>&1")
status, output = getstatusoutput("/bin/mount -t auto -o " + self.mode + ' ' +self.mountDev+" "+self.mountPoint + " 2>&1")
if status != 0:
try:
self.Unmount()
Expand Down Expand Up @@ -276,7 +276,7 @@ def HandleMountFailure(self, inOutput):
raise Exception(inOutput)

realDevice = FileUtils.DeviceFromVDI(self.vdi)
status, output = subprocess.getstatusoutput("/sbin/fdisk -l '" +realDevice+"'")
status, output = getstatusoutput("/sbin/fdisk -l '" +realDevice+"'")
if status != 0:
raise Exception(output)

Expand Down Expand Up @@ -313,7 +313,7 @@ def Scan(self, inRegExp = None, inNumToReturn = None):
def Unmount(self):
status = 0
if self.mountedVBD:
status, output = subprocess.getstatusoutput("/bin/umount '"+self.mountPoint + "' 2>&1")
status, output = getstatusoutput("/bin/umount '"+self.mountPoint + "' 2>&1")
os.rmdir(self.mountPoint)
self.mountedVBD = False
if self.pluggedVBD:
Expand Down Expand Up @@ -360,7 +360,7 @@ def __init__(self, inVDI, inMode = None):
FileUtils.AssertSafePath(self.mountDev)
self.mountPoint = tempfile.mkdtemp(".xsconsole")

status, output = subprocess.getstatusoutput("/bin/mount -t auto -o " + self.mode + ' ' +self.mountDev+" "+self.mountPoint + " 2>&1")
status, output = getstatusoutput("/bin/mount -t auto -o " + self.mode + ' ' +self.mountDev+" "+self.mountPoint + " 2>&1")
if status != 0:
try:
self.Unmount()
Expand All @@ -382,7 +382,7 @@ def HandleMountFailure(self, inStatus, inOutput):
# Entered after self.Unmount has run
if self.vdi['SR']['type'] != 'udev' or self.vdi['SR']['content_type'] != 'disk':
# Take special action for USB devices only, i.e. don't reformat SCSI disks, etc.
if inStatus == 8192: # Return code for empty CD drive
if inStatus == 32: # 32 is the mount(8) return code for mount failure, assuming empty CD drive
raise Exception(Lang("Drive is empty"))
raise Exception(inOutput)

Expand All @@ -400,7 +400,7 @@ def HandleMountFailure(self, inStatus, inOutput):
raise Exception(inOutput)

realDevice = FileUtils.DeviceFromVDI(self.vdi)
status, output = subprocess.getstatusoutput("/sbin/fdisk -l '" +realDevice+"'")
status, output = getstatusoutput("/sbin/fdisk -l '" +realDevice+"'")
if status != 0:
raise Exception(output)

Expand Down Expand Up @@ -437,7 +437,7 @@ def Scan(self, inRegExp = None, inNumToReturn = None):
def Unmount(self):
status = 0
if self.mountedVDI:
status, output = subprocess.getstatusoutput("/bin/umount '"+self.mountPoint + "' 2>&1")
status, output = getstatusoutput("/bin/umount '"+self.mountPoint + "' 2>&1")
os.rmdir(self.mountPoint)
self.mountedVDI = False
XSLog('Unmounted '+self.mountPoint)
Expand Down
7 changes: 2 additions & 5 deletions plugins-base/XSFeatureDRBackup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
if __name__ == "__main__":
raise Exception("This script is a plugin for xsconsole and cannot run independently")

import subprocess

from XSConsoleStandard import *

class DRBackupDialogue(SRDialogue):
Expand All @@ -38,8 +36,7 @@ def DoAction(self, inSR):
sr_uuid = inSR['uuid']
command = "%s/xe-backup-metadata -n -u %s" % (Config.Inst().HelperPath(), sr_uuid)

status, output = subprocess.getstatusoutput(command)
status = os.WEXITSTATUS(status)
status, output = getstatusoutput(command)
initalize_vdi = ""
if status == 3:
initalize_vdi = "-c"
Expand All @@ -48,7 +45,7 @@ def DoAction(self, inSR):

Layout.Inst().TransientBanner(Lang("Backing up metadata... This may take several minutes."))
command = "%s/xe-backup-metadata %s -u %s" % (Config.Inst().HelperPath(), initalize_vdi, sr_uuid)
status, output = subprocess.getstatusoutput(command)
status, output = getstatusoutput(command)
if status == 0:
Layout.Inst().PushDialogue(InfoDialogue(Lang("Backup Successful"), output))
else:
Expand Down
3 changes: 1 addition & 2 deletions plugins-base/XSFeatureDRRestore.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,7 @@ def HandleMethodChoice(self, inChoice, dryRun):
dry_flag=""
Layout.Inst().TransientBanner(Lang("Restoring VM Metadata. This may take a few minutes..."))
command = "%s/xe-restore-metadata -y %s -u %s -x %s -d %s -m %s" % (Config.Inst().HelperPath(), dry_flag, self.sr_uuid, self.vdi_uuid, self.chosen_date, chosen_mode)
status, output = subprocess.getstatusoutput(command)
status = os.WEXITSTATUS(status)
status, output = getstatusoutput(command)
Layout.Inst().PopDialogue()
if status == 0:
Layout.Inst().PushDialogue(InfoDialogue(Lang("Metadata Restore Succeeded: ") + output))
Expand Down
6 changes: 2 additions & 4 deletions plugins-base/XSFeatureSRCreate.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
if __name__ == "__main__":
raise Exception("This script is a plugin for xsconsole and cannot run independently")

import subprocess

from XSConsoleStandard import *
import xml.dom.minidom

Expand Down Expand Up @@ -1288,7 +1286,7 @@ def CommitNFS_ATTACH(self):
'user')

def CommitNFS_ISO_ATTACH(self):
self.srParams['uuid'] = subprocess.getoutput('/usr/bin/uuidgen')
self.srParams['uuid'] = getoutput('/usr/bin/uuidgen')
self.CommitAttach(self.srTypes['NFS_ISO'], { # device_config
'location':self.srParams['server']+':'+self.srParams['serverpath'],
'options':self.srParams['options']
Expand All @@ -1300,7 +1298,7 @@ def CommitNFS_ISO_ATTACH(self):
)

def CommitCIFS_ISO_ATTACH(self):
self.srParams['uuid'] = subprocess.getoutput('/usr/bin/uuidgen')
self.srParams['uuid'] = getoutput('/usr/bin/uuidgen')
deviceConfig = {
'location':'//'+self.srParams['server']+'/'+self.srParams['serverpath'],
'type':'cifs',
Expand Down
4 changes: 1 addition & 3 deletions plugins-base/XSFeatureSaveBugReport.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
if __name__ == "__main__":
raise Exception("This script is a plugin for xsconsole and cannot run independently")

import subprocess

from XSConsoleStandard import *

class SaveBugReportDialogue(FileDialogue):
Expand Down Expand Up @@ -52,7 +50,7 @@ def DoAction(self):
file = open(filename, "w")
# xen-bugtool requires a value for $USER
command = "( export USER=root && /usr/sbin/xen-bugtool --yestoall --silent --output=tar --outfd="+str(file.fileno()) + ' )'
status, output = subprocess.getstatusoutput(command)
status, output = getstatusoutput(command)
file.close()

if status != 0:
Expand Down
4 changes: 1 addition & 3 deletions plugins-base/XSFeatureUploadBugReport.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
if __name__ == "__main__":
raise Exception("This script is a plugin for xsconsole and cannot run independently")

import subprocess

from XSConsoleStandard import *

class UploadBugReportDialogue(InputDialogue):
Expand Down Expand Up @@ -48,7 +46,7 @@ def HandleCommit(self, inValues):
if proxy != '':
command += " http_proxy='"+proxy+"'"

status, output = subprocess.getstatusoutput(command)
status, output = getstatusoutput(command)

if status != 0:
XSLogError('Upload bugreport failed', output) # Error output can be verbose, so syslog only
Expand Down
4 changes: 1 addition & 3 deletions plugins-oem/XSFeatureOEMBackup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
if __name__ == "__main__":
raise Exception("This script is a plugin for xsconsole and cannot run independently")

import subprocess

from XSConsoleStandard import *

class OEMBackupDialogue(FileDialogue):
Expand Down Expand Up @@ -70,7 +68,7 @@ def DoCommit(self):
filename = self.vdiMount.MountedPath(self.filename)
FileUtils.AssertSafePath(filename)
command = "/opt/xensource/bin/xe host-backup file-name='"+filename+"' host="+hostRef
status, output = subprocess.getstatusoutput(command)
status, output = getstatusoutput(command)

if status != 0:
raise Exception(output)
Expand Down
Loading