changeset 1134:3d102042f1df

PR1445: Thermostat storage should have a --status command Reviewed-by: omajid, neugens Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2013-June/006933.html
author Andriy Petrus <apetrus@redhat.com>
date Wed, 05 Jun 2013 15:48:48 -0400
parents 265c548475e7
children de36c4b57bb4
files agent/cli/src/main/java/com/redhat/thermostat/agent/cli/impl/db/DBOptionParser.java agent/cli/src/main/java/com/redhat/thermostat/agent/cli/impl/db/MongoProcessRunner.java agent/cli/src/main/java/com/redhat/thermostat/agent/cli/impl/db/StorageCommand.java agent/cli/src/main/java/com/redhat/thermostat/agent/cli/impl/locale/LocaleResources.java agent/cli/src/main/resources/com/redhat/thermostat/agent/cli/impl/strings.properties distribution/config/commands/storage.properties integration-tests/src/test/java/com/redhat/thermostat/itest/StorageTest.java
diffstat 7 files changed, 50 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/agent/cli/src/main/java/com/redhat/thermostat/agent/cli/impl/db/DBOptionParser.java	Wed Jun 05 21:32:09 2013 +0200
+++ b/agent/cli/src/main/java/com/redhat/thermostat/agent/cli/impl/db/DBOptionParser.java	Wed Jun 05 15:48:48 2013 -0400
@@ -39,7 +39,6 @@
 import com.redhat.thermostat.common.cli.Arguments;
 import com.redhat.thermostat.agent.cli.impl.locale.LocaleResources;
 import com.redhat.thermostat.common.config.ThermostatOptionParser;
-import com.redhat.thermostat.common.tools.ApplicationState;
 import com.redhat.thermostat.shared.config.InvalidConfigurationException;
 import com.redhat.thermostat.shared.locale.Translate;
 
@@ -69,6 +68,8 @@
             serviceAction = DBArgs.START;
         } else if (args.hasArgument(DBArgs.STOP.option)) {
             serviceAction = DBArgs.STOP;
+        } else if (args.hasArgument(DBArgs.STATUS.option)) {
+            serviceAction = DBArgs.STATUS;
         } else {
             throw new InvalidConfigurationException(translator.localize(LocaleResources.COMMAND_STORAGE_ARGUMENT_REQUIRED));
         }
@@ -91,27 +92,27 @@
         return dryRun;
     }
     
-    public ApplicationState getAction() {
-        return serviceAction.state;
+    public DBArgs getAction() {
+        return serviceAction;
     }
 
     static enum DBArgs {
                 
-        DRY("dryRun", ApplicationState.NONE),
+        DRY("dryRun"),
         
-        HELP("help", ApplicationState.HELP),
+        HELP("help"),
         
-        START("start", ApplicationState.START),
-        STOP("stop", ApplicationState.STOP),
+        START("start"),
+        STOP("stop"),
         
-        QUIET("quiet", ApplicationState.NONE);
+        QUIET("quiet"),
+        
+        STATUS("status");
         
         private String option;
-        private ApplicationState state;
         
-        DBArgs(String option, ApplicationState state) {
+        DBArgs(String option) {
             this.option = option;
-            this.state = state;
         }
     }
 
--- a/agent/cli/src/main/java/com/redhat/thermostat/agent/cli/impl/db/MongoProcessRunner.java	Wed Jun 05 21:32:09 2013 +0200
+++ b/agent/cli/src/main/java/com/redhat/thermostat/agent/cli/impl/db/MongoProcessRunner.java	Wed Jun 05 15:48:48 2013 -0400
@@ -141,6 +141,10 @@
         return processName != null && processName.equalsIgnoreCase(MONGO_PROCESS);
     }
     
+    public boolean isStorageRunning() {
+        return getPid() != null;
+    }
+    
     public void startService() throws IOException, InterruptedException,
             ApplicationException, InvalidConfigurationException {
 
--- a/agent/cli/src/main/java/com/redhat/thermostat/agent/cli/impl/db/StorageCommand.java	Wed Jun 05 21:32:09 2013 +0200
+++ b/agent/cli/src/main/java/com/redhat/thermostat/agent/cli/impl/db/StorageCommand.java	Wed Jun 05 15:48:48 2013 -0400
@@ -102,6 +102,9 @@
             case STOP:
                 stopService();
                 break;
+            case STATUS:
+                printServiceStatus(ctx);
+                break;
              default:
                 break;
             }
@@ -141,6 +144,14 @@
         getNotifier().fireAction(ApplicationState.STOP);
     }
     
