You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by nc...@apache.org on 2017/06/12 16:45:16 UTC

[09/50] [abbrv] ambari git commit: AMBARI-21122 - Part One: Specify the script directly in alert target for script-based alert dispatchers (Yao Lei via jonathanhurley)

AMBARI-21122 - Part One:  Specify the script directly in alert target for script-based alert dispatchers (Yao Lei via jonathanhurley)


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

Branch: refs/heads/branch-feature-AMBARI-12556
Commit: 4247f6919c329fc3da9e4ea8a0aa62aacd4793e3
Parents: e61fea5
Author: Jonathan Hurley <jh...@hortonworks.com>
Authored: Tue Jun 6 10:22:45 2017 -0400
Committer: Jonathan Hurley <jh...@hortonworks.com>
Committed: Tue Jun 6 10:22:45 2017 -0400

----------------------------------------------------------------------
 .../server/configuration/Configuration.java     | 17 +++++
 .../dispatchers/AlertScriptDispatcher.java      | 45 ++++++++++++-
 .../dispatchers/AlertScriptDispatcherTest.java  | 67 ++++++++++++++++++++
 3 files changed, 127 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/4247f691/ambari-server/src/main/java/org/apache/ambari/server/configuration/Configuration.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/configuration/Configuration.java b/ambari-server/src/main/java/org/apache/ambari/server/configuration/Configuration.java
index 965b57b..fb06e6d 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/configuration/Configuration.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/configuration/Configuration.java
@@ -2721,6 +2721,14 @@ public class Configuration {
   public static final ConfigurationProperty<Integer> TLS_EPHEMERAL_DH_KEY_SIZE = new ConfigurationProperty<>(
     "security.server.tls.ephemeral_dh_key_size", 2048);
 
+  /**
+   * The directory for scripts which are used by the alert notification dispatcher.
+   */
+  @Markdown(description = "The directory for scripts which are used by the alert notification dispatcher.")
+  public static final ConfigurationProperty<String> DISPATCH_PROPERTY_SCRIPT_DIRECTORY = new ConfigurationProperty<>(
+          "notification.dispatch.alert.script.directory",AmbariPath.getPath("/var/lib/ambari-server/resources/scripts"));
+
+
   private static final Logger LOG = LoggerFactory.getLogger(
     Configuration.class);
 
@@ -5587,6 +5595,15 @@ public class Configuration {
   }
 
   /**
+   * Gets the dispatch script directory.
+   *
+   * @return the dispatch script directory
+   */
+  public String getDispatchScriptDirectory() {
+    return getProperty(DISPATCH_PROPERTY_SCRIPT_DIRECTORY);
+  }
+
+  /**
    * Generates a markdown table which includes:
    * <ul>
    * <li>Property key name</li>

http://git-wip-us.apache.org/repos/asf/ambari/blob/4247f691/ambari-server/src/main/java/org/apache/ambari/server/notifications/dispatchers/AlertScriptDispatcher.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/notifications/dispatchers/AlertScriptDispatcher.java b/ambari-server/src/main/java/org/apache/ambari/server/notifications/dispatchers/AlertScriptDispatcher.java
index 84bfe52..60fe4f4 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/notifications/dispatchers/AlertScriptDispatcher.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/notifications/dispatchers/AlertScriptDispatcher.java
@@ -17,6 +17,7 @@
  */
 package org.apache.ambari.server.notifications.dispatchers;
 
+import java.io.File;
 import java.util.Map;
 import java.util.concurrent.Executor;
 import java.util.concurrent.LinkedBlockingQueue;
@@ -74,6 +75,13 @@ public class AlertScriptDispatcher implements NotificationDispatcher {
   public static final String DISPATCH_PROPERTY_SCRIPT_CONFIG_KEY = "ambari.dispatch-property.script";
 
   /**
+   * A dispatch property that instructs this dispatcher to lookup script by filename
+   * from {@link org.apache.ambari.server.state.alert.AlertTarget}.
+   */
+  public static final String DISPATCH_PROPERTY_SCRIPT_FILENAME_KEY  = "ambari.dispatch-property.script.filename";
+
+
+  /**
    * Logger.
    */
   private static final Logger LOG = LoggerFactory.getLogger(AlertScriptDispatcher.class);
@@ -166,8 +174,13 @@ public class AlertScriptDispatcher implements NotificationDispatcher {
    */
   @Override
   public void dispatch(Notification notification) {
-    String scriptKey = getScriptConfigurationKey(notification);
-    String script = m_configuration.getProperty(scriptKey);
+    String scriptKey = null;
+    String script = getScriptLocation(notification);
+
+    if( null == script){ // Script filename is null.
+        scriptKey = getScriptConfigurationKey(notification);
+        script = m_configuration.getProperty(scriptKey);
+    }
 
     // this dispatcher requires a script to run
     if (null == script) {
@@ -208,6 +221,34 @@ public class AlertScriptDispatcher implements NotificationDispatcher {
   }
 
   /**
+   * Gets the dispatch script location from ambari.properties and notification.
+   *
+   * @param notification
+   * @return the dispatch script location.If script filename is {@code null},
+   *         {@code null} will be returned.
+   */
+
+  String getScriptLocation(Notification notification){
+    String scriptName = null;
+    String scriptDir = null;
+
+    if( null == notification || null == notification.DispatchProperties )
+        return null;
+
+    scriptName = notification.DispatchProperties.get(DISPATCH_PROPERTY_SCRIPT_FILENAME_KEY);
+    if( null == scriptName) {
+        LOG.warn("the {} configuration property was not found for dispatching notification",
+                DISPATCH_PROPERTY_SCRIPT_FILENAME_KEY);
+        return null;
+    }
+
+    scriptDir = m_configuration.getDispatchScriptDirectory();
+
+    return scriptDir + File.separator + scriptName;
+  }
+
+
+  /**
    * {@inheritDoc}
    * <p/>
    * Returns {@code false} always.

http://git-wip-us.apache.org/repos/asf/ambari/blob/4247f691/ambari-server/src/test/java/org/apache/ambari/server/notifications/dispatchers/AlertScriptDispatcherTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/notifications/dispatchers/AlertScriptDispatcherTest.java b/ambari-server/src/test/java/org/apache/ambari/server/notifications/dispatchers/AlertScriptDispatcherTest.java
index f1f320d..4b1480c 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/notifications/dispatchers/AlertScriptDispatcherTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/notifications/dispatchers/AlertScriptDispatcherTest.java
@@ -60,6 +60,8 @@ public class AlertScriptDispatcherTest {
 
   private static final String SCRIPT_CONFIG_VALUE = "/foo/script.py";
 
+  private static final String DISPATCH_PROPERTY_SCRIPT_DIRECTORY_KEY = "notification.dispatch.alert.script.directory";
+
   private Injector m_injector;
 
   @Inject
@@ -190,6 +192,71 @@ public class AlertScriptDispatcherTest {
   }
 
   /**
+   * Tests the invocation of method getScriptLocation().
+   */
+  @Test
+  public void testGetScriptLocation() throws Exception {
+    AlertScriptDispatcher dispatcher = (AlertScriptDispatcher) m_dispatchFactory.getDispatcher(TargetType.ALERT_SCRIPT.name());
+    m_injector.injectMembers(dispatcher);
+
+    DispatchCallback callback = EasyMock.createNiceMock(DispatchCallback.class);
+    AlertNotification notification = new AlertNotification();
+    notification.Callback = callback;
+    notification.CallbackIds = Collections.singletonList(UUID.randomUUID().toString());
+    notification.DispatchProperties = new HashMap();
+
+    //1.ambari.dispatch-property.script.filename is not set in notification
+    Assert.assertEquals(dispatcher.getScriptLocation(notification),null);
+
+    //2.ambari.dispatch-property.script.filename is set in notification,but notification.dispatch.alert.script.directory not in ambari.properties
+    final String filename = "foo.py";
+    notification.DispatchProperties.put(AlertScriptDispatcher.DISPATCH_PROPERTY_SCRIPT_FILENAME_KEY,filename);
+    Assert.assertEquals(dispatcher.getScriptLocation(notification),"/var/lib/ambari-server/resources/scripts/foo.py");
+
+    //3.both properties are set
+    final String scriptDirectory = "/var/lib/ambari-server/resources/scripts/foo";
+    m_configuration.setProperty(DISPATCH_PROPERTY_SCRIPT_DIRECTORY_KEY,scriptDirectory);
+    Assert.assertEquals(dispatcher.getScriptLocation(notification),"/var/lib/ambari-server/resources/scripts/foo/foo.py");
+  }
+
+
+  /**
+   * Tests that we will pickup the correct script when script filename is specified on the notification
+   */
+  @Test
+  public void testCustomScriptConfigurationByScriptFilename() throws Exception {
+    final String filename = "foo.py";
+    final String scriptDirectory = "/var/lib/ambari-server/resources/scripts/foo";
+    m_configuration.setProperty(DISPATCH_PROPERTY_SCRIPT_DIRECTORY_KEY,scriptDirectory);
+
+    DispatchCallback callback = EasyMock.createNiceMock(DispatchCallback.class);
+    AlertNotification notification = new AlertNotification();
+    notification.Callback = callback;
+    notification.CallbackIds = Collections.singletonList(UUID.randomUUID().toString());
+
+    notification.DispatchProperties = new HashMap();
+    notification.DispatchProperties.put(AlertScriptDispatcher.DISPATCH_PROPERTY_SCRIPT_FILENAME_KEY,filename);
+
+    callback.onSuccess(notification.CallbackIds);
+    EasyMock.expectLastCall().once();
+
+    AlertScriptDispatcher dispatcher = (AlertScriptDispatcher) m_dispatchFactory.getDispatcher(TargetType.ALERT_SCRIPT.name());
+    m_injector.injectMembers(dispatcher);
+
+    ProcessBuilder powerMockProcessBuilder = m_injector.getInstance(ProcessBuilder.class);
+    EasyMock.expect(dispatcher.getProcessBuilder(dispatcher.getScriptLocation(notification), notification)).andReturn(
+            powerMockProcessBuilder).once();
+
+    EasyMock.replay(callback, dispatcher);
+
+    dispatcher.dispatch(notification);
+
+    EasyMock.verify(callback, dispatcher);
+    PowerMock.verifyAll();
+  }
+
+
+  /**
    * Tests that a process with an error code of 255 causes the failure callback
    * to be invoked.
    *