-
Notifications
You must be signed in to change notification settings - Fork 589
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NMS-16226: Clear Karaf's data directory on upgrade
- Loading branch information
1 parent
c0f13c3
commit b8a75bf
Showing
7 changed files
with
294 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
...ade/src/main/java/org/opennms/upgrade/implementations/ClearKarafCacheMigratorOffline.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
...e/src/test/java/org/opennms/upgrade/implementations/ClearKarafCacheMigratorOfflineIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -101,5 +101,4 @@ public void execute() throws OnmsUpgradeException { | |
public boolean requiresOnmsRunning() { | ||
return false; | ||
} | ||
|
||
} |
78 changes: 78 additions & 0 deletions
78
core/upgrade/src/test/java/org/opennms/upgrade/tests/TestUpgradeEverytime.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |