You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lens.apache.org by pr...@apache.org on 2015/08/10 12:39:17 UTC

incubator-lens git commit: LENS-701 : Add cli command to fetch logs

Repository: incubator-lens
Updated Branches:
  refs/heads/master c2a27c0c8 -> 4ff8ac47c


LENS-701 : Add cli command to fetch logs


Project: http://git-wip-us.apache.org/repos/asf/incubator-lens/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-lens/commit/4ff8ac47
Tree: http://git-wip-us.apache.org/repos/asf/incubator-lens/tree/4ff8ac47
Diff: http://git-wip-us.apache.org/repos/asf/incubator-lens/diff/4ff8ac47

Branch: refs/heads/master
Commit: 4ff8ac47c031fb0c10bb4c1a21937f9b20f285eb
Parents: c2a27c0
Author: Raju Bairishetti <ra...@gmail.com>
Authored: Mon Aug 10 16:08:44 2015 +0530
Committer: Rajat Khandelwal <pr...@apache.org>
Committed: Mon Aug 10 16:08:44 2015 +0530

----------------------------------------------------------------------
 .../cli/commands/LensLogResourceCommands.java   | 77 ++++++++++++++++
 .../META-INF/spring/spring-shell-plugin.xml     |  3 +
 .../lens/cli/TestLensLogResourceCommands.java   | 97 ++++++++++++++++++++
 .../lens/cli/doc/TestGenerateCLIUserDoc.java    |  3 +-
 .../java/org/apache/lens/client/LensClient.java |  3 +
 .../apache/lens/client/LensClientConfig.java    |  6 ++
 .../org/apache/lens/client/LensConnection.java  | 20 ++++
 .../lens/client/LensConnectionParams.java       |  4 +
 src/site/apt/user/cli.apt                       | 13 +++
 9 files changed, 225 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-lens/blob/4ff8ac47/lens-cli/src/main/java/org/apache/lens/cli/commands/LensLogResourceCommands.java
