Commit 4498bce4 authored by Devaev Maxim's avatar Devaev Maxim

Moved raising SubprocessFailure to tools module

parent d1d97e64
...@@ -94,23 +94,11 @@ class CommonInfo(service.FunctionObject) : ...@@ -94,23 +94,11 @@ class CommonInfo(service.FunctionObject) :
def lsbOption(self, option) : def lsbOption(self, option) :
proc_args = "%s %s" % (config.value(SERVICE_NAME, "lsb_release_prog_path"), option) proc_args = "%s %s" % (config.value(SERVICE_NAME, "lsb_release_prog_path"), option)
(proc_stdout, proc_stderr, proc_returncode) = tools.execProcess(proc_args) return ":".join(tools.execProcess(proc_args)[0].split(":")[1:]).strip()
if proc_returncode != 0 :
raise tools.SubprocessFailure("Error while execute \"%s\"\nStdout: %s\nStderr: %s\nReturn code: %d" % (
proc_args, proc_stdout.strip(), proc_stderr.strip(), proc_returncode ))
return ":".join(proc_stdout.split(":")[1:]).strip()
def unameOption(self, option) : def unameOption(self, option) :
proc_args = "%s %s" % (config.value(SERVICE_NAME, "uname_prog_path"), option) proc_args = "%s %s" % (config.value(SERVICE_NAME, "uname_prog_path"), option)
(proc_stdout, proc_stderr, proc_returncode) = tools.execProcess(proc_args) return tools.execProcess(proc_args)[0].strip()
if proc_returncode != 0 :
raise tools.SubprocessFailure("Error while execute \"%s\"\nStdout: %s\nStderr: %s\nReturn code: %d" % (
proc_args, proc_stdout.strip(), proc_stderr.strip(), proc_returncode ))
return proc_stdout.strip()
##### Public classes ##### ##### Public classes #####
......
...@@ -37,15 +37,10 @@ class Disk(service.FunctionObject) : ...@@ -37,15 +37,10 @@ class Disk(service.FunctionObject) :
@service.functionMethod(SMART_METHODS_NAMESPACE, out_signature="a(isiiiissss)") @service.functionMethod(SMART_METHODS_NAMESPACE, out_signature="a(isiiiissss)")
def attributes(self) : def attributes(self) :
proc_args = "%s -A %s" % (config.value(SERVICE_NAME, "smartctl_prog_path"), self.__device_file_path) proc_args = "%s -A %s" % (config.value(SERVICE_NAME, "smartctl_prog_path"), self.__device_file_path)
(proc_stdout, proc_stderr, proc_returncode) = tools.execProcess(proc_args)
if proc_returncode != 0 :
raise tools.SubprocessFailure("Error while execute \"%s\"\nStdout: %s\nStderr: %s\nReturn code: %d" % (
proc_args, proc_stdout.strip(), proc_stderr.strip(), proc_returncode ))
attrs_list = [] attrs_list = []
attrs_found_flag = False attrs_found_flag = False
for attrs_list_item in proc_stdout.split("\n") : for attrs_list_item in tools.execProcess(proc_args)[0].split("\n") :
attrs_list_item = attrs_list_item.strip() attrs_list_item = attrs_list_item.strip()
if attrs_found_flag : if attrs_found_flag :
...@@ -69,15 +64,10 @@ class Disk(service.FunctionObject) : ...@@ -69,15 +64,10 @@ class Disk(service.FunctionObject) :
@service.functionMethod(SMART_METHODS_NAMESPACE, out_signature="b") @service.functionMethod(SMART_METHODS_NAMESPACE, out_signature="b")
def health(self) : def health(self) :
proc_args = "%s -H %s" % (config.value(SERVICE_NAME, "smartctl_prog_path"), self.__device_file_path) proc_args = "%s -H %s" % (config.value(SERVICE_NAME, "smartctl_prog_path"), self.__device_file_path)
(proc_stdout, proc_stderr, proc_returncode) = tools.execProcess(proc_args)
if proc_returncode != 0 :
raise tools.SubprocessFailure("Error while execute \"%s\"\nStdout: %s\nStderr: %s\nReturn code: %d" % (
proc_args, proc_stdout.strip(), proc_stderr.strip(), proc_returncode ))
disk_health_flag = False disk_health_flag = False
health_found_flag = False health_found_flag = False
for health_list_item in proc_stdout.split("\n") : for health_list_item in tools.execProcess(proc_args)[0].split("\n") :
health_list_item = health_list_item.strip() health_list_item = health_list_item.strip()
if health_found_flag : if health_found_flag :
...@@ -150,10 +140,8 @@ class Service(service.Service) : ...@@ -150,10 +140,8 @@ class Service(service.Service) :
### Private ### ### Private ###
def smartAvailable(self, device_file_path) : def smartAvailable(self, device_file_path) :
# FIXME: Add normal checking for SMART support
proc_args = "%s %s" % (config.value(SERVICE_NAME, "smartctl_prog_path"), device_file_path) proc_args = "%s %s" % (config.value(SERVICE_NAME, "smartctl_prog_path"), device_file_path)
(proc_stdout, proc_stderr, proc_returncode) = tools.execProcess(proc_args) return not bool(tools.execProcess(proc_args, False)[2])
return not bool(proc_returncode)
### ###
......
...@@ -17,9 +17,6 @@ RUNLEVELS_METHODS_NAMESPACE = "runlevels" ...@@ -17,9 +17,6 @@ RUNLEVELS_METHODS_NAMESPACE = "runlevels"
RUNLEVELS = "0123456" RUNLEVELS = "0123456"
SHUTDOWN_OPTION_HALT = "-h"
SHUTDOWN_OPTION_REBOOT = "-r"
##### Private classes ##### ##### Private classes #####
class Machine(service.FunctionObject) : class Machine(service.FunctionObject) :
...@@ -28,13 +25,11 @@ class Machine(service.FunctionObject) : ...@@ -28,13 +25,11 @@ class Machine(service.FunctionObject) :
@service.functionMethod(POWER_METHODS_NAMESPACE, out_signature="i") @service.functionMethod(POWER_METHODS_NAMESPACE, out_signature="i")
def shutdown(self) : def shutdown(self) :
proc_args = "%s %s now" % (config.value(SERVICE_NAME, "shutdown_prog_path"), SHUTDOWN_OPTION_HALT) return tools.execProcess("%s -h now" % (config.value(SERVICE_NAME, "shutdown_prog_path")))[2]
return tools.execProcess(proc_args)[2]
@service.functionMethod(POWER_METHODS_NAMESPACE, out_signature="i") @service.functionMethod(POWER_METHODS_NAMESPACE, out_signature="i")
def reboot(self) : def reboot(self) :
proc_args = "%s %s now" % (config.value(SERVICE_NAME, "shutdown_prog_path"), SHUTDOWN_OPTION_REBOOT) return tools.execProcess("%s -r now" % (config.value(SERVICE_NAME, "shutdown_prog_path")))[2]
return tools.execProcess(proc_args)[2]
@service.functionMethod(POWER_METHODS_NAMESPACE, out_signature="i") @service.functionMethod(POWER_METHODS_NAMESPACE, out_signature="i")
def suspend(self) : def suspend(self) :
...@@ -54,9 +49,8 @@ class Machine(service.FunctionObject) : ...@@ -54,9 +49,8 @@ class Machine(service.FunctionObject) :
@service.functionMethod(RUNLEVELS_METHODS_NAMESPACE, out_signature="i") @service.functionMethod(RUNLEVELS_METHODS_NAMESPACE, out_signature="i")
def currentLevel(self) : def currentLevel(self) :
proc_args = config.value(SERVICE_NAME, "runlevel_prog_path") proc_args = config.value(SERVICE_NAME, "runlevel_prog_path")
(proc_stdout, proc_stderr, proc_returncode) = tools.execProcess(proc_args)
level_pairs_list = proc_stdout.strip().split(" ") level_pairs_list = tools.execProcess(proc_args)[0].strip().split(" ")
if len(level_pairs_list) != 2 or not level_pairs_list[1] in RUNLEVELS : if len(level_pairs_list) != 2 or not level_pairs_list[1] in RUNLEVELS :
raise tools.SubprocessFailure("Error while execute \"%s\"\nStdout: %s\nStderr: %s\nReturn code: %d" % ( raise tools.SubprocessFailure("Error while execute \"%s\"\nStdout: %s\nStderr: %s\nReturn code: %d" % (
proc_args, proc_stdout.strip(), proc_stderr.strip(), proc_returncode )) proc_args, proc_stdout.strip(), proc_stderr.strip(), proc_returncode ))
...@@ -66,9 +60,8 @@ class Machine(service.FunctionObject) : ...@@ -66,9 +60,8 @@ class Machine(service.FunctionObject) :
@service.functionMethod(RUNLEVELS_METHODS_NAMESPACE, out_signature="i") @service.functionMethod(RUNLEVELS_METHODS_NAMESPACE, out_signature="i")
def previousLevel(self) : def previousLevel(self) :
proc_args = config.value(SERVICE_NAME, "runlevel_prog_path") proc_args = config.value(SERVICE_NAME, "runlevel_prog_path")
(proc_stdout, proc_stderr, proc_returncode) = tools.execProcess(proc_args)
level_pairs_list = proc_stdout.strip().split(" ") level_pairs_list = tools.execProcess(proc_args)[0].strip().split(" ")
if len(level_pairs_list) != 2 or not level_pairs_list[1] in RUNLEVELS+"N" : if len(level_pairs_list) != 2 or not level_pairs_list[1] in RUNLEVELS+"N" :
raise tools.SubprocessFailure("Error while execute \"%s\"\nStdout: %s\nStderr: %s\nReturn code: %d" % ( raise tools.SubprocessFailure("Error while execute \"%s\"\nStdout: %s\nStderr: %s\nReturn code: %d" % (
proc_args, proc_stdout.strip(), proc_stderr.strip(), proc_returncode )) proc_args, proc_stdout.strip(), proc_stderr.strip(), proc_returncode ))
......
...@@ -59,11 +59,7 @@ class NtpConfig(service.FunctionObject) : ...@@ -59,11 +59,7 @@ class NtpConfig(service.FunctionObject) :
@service.functionMethod(NTP_METHODS_NAMESPACE) @service.functionMethod(NTP_METHODS_NAMESPACE)
def request(self) : def request(self) :
proc_args = "%s %s" % (config.value(SERVICE_NAME, "ntpdate_prog_path"), " ".join(self.servers())) proc_args = "%s %s" % (config.value(SERVICE_NAME, "ntpdate_prog_path"), " ".join(self.servers()))
(proc_stdout, proc_stderr, proc_returncode) = tools.execProcess(proc_args) tools.execProcess(proc_args)
if proc_returncode != 0 :
raise tools.SubprocessFailure("Error while execute \"%s\"\nStdout: %s\nStderr: %s\nReturn code: %d" % (
proc_args, proc_stdout.strip(), proc_stderr.strip(), proc_returncode ))
##### Public classes ##### ##### Public classes #####
......
...@@ -48,13 +48,9 @@ class SystemService(service.FunctionObject) : ...@@ -48,13 +48,9 @@ class SystemService(service.FunctionObject) :
@service.functionMethod(SYSTEM_SERVICE_METHODS_NAMESPACE, out_signature="s") @service.functionMethod(SYSTEM_SERVICE_METHODS_NAMESPACE, out_signature="s")
def levelsMap(self) : def levelsMap(self) :
proc_args = "%s --list %s" % (config.value(SERVICE_NAME, "chkconfig_prog_path"), self.systemServiceName()) proc_args = "%s --list %s" % (config.value(SERVICE_NAME, "chkconfig_prog_path"), self.__system_service_name)
(proc_stdout, proc_stderr, proc_returncode) = tools.execProcess(proc_args) (proc_stdout, proc_stderr, proc_returncode) = tools.execProcess(proc_args)
if proc_returncode != 0 :
raise tools.SubprocessFailure("Error while execute \"%s\"\nStdout: %s\nStderr: %s\nReturn code: %d" % (
proc_args, proc_stdout.strip(), proc_stderr.strip(), proc_returncode ))
service_record_list = re.split(r"\s+", proc_stdout.split("\n")[0]) service_record_list = re.split(r"\s+", proc_stdout.split("\n")[0])
levels_list = ["0"]*(len(service_record_list) - 1) levels_list = ["0"]*(len(service_record_list) - 1)
for count in xrange(1, len(service_record_list)) : for count in xrange(1, len(service_record_list)) :
...@@ -77,41 +73,30 @@ class SystemService(service.FunctionObject) : ...@@ -77,41 +73,30 @@ class SystemService(service.FunctionObject) :
@service.functionMethod(SYSTEM_SERVICE_METHODS_NAMESPACE, out_signature="i") @service.functionMethod(SYSTEM_SERVICE_METHODS_NAMESPACE, out_signature="i")
def start(self) : def start(self) :
logger.verbose("{mod}: Request to start service \"%s\"" % (self.systemServiceName())) logger.verbose("{mod}: Request to start service \"%s\"" % (self.__system_service_name))
return tools.execProcess("%s start" % (os.path.join(config.value(SERVICE_NAME, "initd_dir_path"), self.systemServiceName())))[2] return tools.execProcess("%s start" % (os.path.join(config.value(SERVICE_NAME, "initd_dir_path"), self.__system_service_name)))[2]
@service.functionMethod(SYSTEM_SERVICE_METHODS_NAMESPACE, out_signature="i") @service.functionMethod(SYSTEM_SERVICE_METHODS_NAMESPACE, out_signature="i")
def stop(self) : def stop(self) :
logger.verbose("{mod}: Request to stop service \"%s\"" % (self.systemServiceName())) logger.verbose("{mod}: Request to stop service \"%s\"" % (self.__system_service_name))
return tools.execProcess("%s stop" % (os.path.join(config.value(SERVICE_NAME, "initd_dir_path"), self.systemServiceName())))[2] return tools.execProcess("%s stop" % (os.path.join(config.value(SERVICE_NAME, "initd_dir_path"), self.__system_service_name)))[2]
@service.functionMethod(SYSTEM_SERVICE_METHODS_NAMESPACE, out_signature="i") @service.functionMethod(SYSTEM_SERVICE_METHODS_NAMESPACE, out_signature="i")
def status(self) : def status(self) :
return tools.execProcess("%s status" % (os.path.join(config.value(SERVICE_NAME, "initd_dir_path"), self.systemServiceName())))[2] return tools.execProcess("%s status" % (os.path.join(config.value(SERVICE_NAME, "initd_dir_path"), self.__system_service_name)))[2]
### Private ### ### Private ###
def systemServiceName(self) :
return self.__system_service_name
###
def setLevels(self, levels, enabled_flag) : def setLevels(self, levels, enabled_flag) :
levels = self.validLevels(levels) levels = self.validLevels(levels)
logger.verbose("Request to %s service \"%s\" on runlevels \"%s\"" % ( ( "enable" if enabled_flag else "disable" ), logger.verbose("Request to %s service \"%s\" on runlevels \"%s\"" % ( ( "enable" if enabled_flag else "disable" ),
self.systemServiceName(), ( levels if levels != None else "default" ) )) self.__system_service_name, ( levels if levels != None else "default" ) ))
proc_args = "%s %s %s %s" % ( config.value(SERVICE_NAME, "chkconfig_prog_path"), ( "--level %s" % (levels) if levels != None else "" ), proc_args = "%s %s %s %s" % ( config.value(SERVICE_NAME, "chkconfig_prog_path"), ( "--level %s" % (levels) if levels != None else "" ),
self.systemServiceName(), ( "on" if enabled_flag else "off" ) ) self.__system_service_name, ( "on" if enabled_flag else "off" ) )
(proc_stdout, proc_stderr, proc_returncode) = tools.execProcess(proc_args) return tools.execProcess(proc_args, False)[2]
if proc_returncode != 0 :
raise tools.SubprocessFailure("Error while execute \"%s\"\nStdout: %s\nStderr: %s\nReturn code: %d" % (
proc_args, proc_stdout.strip(), proc_stderr.strip(), proc_returncode ))
return proc_returncode
### ###
......
...@@ -12,12 +12,21 @@ class SubprocessFailure(Exception) : ...@@ -12,12 +12,21 @@ class SubprocessFailure(Exception) :
##### Public methods ##### ##### Public methods #####
def execProcess(proc_args) : def execProcess(proc_args, fatal_flag = True) :
logger.debug("{submod}: Executing child process \"%s\"" % (proc_args)) logger.debug("{submod}: Executing child process \"%s\"" % (proc_args))
proc = subprocess.Popen(proc_args, shell=True, bufsize=1024, close_fds=True, proc = subprocess.Popen(proc_args, shell=True, bufsize=1024, close_fds=True,
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
env={ "LC_ALL" : "C" }) env={ "LC_ALL" : "C" })
(proc_stdout, proc_stderr) = proc.communicate() (proc_stdout, proc_stderr) = proc.communicate()
if proc.returncode != 0 :
error_text = "Error while execute \"%s\"\nStdout: %s\nStderr: %s\nReturn code: %d" % (
proc_args, proc_stdout.strip(), proc_stderr.strip(), proc.returncode )
if fatal_flag :
raise SubprocessFailure(error_text)
logger.error("{submod}: "+error_text)
logger.debug("{submod}: Child process \"%s\" finished, return_code=%d" % (proc_args, proc.returncode)) logger.debug("{submod}: Child process \"%s\" finished, return_code=%d" % (proc_args, proc.returncode))
return (proc_stdout, proc_stderr, proc.returncode) return (proc_stdout, proc_stderr, proc.returncode)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment