Commit 32a052f2 authored by Ad Schellevis's avatar Ad Schellevis

refactoring configd (check_reload_status)

parent a8350783
...@@ -43,6 +43,7 @@ import threading ...@@ -43,6 +43,7 @@ import threading
import ConfigParser import ConfigParser
import glob import glob
import time import time
import shlex
import ph_inline_actions import ph_inline_actions
class Handler(object): class Handler(object):
...@@ -146,7 +147,7 @@ class HandlerClient(threading.Thread): ...@@ -146,7 +147,7 @@ class HandlerClient(threading.Thread):
# receive command, maximum data length is 4k... longer messages will be truncated # receive command, maximum data length is 4k... longer messages will be truncated
data = self.connection.recv(4096) data = self.connection.recv(4096)
# map command to action # map command to action
data_parts = data.strip().split(' ') data_parts = shlex.split(data)
if len(data_parts) == 0 or len(data_parts[0]) == 0: if len(data_parts) == 0 or len(data_parts[0]) == 0:
# no data found # no data found
self.connection.sendall('no data\n') self.connection.sendall('no data\n')
...@@ -315,68 +316,55 @@ class Action(object): ...@@ -315,68 +316,55 @@ class Action(object):
:param parameters: list of parameters :param parameters: list of parameters
:return: :return:
""" """
# send-out syslog message
if self.message != None:
if self.message.count('%s') > 0 and parameters != None and len(parameters) > 0:
syslog.syslog(syslog.LOG_NOTICE,self.message % tuple(parameters[0:self.message.count('%s')]) )
else:
syslog.syslog(syslog.LOG_NOTICE,self.message)
# validate input # validate input
if self.type == None: if self.type == None:
# no action type, nothing to do here
return 'No action type' return 'No action type'
elif self.type.lower() == 'script': elif self.type.lower() in ('script','script_output'):
# script command, execute a shell script and return (simple) status # script type commands, basic script type only uses exit statuses, script_output sends back stdout data.
if self.command == None: if self.command == None:
# no command supplied, exit
return 'No command' return 'No command'
try:
script_command = self.command
if self.parameters != None and type(self.parameters) == str:
script_command = '%s %s'%(script_command,self.parameters)
if script_command.find('%s') > -1 and len(parameters) > 0:
# use command execution parameters in action parameter template
# use quotes to prevent code injection
script_command = script_command % tuple(map(lambda x:'"'+x+'"',
parameters[0:script_command.count('%s')]))
# execute script command
if self.message != None:
if self.message.count('%s') > 0 and parameters != None and len(parameters) > 0:
syslog.syslog(syslog.LOG_NOTICE,self.message % tuple(parameters[0:self.message.count('%s')]) )
else:
syslog.syslog(syslog.LOG_NOTICE,self.message)
exit_status = subprocess.call(script_command, shell=True) # build script command to execute, shared for both types
except: script_command = self.command
syslog.syslog(syslog.LOG_ERR, 'Script action failed at %s'%traceback.format_exc()) if self.parameters is not None and type(self.parameters) == str:
return 'Execute error' script_command = '%s %s'%(script_command,self.parameters)
if script_command.find('%s') > -1 and len(parameters) > 0:
# send response # use command execution parameters in action parameter template
if exit_status == 0 : # use quotes on parameters to prevent code injection
return 'OK' script_command = script_command % tuple(map(lambda x:'"'+x.replace('"','\\"')+'"',
else: parameters[0:script_command.count('%s')]))
return 'Error (%d)'%exit_status
elif self.type.lower() == 'script_output': if self.type.lower() == 'script':
# script command returning output, execute a shell script and return stdout # execute script type command
if self.command == None: try:
return 'No command' exit_status = subprocess.call(script_command, shell=True)
try: # send response
script_command = self.command if exit_status == 0 :
if self.parameters != None and type(self.parameters) == str: return 'OK'
script_command = '%s %s'%(script_command,self.parameters)
if script_command.find('%s') > -1 and len(parameters) > 0:
# use command execution parameters in action parameter template
# use quotes to prevent code injection
script_command = script_command % tuple(map(lambda x:'"'+x+'"',
parameters[0:script_command.count('%s')]))
# execute script command
if self.message != None:
if self.message.count('%s') > 0 and parameters != None and len(parameters) > 0:
syslog.syslog(syslog.LOG_NOTICE,self.message % tuple(parameters[0:self.message.count('%s')]) )
else: else:
syslog.syslog(syslog.LOG_NOTICE,self.message) return 'Error (%d)'%exit_status
except:
script_output = subprocess.check_output(script_command, shell=True) syslog.syslog(syslog.LOG_ERR, 'Script action failed at %s'%traceback.format_exc())
return script_output return 'Execute error'
except: elif self.type.lower() == 'script_output':
syslog.syslog(syslog.LOG_ERR, 'Script action failed at %s'%traceback.format_exc()) try:
return 'Execute error' script_output = subprocess.check_output(script_command, shell=True)
return script_output
except:
syslog.syslog(syslog.LOG_ERR, 'Script action failed at %s'%traceback.format_exc())
return 'Execute error'
# fallback should never get here
return "type error"
elif self.type.lower() == 'inline': elif self.type.lower() == 'inline':
# Handle inline service actions # Handle inline service actions
try: try:
...@@ -386,12 +374,6 @@ class Action(object): ...@@ -386,12 +374,6 @@ class Action(object):
else: else:
inline_act_parameters = '' inline_act_parameters = ''
# send message to syslog
if self.message != None:
if self.message.count('%s') > 0 and parameters != None and len(parameters) > 0:
syslog.syslog(syslog.LOG_NOTICE,self.message % tuple(parameters[0:self.message.count('%s')]) )
else:
syslog.syslog(syslog.LOG_NOTICE,self.message)
return ph_inline_actions.execute(self,inline_act_parameters) return ph_inline_actions.execute(self,inline_act_parameters)
......
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