geode-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dschnei...@apache.org
Subject [1/2] incubator-geode git commit: GEODE-79: Imported logcollectionutil from geode-1.0.0-SNAPSHOT-2.src.tar
Date Mon, 06 Jul 2015 21:46:58 GMT
Repository: incubator-geode
Updated Branches:
  refs/heads/feature/GEODE-79 [created] e189c21a6


http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/e189c21a/logcollectionutil/sendlogs-java/src/com/pivotal/gemfire/sendlogs/utilities/CliParser.java
----------------------------------------------------------------------
diff --git a/logcollectionutil/sendlogs-java/src/com/pivotal/gemfire/sendlogs/utilities/CliParser.java
b/logcollectionutil/sendlogs-java/src/com/pivotal/gemfire/sendlogs/utilities/CliParser.java
new file mode 100644
index 0000000..411d668
--- /dev/null
+++ b/logcollectionutil/sendlogs-java/src/com/pivotal/gemfire/sendlogs/utilities/CliParser.java
@@ -0,0 +1,225 @@
+package com.pivotal.gemfire.sendlogs.utilities;
+
+import java.io.File;
+import java.util.Arrays;
+import java.util.List;
+
+import org.apache.log4j.Level;
+import org.apache.log4j.Logger;
+
+import joptsimple.OptionParser;
+import joptsimple.OptionSet;
+
+/**
+ * Parse the command line options passed into the application by the user.
+ * 
+ * Known issues:
+ * 
+ * 1. The usage should be built from this class rather than it's own manual "Usage" class.
+ * 2. withValuesSeparatedBy() stinks.  You have to supply commands either by:
+ *    -a value1 -a value2 -a value3 
+ *    or 
+ *    -a value1,value2,value3
+ *    -a value1, value2, value3 - WILL NOT WORK.
+ * 3. The short values should have corresponding long values.
+ * 
+ * @author ablakema
+ */
+public class CliParser {
+	private static final String DEFAULTFTPSERVER = "ftp.gemstone.com";
+	private static final Logger logger = Logger.getLogger(CliParser.class.getName());
+	private final OptionSet options;
+	
+	/**
+	 * Parse options handed in 
+	 * @param argsFromUser Should be 'args' from the {@link com.pivotal.gemfire.sendlogs.driver.Driver#main(String[])
main} method.
+	 */
+	public CliParser(String[] argsFromUser) {
+		/* required
+		 * --------
+		 * c - customer name
+		 * o - output directory
+		 * 
+		 * optional
+		 * ---------
+		 * a - addresses
+		 * u - username
+		 * i - identity file
+		 * h - help
+		 * p - prompt for password
+		 * t - ticket number
+		 * v - version
+		 * d - don't remove temporary files.
+		 * s - send file to GemStone support.
+		 * f - specify an ftp server to use (defaults to ftp.gemstone.com)
+		 * 
+		 *
+		 * debug - enable debug output - This needs added to the "Usage" output once it's better
fleshed out.
+		 */
+		OptionParser parser = new OptionParser();
+		parser.accepts("c").withRequiredArg().describedAs("Company name");
+		parser.accepts("o").withRequiredArg().describedAs("Output directory");
+		parser.accepts("a").withRequiredArg().withValuesSeparatedBy(",").describedAs("");
+		parser.accepts("u").withRequiredArg();
+		parser.accepts("i").withRequiredArg();
+		parser.accepts("h");
+		parser.accepts("p");
+		parser.accepts("t").withRequiredArg();
+		parser.accepts("v");
+		parser.accepts("d");
+		parser.accepts("s");
+		parser.accepts("f").withRequiredArg();
+		parser.accepts("m").withRequiredArg();
+		parser.accepts("debug");
+	    options = parser.parse(argsFromUser);
+	    
+	    if (options.has("h")) {
+	    	Usage.printUsage();
+	    	CloseGracefully.exitApp();
+	    }
+	    
+	    if (options.has("v")) {
+	    	Version.printVersion();
+	    	CloseGracefully.exitApp();
+	    }
+	    
+	    /* Verify required options */
+	    if (!options.has("c")) {
+	    	exitMissingArgument("-c");
+	    }
+	    
+	    if (!options.has("o")) {
+	    	exitMissingArgument("-o");
+	    }
+	}
+	
+	/**
+	 * Exit the application because of a missing required option.
+	 * @param missingArgument Required option that was missing.
+	 */
+	private void exitMissingArgument(String missingArgument) {
+    	Log.setupLogging(null, Level.INFO);
+    	logger.error("Missing required option \"" + missingArgument + "\"");
+    	Usage.printUsage();
+		CloseGracefully.exitApp();
+	}
+
+	/**
+	 * Get the list of addresses that the user supplied.
+	 * @return List of addresses supplied by the user or "localhost" if none were specified.
+	 */
+	@SuppressWarnings("unchecked")
+	public List<String> getAddresses() {
+		if (!options.has("a")) return Arrays.asList("localhost");
+		return (List<String>) options.valuesOf("a");
+	}
+	
+	/**
+	 * @return Output directory specified by the user.
+	 */
+	public String getOutputDir() {
+		return (String) options.valueOf("o");
+	}
+	
+	/**
+	 * @return identity file location or null if unavailable.
+	 */
+	public File getIdentityFile() {
+		String identityFileString = (String) options.valueOf("i");
+		File identityFile = new File(identityFileString);
+		if (identityFile.isFile()) {
+			return identityFile;
+		} else {
+			return null;
+		}
+	}
+
+	/**
+	 * @return True if the user specified an identity file
+	 */
+	public boolean hasIdentityFileFromUser() {
+		return options.has("i");
+	}
+	
+	/**
+	 * @return True if user specified a different username
+	 */
+	public boolean hasUsername() {
+		return options.has("u");
+	}
+	
+	/**
+	 * @return username specified by the user or the current logged in user.
+	 */
+	public String getUsername() {
+		return hasUsername() ? (String) options.valueOf("u") : System.getProperty("user.name");
+	}
+	
+	/**
+	 * @return Customer name from the user.
+	 */
+	public String getCustomerName() {
+		return (String) options.valueOf("c");
+	}
+	
+	/**
+	 * @return True if debug mode has been enabled by the user.
+	 */
+	public boolean debugMode() {
+		return options.has("debug");
+	}
+	
+	/**
+	 * @return True if the user has specified that they would like to be prompted for a password.
+	 */
+	public boolean promptForPassword() {
+		return options.has("p");
+	}
+	
+	/**
+	 * @return Ticket number if specified by the user or "00000" as a default.
+	 */
+	public String getTicketNumber() {
+		if (!options.has("t")) return "00000";
+		return (String) options.valueOf("t");
+	}
+	
+	/**
+	 * @return Return true if the user has specified that files should be cleaned up.
+	 */
+	public boolean shouldRemoveTempFiles() {
+		return options.has("d");
+	}
+
+	/**
+	 * @return True if the user has specified that the Zip should be uploaded.
+ 	 */
+	public boolean shouldUploadZipToPivotal() {
+		return options.has("s");
+	}
+	
+	/**
+	 * @return True if the user has opted for static copy mode
+	 */
+	public boolean staticCopyMode() {
+		return options.has("m");
+	}
+	
+	/**
+	 * @return User defined location of static host->directory mappings file.
+	 */
+	public String getStaticFileLocation() {
+		return (String) options.valueOf("m");
+	}
+
+	/**
+	 * @return String representation of the FTP server to use for connections.  Defaults to
ftp.gemstone.com.
+	 */
+	public String getFtpServer() {
+		if (options.has("f")) {
+			return (String) options.valueOf("f");
+		} else {
+			return DEFAULTFTPSERVER;
+		}
+	}
+}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/e189c21a/logcollectionutil/sendlogs-java/src/com/pivotal/gemfire/sendlogs/utilities/CloseGracefully.java
----------------------------------------------------------------------
diff --git a/logcollectionutil/sendlogs-java/src/com/pivotal/gemfire/sendlogs/utilities/CloseGracefully.java
b/logcollectionutil/sendlogs-java/src/com/pivotal/gemfire/sendlogs/utilities/CloseGracefully.java
new file mode 100644
index 0000000..512b007
--- /dev/null
+++ b/logcollectionutil/sendlogs-java/src/com/pivotal/gemfire/sendlogs/utilities/CloseGracefully.java
@@ -0,0 +1,82 @@
+package com.pivotal.gemfire.sendlogs.utilities;
+
+import java.io.BufferedReader;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+
+/**
+ * Gracefully handle closing various things so that I don't have to litter my
+ * object's finally blocks with try\catch statements. 
+ * 
+ * @author ablakema
+ *
+ */
+
+public class CloseGracefully {
+	
+	/**
+	 * Try to close the BufferedReader.  Prints a stack trace if unsuccessful.
+	 * @param br {@link java.io.BufferedReader} that should be closed.
+	 */
+	public static void close(BufferedReader br) {
+		try {
+			br.close();
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+	}
+	
+	/**
+	 * Try to close the FileWriter.  Prints a stack trace if unsuccessful.
+	 * @param fw {@link java.io.FileWriter} to be closed.
+	 */
+	public static void close(FileWriter fw) {
+		try {
+			fw.close();
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+	}
+
+	/**
+	 * Try to close the InputStream.  Prints a stack trace if unsuccessful.
+	 * @param is {@link java.io.InputStream} to be closed.
+	 */
+	public static void close(InputStream is) {
+		try {
+			is.close();
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+	}
+
+	/**
+	 * Try to close the InputStreamReader.  Prints a stack trace if unsuccessful.
+	 * @param isr {@link java.io.InputStreamReader} to be closed.
+	 */
+	public static void close(InputStreamReader isr) {
+		try {
+			isr.close();
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+	}
+	
+	/**
+	 * Exit this application with a specified message.
+	 * @param message Message to print to STDOUT before exiting.
+	 */
+	public static void exitApp(String message) {
+		System.out.println(message);
+		System.exit(0);
+	}
+	
+	/**
+	 * Exit the application with no message.
+	 */
+	public static void exitApp() {
+		System.exit(0);
+	}
+}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/e189c21a/logcollectionutil/sendlogs-java/src/com/pivotal/gemfire/sendlogs/utilities/CompressArtifacts.java
----------------------------------------------------------------------
diff --git a/logcollectionutil/sendlogs-java/src/com/pivotal/gemfire/sendlogs/utilities/CompressArtifacts.java
b/logcollectionutil/sendlogs-java/src/com/pivotal/gemfire/sendlogs/utilities/CompressArtifacts.java
new file mode 100644
index 0000000..93c7f39
--- /dev/null
+++ b/logcollectionutil/sendlogs-java/src/com/pivotal/gemfire/sendlogs/utilities/CompressArtifacts.java
@@ -0,0 +1,80 @@
+package com.pivotal.gemfire.sendlogs.utilities;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.zip.ZipOutputStream;
+
+import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
+import org.apache.commons.io.IOUtils;
+
+/**
+ * Instantiating this class creates the zip file.  Files are added to the zip file via the

+ * {@link #addFileToZip(String, String)} method.  Once all of the files have been added the
zip footer
+ * data needs to be written by calling {@link #close}.
+ * 
+ * @author ablakema
+ */
+public class CompressArtifacts {
+	private OutputStream zipOut;
+	private ZipOutputStream aos;
+	private final String zipFilename;
+
+	/**
+	 * Create the zip file and fill it's header.
+	 * @param baseDir base directory where the zip should be stored
+	 * @param customerName customer name to append to the zip filename for easy identification
+	 * @param ticketNumber ticket number to append to zip filename
+	 */
+	public CompressArtifacts(String baseDir, String customerName, String ticketNumber) {
+		zipFilename = String.format("%s/%s_%s_%s_log_artifacts.zip", baseDir, customerName, ticketNumber,
Time.getTimestamp());
+		try {
+			zipOut = new FileOutputStream(new File(zipFilename));
+			aos = new ZipOutputStream(zipOut);
+		} catch (IOException e) {
+			SendlogsExceptionHandler.handleException(e);
+		}
+	}
+	
+	/**
+	 * Add a file to zip archive.
+	 * 
+	 * In the future this may be re-written to accept an input stream which could
+	 * write to the zip file directly instead of using the temporary files.
+	 * 
+	 * @param fullFilename Local full path to the file to be zipped.
+	 * @param workDir The relative directory for "inside" zip file.  
+	 */
+	public void addFileToZip(String fullFilename, String workDir) {
+		try {
+			File fullFile = new File(fullFilename);
+			aos.putNextEntry(new ZipArchiveEntry(workDir + "/" + fullFile.getName()));
+			IOUtils.copy(new FileInputStream(fullFile), aos);
+			aos.closeEntry();
+		} catch (IOException e) {
+			SendlogsExceptionHandler.handleException(e);
+		} 
+	}
+	
+	/**
+	 * Write out the zip footer and close the zip file.
+	 */
+	public void close() {
+		try {
+			aos.finish();
+			zipOut.close();
+		} catch (IOException e) {
+			SendlogsExceptionHandler.handleException(e);
+		}
+	}
+	
+	/**
+	 * Get the filename of the zip file.
+	 * @return full path of the zip file.
+	 */
+	public String getZipFilename() {
+		return zipFilename;
+	}
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/e189c21a/logcollectionutil/sendlogs-java/src/com/pivotal/gemfire/sendlogs/utilities/FileSystem.java
----------------------------------------------------------------------
diff --git a/logcollectionutil/sendlogs-java/src/com/pivotal/gemfire/sendlogs/utilities/FileSystem.java
b/logcollectionutil/sendlogs-java/src/com/pivotal/gemfire/sendlogs/utilities/FileSystem.java
new file mode 100644
index 0000000..91ef90f
--- /dev/null
+++ b/logcollectionutil/sendlogs-java/src/com/pivotal/gemfire/sendlogs/utilities/FileSystem.java
@@ -0,0 +1,66 @@
+package com.pivotal.gemfire.sendlogs.utilities;
+
+import java.io.File;
+
+
+/**
+ * This class handles all filesystem based operations.
+ * @author ablakema
+ */
+public class FileSystem {
+	private final String baseDir;
+	private String tempDirName;
+	
+	public FileSystem (String baseDir) {
+		this.baseDir = baseDir;
+	}
+	
+	/** 
+	 * Makes sure that the specified directory exists and creates it if not.
+	 * Does not check permissions on the directory, that will be done when files
+	 * are trying to be created.
+	 * @return True if the base directory is created or already exists as a directory.
+	 */
+	public boolean validateOrCreateBaseDirectory() {
+		File outputDirectory = new File(baseDir);
+		if (outputDirectory.isDirectory()) {
+			return true;
+		} else if (outputDirectory.mkdir()) {
+			return true;
+		} else {
+			throw new RuntimeException(String.format("Unable to create directory %s.", baseDir));
+		}
+	}
+	
+	/**
+	 * Creates the temporary directory that is used as the new base to store all
+	 * collected log files, etc.  This is also the directory that the zip file writes into.
+	 * @return True if the temporary directory was successfully created.  Directory 
+	 * is in the format: gemfire-support-164852-01-13-2014
+	 */
+	public boolean createTemporaryDirectory() {
+		tempDirName = String.format("%s/gemfire-support-%s", baseDir, Time.getTimestamp());
+		final File tempDir = new File(tempDirName);
+		return tempDir.mkdirs();
+	}
+	
+	/**
+	 * 
+	 * @return Full path of the temporary directory used for storing files.
+	 */
+	public String getTempLogDir() {
+		return tempDirName;
+	}
+	
+	/**
+	 * Static class to create directories based on a string.
+	 * @param dirName Directory name to create.
+	 * @return True if the directory was successfully created.
+	 */
+	public static boolean createDirectory(String dirName) {
+		File newDir = new File(dirName);
+		return newDir.mkdirs();
+	}
+	
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/e189c21a/logcollectionutil/sendlogs-java/src/com/pivotal/gemfire/sendlogs/utilities/Log.java
----------------------------------------------------------------------
diff --git a/logcollectionutil/sendlogs-java/src/com/pivotal/gemfire/sendlogs/utilities/Log.java
b/logcollectionutil/sendlogs-java/src/com/pivotal/gemfire/sendlogs/utilities/Log.java
new file mode 100644
index 0000000..e68a72b
--- /dev/null
+++ b/logcollectionutil/sendlogs-java/src/com/pivotal/gemfire/sendlogs/utilities/Log.java
@@ -0,0 +1,37 @@
+package com.pivotal.gemfire.sendlogs.utilities;
+
+import java.io.IOException;
+
+import org.apache.log4j.ConsoleAppender;
+import org.apache.log4j.FileAppender;
+import org.apache.log4j.Level;
+import org.apache.log4j.Logger;
+import org.apache.log4j.PatternLayout;
+
+public class Log {
+
+	/**
+	 * @param logFilename Location to store a file that contains the same output as STDOUT.
 null if no file based logging is required.
+	 * @param level Debugging level.
+	 */
+	public static void setupLogging(String logFilename, Level level) {
+		/* Format of logging output:
+		 * [INFO] Collecting logs from: titanium
+		 * [ERROR] Authentication failed for titanium
+		 */
+		PatternLayout pl = new PatternLayout("[%p] %m%n");
+	    Logger rootLogger = Logger.getRootLogger();
+    	rootLogger.setLevel(level);
+	    rootLogger.addAppender(new ConsoleAppender(pl));
+	    /**
+	     * Add logging output to a file, if a filename was specified.
+	     */
+    	if (logFilename != null) {
+		    try {
+				rootLogger.addAppender(new FileAppender(pl, logFilename));
+			} catch (IOException e) {
+				SendlogsExceptionHandler.handleException(e);
+			}
+    	}
+	}
+}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/e189c21a/logcollectionutil/sendlogs-java/src/com/pivotal/gemfire/sendlogs/utilities/SendlogsExceptionHandler.java
----------------------------------------------------------------------
diff --git a/logcollectionutil/sendlogs-java/src/com/pivotal/gemfire/sendlogs/utilities/SendlogsExceptionHandler.java
b/logcollectionutil/sendlogs-java/src/com/pivotal/gemfire/sendlogs/utilities/SendlogsExceptionHandler.java
new file mode 100644
index 0000000..abeb8b2
--- /dev/null
+++ b/logcollectionutil/sendlogs-java/src/com/pivotal/gemfire/sendlogs/utilities/SendlogsExceptionHandler.java
@@ -0,0 +1,29 @@
+package com.pivotal.gemfire.sendlogs.utilities;
+
+import java.io.PrintWriter;
+import java.io.StringWriter;
+
+import org.apache.log4j.Logger;
+
+/**
+ * TODO: Figure out the best way to handle this...
+ * @author ablakeman
+ */
+public class SendlogsExceptionHandler {
+	private static final Logger logger = Logger.getLogger(SendlogsExceptionHandler.class.getName());
+
+	public static void handleException(Exception e) {
+		/* Needed to record full exception in log4j output */
+		StringWriter errors = new StringWriter();
+		e.printStackTrace(new PrintWriter(errors));
+		/* Print a 1 liner to the console */
+		logger.error(e);
+		/* If in debug print the whole stack */
+		logger.debug(errors.toString());
+	}
+	
+	public static void handleException(String errorMesg, Exception e) {
+		logger.error(errorMesg, e);
+	}
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/e189c21a/logcollectionutil/sendlogs-java/src/com/pivotal/gemfire/sendlogs/utilities/Time.java
----------------------------------------------------------------------
diff --git a/logcollectionutil/sendlogs-java/src/com/pivotal/gemfire/sendlogs/utilities/Time.java
b/logcollectionutil/sendlogs-java/src/com/pivotal/gemfire/sendlogs/utilities/Time.java
new file mode 100644
index 0000000..4b12d5c
--- /dev/null
+++ b/logcollectionutil/sendlogs-java/src/com/pivotal/gemfire/sendlogs/utilities/Time.java
@@ -0,0 +1,23 @@
+package com.pivotal.gemfire.sendlogs.utilities;
+
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+
+/**
+ * Time operations for various classes.
+ * @author ablakema
+ */
+public class Time {
+	private static final String DATE_FORMAT = "HHmmss-MM-dd-yyyy";
+	
+	/**
+	 * Get a timestamp based on the current date.
+	 * This includes minutes and seconds so that the user can run multiple times if need be.
+	 * @return String with the timestamp.
+	 */
+	public static String getTimestamp() {
+      SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
+      Calendar cal = Calendar.getInstance();
+      return sdf.format(cal.getTime());
+	}
+}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/e189c21a/logcollectionutil/sendlogs-java/src/com/pivotal/gemfire/sendlogs/utilities/Usage.java
----------------------------------------------------------------------
diff --git a/logcollectionutil/sendlogs-java/src/com/pivotal/gemfire/sendlogs/utilities/Usage.java
b/logcollectionutil/sendlogs-java/src/com/pivotal/gemfire/sendlogs/utilities/Usage.java
new file mode 100644
index 0000000..9bd5877
--- /dev/null
+++ b/logcollectionutil/sendlogs-java/src/com/pivotal/gemfire/sendlogs/utilities/Usage.java
@@ -0,0 +1,36 @@
+package com.pivotal.gemfire.sendlogs.utilities;
+
+
+/**
+ * Print the usage of this application.
+ * This should probably be created from jopt instead of maintained separated.
+ * 
+ * @author ablakema
+ */
+public class Usage {
+	
+	public static void printUsage() {
+		StringBuilder usage = new StringBuilder();
+		usage.append("java -jar gfe-logcollect.jar -c <company> -o <output dir> [OPTIONS]\n\n");
+		usage.append("Required arguments:\n");
+		usage.append("\t-c company name to append to output filename\n");
+		usage.append("\t-o output directory to store all collected log files\n\n");
+		usage.append("Optional arguments:\n");
+		usage.append("\t-a comma separated list of hosts with no spaces. EG. host1,host2,host3
(defaults to localhost)\n");
+		usage.append("\t-u username to use to connect via ssh (defaults to current user)\n");
+		usage.append("\t-i identity file to use for PKI based ssh (defaults to ~/.ssh/id_[dsa|rsa]\n");
+		usage.append("\t-p prompt for a password to use for ssh connections\n");
+		usage.append("\t-t ticket number to append to created zip file\n");
+		usage.append("\t-d clean up collected log files after the zip has been created\n");
+		usage.append("\t-s send the zip file to Pivotal support\n");
+		usage.append("\t-f ftp server to upload collected logs to.  Defaults to ftp.gemstone.com
\n");
+		usage.append("\t-v print version of this utility\n");
+		usage.append("\t-h print this help information\n");
+		usage.append("\nStatic Copy Mode\n");
+		usage.append("\t-m <file> Use a file with log locations instead of scanning for logs.\n");
+		usage.append("\t   Entries should be in the format hostname:/log/location\n");
+		
+		System.out.println(usage.toString());
+	}
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/e189c21a/logcollectionutil/sendlogs-java/src/com/pivotal/gemfire/sendlogs/utilities/Version.java
----------------------------------------------------------------------
diff --git a/logcollectionutil/sendlogs-java/src/com/pivotal/gemfire/sendlogs/utilities/Version.java
b/logcollectionutil/sendlogs-java/src/com/pivotal/gemfire/sendlogs/utilities/Version.java
new file mode 100644
index 0000000..5ceae12
--- /dev/null
+++ b/logcollectionutil/sendlogs-java/src/com/pivotal/gemfire/sendlogs/utilities/Version.java
@@ -0,0 +1,25 @@
+package com.pivotal.gemfire.sendlogs.utilities;
+
+
+/**
+ * Get the version of this application.
+ * @author ablakema
+ */
+public class Version {
+	private static final String VERSION = "1.0";
+	
+	/**
+	 * @return This application's version.
+	 */
+	public static String getVersion() {
+		return VERSION;
+	}
+	
+	/**
+	 * Print out this application's version to STDOUT.
+	 */
+	public static void printVersion() {
+		System.out.println("gfe-sendlogs version: " + getVersion());
+	}
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/e189c21a/logcollectionutil/sendlogs-java/tests/com/pivotal/gemfire/sendlogs/driver/CollectLogsFromFileMapTest.java
----------------------------------------------------------------------
diff --git a/logcollectionutil/sendlogs-java/tests/com/pivotal/gemfire/sendlogs/driver/CollectLogsFromFileMapTest.java
b/logcollectionutil/sendlogs-java/tests/com/pivotal/gemfire/sendlogs/driver/CollectLogsFromFileMapTest.java
new file mode 100644
index 0000000..8658a73
--- /dev/null
+++ b/logcollectionutil/sendlogs-java/tests/com/pivotal/gemfire/sendlogs/driver/CollectLogsFromFileMapTest.java
@@ -0,0 +1,78 @@
+package com.pivotal.gemfire.sendlogs.driver;
+
+import static org.junit.Assert.*;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.HashMap;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.log4j.Level;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import com.pivotal.gemfire.sendlogs.utilities.CloseGracefully;
+import com.pivotal.gemfire.sendlogs.utilities.Log;
+
+public class CollectLogsFromFileMapTest {
+	private File fileMapLocation = new File("/tmp/CollectLogsTest.txt");
+	private final String logOutput = "/tmp/junit_logs.txt";
+
+	@Before
+	public void setUpLogging() {
+		FileWriter fw = null;
+		Log.setupLogging(logOutput, Level.DEBUG);
+		try {
+			fw = new FileWriter(fileMapLocation);
+			fw.write("titanium:/tmp\n");
+			fw.write("titanium/foo\n");
+			fw.write("elmax:/var/log\n");
+		} catch (IOException e) {
+			e.printStackTrace();
+		} finally {
+			if (fw != null) CloseGracefully.close(fw);
+		}
+	}
+
+	@Test
+	public void collectLogLogcationsTest() {
+		CollectLogsFromFileMap clffm = new CollectLogsFromFileMap(fileMapLocation);
+		HashMap<String, HashSet<String>> testCollection = clffm.collectLogLocations();
+		ArrayList<String> dirs = new ArrayList<String>();
+		ArrayList<String> hosts = new ArrayList<String>();
+
+		for (String hostname : testCollection.keySet()) {
+			HashSet<String> dirsForHost = testCollection.get(hostname);
+			hosts.add(hostname);
+			for (String dir : dirsForHost) {
+				dirs.add(dir);
+			}
+		}
+		String s = null;
+		try {
+			s = FileUtils.readFileToString(new File(logOutput));
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+		if (s == null) {
+			fail("Can't read log output");
+		} else {
+			/* Verify that we logged an error on invalid input */
+			assertTrue(s.contains("Encountered invalid line: titanium/foo"));
+			/* Verify that the line elmax:/var/log was processed correctly */
+			assertTrue(hosts.contains("elmax"));
+			assertTrue(dirs.contains("/var/log"));
+		}
+	}
+	
+	@After
+	public void cleanup() {
+		fileMapLocation.delete();
+		new File("/tmp/junit_logs.txt").delete();
+	}
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/e189c21a/logcollectionutil/sendlogs-java/tests/com/pivotal/gemfire/sendlogs/driver/GemFireHostTest.java
----------------------------------------------------------------------
diff --git a/logcollectionutil/sendlogs-java/tests/com/pivotal/gemfire/sendlogs/driver/GemFireHostTest.java
b/logcollectionutil/sendlogs-java/tests/com/pivotal/gemfire/sendlogs/driver/GemFireHostTest.java
new file mode 100644
index 0000000..3d347ed
--- /dev/null
+++ b/logcollectionutil/sendlogs-java/tests/com/pivotal/gemfire/sendlogs/driver/GemFireHostTest.java
@@ -0,0 +1,61 @@
+package com.pivotal.gemfire.sendlogs.driver;
+
+import static org.junit.Assert.*;
+
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.PrintStream;
+
+import org.apache.log4j.Level;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import com.pivotal.gemfire.sendlogs.remote.RemoteHost;
+import com.pivotal.gemfire.sendlogs.utilities.CompressArtifacts;
+import com.pivotal.gemfire.sendlogs.utilities.Log;
+import com.pivotal.gemfire.sendlogs.utilities.Time;
+
+/**
+ * This needs completely re-worked with mocking.
+ * @author ablakema
+ *
+ */
+public class GemFireHostTest {
+	private RemoteHost rh;
+	private CompressArtifacts ca;
+	private GemFireHost gfh;
+
+	private final String BASEDIR = "/tmp";
+	private final String EXPECTEDOUTPUT = String.format("pivotal_12345_%s_log_artifacts.zip",
Time.getTimestamp());
+	private final File output = new File(BASEDIR + "/" + EXPECTEDOUTPUT);
+	private final ByteArrayOutputStream outPut = new ByteArrayOutputStream();
+
+
+	@Before
+	public void setup() {
+		/* Change STDOUT so as not to get a npe during logger.debug warnings */
+	    System.setOut(new PrintStream(outPut));
+		Log.setupLogging("/tmp/output", Level.FATAL);
+		if (output.exists()) output.delete();
+		rh = new RemoteHost(System.getProperty("user.name"), "localhost");
+		ca = new CompressArtifacts(BASEDIR, "pivotal", "12345");
+		
+	}
+
+	@Test
+	public void gemfireHostTest() {
+		gfh = new GemFireHost(rh, "/tmp", ca);
+		gfh.findAndRetrieveLogs();
+		gfh.close();
+		assertTrue(output.exists());
+	}
+	
+	@After
+	public void cleanup() {
+		File output = new File(BASEDIR + "/" + EXPECTEDOUTPUT);
+		File log = new File("/tmp/output");
+		log.delete();
+		output.delete();
+	}
+}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/e189c21a/logcollectionutil/sendlogs-java/tests/com/pivotal/gemfire/sendlogs/remote/FTPUploaderTest.java
----------------------------------------------------------------------
diff --git a/logcollectionutil/sendlogs-java/tests/com/pivotal/gemfire/sendlogs/remote/FTPUploaderTest.java
b/logcollectionutil/sendlogs-java/tests/com/pivotal/gemfire/sendlogs/remote/FTPUploaderTest.java
new file mode 100644
index 0000000..73e157c
--- /dev/null
+++ b/logcollectionutil/sendlogs-java/tests/com/pivotal/gemfire/sendlogs/remote/FTPUploaderTest.java
@@ -0,0 +1,49 @@
+package com.pivotal.gemfire.sendlogs.remote;
+
+import static org.junit.Assert.*;
+
+import java.io.File;
+import java.io.IOException;
+import java.security.SecureRandom;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class FTPUploaderTest {
+	private String testFileToUpload;
+	File fileToCreate;
+	
+	/**
+	 * Create a file to upload
+	 */
+	@Before
+	public void setUpTestFile() {
+        SecureRandom random = new SecureRandom();
+        long randomNumber = random.nextLong();
+		testFileToUpload = "sendlogs-" + randomNumber;
+		fileToCreate = new File(testFileToUpload);
+		try {
+			fileToCreate.createNewFile();
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+	}
+
+	/**
+	 * This will actually upload a file to our FTP server.  Anonymous users
+	 * can't delete this so it will either have to be manually removed or 
+	 * it will be automatically cleaned up after 15 days.
+	 */
+	@Test
+	public void testSendingFile() {
+		FTPUploader ftpu = new FTPUploader("ftp.gemstone.com");
+		assertEquals(true, ftpu.sendFile(testFileToUpload));
+	}
+	
+	@After
+	public void cleanup() {
+		fileToCreate.delete();
+	}
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/e189c21a/logcollectionutil/sendlogs-java/tests/com/pivotal/gemfire/sendlogs/remote/RemoteHostTest.java
----------------------------------------------------------------------
diff --git a/logcollectionutil/sendlogs-java/tests/com/pivotal/gemfire/sendlogs/remote/RemoteHostTest.java
b/logcollectionutil/sendlogs-java/tests/com/pivotal/gemfire/sendlogs/remote/RemoteHostTest.java
new file mode 100644
index 0000000..17f6148
--- /dev/null
+++ b/logcollectionutil/sendlogs-java/tests/com/pivotal/gemfire/sendlogs/remote/RemoteHostTest.java
@@ -0,0 +1,35 @@
+package com.pivotal.gemfire.sendlogs.remote;
+
+import static org.junit.Assert.*;
+
+import java.io.File;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class RemoteHostTest {
+	private final File localFile = new File("/tmp/passwd");
+	
+	@Before
+	public void ensureFileDoesntExist() {
+		if (localFile.exists()) {
+			cleanUp();
+		}
+	}
+	
+	@Test
+	public void testRemoteHostWithPubkey() {
+		RemoteHost ack = new RemoteHost(System.getProperty("user.name"), "localhost");
+	
+		/* Verify that the file got copied */
+		ack.getRemoteFile("/etc/passwd", "/tmp");
+		assertTrue(localFile.exists());
+	}
+	
+	@After
+	public void cleanUp() {
+		localFile.delete();
+	}
+	
+}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/e189c21a/logcollectionutil/sendlogs-java/tests/com/pivotal/gemfire/sendlogs/utilities/CliParserTest.java
----------------------------------------------------------------------
diff --git a/logcollectionutil/sendlogs-java/tests/com/pivotal/gemfire/sendlogs/utilities/CliParserTest.java
b/logcollectionutil/sendlogs-java/tests/com/pivotal/gemfire/sendlogs/utilities/CliParserTest.java
new file mode 100644
index 0000000..a3e63d6
--- /dev/null
+++ b/logcollectionutil/sendlogs-java/tests/com/pivotal/gemfire/sendlogs/utilities/CliParserTest.java
@@ -0,0 +1,41 @@
+package com.pivotal.gemfire.sendlogs.utilities;
+
+import static org.junit.Assert.*;
+
+import java.util.List;
+
+import org.junit.Test;
+
+public class CliParserTest {
+	/** 
+	 * Verify that the required options are accepted.
+	 */
+	@Test
+	public void testRequiredOptionsWork() {
+		String args[] = { "-c", "test", "-o", "blah" };
+		CliParser clip = new CliParser(args);
+		assertEquals("test", clip.getCustomerName());
+	}
+	
+	/**
+	 * This should throw a runtime exception that -o is unavailable.
+	 */
+	@Test(expected=RuntimeException.class)
+	public void testRequiredOptionsAreNeeded() {
+		String args[] = { "-c", "test"};
+		CliParser clip = new CliParser(args);
+		/* This shouldn't actually make it this far*/
+		assertTrue(clip.hasUsername());
+	}
+	
+	/**
+	 * Verify that given a list of three hosts the correct List is returned.
+	 */
+	@Test
+	public void testHostAddressList() {
+		String args[] = { "-c", "test", "-o", "blah", "-a", "seiryi, titanium, elmax", "-p" };
+		CliParser clip = new CliParser(args);
+		List<String> l = clip.getAddresses();
+		assertEquals(l.get(1).trim(), "titanium");
+	}
+}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/e189c21a/logcollectionutil/sendlogs-java/tests/com/pivotal/gemfire/sendlogs/utilities/CompressArtifactsTest.java
----------------------------------------------------------------------
diff --git a/logcollectionutil/sendlogs-java/tests/com/pivotal/gemfire/sendlogs/utilities/CompressArtifactsTest.java
b/logcollectionutil/sendlogs-java/tests/com/pivotal/gemfire/sendlogs/utilities/CompressArtifactsTest.java
new file mode 100644
index 0000000..75e8094
--- /dev/null
+++ b/logcollectionutil/sendlogs-java/tests/com/pivotal/gemfire/sendlogs/utilities/CompressArtifactsTest.java
@@ -0,0 +1,30 @@
+package com.pivotal.gemfire.sendlogs.utilities;
+
+import static org.junit.Assert.*;
+
+import java.io.File;
+
+import org.junit.After;
+import org.junit.Test;
+
+public class CompressArtifactsTest {
+	private final String outputFilename = String.format("pivotal_12345_%s_log_artifacts.zip",
Time.getTimestamp());
+
+	@Test
+	public void testArtifactCompression() {
+		final String baseDir = "/tmp";
+		final String customerName = "pivotal";
+		final String ticketNumber = "12345";
+		CompressArtifacts ca = new CompressArtifacts(baseDir, customerName, ticketNumber);
+		ca.addFileToZip("/etc/passwd", "junit");
+		ca.close();
+		assertTrue(new File("/tmp/" + outputFilename).exists());
+	}
+	
+	@After
+	public void cleanUp() {
+		File f = new File(outputFilename);
+		f.delete();
+	}
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/e189c21a/logcollectionutil/sendlogs-java/tests/com/pivotal/gemfire/sendlogs/utilities/UsageTest.java
----------------------------------------------------------------------
diff --git a/logcollectionutil/sendlogs-java/tests/com/pivotal/gemfire/sendlogs/utilities/UsageTest.java
b/logcollectionutil/sendlogs-java/tests/com/pivotal/gemfire/sendlogs/utilities/UsageTest.java
new file mode 100644
index 0000000..469470e
--- /dev/null
+++ b/logcollectionutil/sendlogs-java/tests/com/pivotal/gemfire/sendlogs/utilities/UsageTest.java
@@ -0,0 +1,37 @@
+package com.pivotal.gemfire.sendlogs.utilities;
+
+import static org.junit.Assert.*;
+
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class UsageTest {
+	
+	/**
+	 * Change stdout so that we can compare what's printed to the console more easily
+	 */
+	private final ByteArrayOutputStream outPut = new ByteArrayOutputStream();
+	private final ByteArrayOutputStream errOut = new ByteArrayOutputStream();
+
+	@Before
+	public void setUpStreams() {
+	    System.setOut(new PrintStream(outPut));
+	    System.setErr(new PrintStream(errOut));
+	}
+
+	@Test
+	public void test() {
+		Usage.printUsage();
+		assertTrue(outPut.toString().contains("gfe-sendlogs -c <company> -o <output dir>
[OPTIONS]\n\n"));
+	}
+
+	@After
+	public void cleanUpStreams() {
+	    System.setOut(null);
+	    System.setErr(null);
+	}
+}


Mime
View raw message