----------------------------------------------------------------------
diff --git a/lens-cli/src/main/java/org/apache/lens/cli/commands/LensLogResourceCommands.java b/lens-cli/src/main/java/org/apache/lens/cli/commands/LensLogResourceCommands.java
new file mode 100644
index 0000000..c43a5ff
--- /dev/null
+++ b/lens-cli/src/main/java/org/apache/lens/cli/commands/LensLogResourceCommands.java
@@ -0,0 +1,77 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.lens.cli.commands;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.PrintStream;
+
+import javax.ws.rs.core.Response;
+
+import org.apache.lens.cli.commands.annotations.UserDocumentation;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.lang.StringUtils;
+
+import org.springframework.shell.core.annotation.CliCommand;
+import org.springframework.shell.core.annotation.CliOption;
+import org.springframework.stereotype.Component;
+
+/**
+ * The Class LensLogResourceCommands.
+ * SUSPEND CHECKSTYLE CHECK InnerAssignmentCheck
+ */
+@Component
+@UserDocumentation(title = "Access to Log Resouces",
+  description = "This section provides commands for fetching logs under LENS_LOG_DIR.")
+public class LensLogResourceCommands extends BaseLensCommand {
+
+  @CliCommand(value = "show logs", help = "show logs for a given query handle or log file")
+  public String getLogs(
+    @CliOption(key = {"", "log_handle"}, mandatory = true, help = "log handle can be be query_handle for queries")
+    String logFile, @CliOption(key = {"save_location"}, mandatory = false, help = "<save_location>") String location) {
+    try {
+      Response response = getClient().getLogs(logFile);
+      if (response.getStatus() == Response.Status.OK.getStatusCode()) {
+        if (StringUtils.isBlank(location)) {
+          OutputStream outStream = new PrintStream(System.out, true, "UTF-8");
+          try (InputStream stream = response.readEntity(InputStream.class);) {
+            IOUtils.copy(stream, outStream);
+            return "printed complete log content";
+          }
+        } else {
+          location = getValidPath(new File(location), true, true);
+          String fileName = logFile;
+          location = getValidPath(new File(location + File.separator + fileName), false, false);
+          try (InputStream stream = response.readEntity(InputStream.class);
+              FileOutputStream outStream = new FileOutputStream(new File(location));) {
+            IOUtils.copy(stream, outStream);
+            return "Saved to " + location;
+          }
+        }
+      } else {
+        return response.toString();
+      }
+    } catch (Throwable t) {
+      return t.getMessage();
+    }
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-lens/blob/4ff8ac47/lens-cli/src/main/resources/META-INF/spring/spring-shell-plugin.xml
----------------------------------------------------------------------
diff --git a/lens-cli/src/main/resources/META-INF/spring/spring-shell-plugin.xml b/lens-cli/src/main/resources/META-INF/spring/spring-shell-plugin.xml
index 5ee10ba..bfcf16b 100644
--- a/lens-cli/src/main/resources/META-INF/spring/spring-shell-plugin.xml
+++ b/lens-cli/src/main/resources/META-INF/spring/spring-shell-plugin.xml
@@ -59,4 +59,7 @@
     class="org.apache.lens.cli.commands.LensNativeTableCommands">
   </bean>
 
+  <bean id="lensLogResourceCommands"
+    class="org.apache.lens.cli.commands.LensLogResourceCommands">
+  </bean>
 </beans>

http://git-wip-us.apache.org/repos/asf/incubator-lens/blob/4ff8ac47/lens-cli/src/test/java/org/apache/lens/cli/TestLensLogResourceCommands.java
----------------------------------------------------------------------
diff --git a/lens-cli/src/test/java/org/apache/lens/cli/TestLensLogResourceCommands.java b/lens-cli/src/test/java/org/apache/lens/cli/TestLensLogResourceCommands.java
new file mode 100644
index 0000000..f4b043e
--- /dev/null
+++ b/lens-cli/src/test/java/org/apache/lens/cli/TestLensLogResourceCommands.java
@@ -0,0 +1,97 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.lens.cli;
+
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+
+import org.apache.lens.cli.commands.LensLogResourceCommands;
+import org.apache.lens.client.LensClient;
+
+import org.apache.commons.io.FileUtils;
+
+import org.testng.Assert;
+import org.testng.annotations.BeforeTest;
+import org.testng.annotations.Test;
+
+public class TestLensLogResourceCommands extends LensCliApplicationTest {
+
+  private LensClient client = null;
+  private LensLogResourceCommands commands = null;
+
+  @BeforeTest
+  public void setup() {
+    client = new LensClient();
+    commands = new LensLogResourceCommands();
+    commands.setClient(client);
+  }
+
+  @Test
+  public void testLogResourceFileDoesNotExist() throws IOException {
+    // check for 404 response
+    String response = commands.getLogs("randomFile", null);
+    Assert.assertTrue(response.contains("404"));
+  }
+
+  @Test
+  public void testLogsCommand() throws IOException {
+    String request = "testId";
+    File file = createFileWithContent(request, "test log resource");
+
+    // create output directory to store the resulted log file
+    String outputDirName = "target/sample-logs/";
+    File dir = new File(outputDirName);
+    dir.mkdirs();
+
+    String response = commands.getLogs(request, outputDirName);
+    File outputFile = new File(outputDirName + request);
+    Assert.assertTrue(FileUtils.contentEquals(file, outputFile));
+    Assert.assertTrue(response.contains("Saved to"));
+
+    response = commands.getLogs(request, null);
+    Assert.assertTrue(response.contains("printed complete log content"));
+
+    // check 404 response
+    response = commands.getLogs("random", null);
+    Assert.assertTrue(response.contains("404"));
+  }
+
+  private File createNewPath(String fileName) {
+    File f = new File(fileName);
+    try {
+      if (!f.exists()) {
+        f.createNewFile();
+      }
+    } catch (IOException e) {
+      Assert.fail("Unable to create test file, so bailing out.");
+    }
+    return f;
+  }
+
+  private File createFileWithContent(String filename, String content) throws IOException {
+    File file = createNewPath("target/" + filename + ".log");
+    FileWriter fw = new FileWriter(file.getAbsoluteFile());
+    BufferedWriter bw = new BufferedWriter(fw);
+    bw.write(content);
+    bw.close();
+    return file;
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-lens/blob/4ff8ac47/lens-cli/src/test/java/org/apache/lens/cli/doc/TestGenerateCLIUserDoc.java
----------------------------------------------------------------------
diff --git a/lens-cli/src/test/java/org/apache/lens/cli/doc/TestGenerateCLIUserDoc.java b/lens-cli/src/test/java/org/apache/lens/cli/doc/TestGenerateCLIUserDoc.java
index 1910be9..95e6b4e 100644
--- a/lens-cli/src/test/java/org/apache/lens/cli/doc/TestGenerateCLIUserDoc.java
+++ b/lens-cli/src/test/java/org/apache/lens/cli/doc/TestGenerateCLIUserDoc.java
@@ -54,7 +54,8 @@ public class TestGenerateCLIUserDoc {
       LensFactCommands.class,
       LensDimensionTableCommands.class,
       LensNativeTableCommands.class,
-      LensQueryCommands.class
+      LensQueryCommands.class,
+      LensLogResourceCommands.class
     );
     for (Class claz : classes) {
       UserDocumentation doc = (UserDocumentation) claz.getAnnotation(UserDocumentation.class);

http://git-wip-us.apache.org/repos/asf/incubator-lens/blob/4ff8ac47/lens-client/src/main/java/org/apache/lens/client/LensClient.java
----------------------------------------------------------------------
diff --git a/lens-client/src/main/java/org/apache/lens/client/LensClient.java b/lens-client/src/main/java/org/apache/lens/client/LensClient.java
index cd34642..afc76e5 100644
--- a/lens-client/src/main/java/org/apache/lens/client/LensClient.java
+++ b/lens-client/src/main/java/org/apache/lens/client/LensClient.java
@@ -606,4 +606,7 @@ public class LensClient {
     return this.connection.listResourcesFromConnection(type);
   }
 
+  public Response getLogs(String logFile) {
+    return this.connection.getLogs(logFile);
+  }
 }

http://git-wip-us.apache.org/repos/asf/incubator-lens/blob/4ff8ac47/lens-client/src/main/java/org/apache/lens/client/LensClientConfig.java
----------------------------------------------------------------------
diff --git a/lens-client/src/main/java/org/apache/lens/client/LensClientConfig.java b/lens-client/src/main/java/org/apache/lens/client/LensClientConfig.java
index edb56ac..f71c58f 100644
--- a/lens-client/src/main/java/org/apache/lens/client/LensClientConfig.java
+++ b/lens-client/src/main/java/org/apache/lens/client/LensClientConfig.java
@@ -66,6 +66,8 @@ public class LensClientConfig extends Configuration {
   /** The Constant DEFAULT_SESSION_RESOURCE_PATH. */
   public static final String DEFAULT_SESSION_RESOURCE_PATH = "session";
 
+  public static final String DEFAULT_LOG_RESOURCE_PATH = "logs";
+
   // server side conf properties copied here
   /** The Constant SERVER_BASE_URL. */
   public static final String SERVER_BASE_URL = "lens.server.base.url";
@@ -145,4 +147,8 @@ public class LensClientConfig extends Configuration {
   public void setBaseUrl(String baseUrl) {
     this.set(SERVER_BASE_URL, baseUrl);
   }
+
+  public String getLogResourcePath() {
+    return DEFAULT_LOG_RESOURCE_PATH;
+  }
 }

http://git-wip-us.apache.org/repos/asf/incubator-lens/blob/4ff8ac47/lens-client/src/main/java/org/apache/lens/client/LensConnection.java
----------------------------------------------------------------------
diff --git a/lens-client/src/main/java/org/apache/lens/client/LensConnection.java b/lens-client/src/main/java/org/apache/lens/client/LensConnection.java
index 10819ea..5de8c08 100644
--- a/lens-client/src/main/java/org/apache/lens/client/LensConnection.java
+++ b/lens-client/src/main/java/org/apache/lens/client/LensConnection.java
@@ -118,6 +118,15 @@ public class LensConnection {
     return getMetastoreWebTarget(client);
   }
 
+  public WebTarget getLogWebTarget() {
+    Client client = ClientBuilder.newBuilder().register(MultiPartFeature.class).build();
+    return getLogWebTarget(client);
+  }
+
+  public WebTarget getLogWebTarget(Client client) {
+    return client.target(params.getBaseConnectionUrl()).path(params.getLogResourcePath());
+  }
+
   /**
    * Open.
    *
@@ -241,6 +250,17 @@ public class LensConnection {
   }
 
   /**
+   * get the logs for a given log file
+   *
+   * @param logFile log segregation
+   */
+  public Response getLogs(String logFile) {
+    WebTarget target = getLogWebTarget();
+    Response result = target.path(logFile).request(MediaType.APPLICATION_OCTET_STREAM).get();
+    return result;
+  }
+
+  /**
    * Sets the connection params.
    *
    * @param key   the key

http://git-wip-us.apache.org/repos/asf/incubator-lens/blob/4ff8ac47/lens-client/src/main/java/org/apache/lens/client/LensConnectionParams.java
----------------------------------------------------------------------
diff --git a/lens-client/src/main/java/org/apache/lens/client/LensConnectionParams.java b/lens-client/src/main/java/org/apache/lens/client/LensConnectionParams.java
index 6b5b45c..ccd4a98 100644
--- a/lens-client/src/main/java/org/apache/lens/client/LensConnectionParams.java
+++ b/lens-client/src/main/java/org/apache/lens/client/LensConnectionParams.java
@@ -106,6 +106,10 @@ public class LensConnectionParams {
     return this.conf.getMetastoreResourcePath();
   }
 
+  public String getLogResourcePath() {
+    return this.conf.getLogResourcePath();
+  }
+
   public long getQueryPollInterval() {
     return this.conf.getQueryPollInterval();
   }

http://git-wip-us.apache.org/repos/asf/incubator-lens/blob/4ff8ac47/src/site/apt/user/cli.apt
----------------------------------------------------------------------
diff --git a/src/site/apt/user/cli.apt b/src/site/apt/user/cli.apt
index 6273d89..be93b8c 100644
--- a/src/site/apt/user/cli.apt
+++ b/src/site/apt/user/cli.apt
@@ -354,3 +354,16 @@ User CLI Commands
 
 ===
 
+** Access to Log Resouces
+
+  This section provides commands for fetching logs under LENS_LOG_DIR.
+
+*--+--+
+|<<Command>>|<<Description>>|
+*--+--+
+|show logs [--log_handle] log handle can be be query_handle for queries [--save_location \<save_location\>]|show logs for a given query handle or log file|
+*--+--+
+  <<Lens Log Resource Commands>>
+
+===
+