modules/enterprise/agent/src/main/java/org/rhq/enterprise/agent/i18n/AgentI18NResourceKeys.java
| 14 ++
modules/enterprise/agent/src/main/java/org/rhq/enterprise/agent/promptcmd/InventoryPromptCommand.java
| 65 +++++++++-
2 files changed, 73 insertions(+), 6 deletions(-)
New commits:
commit 148c25cc279be5f96923c149fc0530512afdb9a0
Author: John Mazzitelli <mazz(a)redhat.com>
Date: Wed Jul 28 14:24:20 2010 -0400
bz 619138 - add "inventory --sync" command
diff --git
a/modules/enterprise/agent/src/main/java/org/rhq/enterprise/agent/i18n/AgentI18NResourceKeys.java
b/modules/enterprise/agent/src/main/java/org/rhq/enterprise/agent/i18n/AgentI18NResourceKeys.java
index 7e621e8..eadb493 100644
---
a/modules/enterprise/agent/src/main/java/org/rhq/enterprise/agent/i18n/AgentI18NResourceKeys.java
+++
b/modules/enterprise/agent/src/main/java/org/rhq/enterprise/agent/i18n/AgentI18NResourceKeys.java
@@ -1922,7 +1922,7 @@ public interface AgentI18NResourceKeys {
@I18NMessage("inventory")
String INVENTORY = "PromptCommand.inventory";
- @I18NMessage("inventory [--xml] [--export=<file>]
[--norecurse]\\n\\\n"
+ @I18NMessage("inventory [--sync] [--xml] [--export=<file>]
[--norecurse]\\n\\\n"
+ "\\ [--id=<#>] | --types |
<inventory-binary-file>]")
String INVENTORY_SYNTAX = "PromptCommand.inventory.syntax";
@@ -1931,6 +1931,9 @@ public interface AgentI18NResourceKeys {
@I18NMessage("This will allow you to view the resources currently in
inventory.\\n\\\n"
+ "The valid command line arguments are:\\n\\\n"
+ + "\\ -s, --sync : Purges the agent's inventory and re-syncs it with the
server.\\n\\\n"
+ + "\\ This forces the agent's plugin container to
restart.\\n\\\n"
+ + "\\ All other options are ignored if this is
specified.\\n\\\n"
+ "\\ -e, --export=file : Writes the inventory information to the given
file.\\n\\\n"
+ "\\ If this is not specified, the output will go to
the\\n\\\n"
+ "\\ console window.\\n\\\n"
@@ -1980,6 +1983,15 @@ public interface AgentI18NResourceKeys {
+ "two are mutually exclusive")
String INVENTORY_ID_AND_DUMP_TYPES_SPECIFIED =
"PromptCommand.inventory.id-and-dump-types-specified";
+ @I18NMessage("Cannot sync inventory - not currently able to talk to the RHQ
Server.")
+ String INVENTORY_ERROR_NOT_SENDING =
"PromptCommand.inventory.error-not-sending";
+
+ @I18NMessage("Purged the persisted inventory found at [{0}], sync will occur
when plugin container restarts")
+ String INVENTORY_DATA_FILE_DELETED =
"PromptCommand.inventory.data-file-deleted";
+
+ @I18NMessage("Failed to purge the inventory data file [{0}], sync may
fail.")
+ String INVENTORY_DATA_FILE_DELETION_FAILURE =
"PromptCommand.inventory.data-file-deletion-failure";
+
@I18NMessage("avail")
String AVAILABILITY = "PromptCommand.availability";
diff --git
a/modules/enterprise/agent/src/main/java/org/rhq/enterprise/agent/promptcmd/InventoryPromptCommand.java
b/modules/enterprise/agent/src/main/java/org/rhq/enterprise/agent/promptcmd/InventoryPromptCommand.java
index cb48f38..661a84a 100644
---
a/modules/enterprise/agent/src/main/java/org/rhq/enterprise/agent/promptcmd/InventoryPromptCommand.java
+++
b/modules/enterprise/agent/src/main/java/org/rhq/enterprise/agent/promptcmd/InventoryPromptCommand.java
@@ -20,11 +20,14 @@ package org.rhq.enterprise.agent.promptcmd;
import gnu.getopt.Getopt;
import gnu.getopt.LongOpt;
+
import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.Set;
+
import mazz.i18n.Msg;
+
import org.rhq.core.clientapi.agent.PluginContainerException;
import org.rhq.core.domain.resource.ResourceType;
import org.rhq.core.pc.PluginContainer;
@@ -34,6 +37,7 @@ import org.rhq.core.pc.util.InventoryPrinter;
import org.rhq.enterprise.agent.AgentMain;
import org.rhq.enterprise.agent.i18n.AgentI18NFactory;
import org.rhq.enterprise.agent.i18n.AgentI18NResourceKeys;
+import org.rhq.enterprise.communications.command.client.ClientCommandSender;
/**
* Provides a view into the inventory.
@@ -92,12 +96,15 @@ public class InventoryPromptCommand implements AgentPromptCommand {
boolean dumpTypesOnly = false;
Integer id = null;
boolean noRecurse = false;
+ boolean sync = false;
- String sopts = "-e:i:ntx";
- LongOpt[] lopts = { new LongOpt("export", LongOpt.REQUIRED_ARGUMENT,
null, 'e'),
- new LongOpt("id", LongOpt.REQUIRED_ARGUMENT, null, 'i'),
- new LongOpt("norecurse", LongOpt.NO_ARGUMENT, null, 'n'),
- new LongOpt("types", LongOpt.NO_ARGUMENT, null, 't'), new
LongOpt("xml", LongOpt.NO_ARGUMENT, null, 'x') };
+ String sopts = "-se:i:ntx";
+ LongOpt[] lopts = { new LongOpt("export", LongOpt.REQUIRED_ARGUMENT,
null, 'e'), //
+ new LongOpt("id", LongOpt.REQUIRED_ARGUMENT, null, 'i'),
//
+ new LongOpt("norecurse", LongOpt.NO_ARGUMENT, null, 'n'),
//
+ new LongOpt("sync", LongOpt.NO_ARGUMENT, null, 's'), //
+ new LongOpt("types", LongOpt.NO_ARGUMENT, null, 't'), //
+ new LongOpt("xml", LongOpt.NO_ARGUMENT, null, 'x') };
Getopt getopt = new Getopt(getPromptCommandString(), args, sopts, lopts);
int code;
@@ -116,6 +123,11 @@ public class InventoryPromptCommand implements AgentPromptCommand {
break;
}
+ case 's': {
+ sync = true;
+ break;
+ }
+
case 'e': {
exportFile = getopt.getOptarg();
break;
@@ -156,6 +168,13 @@ public class InventoryPromptCommand implements AgentPromptCommand {
return;
}
+ // if being asked to sync, we shutdown the PC, delete inventory.dat and restart
PC.
+ // all other options are ignored
+ if (sync) {
+ syncInventory(agent, out);
+ return;
+ }
+
if ((inventoryBinaryFile != null) && dumpTypesOnly) {
out.println(MSG.getMsg(AgentI18NResourceKeys.INVENTORY_DUMP_TYPES_AND_BINARY_FILE_SPECIFIED));
out.println(MSG.getMsg(AgentI18NResourceKeys.HELP_SYNTAX_LABEL,
getSyntax()));
@@ -228,4 +247,40 @@ public class InventoryPromptCommand implements AgentPromptCommand {
return;
}
+
+ private void syncInventory(AgentMain agent, PrintWriter out) {
+ // if the PC is already started, we need to shut it down and restart it after we
get the new plugins
+ boolean recyclePC = agent.isPluginContainerStarted();
+
+ // make sure our agent is currently in communications with the server
+ ClientCommandSender clientCommandSender = agent.getClientCommandSender();
+ if (clientCommandSender == null || !clientCommandSender.isSending()) {
+ out.println(MSG.getMsg(AgentI18NResourceKeys.INVENTORY_ERROR_NOT_SENDING));
+ return;
+ }
+
+ // use the PC prompt command to stop the PC - needed because the PC cannot
hot-deploy plugins
+ if (recyclePC) {
+ executePCCommand(agent, "stop");
+ }
+
+ File dataDir =
agent.getConfiguration().getPluginContainerConfiguration().getDataDirectory();
+ File inventoryDataFile = new File(dataDir, "inventory.dat");
+ inventoryDataFile.delete();
+ if (!inventoryDataFile.exists()) {
+ out.println(MSG.getMsg(AgentI18NResourceKeys.INVENTORY_DATA_FILE_DELETED,
inventoryDataFile));
+ } else {
+
out.println(MSG.getMsg(AgentI18NResourceKeys.INVENTORY_DATA_FILE_DELETION_FAILURE,
inventoryDataFile));
+ }
+
+ if (recyclePC) {
+ executePCCommand(agent, "start");
+ }
+ }
+
+ private void executePCCommand(AgentMain agent, String startOrStop) {
+ PluginContainerPromptCommand command = new PluginContainerPromptCommand();
+ command.execute(agent, new String[] { command.getPromptCommandString(),
startOrStop });
+ }
+
}
\ No newline at end of file