Jelajahi Sumber

AMBARI-7998. Expose the ability to trigger all Host Checks on demand via API (Szilard Nemethy via ncole)

Nate Cole 10 tahun lalu
induk
melakukan
22c5e69d75

+ 20 - 1
ambari-server/src/main/resources/custom_actions/check_host.py

@@ -25,10 +25,12 @@ import subprocess
 import socket
 
 from resource_management import Script, Execute, format
+from ambari_agent.HostInfo import HostInfo
 
 CHECK_JAVA_HOME = "java_home_check"
 CHECK_DB_CONNECTION = "db_connection_check"
 CHECK_HOST_RESOLUTION = "host_resolution_check"
+CHECK_LAST_AGENT_ENV = "last_agent_env_check"
 
 DB_MYSQL = "mysql"
 DB_ORACLE = "oracle"
@@ -72,12 +74,19 @@ class CheckHost(Script):
         structured_output[CHECK_DB_CONNECTION] = {"exit_code" : 1, "message": str(exception)}
 
     if CHECK_HOST_RESOLUTION in check_execute_list:
-      try : 
+      try :
         host_resolution_structured_output = self.execute_host_resolution_check(config)
         structured_output[CHECK_HOST_RESOLUTION] = host_resolution_structured_output
       except Exception, exception :
         print "There was an unknown error while checking IP address lookups: " + str(exception)
         structured_output[CHECK_HOST_RESOLUTION] = {"exit_code" : 1, "message": str(exception)}
+    if CHECK_LAST_AGENT_ENV in check_execute_list:
+      try :
+        last_agent_env_structured_output = self.execute_last_agent_env_check()
+        structured_output[CHECK_LAST_AGENT_ENV] = last_agent_env_structured_output
+      except Exception, exception :
+        print "There was an unknown error while checking last host environment details: " + str(exception)
+        structured_output[CHECK_LAST_AGENT_ENV] = {"exit_code" : 1, "message": str(exception)}
 
     self.put_structured_out(structured_output)
 
@@ -268,5 +277,15 @@ class CheckHost(Script):
     
     return host_resolution_check_structured_output
 
+  # computes and returns the host information of the agent
+  def execute_last_agent_env_check(self):
+    print "Last Agent Env check started."
+    hostInfo = HostInfo()
+    last_agent_env_check_structured_output = { }
+    hostInfo.register(last_agent_env_check_structured_output)
+    print "Last Agent Env check completed successfully."
+
+    return last_agent_env_check_structured_output
+
 if __name__ == "__main__":
   CheckHost().execute()

+ 52 - 0
ambari-server/src/test/python/TestCheckHost.py

@@ -231,3 +231,55 @@ class TestCheckHost(TestCase):
     # ensure the correct function was called
     self.assertTrue(structured_out_mock.called)
     structured_out_mock.assert_called_with({})
+
+  @patch.object(Script, 'get_config')
+  @patch.object(Script, 'get_tmp_dir')
+  @patch('resource_management.libraries.script.Script.put_structured_out')
+  @patch('ambari_agent.HostInfo.HostInfo.javaProcs')
+  @patch('ambari_agent.HostInfo.HostInfo.checkLiveServices')
+  @patch('ambari_agent.HostInfo.HostInfo.getUMask')
+  @patch('ambari_agent.HostInfo.HostInfo.getTransparentHugePage')
+  @patch('ambari_agent.HostInfo.HostInfo.checkIptables')
+  @patch('ambari_agent.HostInfo.HostInfo.checkReverseLookup')
+  @patch('time.time')
+  def testLastAgentEnv(self, time_mock, checkReverseLookup_mock, checkIptables_mock, getTransparentHugePage_mock,
+                       getUMask_mock, checkLiveServices_mock, javaProcs_mock, put_structured_out_mock,
+                       get_tmp_dir_mock, get_config_mock):
+    jsonFilePath = os.path.join("../resources/custom_actions", "check_last_agent_env.json")
+    with open(jsonFilePath, "r") as jsonFile:
+      jsonPayload = json.load(jsonFile)
+
+    get_config_mock.return_value = ConfigDictionary(jsonPayload)
+    get_tmp_dir_mock.return_value = "/tmp"
+
+    checkHost = CheckHost()
+    checkHost.actionexecute(None)
+
+    # ensure the correct function was called
+    self.assertTrue(time_mock.called)
+    self.assertTrue(checkReverseLookup_mock.called)
+    self.assertTrue(checkIptables_mock.called)
+    self.assertTrue(getTransparentHugePage_mock.called)
+    self.assertTrue(getUMask_mock.called)
+    self.assertTrue(checkLiveServices_mock.called)
+    self.assertTrue(javaProcs_mock.called)
+    self.assertTrue(put_structured_out_mock.called)
+    # ensure the correct keys are in the result map
+    last_agent_env_check_result = put_structured_out_mock.call_args[0][0]
+    self.assertTrue('last_agent_env_check' in last_agent_env_check_result)
+    self.assertTrue('hostHealth' in last_agent_env_check_result['last_agent_env_check'])
+    self.assertTrue('iptablesIsRunning' in last_agent_env_check_result['last_agent_env_check'])
+    self.assertTrue('reverseLookup' in last_agent_env_check_result['last_agent_env_check'])
+    self.assertTrue('alternatives' in last_agent_env_check_result['last_agent_env_check'])
+    self.assertTrue('umask' in last_agent_env_check_result['last_agent_env_check'])
+    self.assertTrue('stackFoldersAndFiles' in last_agent_env_check_result['last_agent_env_check'])
+    self.assertTrue('existingRepos' in last_agent_env_check_result['last_agent_env_check'])
+    self.assertTrue('installedPackages' in last_agent_env_check_result['last_agent_env_check'])
+    self.assertTrue('existingUsers' in last_agent_env_check_result['last_agent_env_check'])
+
+    # try it now with errors
+    javaProcs_mock.side_effect = Exception("test exception")
+    checkHost.actionexecute(None)
+
+    #ensure the correct response is returned
+    put_structured_out_mock.assert_called_with({'last_agent_env_check': {'message': 'test exception', 'exit_code': 1}})

+ 30 - 0
ambari-server/src/test/resources/custom_actions/check_last_agent_env.json

@@ -0,0 +1,30 @@
+{
+    "clusterName": "c1",
+    "configuration_attributes": {},
+    "roleParams": {
+        "threshold": "20",
+        "check_execute_list": "last_agent_env_check",
+        "jdk_location": "http://c6401.ambari.apache.org:8080/resources/"
+    },
+    "hostname": "c6402.ambari.apache.org",
+    "passiveInfo": [],
+    "hostLevelParams": {},
+    "commandType": "EXECUTION_COMMAND",
+    "roleCommand": "ACTIONEXECUTE",
+    "serviceName": "null",
+    "role": "check_host",
+    "forceRefreshConfigTags": [],
+    "taskId": 4,
+    "public_hostname": "c6402.ambari.apache.org",
+    "configurations": {},
+    "commandParams": {
+        "jdk_location": "http://c6401.ambari.apache.org:8080/resources/",
+        "script": "check_host.py",
+        "check_execute_list": "last_agent_env_check",
+        "threshold": "20",
+        "command_timeout": "60",
+        "script_type": "PYTHON"
+    },
+    "commandId": "4-1",
+    "clusterHostInfo": {}
+}