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:
# build script command to execute, shared for both types
script_command = self.command script_command = self.command
if self.parameters != None and type(self.parameters) == str: if self.parameters is not None and type(self.parameters) == str:
script_command = '%s %s'%(script_command,self.parameters) script_command = '%s %s'%(script_command,self.parameters)
if script_command.find('%s') > -1 and len(parameters) > 0: if script_command.find('%s') > -1 and len(parameters) > 0:
# use command execution parameters in action parameter template # use command execution parameters in action parameter template
# use quotes to prevent code injection # use quotes on parameters to prevent code injection
script_command = script_command % tuple(map(lambda x:'"'+x+'"', script_command = script_command % tuple(map(lambda x:'"'+x.replace('"','\\"')+'"',
parameters[0:script_command.count('%s')])) parameters[0:script_command.count('%s')]))
# execute script command if self.type.lower() == 'script':
if self.message != None: # execute script type command
if self.message.count('%s') > 0 and parameters != None and len(parameters) > 0: try:
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) exit_status = subprocess.call(script_command, shell=True)
except:
syslog.syslog(syslog.LOG_ERR, 'Script action failed at %s'%traceback.format_exc())
return 'Execute error'
# send response # send response
if exit_status == 0 : if exit_status == 0 :
return 'OK' return 'OK'
else: else:
return 'Error (%d)'%exit_status return 'Error (%d)'%exit_status
except:
syslog.syslog(syslog.LOG_ERR, 'Script action failed at %s'%traceback.format_exc())
return 'Execute error'
elif self.type.lower() == 'script_output': elif self.type.lower() == 'script_output':
# script command returning output, execute a shell script and return stdout
if self.command == None:
return 'No command'
try: 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)
script_output = subprocess.check_output(script_command, shell=True) script_output = subprocess.check_output(script_command, shell=True)
return script_output return script_output
except: except:
syslog.syslog(syslog.LOG_ERR, 'Script action failed at %s'%traceback.format_exc()) syslog.syslog(syslog.LOG_ERR, 'Script action failed at %s'%traceback.format_exc())
return 'Execute error' 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