modules/core/util/src/main/java/org/rhq/core/util/file/FileReverter.java
| 63 ++++++++++
modules/enterprise/server/server-control/src/main/java/org/rhq/server/control/ControlCommand.java
| 4
modules/enterprise/server/server-control/src/main/java/org/rhq/server/control/command/AbstractInstall.java
| 17 ++
modules/enterprise/server/server-control/src/main/java/org/rhq/server/control/command/Install.java
| 14 ++
modules/enterprise/server/server-control/src/main/java/org/rhq/server/control/command/Upgrade.java
| 14 ++
5 files changed, 109 insertions(+), 3 deletions(-)
New commits:
commit 093b388081bafff554293bd24ed52d1157aed376
Author: John Mazzitelli <mazz(a)redhat.com>
Date: Wed Sep 25 17:22:12 2013 -0400
revert rhq-server.properties and mgmt-users.properties on installation failure
diff --git a/modules/core/util/src/main/java/org/rhq/core/util/file/FileReverter.java
b/modules/core/util/src/main/java/org/rhq/core/util/file/FileReverter.java
new file mode 100644
index 0000000..e05c914
--- /dev/null
+++ b/modules/core/util/src/main/java/org/rhq/core/util/file/FileReverter.java
@@ -0,0 +1,63 @@
+/*
+ * RHQ Management Platform
+ * Copyright (C) 2005-2013 Red Hat, Inc.
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License, version 2, as
+ * published by the Free Software Foundation, and/or the GNU Lesser
+ * General Public License, version 2.1, also as published by the Free
+ * Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License and the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * and the GNU Lesser General Public License along with this program;
+ * if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+package org.rhq.core.util.file;
+
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+
+import org.rhq.core.util.stream.StreamUtil;
+
+/**
+ * This will slurp in the original content of a file and store it in memory. If, later,
you need to revert
+ * that file back to its original content, you can call #revert.
+ *
+ * NOTE: do not use this with large files. It will store the content in memory; large
files will result in
+ * out-of-memory exceptions.
+ *
+ * @author John Mazzitelli
+ *
+ */
+public class FileReverter {
+ private final File file;
+ private final byte[] originalContent;
+
+ public FileReverter(File file) {
+ this.file = file;
+ try {
+ this.originalContent = StreamUtil.slurp(new FileInputStream(file));
+ } catch (Exception e) {
+ throw new RuntimeException("Failed to retrieve content from file: "
+ file);
+ }
+ }
+
+ public void revert() {
+ try {
+ StreamUtil.copy(new ByteArrayInputStream(this.originalContent), new
FileOutputStream(this.file));
+ } catch (Exception e) {
+ throw new RuntimeException("Failed to revert file: " + file);
+ }
+ }
+}
diff --git
a/modules/enterprise/server/server-control/src/main/java/org/rhq/server/control/ControlCommand.java
b/modules/enterprise/server/server-control/src/main/java/org/rhq/server/control/ControlCommand.java
index 6a8f70d..b45bca2 100644
---
a/modules/enterprise/server/server-control/src/main/java/org/rhq/server/control/ControlCommand.java
+++
b/modules/enterprise/server/server-control/src/main/java/org/rhq/server/control/ControlCommand.java
@@ -186,6 +186,10 @@ public abstract class ControlCommand {
return new File(baseDir, "jbossas/standalone/data/rhq.installed");
}
+ protected File getServerPropertiesFile() {
+ return new File(getBaseDir(), "bin/rhq-server.properties");
+ }
+
protected boolean isServerInstalled(File baseDir) {
File markerFile = getServerInstalledMarkerFile(baseDir);
return markerFile.exists();
diff --git
a/modules/enterprise/server/server-control/src/main/java/org/rhq/server/control/command/AbstractInstall.java
b/modules/enterprise/server/server-control/src/main/java/org/rhq/server/control/command/AbstractInstall.java
index 7ff0990..3d56c60 100644
---
a/modules/enterprise/server/server-control/src/main/java/org/rhq/server/control/command/AbstractInstall.java
+++
b/modules/enterprise/server/server-control/src/main/java/org/rhq/server/control/command/AbstractInstall.java
@@ -46,6 +46,7 @@ import org.jboss.as.controller.client.ModelControllerClient;
import org.rhq.common.jbossas.client.controller.DeploymentJBossASClient;
import org.rhq.common.jbossas.client.controller.MCCHelper;
+import org.rhq.core.util.file.FileReverter;
import org.rhq.core.util.file.FileUtil;
import org.rhq.server.control.ControlCommand;
import org.rhq.server.control.RHQControlException;
@@ -183,7 +184,7 @@ public abstract class AbstractInstall extends ControlCommand {
Properties props = new Properties();
try {
- File propsFile = new File(getBaseDir(),
"bin/rhq-server.properties");
+ File propsFile = getServerPropertiesFile();
reader = new BufferedReader(new FileReader(propsFile));
props.load(reader);
@@ -427,10 +428,20 @@ public abstract class AbstractInstall extends ControlCommand {
try {
log.info("Installing RHQ server");
- // if the install fails, we will remove the install marker file allowing the
installer to be able to run again
+ // If the install fails, we will remove the install marker file allowing the
installer to be able to run again.
+ // We also need to revert mgmt-users.properties
+ File mgmtUserPropertiesFile = new File(getBaseDir(),
+ "jbossas/standalone/configuration/mgmt-users.properties");
+ final FileReverter mgmtUserPropertiesReverter = new
FileReverter(mgmtUserPropertiesFile);
addUndoTask(new Runnable() {
public void run() {
getServerInstalledMarkerFile(getBaseDir()).delete();
+ try {
+ mgmtUserPropertiesReverter.revert();
+ } catch (Exception e) {
+ throw new RuntimeException(
+ "Cannot revert mgmt user - you may have to revert
settings manually", e);
+ }
}
});
@@ -441,7 +452,7 @@ public abstract class AbstractInstall extends ControlCommand {
executor.execute(commandLine, new DefaultExecuteResultHandler());
log.info("The server installer is running");
- } catch (IOException e) {
+ } catch (Exception e) {
log.error("An error occurred while starting the server installer: "
+ e.getMessage());
}
}
diff --git
a/modules/enterprise/server/server-control/src/main/java/org/rhq/server/control/command/Install.java
b/modules/enterprise/server/server-control/src/main/java/org/rhq/server/control/command/Install.java
index 4b1fd4e..88ec433 100644
---
a/modules/enterprise/server/server-control/src/main/java/org/rhq/server/control/command/Install.java
+++
b/modules/enterprise/server/server-control/src/main/java/org/rhq/server/control/command/Install.java
@@ -42,6 +42,7 @@ import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.Executor;
import org.apache.commons.exec.PumpStreamHandler;
+import org.rhq.core.util.file.FileReverter;
import org.rhq.core.util.file.FileUtil;
import org.rhq.core.util.stream.StreamUtil;
import org.rhq.server.control.RHQControlException;
@@ -123,6 +124,19 @@ public class Install extends AbstractInstall {
return;
}
+ // If any failures occur, we know we need to reset rhq-server.properties.
+ final FileReverter serverPropFileReverter = new
FileReverter(getServerPropertiesFile());
+ addUndoTask(new Runnable() {
+ public void run() {
+ try {
+ serverPropFileReverter.revert();
+ } catch (Exception e) {
+ throw new RuntimeException(
+ "Cannot reset rhq-server.properties - you may have to
revert settings manually", e);
+ }
+ }
+ });
+
// if no options specified, then install whatever is not installed yet
if (!(commandLine.hasOption(STORAGE_OPTION) ||
commandLine.hasOption(SERVER_OPTION) || commandLine
.hasOption(AGENT_OPTION))) {
diff --git
a/modules/enterprise/server/server-control/src/main/java/org/rhq/server/control/command/Upgrade.java
b/modules/enterprise/server/server-control/src/main/java/org/rhq/server/control/command/Upgrade.java
index 8bfa49e..1d2f817 100644
---
a/modules/enterprise/server/server-control/src/main/java/org/rhq/server/control/command/Upgrade.java
+++
b/modules/enterprise/server/server-control/src/main/java/org/rhq/server/control/command/Upgrade.java
@@ -43,6 +43,7 @@ import org.apache.commons.exec.PumpStreamHandler;
import org.rhq.core.util.PropertiesFileUpdate;
import org.rhq.core.util.exception.ThrowableUtil;
+import org.rhq.core.util.file.FileReverter;
import org.rhq.core.util.file.FileUtil;
import org.rhq.core.util.stream.StreamUtil;
import org.rhq.server.control.RHQControlException;
@@ -173,6 +174,19 @@ public class Upgrade extends AbstractInstall {
return;
}
+ // If any failures occur during upgrade, we know we need to reset
rhq-server.properties.
+ final FileReverter serverPropFileReverter = new
FileReverter(getServerPropertiesFile());
+ addUndoTask(new Runnable() {
+ public void run() {
+ try {
+ serverPropFileReverter.revert();
+ } catch (Exception e) {
+ throw new RuntimeException(
+ "Cannot reset rhq-server.properties - you may have to
revert settings manually", e);
+ }
+ }
+ });
+
// now upgrade everything and start them up again
upgradeStorage(commandLine);
upgradeServer(commandLine);