Updated Branches: refs/heads/trunk d4387a0ef -> d0eeac994
AMBARI-4535. RMF. Execute should be able to skip waitting, since Storm processes don't close output (aonishuk) Project: http://git-wip-us.apache.org/repos/asf/ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/d0eeac99 Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/d0eeac99 Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/d0eeac99 Branch: refs/heads/trunk Commit: d0eeac994758e3fcd1892ea95b6facfc28aa9ee6 Parents: d4387a0 Author: Andrew Onischuk <[email protected]> Authored: Wed Feb 5 08:07:51 2014 -0800 Committer: Andrew Onischuk <[email protected]> Committed: Wed Feb 5 08:11:04 2014 -0800 ---------------------------------------------------------------------- .../resource_management/core/providers/system.py | 3 ++- .../resource_management/core/resources/system.py | 11 +++++++++++ .../main/python/resource_management/core/shell.py | 17 ++++++++++------- .../resource_management/TestExecuteResource.py | 12 ++++++++++++ 4 files changed, 35 insertions(+), 8 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ambari/blob/d0eeac99/ambari-agent/src/main/python/resource_management/core/providers/system.py ---------------------------------------------------------------------- diff --git a/ambari-agent/src/main/python/resource_management/core/providers/system.py b/ambari-agent/src/main/python/resource_management/core/providers/system.py index 98c0112..a37ba85 100644 --- a/ambari-agent/src/main/python/resource_management/core/providers/system.py +++ b/ambari-agent/src/main/python/resource_management/core/providers/system.py @@ -230,7 +230,8 @@ class ExecuteProvider(Provider): try: shell.checked_call(self.resource.command, logoutput=self.resource.logoutput, cwd=self.resource.cwd, env=self.resource.environment, - preexec_fn=_preexec_fn(self.resource), user=self.resource.user) + preexec_fn=_preexec_fn(self.resource), user=self.resource.user, + wait_for_finish=self.resource.wait_for_finish) break except Fail as ex: if i == self.resource.tries-1: # last try http://git-wip-us.apache.org/repos/asf/ambari/blob/d0eeac99/ambari-agent/src/main/python/resource_management/core/resources/system.py ---------------------------------------------------------------------- diff --git a/ambari-agent/src/main/python/resource_management/core/resources/system.py b/ambari-agent/src/main/python/resource_management/core/resources/system.py index 85f4b04..45d7a60 100644 --- a/ambari-agent/src/main/python/resource_management/core/resources/system.py +++ b/ambari-agent/src/main/python/resource_management/core/resources/system.py @@ -84,6 +84,17 @@ class Execute(Resource): path = ForcedListArgument(default=[]) actions = Resource.actions + ["run"] logoutput = BooleanArgument(default=False) + """ + Wait for command to finish or not. + + NOTE: + In case of False, since any command results are skipped, it disables some functionality: + - non-zero return code failure + - logoutput + - tries + - try_sleep + """ + wait_for_finish = BooleanArgument(default=True) class ExecuteScript(Resource): http://git-wip-us.apache.org/repos/asf/ambari/blob/d0eeac99/ambari-agent/src/main/python/resource_management/core/shell.py ---------------------------------------------------------------------- diff --git a/ambari-agent/src/main/python/resource_management/core/shell.py b/ambari-agent/src/main/python/resource_management/core/shell.py index 5a8765e..d261043 100644 --- a/ambari-agent/src/main/python/resource_management/core/shell.py +++ b/ambari-agent/src/main/python/resource_management/core/shell.py @@ -28,16 +28,16 @@ from exceptions import Fail from resource_management.core.logger import Logger def checked_call(command, logoutput=False, - cwd=None, env=None, preexec_fn=None, user=None): - return _call(command, logoutput, True, cwd, env, preexec_fn, user) + cwd=None, env=None, preexec_fn=None, user=None, wait_for_finish=True): + return _call(command, logoutput, True, cwd, env, preexec_fn, user, wait_for_finish) def call(command, logoutput=False, - cwd=None, env=None, preexec_fn=None, user=None): - return _call(command, logoutput, False, cwd, env, preexec_fn, user) + cwd=None, env=None, preexec_fn=None, user=None, wait_for_finish=True): + return _call(command, logoutput, False, cwd, env, preexec_fn, user, wait_for_finish) def _call(command, logoutput=False, throw_on_failure=True, - cwd=None, env=None, preexec_fn=None, user=None): + cwd=None, env=None, preexec_fn=None, user=None, wait_for_finish=True): """ Execute shell command @@ -60,11 +60,14 @@ def _call(command, logoutput=False, throw_on_failure=True, proc = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=cwd, env=env, shell=False, preexec_fn=preexec_fn) - + + if not wait_for_finish: + return None, None + out = proc.communicate()[0].strip('\n') code = proc.returncode - if logoutput and out and out!="": + if logoutput and out: Logger.info(out) if throw_on_failure and code: http://git-wip-us.apache.org/repos/asf/ambari/blob/d0eeac99/ambari-agent/src/test/python/resource_management/TestExecuteResource.py ---------------------------------------------------------------------- diff --git a/ambari-agent/src/test/python/resource_management/TestExecuteResource.py b/ambari-agent/src/test/python/resource_management/TestExecuteResource.py index d2ade81..f0a4539 100644 --- a/ambari-agent/src/test/python/resource_management/TestExecuteResource.py +++ b/ambari-agent/src/test/python/resource_management/TestExecuteResource.py @@ -49,6 +49,18 @@ class TestExecuteResource(TestCase): info_mock.assert_called('1') self.assertTrue("call('2')" not in str(info_mock.mock_calls)) + + @patch('subprocess.Popen.communicate') + @patch('subprocess.Popen') + def test_attribute_wait(self, popen_mock, proc_communicate_mock): + with Environment("/") as env: + Execute('echo "1"', + wait_for_finish=False) + Execute('echo "2"', + wait_for_finish=False) + + self.assertTrue(popen_mock.called, 'subprocess.Popen should have been called!') + self.assertFalse(proc_communicate_mock.called, 'proc.communicate should not have been called!') @patch.object(os.path, "exists") @patch.object(subprocess, "Popen")
