Skip to content

Commit

Permalink
NMS-16226: Clear Karaf's data directory on upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
christianpape authored Nov 22, 2023
1 parent c0f13c3 commit b8a75bf
Show file tree
Hide file tree
Showing 7 changed files with 294 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,15 @@ public interface OnmsUpgrade {
* if OpenNMS must be stopped.
*/
boolean requiresOnmsRunning();

/**
* Run only once and mark when successfully executed?
*
* @return true, whether this job should only be run once
*/
default boolean runOnlyOnce() {
return true;
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*******************************************************************************
* This file is part of OpenNMS(R).
*
* Copyright (C) 2023 The OpenNMS Group, Inc.
* OpenNMS(R) is Copyright (C) 1999-2023 The OpenNMS Group, Inc.
*
* OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
*
* OpenNMS(R) is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* OpenNMS(R) 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with OpenNMS(R). If not, see:
* http://www.gnu.org/licenses/
*
* For more information contact:
* OpenNMS(R) Licensing <[email protected]>
* http://www.opennms.org/
* http://www.opennms.com/
*******************************************************************************/

package org.opennms.upgrade.implementations;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Comparator;

import org.opennms.core.utils.ConfigFileConstants;
import org.opennms.upgrade.api.AbstractOnmsUpgrade;
import org.opennms.upgrade.api.OnmsUpgradeException;

public class ClearKarafCacheMigratorOffline extends AbstractOnmsUpgrade {

private final Path opennmsDataPath;

public ClearKarafCacheMigratorOffline() throws OnmsUpgradeException {
this.opennmsDataPath = Path.of(ConfigFileConstants.getHome()).resolve("data");
}

@Override
public int getOrder() {
return 16;
}

@Override
public String getDescription() {
return String.format("Clears the Karaf cache in '%s', see NMS-16226", this.opennmsDataPath);
}

@Override
public void preExecute() throws OnmsUpgradeException {
}

@Override
public void postExecute() throws OnmsUpgradeException {
}

@Override
public void rollback() throws OnmsUpgradeException {
}

@Override
public void execute() throws OnmsUpgradeException {
final Path historyFilePath = this.opennmsDataPath.resolve("history.txt");
try {
Files.walk(this.opennmsDataPath)
.filter((path) -> !path.equals(historyFilePath) && !path.equals(this.opennmsDataPath))
.sorted(Comparator.reverseOrder())
.map(Path::toFile)
.forEach(File::delete);
} catch (IOException e) {
throw new OnmsUpgradeException(String.format("Error pruning Karaf's data directory '%s'.", this.opennmsDataPath), e);
}
}

@Override
public boolean requiresOnmsRunning() {
return false;
}

@Override
public boolean runOnlyOnce() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,12 @@ protected void executeUpgrade(OnmsUpgrade upg) {
try {
log("- Running execution phase\n");
upg.execute();
log("- Saving the execution state\n");
markAsExecuted(upg);
if (upg.runOnlyOnce()) {
log("- Saving the execution state\n");
markAsExecuted(upg);
} else {
log("- Ignore the execution status, as this task is executed with every call\n");
}
} catch (OnmsUpgradeException executeException) {
log(" Warning: can't perform the upgrade operation because: %s\n", executeException.getMessage());
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*******************************************************************************
* This file is part of OpenNMS(R).
*
* Copyright (C) 2023 The OpenNMS Group, Inc.
* OpenNMS(R) is Copyright (C) 1999-2023 The OpenNMS Group, Inc.
*
* OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
*
* OpenNMS(R) is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* OpenNMS(R) 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with OpenNMS(R). If not, see:
* http://www.gnu.org/licenses/
*
* For more information contact:
* OpenNMS(R) Licensing <[email protected]>
* http://www.opennms.org/
* http://www.opennms.com/
*******************************************************************************/

package org.opennms.upgrade.implementations;

import static org.junit.Assert.assertThat;

import java.nio.file.Path;

import org.hamcrest.core.Is;
import org.hamcrest.text.StringContainsInOrder;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.opennms.upgrade.api.OnmsUpgradeException;

import com.google.common.collect.Lists;

public class ClearKarafCacheMigratorOfflineIT {

@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();

@Before
public void before() throws Exception {
temporaryFolder.newFolder("data", "aaa");
temporaryFolder.newFolder("data", "bbb");
temporaryFolder.newFolder("etc");
temporaryFolder.newFile("data/history.txt");
temporaryFolder.newFile("data/foo.bar");
temporaryFolder.newFile("etc/opennms.properties");
temporaryFolder.newFile("etc/rrd-configuration.properties");
System.setProperty("opennms.home", temporaryFolder.getRoot().getAbsolutePath());
}

@Test
public void testDeletion() {
final Path temporaryPath = temporaryFolder.getRoot().toPath();
assertThat("temporary folder must exist", temporaryPath.toFile().exists(), Is.is(true));
assertThat("data directory must exist", temporaryPath.resolve("data").toFile().exists(), Is.is(true));
assertThat("data/history.txt file must exist", temporaryPath.resolve("data").resolve("history.txt").toFile().exists(), Is.is(true));
assertThat("data/foo.bar file must exist", temporaryPath.resolve("data").resolve("foo.bar").toFile().exists(), Is.is(true));
assertThat("data/aaa directory must exist", temporaryPath.resolve("data").resolve("aaa").toFile().exists(), Is.is(true));
assertThat("data/bbb directory must exist", temporaryPath.resolve("data").resolve("bbb").toFile().exists(), Is.is(true));
assertThat("etc/opennms.properties file must exist", temporaryPath.resolve("etc").resolve("opennms.properties").toFile().exists(), Is.is(true));
assertThat("etc/rrd-configuration.properties file must exist", temporaryPath.resolve("etc").resolve("rrd-configuration.properties").toFile().exists(), Is.is(true));

try {
new ClearKarafCacheMigratorOffline().execute();
} catch (OnmsUpgradeException e) {
assertThat("Exception message must match `Karaf's data directory ... pruned'", e.getMessage(), StringContainsInOrder.stringContainsInOrder(Lists.newArrayList("Karaf's data directory", "pruned")));
}

assertThat("temporary folder must exist", temporaryPath.toFile().exists(), Is.is(true));
assertThat("data directory must exist", temporaryPath.resolve("data").toFile().exists(), Is.is(true));
assertThat("data/history.txt file must exist", temporaryPath.resolve("data").resolve("history.txt").toFile().exists(), Is.is(true));
assertThat("data/foo.bar file must not exist", temporaryPath.resolve("data").resolve("foo.bar").toFile().exists(), Is.is(false));
assertThat("data/aaa directory must not exist", temporaryPath.resolve("data").resolve("aaa").toFile().exists(), Is.is(false));
assertThat("data/bbb directory must not exist", temporaryPath.resolve("data").resolve("bbb").toFile().exists(), Is.is(false));
assertThat("etc/opennms.properties file must exist", temporaryPath.resolve("etc").resolve("opennms.properties").toFile().exists(), Is.is(true));
assertThat("etc/rrd-configuration.properties file must exist", temporaryPath.resolve("etc").resolve("rrd-configuration.properties").toFile().exists(), Is.is(true));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.opennms.upgrade.api.OnmsUpgradeException;
import org.opennms.upgrade.tests.TestUpgradeA;
import org.opennms.upgrade.tests.TestUpgradeB;
import org.opennms.upgrade.tests.TestUpgradeEverytime;
import org.opennms.upgrade.tests.TestUpgradeExecuted;
import org.opennms.upgrade.tests.TestUpgradeNothing;
import org.opennms.upgrade.tests.bad.TestUpgradeWIthException;
Expand Down Expand Up @@ -69,6 +70,8 @@ public void setUp() throws Exception {
p.put(new TestUpgradeExecuted().getId(), new Date().toString());
p.store(new FileWriter(statusFile), null);
upgradeStatus = new UpgradeStatus(statusFile);
UpgradeHelper.executed.clear();
UpgradeHelper.rolledback.clear();
}

/**
Expand All @@ -91,14 +94,28 @@ public void testUpgrade() throws Exception {
Assert.assertFalse(upgradeStatus.wasExecuted(new TestUpgradeNothing()));
performUpgrade("org.opennms.upgrade.tests");
Assert.assertTrue(upgradeStatus.wasExecuted(new TestUpgradeNothing()));
Assert.assertEquals(3, UpgradeHelper.getExecutedList().size());
Assert.assertEquals(4, UpgradeHelper.getExecutedList().size());
Assert.assertEquals(TestUpgradeNothing.class.getName(), UpgradeHelper.getExecutedList().get(0));
Assert.assertEquals(TestUpgradeA.class.getName(), UpgradeHelper.getExecutedList().get(1));
Assert.assertEquals(TestUpgradeB.class.getName(), UpgradeHelper.getExecutedList().get(2));
Assert.assertEquals(1, UpgradeHelper.getRolledBackList().size());
Assert.assertEquals(TestUpgradeWIthException.class.getName(), UpgradeHelper.getRolledBackList().get(0));
}

@Test
public void testUpgradeMoreThanOnce() throws Exception {
Assert.assertFalse(upgradeStatus.wasExecuted(new TestUpgradeNothing()));
Assert.assertEquals(0, UpgradeHelper.getExecutedList().stream().filter(c -> TestUpgradeA.class.getName().equals(c)).count());
Assert.assertEquals(0, UpgradeHelper.getExecutedList().stream().filter(c -> TestUpgradeEverytime.class.getName().equals(c)).count());
performUpgrade("org.opennms.upgrade.tests");
Assert.assertEquals(1, UpgradeHelper.getExecutedList().stream().filter(c -> TestUpgradeA.class.getName().equals(c)).count());
Assert.assertEquals(1, UpgradeHelper.getExecutedList().stream().filter(c -> TestUpgradeEverytime.class.getName().equals(c)).count());
performUpgrade("org.opennms.upgrade.tests");
performUpgrade("org.opennms.upgrade.tests");
Assert.assertEquals(1, UpgradeHelper.getExecutedList().stream().filter(c -> TestUpgradeA.class.getName().equals(c)).count());
Assert.assertEquals(3, UpgradeHelper.getExecutedList().stream().filter(c -> TestUpgradeEverytime.class.getName().equals(c)).count());
}

/**
* Perform upgrade.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,4 @@ public void execute() throws OnmsUpgradeException {
public boolean requiresOnmsRunning() {
return false;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*******************************************************************************
* This file is part of OpenNMS(R).
*
* Copyright (C) 2023 The OpenNMS Group, Inc.
* OpenNMS(R) is Copyright (C) 1999-2023 The OpenNMS Group, Inc.
*
* OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
*
* OpenNMS(R) is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* OpenNMS(R) 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with OpenNMS(R). If not, see:
* http://www.gnu.org/licenses/
*
* For more information contact:
* OpenNMS(R) Licensing <[email protected]>
* http://www.opennms.org/
* http://www.opennms.com/
*******************************************************************************/

package org.opennms.upgrade.tests;

import org.opennms.upgrade.api.OnmsUpgrade;
import org.opennms.upgrade.api.OnmsUpgradeException;
import org.opennms.upgrade.support.UpgradeHelper;

public class TestUpgradeEverytime implements OnmsUpgrade {
@Override
public int getOrder() {
return 301;
}

@Override
public String getId() {
return getClass().getName();
}

@Override
public String getDescription() {
return "Testing class that runs everytime";
}

@Override
public void preExecute() throws OnmsUpgradeException {
}

@Override
public void postExecute() throws OnmsUpgradeException {
}

@Override
public void rollback() throws OnmsUpgradeException {
UpgradeHelper.addRolledBack(getId());
}

@Override
public void execute() throws OnmsUpgradeException {
UpgradeHelper.addExecuted(getId());
}

@Override
public boolean requiresOnmsRunning() {
return false;
}

@Override
public boolean runOnlyOnce() {
return false;
}
}

0 comments on commit b8a75bf

Please sign in to comment.