From 7cfa3917a4663f3bb048fdc86c80b7332e1e679b Mon Sep 17 00:00:00 2001 From: Kengo Takahara Date: Thu, 9 Feb 2017 19:49:47 +0900 Subject: [PATCH] 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 --- .../process_handler/handle_process.py | 43 ++++++++++++++++--- .../process_handler/test_handle_process.py | 26 ++++++++--- 2 files changed, 59 insertions(+), 10 deletions(-) diff --git a/masakarimonitors/processmonitor/process_handler/handle_process.py b/masakarimonitors/processmonitor/process_handler/handle_process.py index d2bd74b..6dea305 100644 --- a/masakarimonitors/processmonitor/process_handler/handle_process.py +++ b/masakarimonitors/processmonitor/process_handler/handle_process.py @@ -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']) diff --git a/masakarimonitors/tests/unit/processmonitor/process_handler/test_handle_process.py b/masakarimonitors/tests/unit/processmonitor/process_handler/test_handle_process.py index 0defdef..e8f1aaf 100644 --- a/masakarimonitors/tests/unit/processmonitor/process_handler/test_handle_process.py +++ b/masakarimonitors/tests/unit/processmonitor/process_handler/test_handle_process.py @@ -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'))