ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From d...@apache.org
Subject ambari git commit: AMBARI-8462 Ambari agent should catch all exceptions which raised during bootstrap agent process and traceback they only if it "-v" was setup (dsen)
Date Mon, 26 Jan 2015 14:21:11 GMT
Repository: ambari
Updated Branches:
  refs/heads/trunk 03456c57d -> 7552355ce


AMBARI-8462 Ambari agent should catch all exceptions which raised during bootstrap agent process
and traceback they only if it "-v" was setup (dsen)


Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/7552355c
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/7552355c
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/7552355c

Branch: refs/heads/trunk
Commit: 7552355ce0223313e2bbc34a7c8b7d27786ec831
Parents: 03456c5
Author: Dmytro Sen <dsen@apache.org>
Authored: Mon Jan 26 15:36:31 2015 +0200
Committer: Dmytro Sen <dsen@apache.org>
Committed: Mon Jan 26 16:20:38 2015 +0200

----------------------------------------------------------------------
 ambari-server/src/main/python/setupAgent.py     | 43 ++++++++++++++------
 ambari-server/src/test/python/TestSetupAgent.py | 24 +++++++++--
 2 files changed, 51 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/7552355c/ambari-server/src/main/python/setupAgent.py
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/python/setupAgent.py b/ambari-server/src/main/python/setupAgent.py
index 69be139..8f9c48f 100755
--- a/ambari-server/src/main/python/setupAgent.py
+++ b/ambari-server/src/main/python/setupAgent.py
@@ -72,11 +72,13 @@ def configureAgent(server_hostname, user_run_as):
   execOsCommand(osCommand)
   return
 
-
-def runAgent(passPhrase, expected_hostname, user_run_as):
+def runAgent(passPhrase, expected_hostname, user_run_as, verbose):
   os.environ[AMBARI_PASSPHRASE_VAR] = passPhrase
-  agent_retcode = subprocess.call("su - %1s -c '/usr/sbin/ambari-agent restart --expected-hostname=%2s'"
% (user_run_as, expected_hostname)
-                                  , shell=True)
+  vo = ""
+  if verbose:
+    vo = " -v"
+  cmd = "su - {0} -c '/usr/sbin/ambari-agent restart --expected-hostname={1} {2}'".format(user_run_as,
expected_hostname, vo)
+  agent_retcode = subprocess.call(cmd, shell=True)
   for i in range(3):
     time.sleep(1)
     ret = execOsCommand(["tail", "-20", "/var/log/ambari-agent/ambari-agent.log"])
@@ -88,9 +90,15 @@ def runAgent(passPhrase, expected_hostname, user_run_as):
       print log
       return agent_retcode
   return agent_retcode
-
+ 
 def tryStopAgent():
+  verbose = False
+  cmds = ["bash", "-c", "ps aux | grep 'AmbariAgent.py' | grep ' \-v'"]
+  cmdl = ["bash", "-c", "ps aux | grep 'AmbariAgent.py' | grep ' \--verbose'"]
+  if execOsCommand(cmds)["exitstatus"] == 0 or execOsCommand(cmdl)["exitstatus"] == 0:
+    verbose = True
   subprocess.call("/usr/sbin/ambari-agent stop", shell=True)
+  return verbose  
 
 def getOptimalVersion(initialProjectVersion):
   optimalVersion = initialProjectVersion
@@ -202,20 +210,16 @@ def parseArguments(argv=None):
   return expected_hostname, passPhrase, hostname, user_run_as, projectVersion, server_port
 
 
-def main(argv=None):
+def run_setup(argv=None):
   # Parse passed arguments
   expected_hostname, passPhrase, hostname,\
   user_run_as, projectVersion, server_port = parseArguments(argv)
-
   checkServerReachability(hostname, server_port)
   
   if projectVersion == "null" or projectVersion == "{ambariVersion}" or projectVersion ==
"":
     retcode = getOptimalVersion("")
   else:
     retcode = getOptimalVersion(projectVersion)
