Add implement of calling pre and post script

This patch add implementation of script calling pre and post
on starting and restarting processes.

Change-Id: Id492fab44413d86c27a6e2f188b7fe84780a553c
Implements: bp pythonize-host-and-process-monitor
This commit is contained in:
Kengo Takahara 2017-02-09 19:49:47 +09:00 committed by takahara.kengo
parent d565c47fb1
commit 7cfa3917a4
2 changed files with 59 additions and 10 deletions

View File

@ -80,12 +80,24 @@ class HandleProcess(object):
"""
for process in self.process_list:
cmd_str = process['start_command']
pre_cmd_str = process['pre_start_command']
post_cmd_str = process['post_start_command']
# Execute pre start command.
if pre_cmd_str:
ret = self._execute_cmd(pre_cmd_str, process['run_as_root'])
if ret != 0:
continue
# Execute start command.
LOG.info(
_LI("Start of process with executing command: %s"), cmd_str)
self._execute_cmd(cmd_str, process['run_as_root'])
# Execute post start command.
if post_cmd_str:
ret = self._execute_cmd(post_cmd_str, process['run_as_root'])
def monitor_processes(self):
"""Monitor processes.
@ -150,23 +162,44 @@ class HandleProcess(object):
continue
cmd_str = down_process['restart_command']
pre_cmd_str = down_process['pre_restart_command']
post_cmd_str = down_process['post_restart_command']
LOG.info(
_LI("Retart of process with executing command: %s"), cmd_str)
for retries in range(0, CONF.process.restart_retries + 1):
# Execute pre start command.
if pre_cmd_str:
ret = self._execute_cmd(pre_cmd_str,
down_process['run_as_root'])
if ret != 0:
# Failed to restart process.
eventlet.greenthread.sleep(
CONF.process.restart_interval)
continue
# Execute start command.
ret = self._execute_cmd(cmd_str, down_process['run_as_root'])
if ret == 0:
# Succeeded in restarting process.
break
else:
if ret != 0:
# Failed to restart process.
eventlet.greenthread.sleep(CONF.process.restart_interval)
continue
# Execute post start command.
if post_cmd_str:
ret = self._execute_cmd(post_cmd_str,
down_process['run_as_root'])
if ret != 0:
# Failed to restart process.
eventlet.greenthread.sleep(
CONF.process.restart_interval)
continue
# Succeeded in restarting process.
break
if retries == CONF.process.restart_retries:
# Send a notification.
event = self._make_event(down_process['process_name'])

View File

@ -72,13 +72,21 @@ class TestHandleProcess(testtools.TestCase):
obj = handle_process.HandleProcess()
obj.set_process_list(process_list)
mock_execute.return_value = ('test_stdout', 'test_stderr')
mock_execute.side_effect = [('test_stdout', ''),
('test_stdout', ''),
('test_stdout', 'test_stderr')]
obj.start_processes()
mock_execute.assert_called_once_with(
mock_execute.assert_any_call(
MOCK_PROCESS_LIST[0].get('pre_start_command'),
run_as_root=MOCK_PROCESS_LIST[0].get('run_as_root'))
mock_execute.assert_any_call(
MOCK_PROCESS_LIST[0].get('start_command'),
run_as_root=MOCK_PROCESS_LIST[0].get('run_as_root'))
mock_execute.assert_any_call(
MOCK_PROCESS_LIST[0].get('post_start_command'),
run_as_root=MOCK_PROCESS_LIST[0].get('run_as_root'))
@mock.patch.object(utils, 'execute')
def test_monitor_processes(self,
@ -100,10 +108,18 @@ class TestHandleProcess(testtools.TestCase):
obj.set_process_list(process_list)
down_process_list = MOCK_DOWN_PROCESS_LIST
mock_execute.return_value = ('test_stdout', '')
mock_execute.side_effect = [('test_stdout', ''),
('test_stdout', ''),
('test_stdout', '')]
obj.restart_processes(down_process_list)
mock_execute.assert_called_once_with(
MOCK_PROCESS_LIST[0].get('restart_command'),
mock_execute.assert_any_call(
MOCK_DOWN_PROCESS_LIST[0].get('pre_restart_command'),
run_as_root=MOCK_DOWN_PROCESS_LIST[0].get('run_as_root'))
mock_execute.assert_any_call(
MOCK_DOWN_PROCESS_LIST[0].get('restart_command'),
run_as_root=MOCK_DOWN_PROCESS_LIST[0].get('run_as_root'))
mock_execute.assert_any_call(
MOCK_DOWN_PROCESS_LIST[0].get('post_restart_command'),
run_as_root=MOCK_DOWN_PROCESS_LIST[0].get('run_as_root'))