Pārlūkot izejas kodu

AMBARI-4308. Add optional parameter options and minor fixes to custom action support

Sumit Mohanty 11 gadi atpakaļ
vecāks
revīzija
ad77a8afd5

+ 1 - 1
ambari-agent/conf/unix/ambari-agent.ini

@@ -35,7 +35,7 @@ facter_home=/usr/lib/ambari-agent/lib/facter-1.6.10
 timeout_seconds = 600
 
 [python]
-custom_actions_dir = /var/lib/ambari-agent/resources
+custom_actions_dir = /var/lib/ambari-agent/resources/custom_actions
 
 [command]
 maxretries=2

+ 1 - 1
ambari-agent/pom.xml

@@ -346,7 +346,7 @@
             </mapping>
             <mapping>
               <!-- custom actions root-->
-              <directory>/var/lib/ambari-agent/resources</directory>
+              <directory>/var/lib/ambari-agent/resources/custom_actions</directory>
               <filemode>755</filemode>
               <username>root</username>
               <groupname>root</groupname>

+ 10 - 3
ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariActionExecutionHelper.java

@@ -150,9 +150,16 @@ public class AmbariActionExecutionHelper {
     if (actionDef.getInputs() != null) {
       String[] inputs = actionDef.getInputs().split(",");
       for (String input : inputs) {
-        if (!input.trim().isEmpty() && !actionRequest.getParameters().containsKey(input.trim())) {
-          throw new AmbariException("Action " + actionRequest.getActionName() + " requires input '" +
-              input.trim() + "' that is not provided.");
+        String inputName = input.trim();
+        if (!inputName.isEmpty()) {
+          boolean mandatory = true;
+          if (inputName.startsWith("[") && inputName.endsWith("]")) {
+            mandatory = false;
+          }
+          if (mandatory && !actionRequest.getParameters().containsKey(inputName)) {
+            throw new AmbariException("Action " + actionRequest.getActionName() + " requires input '" +
+                input.trim() + "' that is not provided.");
+          }
         }
       }
     }

+ 4 - 0
ambari-server/src/main/resources/Ambari-DDL-Postgres-CREATE.sql

@@ -362,6 +362,10 @@ create index idx_qrtz_ft_tg on ambari.qrtz_fired_triggers(SCHED_NAME,TRIGGER_GRO
 
 commit;
 
+-- Insert data into the table
+INSERT INTO ambari.action (action_name, action_type, inputs, target_service, target_component, default_timeout, description, target_type)
+  SELECT 'ambari_hdfs_rebalance', 'SYSTEM', 'threshold,[principal],[keytab]', 'HDFS', 'NAMENODE', 600, 'HDFS Rebalance', 'ANY';
+
 -- ambari log4j DDL
 
 --------------------------------------------------

+ 11 - 16
ambari-server/src/main/resources/custom_actions/hdfs_rebalance.py → ambari-server/src/main/resources/custom_actions/ambari_hdfs_rebalance.py

@@ -21,43 +21,38 @@ Ambari Agent
 """
 
 from resource_management import *
-import os
-import json
 
 
 class HdfsRebalance(Script):
   def actionexecute(self, env):
-
     config = Script.get_config()
 
     hdfs_user = config['configurations']['global']['hdfs_user']
-    conf_dir = config['configurations']['global']['hadoop_conf_dir']
+    conf_dir = "/etc/hadoop/conf"
 
     security_enabled = config['configurations']['global']['security_enabled']
-    kinit_path_local = functions.get_kinit_path(
-      [default('kinit_path_local'), "/usr/bin", "/usr/kerberos/bin", "/usr/sbin"])
 
     threshold = config['commandParams']['threshold']
-    principal = config['commandParams']['principal']
-    keytab = config['commandParams']['keytab']
 
     if security_enabled:
+      kinit_path_local = functions.get_kinit_path(
+        [default('kinit_path_local', None), "/usr/bin", "/usr/kerberos/bin", "/usr/sbin"])
+      principal = config['commandParams']['principal']
+      keytab = config['commandParams']['keytab']
       Execute(format("{kinit_path_local}  -kt {keytab} {principal}"))
 
     ExecuteHadoop(format('balancer -threshold {threshold}'),
-      user=hdfs_user,
-      conf_dir=conf_dir
+                  user=hdfs_user,
+                  conf_dir=conf_dir,
+                  logoutput=True
     )
 
     structured_output_example = {
-      'user' : hdfs_user,
-      'conf_dir' : conf_dir,
-      'principal' : principal,
-      'keytab' : keytab
-      }
+      'result': 'Rebalancer completed.'
+    }
 
     self.put_structured_out(structured_output_example)
 
-if __name__ == "__main__":
 
+if __name__ == "__main__":
   HdfsRebalance().execute()

+ 1 - 2
ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariManagementControllerTest.java

@@ -3663,7 +3663,7 @@ public class AmbariManagementControllerTest {
     hdfs.getServiceComponent(Role.DATANODE.name()).addServiceComponentHost("h2").persist();
 
     controller.getActionManager().createActionDefinition(
-        "a1", ActionType.SYSTEM, "test", "Does file exist", "", "",
+        "a1", ActionType.SYSTEM, "test,[optional1]", "Does file exist", "", "",
         TargetHostType.SPECIFIC, Short.valueOf("100"));
 
     controller.getActionManager().createActionDefinition(
@@ -3676,7 +3676,6 @@ public class AmbariManagementControllerTest {
 
     Map<String, String> requestProperties = new HashMap<String, String>();
     requestProperties.put(REQUEST_CONTEXT_PROPERTY, "Called from a test");
-    long now = System.currentTimeMillis();
 
     ArrayList<String> hosts = new ArrayList<String>() {{add("h1");}};