-
-  tryStopAgent()
-
   if retcode["exitstatus"] == 0 and retcode["log"] != None and retcode["log"] != "" and retcode["log"][0].strip()
!= "":
       availiableProjectVersion = retcode["log"].strip()
       if not isAgentPackageAlreadyInstalled(availiableProjectVersion):
@@ -228,10 +232,23 @@ def main(argv=None):
                                         " Repository has following "
                                         "versions of ambari-agent:"+retcode["log"][0].strip()})
   else:
-      sys.exit(retcode)
-  
+       sys.exit(retcode)
+   
   configureAgent(hostname, user_run_as)
-  sys.exit(runAgent(passPhrase, expected_hostname, user_run_as))
+  sys.exit(runAgent(passPhrase, expected_hostname, user_run_as, verbose))
+
+def main(argv=None):
+    #Try stop agent and check --verbose option if agent already run
+  global verbose
+  verbose = tryStopAgent()
+  if verbose:
+      run_setup(argv)
+  else:
+    try:
+        run_setup(argv)
+    except Exception, e:
+      print e
+      sys.exit(-1)
 
 if __name__ == '__main__':
   logging.basicConfig(level=logging.DEBUG)

http://git-wip-us.apache.org/repos/asf/ambari/blob/7552355c/ambari-server/src/test/python/TestSetupAgent.py
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/python/TestSetupAgent.py b/ambari-server/src/test/python/TestSetupAgent.py
index 8293fd0..249ce0b 100644
--- a/ambari-server/src/test/python/TestSetupAgent.py
+++ b/ambari-server/src/test/python/TestSetupAgent.py
@@ -62,25 +62,43 @@ class TestSetupAgent(TestCase):
     call_mock.return_value = 0
     execOsCommand_mock.return_value = {'log': 'log', 'exitstatus': 0}
     # Test if expected_hostname is passed
-    ret = setup_agent.runAgent(passphrase, expected_hostname, "root")
+    ret = setup_agent.runAgent(passphrase, expected_hostname, "root", False)
     cmdStr = str(call_mock.call_args_list[0][0])
     self.assertTrue(expected_hostname in cmdStr)
+    self.assertFalse('-v' in cmdStr)
     self.assertEqual(ret, 0)
     self.assertTrue(sleep_mock.called)
     self.assertEqual(execOsCommand_mock.call_count, 1)
     execOsCommand_mock.reset_mock()
+    call_mock.reset_mock()
+    sleep_mock.reset_mock()
+
+    # Test if verbose=True
+    ret = setup_agent.runAgent(passphrase, expected_hostname, "root", True)
+    self.assertTrue(expected_hostname in cmdStr)
+    cmdStr = str(call_mock.call_args_list[0][0])
+    self.assertTrue('-v' in cmdStr)
+    self.assertEqual(ret, 0)
+    self.assertTrue(sleep_mock.called)
+    self.assertEqual(execOsCommand_mock.call_count, 1)
+    execOsCommand_mock.reset_mock()
+    call_mock.reset_mock()
+    sleep_mock.reset_mock()
+
     # Key 'log' not found
     execOsCommand_mock.return_value = None
-    ret = setup_agent.runAgent(passphrase, expected_hostname, "root")
+    ret = setup_agent.runAgent(passphrase, expected_hostname, "root", False)
     cmdStr = str(call_mock.call_args_list[0][0])
     self.assertTrue(expected_hostname in cmdStr)
     self.assertEqual(ret, 0)
     self.assertEqual(execOsCommand_mock.call_count, 3)
     execOsCommand_mock.reset_mock()
+    call_mock.reset_mock()
+
     # Retcode id not 0
     call_mock.return_value = 2
     execOsCommand_mock.return_value = {'log': 'log', 'exitstatus': 2}
-    ret = setup_agent.runAgent(passphrase, expected_hostname, "root")
+    ret = setup_agent.runAgent(passphrase, expected_hostname, "root", False)
     cmdStr = str(call_mock.call_args_list[0][0])
     self.assertTrue(expected_hostname in cmdStr)
     self.assertEqual(ret, 2)


Mime
View raw message