ARTEMIS-4092: resolve issues with upgrade backups, tweaks for clarity and consistency, additional output detail

Adds test verifications that expected files are backed up and match pre-upgrade reference files.

This closes #4291
This commit is contained in:
Robbie Gemmell 2022-11-23 16:05:37 +00:00
parent d5ecfa26df
commit 481d07f27e
6 changed files with 170 additions and 52 deletions

View File

@ -64,8 +64,10 @@ public class Create extends InstallAbstract {
public static final String ARTEMIS_CMD = "artemis.cmd";
public static final String BIN_ARTEMIS_CMD = "bin/" + ARTEMIS_CMD;
public static final String BIN_ARTEMIS_SERVICE_EXE = "bin/artemis-service.exe";
public static final String BIN_ARTEMIS_SERVICE_EXE_CONFIG = "bin/artemis-service.exe.config";
public static final String ARTEMIS_SERVICE_EXE = "artemis-service.exe";
public static final String BIN_ARTEMIS_SERVICE_EXE = "bin/" + ARTEMIS_SERVICE_EXE;
public static final String ARTEMIS_SERVICE_EXE_CONFIG = "artemis-service.exe.config";
public static final String BIN_ARTEMIS_SERVICE_EXE_CONFIG = "bin/" + ARTEMIS_SERVICE_EXE_CONFIG;
public static final String ARTEMIS_SERVICE_XML = "artemis-service.xml";
public static final String BIN_ARTEMIS_SERVICE_XML = "bin/" + ARTEMIS_SERVICE_XML;
public static final String ETC_ARTEMIS_PROFILE_CMD = "artemis.profile.cmd";

View File

@ -45,7 +45,7 @@ public class Upgrade extends InstallAbstract {
// this is the prefix where we can find the JDK arguments in Linux script
private static final String JDK_PREFIX_LINUX = "JAVA_ARGS=";
protected static final String OLD_LOGGING_PROPERTIES = "logging.properties";
public static final String OLD_LOGGING_PROPERTIES = "logging.properties";
/**
* Checks that the directory provided either exists and is writable or doesn't exist but can be created.
@ -73,26 +73,28 @@ public class Upgrade extends InstallAbstract {
context.out.println("*******************************************************************************************************************************");
context.out.println("Upgrading broker instance " + directory + " to use artemis.home=" + getBrokerHome());
File bkpFolder = findBackup(context);
File binBkp = new File(bkpFolder, "bin");
File etcBkp = new File(bkpFolder, "etc");
File tmp = new File(bkpFolder, "tmp");
binBkp.mkdirs();
etcBkp.mkdirs();
tmp.mkdirs();
final File bkpFolder = findBackup(context);
File bin = new File(directory, "bin");
final File binBkp = new File(bkpFolder, "bin");
final File etcBkp = new File(bkpFolder, "etc");
final File tmp = new File(bkpFolder, "tmp");
Files.createDirectory(binBkp.toPath());
Files.createDirectory(etcBkp.toPath());
Files.createDirectory(tmp.toPath());
final File bin = new File(directory, "bin");
File etcFolder = new File(directory, etc);
final File artemisCmdScript = new File(bin, Create.ARTEMIS_CMD);
final File artemisScript = new File(bin, Create.ARTEMIS);
if (etc == null || etc.equals("etc")) {
if (IS_WINDOWS && !IS_CYGWIN) {
File cmd = new File(bin, Create.ARTEMIS_CMD);
String pattern = "set ARTEMIS_INSTANCE_ETC=";
etcFolder = getETC(context, etcFolder, cmd, pattern);
etcFolder = getETC(context, etcFolder, artemisCmdScript, pattern);
} else {
File cmd = new File(bin, Create.ARTEMIS);
String pattern = "ARTEMIS_INSTANCE_ETC=";
etcFolder = getETC(context, etcFolder, cmd, pattern);
etcFolder = getETC(context, etcFolder, artemisScript, pattern);
}
}
@ -108,43 +110,81 @@ public class Upgrade extends InstallAbstract {
Create.addScriptFilters(filters, getHome(), getInstance(), etcFolder, new File(getInstance(), "notUsed"), new File(getInstance(), "om-not-used.dmp"), javaMemory, javaOptions, "NA");
if (IS_WINDOWS) {
// recreating the service.exe in case we ever upgrade it
// recreating the service.exe and config in case we ever upgrade it
final File serviceExe = new File(directory, Create.BIN_ARTEMIS_SERVICE_EXE);
final File serviceExeBkp = new File(bkpFolder, Create.BIN_ARTEMIS_SERVICE_EXE);
context.out.println("Copying " + serviceExe + " to " + serviceExeBkp);
Files.copy(serviceExe.toPath(), serviceExeBkp.toPath(), StandardCopyOption.REPLACE_EXISTING);
context.out.println("Updating " + serviceExe.toPath());
write(Create.BIN_ARTEMIS_SERVICE_EXE, true);
final File serviceExeConfig = new File(directory, Create.BIN_ARTEMIS_SERVICE_EXE_CONFIG);
final File serviceExeConfigBkp = new File(bkpFolder, Create.BIN_ARTEMIS_SERVICE_EXE_CONFIG);
if (serviceExeConfig.exists()) {
// It didnt exist until more recently
context.out.println("Copying " + serviceExeConfig + " to " + serviceExeConfigBkp);
Files.copy(serviceExeConfig.toPath(), serviceExeConfigBkp.toPath(), StandardCopyOption.REPLACE_EXISTING);
}
context.out.println("Updating " + serviceExeConfig);
write(Create.BIN_ARTEMIS_SERVICE_EXE_CONFIG, true);
write(Create.BIN_ARTEMIS_CMD, new File(tmp, Create.ARTEMIS_CMD), filters, false, false);
upgrade(new File(tmp, Create.ARTEMIS_CMD), new File(bin, Create.ARTEMIS_CMD), binBkp, "set ARTEMIS_INSTANCE_ETC=");
final File artemisCmdScriptTmp = new File(tmp, Create.ARTEMIS_CMD);
final File artemisCmdScriptBkp = new File(binBkp, Create.ARTEMIS_CMD);
write(Create.BIN_ARTEMIS_SERVICE_XML, new File(tmp, Create.ARTEMIS_SERVICE_XML), filters, false, false);
upgrade(new File(tmp, Create.ARTEMIS_SERVICE_XML), new File(bin, Create.ARTEMIS_SERVICE_XML), binBkp,
write(Create.BIN_ARTEMIS_CMD, artemisCmdScriptTmp, filters, false, false);
upgrade(context, artemisCmdScriptTmp, artemisCmdScript, artemisCmdScriptBkp, "set ARTEMIS_INSTANCE_ETC=");
final File serviceXmlTmp = new File(tmp, Create.ARTEMIS_SERVICE_XML);
final File serviceXml = new File(bin, Create.ARTEMIS_SERVICE_XML);
final File serviceXmlBkp = new File(binBkp, Create.ARTEMIS_SERVICE_XML);
write(Create.BIN_ARTEMIS_SERVICE_XML, serviceXmlTmp, filters, false, false);
upgrade(context, serviceXmlTmp, serviceXml, serviceXmlBkp,
"<env name=\"ARTEMIS_INSTANCE\"", "<env name=\"ARTEMIS_INSTANCE_ETC\"",
"<env name=\"ARTEMIS_INSTANCE_URI\"", "<env name=\"ARTEMIS_INSTANCE_ETC_URI\"",
"<env name=\"ARTEMIS_DATA_DIR\"", "<logpath>", "<startargument>-Xmx", "<stopargument>-Xmx",
"<name>", "<id>", "<startargument>-Dhawtio.role=");
write("etc/" + Create.ETC_ARTEMIS_PROFILE_CMD, new File(tmp, Create.ETC_ARTEMIS_PROFILE_CMD), filters, false, false);
upgradeJDK(JDK_PREFIX_WINDOWS, "", KEEPING_JVM_ARGUMENTS, new File(tmp, Create.ETC_ARTEMIS_PROFILE_CMD), new File(etcFolder, Create.ETC_ARTEMIS_PROFILE_CMD), binBkp,
final File artemisProfileCmdTmp = new File(tmp, Create.ETC_ARTEMIS_PROFILE_CMD);
final File artemisProfileCmd = new File(etcFolder, Create.ETC_ARTEMIS_PROFILE_CMD);
final File artemisProfileCmdBkp = new File(etcBkp, Create.ETC_ARTEMIS_PROFILE_CMD);
write("etc/" + Create.ETC_ARTEMIS_PROFILE_CMD, artemisProfileCmdTmp, filters, false, false);
upgradeJDK(context, JDK_PREFIX_WINDOWS, "", KEEPING_JVM_ARGUMENTS, artemisProfileCmdTmp, artemisProfileCmd, artemisProfileCmdBkp,
"set ARTEMIS_INSTANCE=\"", "set ARTEMIS_DATA_DIR=", "set ARTEMIS_ETC_DIR=", "set ARTEMIS_OOME_DUMP=", "set ARTEMIS_INSTANCE_URI=", "set ARTEMIS_INSTANCE_ETC_URI=");
}
if (!IS_WINDOWS || IS_CYGWIN) {
write(Create.BIN_ARTEMIS, new File(tmp, Create.ARTEMIS), filters, false, false);
upgrade(new File(tmp, Create.ARTEMIS), new File(bin, Create.ARTEMIS), binBkp, "ARTEMIS_INSTANCE_ETC=");
final File artemisScriptTmp = new File(tmp, Create.ARTEMIS);
final File artemisScriptBkp = new File(binBkp, Create.ARTEMIS);
write(Create.BIN_ARTEMIS_SERVICE, new File(tmp, Create.ARTEMIS_SERVICE), filters, false, false);
upgrade(new File(tmp, Create.ARTEMIS_SERVICE), new File(bin, Create.ARTEMIS_SERVICE), binBkp); // we replace the whole thing
write(Create.BIN_ARTEMIS, artemisScriptTmp, filters, false, false);
upgrade(context, artemisScriptTmp, artemisScript, artemisScriptBkp, "ARTEMIS_INSTANCE_ETC=");
final File artemisService = new File(bin, Create.ARTEMIS_SERVICE);
final File artemisServiceTmp = new File(tmp, Create.ARTEMIS_SERVICE);
final File artemisServiceBkp = new File(binBkp, Create.ARTEMIS_SERVICE);
write(Create.BIN_ARTEMIS_SERVICE, artemisServiceTmp, filters, false, false);
upgrade(context, artemisServiceTmp, artemisService, artemisServiceBkp); // we replace the whole thing
write("etc/" + Create.ETC_ARTEMIS_PROFILE, new File(tmp, Create.ETC_ARTEMIS_PROFILE), filters, false, false);
upgradeJDK(JDK_PREFIX_LINUX, "\"", KEEPING_JVM_ARGUMENTS,
new File(tmp, Create.ETC_ARTEMIS_PROFILE), new File(etcFolder, Create.ETC_ARTEMIS_PROFILE), etcBkp, "ARTEMIS_INSTANCE=",
upgradeJDK(context, JDK_PREFIX_LINUX, "\"", KEEPING_JVM_ARGUMENTS,
new File(tmp, Create.ETC_ARTEMIS_PROFILE), new File(etcFolder, Create.ETC_ARTEMIS_PROFILE), new File(etcBkp, Create.ETC_ARTEMIS_PROFILE), "ARTEMIS_INSTANCE=",
"ARTEMIS_DATA_DIR=", "ARTEMIS_ETC_DIR=", "ARTEMIS_OOME_DUMP=", "ARTEMIS_INSTANCE_URI=", "ARTEMIS_INSTANCE_ETC_URI=", "HAWTIO_ROLE=");
}
final File bootstrapXml = new File(etcFolder, Create.ETC_BOOTSTRAP_XML);
final File bootstrapXmlTmp = new File(tmp, Create.ETC_BOOTSTRAP_XML);
final File bootstrapXmlBkp = new File(etcBkp, Create.ETC_BOOTSTRAP_XML);
Files.copy( new File(etcFolder, Create.ETC_BOOTSTRAP_XML).toPath(), new File(tmp, Create.ETC_BOOTSTRAP_XML).toPath());
replaceLines(new File(tmp, Create.ETC_BOOTSTRAP_XML), new File(etcFolder, Create.ETC_BOOTSTRAP_XML), binBkp, "<web path", " <web path=\"web\" rootRedirectLocation=\"console\">");
Files.copy(bootstrapXml.toPath(), bootstrapXmlTmp.toPath());
replaceLines(context, bootstrapXmlTmp, bootstrapXml, bootstrapXmlBkp, "<web path", " <web path=\"web\" rootRedirectLocation=\"console\">");
upgradeLogging(context, etcBkp, etcFolder);
upgradeLogging(context, etcFolder, etcBkp);
context.out.println();
context.out.println("*******************************************************************************************************************************");
@ -179,12 +219,12 @@ public class Upgrade extends InstallAbstract {
}
private void upgradeJDK(String jdkPrefix, String endOfLine, String[] keepArguments, File tmpFile, File targetFile, File bkp, String... keepingPrefixes) throws Exception {
private void upgradeJDK(ActionContext context, String jdkPrefix, String endOfLine, String[] keepArguments, File tmpFile, File targetFile, File bkpFile, String... keepingPrefixes) throws Exception {
final HashMap<String, String> replaceMatrix = new HashMap<>();
final HashMap<String, String> currentArguments = new HashMap<>();
doUpgrade(tmpFile, targetFile, bkp,
doUpgrade(context, tmpFile, targetFile, bkpFile,
oldLine -> {
if (oldLine.trim().startsWith(jdkPrefix)) {
JVMArgumentParser.parseOriginalArgs(jdkPrefix, endOfLine, oldLine, keepArguments, currentArguments);
@ -213,8 +253,8 @@ public class Upgrade extends InstallAbstract {
});
}
private void replaceLines(File tmpFile, File targetFile, File bkp, String... replacePairs) throws Exception {
doUpgrade(tmpFile, targetFile, bkp,
private void replaceLines(ActionContext context, File tmpFile, File targetFile, File bkpFile, String... replacePairs) throws Exception {
doUpgrade(context, tmpFile, targetFile, bkpFile,
null,
newLine -> {
for (int i = 0; i < replacePairs.length; i += 2) {
@ -226,30 +266,35 @@ public class Upgrade extends InstallAbstract {
});
}
private void upgrade(File tmpFile, File targetFile, File bkp, String... keepingPrefixes) throws Exception {
private void upgrade(ActionContext context, File tmpFile, File targetFile, File bkpFile, String... keepingPrefixes) throws Exception {
HashMap<String, String> replaceMatrix = new HashMap<>();
doUpgrade(tmpFile, targetFile, bkp,
doUpgrade(context, tmpFile, targetFile, bkpFile,
oldLine -> {
for (String prefix : keepingPrefixes) {
if (oldLine.trim().startsWith(prefix)) {
replaceMatrix.put(prefix, oldLine);
if (keepingPrefixes.length > 0) {
for (String prefix : keepingPrefixes) {
if (oldLine.trim().startsWith(prefix)) {
replaceMatrix.put(prefix, oldLine);
}
}
}
},
newLine -> {
for (String prefix : keepingPrefixes) {
if (newLine.trim().startsWith(prefix)) {
String originalLine = replaceMatrix.get(prefix);
return originalLine;
if (keepingPrefixes.length > 0) {
for (String prefix : keepingPrefixes) {
if (newLine.trim().startsWith(prefix)) {
String originalLine = replaceMatrix.get(prefix);
return originalLine;
}
}
}
return newLine;
});
}
private void doUpgrade(File tmpFile, File targetFile, File bkp, Consumer<String> originalConsumer, Function<String, String> targetFunction) throws Exception {
Files.copy(targetFile.toPath(), bkp.toPath(), StandardCopyOption.REPLACE_EXISTING);
private void doUpgrade(ActionContext context, File tmpFile, File targetFile, File bkpFile, Consumer<String> originalConsumer, Function<String, String> targetFunction) throws Exception {
context.out.println("Copying " + targetFile + " to " + bkpFile);
Files.copy(targetFile.toPath(), bkpFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
// we first scan the original lines on the originalConsumer, giving a chance to the caller to fill out the original matrix
if (originalConsumer != null) {
@ -260,6 +305,8 @@ public class Upgrade extends InstallAbstract {
}
}
context.out.println("Updating " + targetFile);
// now we open the new file from the tmp, and we will give a chance for the targetFunction to replace lines from a matrix
try (Stream<String> lines = Files.lines(tmpFile.toPath());
PrintStream streamOutput = new PrintStream(new FileOutputStream(targetFile))) {
@ -275,21 +322,21 @@ public class Upgrade extends InstallAbstract {
}
}
private void upgradeLogging(ActionContext context, File bkpFolder, File etc) throws Exception {
File oldLogging = new File(etc, OLD_LOGGING_PROPERTIES);
private void upgradeLogging(ActionContext context, File etcFolder, File bkpFolder) throws Exception {
File oldLogging = new File(etcFolder, OLD_LOGGING_PROPERTIES);
if (oldLogging.exists()) {
File oldLoggingCopy = new File(bkpFolder, OLD_LOGGING_PROPERTIES);
context.out.println("Copying " + oldLogging.toPath() + " to " + oldLoggingCopy.toPath());
Files.copy(oldLogging.toPath(), bkpFolder.toPath(), StandardCopyOption.REPLACE_EXISTING);
Files.copy(oldLogging.toPath(), oldLoggingCopy.toPath(), StandardCopyOption.REPLACE_EXISTING);
context.out.println("Removing " + oldLogging.toPath());
if (!oldLogging.delete()) {
context.out.println(oldLogging.toPath() + " could not be removed!");
}
File newLogging = new File(etc, Create.ETC_LOG4J2_PROPERTIES);
File newLogging = new File(etcFolder, Create.ETC_LOG4J2_PROPERTIES);
if (!newLogging.exists()) {
context.out.println("Creating " + newLogging);
try (InputStream inputStream = openStream("etc/" + Create.ETC_LOG4J2_PROPERTIES);
@ -300,11 +347,11 @@ public class Upgrade extends InstallAbstract {
}
}
protected File findBackup(ActionContext context) {
protected File findBackup(ActionContext context) throws IOException {
for (int bkp = 0; bkp < 10; bkp++) {
File bkpFolder = new File(directory, "old-config-bkp." + bkp);
if (!bkpFolder.exists()) {
bkpFolder.mkdirs();
Files.createDirectory(bkpFolder.toPath());
context.out.println("Using " + bkpFolder.getAbsolutePath() + " as a backup folder for the modified files");
return bkpFolder;
}

View File

@ -220,6 +220,22 @@
</resources>
</configuration>
</execution>
<execution>
<id>copy-reference-for-upgrade-backup-checks</id>
<phase>process-test-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/reference-for-backup-check</outputDirectory>
<resources>
<resource>
<directory>src/main/filtered-resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>

View File

@ -0,0 +1 @@
This is a fake exe file, just checking it gets copied before being overwritten.

View File

@ -0,0 +1,6 @@
<configuration>
<startup>
<supportedRuntime version="v2.0.50727" />
<supportedRuntime version="v4.0" />
</startup>
</configuration>

View File

@ -17,6 +17,10 @@
package org.apache.activemq.artemis.tests.smoke.upgradeTest;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.lang.invoke.MethodHandles;
import java.nio.file.Files;
@ -25,6 +29,8 @@ import java.util.Iterator;
import java.util.Map;
import java.util.stream.Stream;
import org.apache.activemq.artemis.cli.commands.Create;
import org.apache.activemq.artemis.cli.commands.Upgrade;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
@ -50,6 +56,12 @@ public class CompareUpgradeTest {
compareDirectories(windowsExpectedBin, windowsBin);
compareDirectories(windowsExpectedETC, windowsETC, "broker.xml", "artemis-users.properties");
String referenceBin = basedir + "/target/reference-for-backup-check/servers/windowsUpgrade/bin";
String referenceEtc = basedir + "/target/reference-for-backup-check/servers/windowsUpgradeETC";
verifyBackupFiles(windows + "/old-config-bkp.0/bin", referenceBin, Create.ARTEMIS_CMD, Create.ARTEMIS_SERVICE_EXE, Create.ARTEMIS_SERVICE_EXE_CONFIG, Create.ARTEMIS_SERVICE_XML);
verifyBackupFiles(windows + "/old-config-bkp.0/etc", referenceEtc, Create.ETC_ARTEMIS_PROFILE_CMD, Create.ETC_BOOTSTRAP_XML, Upgrade.OLD_LOGGING_PROPERTIES);
}
@Test
@ -64,8 +76,42 @@ public class CompareUpgradeTest {
compareDirectories(linuxExpectedBin, linuxBin);
compareDirectories(linuxExpectedETC, linuxETC, "broker.xml", "artemis-users.properties");
String referenceBin = basedir + "/target/reference-for-backup-check/servers/linuxUpgrade/bin";
String referenceEtc = basedir + "/target/reference-for-backup-check/servers/linuxUpgradeETC";
verifyBackupFiles(linux + "/old-config-bkp.0/bin", referenceBin, Create.ARTEMIS, Create.ARTEMIS_SERVICE);
verifyBackupFiles(linux + "/old-config-bkp.0/etc", referenceEtc, Create.ETC_ARTEMIS_PROFILE, Create.ETC_BOOTSTRAP_XML, Upgrade.OLD_LOGGING_PROPERTIES);
}
private void verifyBackupFiles(String backupFolder, String referenceFolder, String... files) throws Exception {
assertTrue("Files to check must be specified", files.length > 0);
File bck = new File(backupFolder);
if (!(bck.exists() && bck.isDirectory())) {
Assert.fail("Backup folder does not exist at: " + bck.getAbsolutePath());
}
File[] backupFiles = bck.listFiles();
assertNotNull("Some backup files must exist", backupFiles);
int backupFilesCount = backupFiles.length;
assertTrue("Some backup files must exist", backupFilesCount > 0);
assertEquals("Different number of backup files found than specified for inspection, update test if backup procedure changed", files.length, backupFilesCount);
for (String f : files) {
File bf = new File(backupFolder, f);
if (!bf.exists()) {
Assert.fail("Expected backup file does not exist at: " + bf.getAbsolutePath());
}
File reference = new File(referenceFolder, bf.getName());
if (!reference.exists()) {
Assert.fail("Reference file does not exist at: " + reference.getAbsolutePath());
}
Assert.assertArrayEquals(bf.getName() + " backup contents do not match reference file", Files.readAllBytes(bf.toPath()), Files.readAllBytes(reference.toPath()));
}
}
private void compareDirectories(String expectedFolder, String upgradeFolder, String... ignoredFiles) throws Exception {
File expectedFolderFile = new File(expectedFolder);