+    private void printServiceStatus(CommandContext ctx) {
+        if (runner.isStorageRunning()) {
+            ctx.getConsole().getOutput().println(t.localize(LocaleResources.STORAGE_RUNNING).getContents());
+        } else {
+            ctx.getConsole().getOutput().println(t.localize(LocaleResources.STORAGE_NOT_RUNNING).getContents());
+        }
+    }
+    
     MongoProcessRunner createRunner() {
         return new MongoProcessRunner(configuration, parser.isQuiet());
     }
--- a/agent/cli/src/main/java/com/redhat/thermostat/agent/cli/impl/locale/LocaleResources.java	Wed Jun 05 21:32:09 2013 +0200
+++ b/agent/cli/src/main/java/com/redhat/thermostat/agent/cli/impl/locale/LocaleResources.java	Wed Jun 05 15:48:48 2013 -0400
@@ -63,6 +63,8 @@
     MISSING_PASSPHRASE,
     MISSING_DB_CONFIG,
     MISSING_DB_DIR,
+    STORAGE_RUNNING,
+    STORAGE_NOT_RUNNING
     ;
 
     static final String RESOURCE_BUNDLE = "com.redhat.thermostat.agent.cli.impl.strings";
--- a/agent/cli/src/main/resources/com/redhat/thermostat/agent/cli/impl/strings.properties	Wed Jun 05 21:32:09 2013 +0200
+++ b/agent/cli/src/main/resources/com/redhat/thermostat/agent/cli/impl/strings.properties	Wed Jun 05 15:48:48 2013 -0400
@@ -21,3 +21,5 @@
 MISSING_PASSPHRASE = No SSL key passphrase set!
 MISSING_DB_CONFIG = can't access database configuration file {0}
 MISSING_DB_DIR = database directories do not exist...
+STORAGE_RUNNING = Storage is running
+STORAGE_NOT_RUNNING = Storage is not running
--- a/distribution/config/commands/storage.properties	Wed Jun 05 21:32:09 2013 +0200
+++ b/distribution/config/commands/storage.properties	Wed Jun 05 15:48:48 2013 -0400
@@ -8,9 +8,9 @@
 
 description = starts and stops the thermostat storage
 
-usage = storage <--start|--stop> [--dryRun] [-q] [-l <level>]
+usage = storage <--start|--stop|--status> [--dryRun] [-q] [-l <level>]
 
-options = dryRun, start|stop, quiet, AUTO_LOG_OPTION
+options = dryRun, start|stop|status, quiet, AUTO_LOG_OPTION
 
 dryRun.short = d
 dryRun.long = dryRun
@@ -28,6 +28,11 @@
 stop.required = false
 stop.description = stop the database
 
+status.long = status
+status.hasarg = false
+status.required = false
+status.description = check if the database is running
+
 quiet.short = q
 quiet.long = quiet
 quiet.hasarg = false
--- a/integration-tests/src/test/java/com/redhat/thermostat/itest/StorageTest.java	Wed Jun 05 21:32:09 2013 +0200
+++ b/integration-tests/src/test/java/com/redhat/thermostat/itest/StorageTest.java	Wed Jun 05 15:48:48 2013 -0400
@@ -55,9 +55,21 @@
 
         assertNoExceptions(storage.getCurrentStandardOutContents(), storage.getCurrentStandardErrContents());
 
+        storage = spawnThermostat("storage", "--status");
+        storage.expect("Storage is running");
+        storage.expectClose();
+        
+        assertNoExceptions(storage.getCurrentStandardOutContents(), storage.getCurrentStandardErrContents());
+        
         storage = spawnThermostat("storage", "--stop");
         storage.expect("server shutdown complete");
         storage.expectClose();
+        
+        assertNoExceptions(storage.getCurrentStandardOutContents(), storage.getCurrentStandardErrContents());
+        
+        storage = spawnThermostat("storage", "--status");
+        storage.expect("Storage is not running");
+        storage.expectClose();
 
         assertNoExceptions(storage.getCurrentStandardOutContents(), storage.getCurrentStandardErrContents());
     }