modules/common/drift/src/main/java/org/rhq/common/drift/DirectoryEntry.java | 16 + modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/ChangeSetManager.java | 4 modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/ChangeSetManagerImpl.java | 78 +++++--- modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DirectoryAnalyzer.java | 15 + modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DriftClient.java | 3 modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DriftDetectionSchedule.java | 4 modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DriftDetector.java | 96 +++++++--- modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DriftManager.java | 6 modules/core/plugin-container/src/test/java/org/rhq/core/pc/drift/ChangeSetManagerImplTest.java | 15 - modules/core/plugin-container/src/test/java/org/rhq/core/pc/drift/DirectoryAnalyzerTest.java | 41 +++- modules/core/plugin-container/src/test/java/org/rhq/core/pc/drift/DriftClientTestStub.java | 5 modules/core/plugin-container/src/test/java/org/rhq/core/pc/drift/DriftDetectorTest.java | 69 ++++++- modules/core/plugin-container/src/test/java/org/rhq/core/pc/drift/DriftTest.java | 4 13 files changed, 281 insertions(+), 75 deletions(-)
New commits: commit f39dd9c2a10e959842f51b8d51c495157bfb07d8 Author: John Sanda jsanda@redhat.com Date: Wed Jul 6 15:49:48 2011 -0400
Removing more unused imports
diff --git a/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DirectoryAnalyzer.java b/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DirectoryAnalyzer.java index 8f4dfa3..5f20699 100644 --- a/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DirectoryAnalyzer.java +++ b/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DirectoryAnalyzer.java @@ -55,8 +55,6 @@ public class DirectoryAnalyzer { Map<String, FileEntry> fileEntries = createFileEntriesMap(); List<String> processed = new ArrayList<String>();
- Set<String> dirEntryFileNames = dirEntryFileNames(); - for (String file : files) { if (!fileEntries.containsKey(file)) { filesAdded.add(addedFileEntry(file, sha256(new File(dir, file)))); diff --git a/modules/core/plugin-container/src/test/java/org/rhq/core/pc/drift/ChangeSetManagerImplTest.java b/modules/core/plugin-container/src/test/java/org/rhq/core/pc/drift/ChangeSetManagerImplTest.java index 5a347ac..e4ab114 100644 --- a/modules/core/plugin-container/src/test/java/org/rhq/core/pc/drift/ChangeSetManagerImplTest.java +++ b/modules/core/plugin-container/src/test/java/org/rhq/core/pc/drift/ChangeSetManagerImplTest.java @@ -1,26 +1,21 @@ package org.rhq.core.pc.drift;
import java.io.File; -import java.util.Arrays; import java.util.List;
-import org.testng.annotations.BeforeClass; import org.testng.annotations.Test;
import org.rhq.common.drift.ChangeSetReader; import org.rhq.common.drift.DirectoryEntry; -import org.rhq.common.drift.FileEntry; import org.rhq.common.drift.Headers; -import org.rhq.core.domain.configuration.Configuration; -import org.rhq.core.domain.configuration.PropertySimple; -import org.rhq.core.domain.drift.DriftChangeSetCategory; -import org.rhq.core.domain.drift.DriftConfiguration;
import static java.util.Arrays.asList; -import static org.apache.commons.io.FileUtils.writeLines; import static org.rhq.core.domain.drift.DriftChangeSetCategory.DRIFT; -import static org.testng.Assert.*; -import static org.apache.commons.io.FileUtils.deleteDirectory; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertNotNull; +import static org.testng.Assert.assertNull; +import static org.testng.Assert.assertTrue;
public class ChangeSetManagerImplTest extends DriftTest {
diff --git a/modules/core/plugin-container/src/test/java/org/rhq/core/pc/drift/DriftDetectorTest.java b/modules/core/plugin-container/src/test/java/org/rhq/core/pc/drift/DriftDetectorTest.java index 1666646..4f27974 100644 --- a/modules/core/plugin-container/src/test/java/org/rhq/core/pc/drift/DriftDetectorTest.java +++ b/modules/core/plugin-container/src/test/java/org/rhq/core/pc/drift/DriftDetectorTest.java @@ -16,17 +16,13 @@ import org.rhq.common.drift.ChangeSetReaderImpl; import org.rhq.common.drift.DirectoryEntry; import org.rhq.common.drift.FileEntry; import org.rhq.common.drift.Headers; -import org.rhq.core.domain.configuration.Configuration; -import org.rhq.core.domain.drift.DriftChangeSetCategory; import org.rhq.core.domain.drift.DriftConfiguration; -import org.rhq.test.AssertUtils;
import static org.apache.commons.io.FileUtils.touch; import static org.rhq.common.drift.FileEntry.addedFileEntry; import static org.rhq.common.drift.FileEntry.removedFileEntry; import static org.rhq.core.domain.drift.DriftChangeSetCategory.COVERAGE; import static org.rhq.core.domain.drift.DriftChangeSetCategory.DRIFT; -import static org.rhq.core.domain.drift.DriftConfigurationDefinition.BaseDirValueContext.fileSystem; import static org.rhq.test.AssertUtils.assertCollectionMatchesNoOrder; import static org.rhq.test.AssertUtils.assertPropertiesMatch; import static org.testng.Assert.assertNotNull;
commit 4430a503e84b8c360786c2b0b1ce17aed6b48058 Author: John Sanda jsanda@redhat.com Date: Wed Jul 6 15:46:05 2011 -0400
Removing unused, unwanted imports
IntelliJ's code completion is a bit over agressive at times.
diff --git a/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/ChangeSetManagerImpl.java b/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/ChangeSetManagerImpl.java index 2f330ba..dbb1cfb 100644 --- a/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/ChangeSetManagerImpl.java +++ b/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/ChangeSetManagerImpl.java @@ -7,9 +7,6 @@ import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException;
-import org.apache.commons.io.FilenameUtils; -import org.apache.commons.io.IOUtils; - import org.rhq.common.drift.ChangeSetReader; import org.rhq.common.drift.ChangeSetReaderImpl; import org.rhq.common.drift.ChangeSetWriter; @@ -19,9 +16,6 @@ import org.rhq.core.domain.drift.DriftChangeSetCategory; import org.rhq.core.domain.drift.DriftConfiguration; import org.rhq.core.util.stream.StreamUtil;
-import net.sourceforge.cobertura.coveragedata.CoverageData; - -import static java.io.File.separator; import static org.rhq.core.domain.drift.DriftChangeSetCategory.COVERAGE;
public class ChangeSetManagerImpl implements ChangeSetManager {
commit dba97250148f2c809afb3fcab81cc5470bc2d658 Author: John Sanda jsanda@redhat.com Date: Wed Jul 6 15:36:13 2011 -0400
Generate updated coverage change set when generating drift change set
diff --git a/modules/common/drift/src/main/java/org/rhq/common/drift/DirectoryEntry.java b/modules/common/drift/src/main/java/org/rhq/common/drift/DirectoryEntry.java index 960f687..c5c5e7a 100644 --- a/modules/common/drift/src/main/java/org/rhq/common/drift/DirectoryEntry.java +++ b/modules/common/drift/src/main/java/org/rhq/common/drift/DirectoryEntry.java @@ -26,6 +26,22 @@ public class DirectoryEntry implements Iterable<FileEntry>, Serializable { return this; }
+ public DirectoryEntry remove(FileEntry entry) { + FileEntry entryToRemove = null; + int i = 0; + for (FileEntry fileEntry : files) { + if (entry.getFile().equals(fileEntry.getFile())) { + entryToRemove = fileEntry; + break; + } + ++i; + } + if (entryToRemove != null) { + files.remove(i); + } + return this; + } + public int getNumberOfFiles() { return files.size(); } diff --git a/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/ChangeSetManager.java b/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/ChangeSetManager.java index 3f946f5..4ef7821 100644 --- a/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/ChangeSetManager.java +++ b/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/ChangeSetManager.java @@ -44,6 +44,10 @@ public interface ChangeSetManager {
ChangeSetWriter getChangeSetWriter(int resourceId, Headers headers) throws IOException;
+ ChangeSetWriter getChangeSetWriterForUpdate(int resourceId, Headers headers) throws IOException; + + void updateChangeSet(int resourceId, Headers headers) throws IOException; + void addFileToChangeSet(int resourceId, DriftConfiguration driftConfiguration, File file);
} diff --git a/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/ChangeSetManagerImpl.java b/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/ChangeSetManagerImpl.java index c412bcb..2f330ba 100644 --- a/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/ChangeSetManagerImpl.java +++ b/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/ChangeSetManagerImpl.java @@ -1,9 +1,14 @@ package org.rhq.core.pc.drift;
+import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; import java.io.IOException;
import org.apache.commons.io.FilenameUtils; +import org.apache.commons.io.IOUtils;
import org.rhq.common.drift.ChangeSetReader; import org.rhq.common.drift.ChangeSetReaderImpl; @@ -12,6 +17,7 @@ import org.rhq.common.drift.ChangeSetWriterImpl; import org.rhq.common.drift.Headers; import org.rhq.core.domain.drift.DriftChangeSetCategory; import org.rhq.core.domain.drift.DriftConfiguration; +import org.rhq.core.util.stream.StreamUtil;
import net.sourceforge.cobertura.coveragedata.CoverageData;
@@ -96,6 +102,31 @@ public class ChangeSetManagerImpl implements ChangeSetManager { }
@Override + public ChangeSetWriter getChangeSetWriterForUpdate(int resourceId, Headers headers) throws IOException { + File resourceDir = new File(changeSetsDir, Integer.toString(resourceId)); + File changeSetDir = new File(resourceDir, headers.getDriftConfigurationName()); + + return new ChangeSetWriterImpl(new File(changeSetDir, "changeset.working"), headers); + } + + @Override + public void updateChangeSet(int resourceId, Headers headers) throws IOException { + File resourceDir = new File(changeSetsDir, Integer.toString(resourceId)); + File changeSetDir = new File(resourceDir, headers.getDriftConfigurationName()); + File newChangeSet = new File(changeSetDir, "changeset.working"); + File changeSet = new File(changeSetDir, "changeset.txt"); + + if (!newChangeSet.exists()) { + return; + } + + changeSet.delete(); + + StreamUtil.copy(new BufferedInputStream(new FileInputStream(newChangeSet)), + new BufferedOutputStream(new FileOutputStream(changeSet))); + } + + @Override public void addFileToChangeSet(int resourceId, DriftConfiguration driftConfiguration, File file) { }
diff --git a/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DriftDetector.java b/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DriftDetector.java index e57d763..f2883b9 100644 --- a/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DriftDetector.java +++ b/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DriftDetector.java @@ -60,12 +60,13 @@ public class DriftDetector implements Runnable { DriftConfiguration driftConfig = schedule.getDriftConfiguration(); int resourceId = schedule.getResourceId(); DriftChangeSetCategory changeSetType = null; + int changes = 0;
try { if (changeSetMgr.changeSetExists(schedule.getResourceId(), new Headers(driftConfig.getName(), basedir(resourceId, driftConfig), COVERAGE))) { changeSetType = DRIFT; - generateDriftChangeSet(schedule); + changes = generateDriftChangeSet(schedule); } else { changeSetType = COVERAGE; generateCoverageChangeSet(schedule); @@ -77,34 +78,72 @@ public class DriftDetector implements Runnable {
schedule.updateShedule(); scheduleQueue.enqueue(schedule); - driftClient.sendChangeSetToServer(schedule.getResourceId(), driftConfig, changeSetType); + + if (changeSetType == COVERAGE || changes > 0) { + driftClient.sendChangeSetToServer(schedule.getResourceId(), driftConfig, changeSetType); + } }
- private void generateDriftChangeSet(DriftDetectionSchedule schedule) throws IOException { + private int generateDriftChangeSet(DriftDetectionSchedule schedule) throws IOException { File basedir = new File(basedir(schedule.getResourceId(), schedule.getDriftConfiguration()));
- ChangeSetWriter writer = changeSetMgr.getChangeSetWriter(schedule.getResourceId(), + ChangeSetWriter driftWriter = changeSetMgr.getChangeSetWriter(schedule.getResourceId(), new Headers(schedule.getDriftConfiguration().getName(), basedir.getAbsolutePath(), DRIFT)); + ChangeSetWriter coverageWriter = changeSetMgr.getChangeSetWriterForUpdate(schedule.getResourceId(), + new Headers(schedule.getDriftConfiguration().getName(), basedir.getAbsolutePath(), COVERAGE)); ChangeSetReader reader = changeSetMgr.getChangeSetReader(schedule.getResourceId(), schedule.getDriftConfiguration().getName());
+ int changes = 0; + for (DirectoryEntry dirEntry : reader) { DirectoryAnalyzer analyzer = new DirectoryAnalyzer(basedir, dirEntry); analyzer.run();
- DirectoryEntry newDirEntry = new DirectoryEntry(dirEntry.getDirectory()); - for (FileEntry entry : analyzer.getFilesAdded()) { - newDirEntry.add(entry); - } - for (FileEntry entry : analyzer.getFilesRemoved()) { - newDirEntry.add(entry); - } - for (FileEntry entry : analyzer.getFilesChanged()) { - newDirEntry.add(entry); + if (analyzer.getFilesAdded().size() > 0 || analyzer.getFilesRemoved().size() > 0 || + analyzer.getFilesChanged().size() > 0) { + DirectoryEntry driftDirEntry = new DirectoryEntry(dirEntry.getDirectory()); + DirectoryEntry coverageDirEntry = new DirectoryEntry(dirEntry.getDirectory()); + + // add new files to the directory entry + for (FileEntry entry : analyzer.getFilesAdded()) { + driftDirEntry.add(entry); + ++changes; + coverageDirEntry.add(entry); + } + + // add removed files to the directory entry + for (FileEntry entry : analyzer.getFilesRemoved()) { + driftDirEntry.add(entry); + ++changes; + dirEntry.remove(entry); + } + + // add changed files to the directory entry + for (FileEntry entry : analyzer.getFilesChanged()) { + driftDirEntry.add(entry); + ++changes; + dirEntry.remove(entry); + coverageDirEntry.add(entry); + } + + // add the remaining unchanged files to the coverage entry + for (FileEntry entry : dirEntry) { + coverageDirEntry.add(entry); + } + + driftWriter.writeDirectoryEntry(driftDirEntry); + coverageWriter.writeDirectoryEntry(coverageDirEntry); + } else { + coverageWriter.writeDirectoryEntry(dirEntry); } - writer.writeDirectoryEntry(newDirEntry); } - writer.close(); + driftWriter.close(); + coverageWriter.close(); + changeSetMgr.updateChangeSet(schedule.getResourceId(), new Headers(schedule.getDriftConfiguration().getName(), + basedir.getAbsolutePath(), COVERAGE)); + + return changes; }
private void generateCoverageChangeSet(DriftDetectionSchedule schedule) throws IOException { diff --git a/modules/core/plugin-container/src/test/java/org/rhq/core/pc/drift/DriftDetectorTest.java b/modules/core/plugin-container/src/test/java/org/rhq/core/pc/drift/DriftDetectorTest.java index 9b66324..1666646 100644 --- a/modules/core/plugin-container/src/test/java/org/rhq/core/pc/drift/DriftDetectorTest.java +++ b/modules/core/plugin-container/src/test/java/org/rhq/core/pc/drift/DriftDetectorTest.java @@ -197,11 +197,21 @@ public class DriftDetectorTest extends DriftTest { scheduleQueue.enqueue(new DriftDetectionSchedule(resourceId(), config)); detector.run();
- File changeSet = changeSet(config.getName(), DRIFT); + File driftChangeSet = changeSet(config.getName(), DRIFT);
- assertTrue(changeSet.exists(), "Expected to find drift change set " + changeSet.getPath()); - assertHeaderEquals(changeSet, new Headers(config.getName(), resourceDir.getAbsolutePath(), DRIFT)); - assertChangeSetContainsDirEntry(changeSet, new DirectoryEntry("conf") + assertTrue(driftChangeSet.exists(), "Expected to find drift change set " + driftChangeSet.getPath()); + + // verify that the drift change set was generated + assertHeaderEquals(driftChangeSet, new Headers(config.getName(), resourceDir.getAbsolutePath(), DRIFT)); + assertChangeSetContainsDirEntry(driftChangeSet, new DirectoryEntry("conf") + .add(addedFileEntry("server-2.conf", sha256(server2Conf)))); + + File coverageChangeSet = changeSet(config.getName(), COVERAGE); + + // verify that the coverage change set was updated + assertHeaderEquals(coverageChangeSet, new Headers(config.getName(), resourceDir.getAbsolutePath(), COVERAGE)); + assertChangeSetContainsDirEntry(coverageChangeSet, new DirectoryEntry("conf") + .add(addedFileEntry("server-1.conf", sha256(server1Conf))) .add(addedFileEntry("server-2.conf", sha256(server2Conf)))); }
@@ -236,16 +246,21 @@ public class DriftDetectorTest extends DriftTest { scheduleQueue.enqueue(new DriftDetectionSchedule(resourceId(), config)); detector.run();
- File changeSet = changeSet(config.getName(), DRIFT); + File driftChangeSet = changeSet(config.getName(), DRIFT);
- assertTrue(changeSet.exists(), "Expected to find drift change set " + changeSet.getPath()); - assertHeaderEquals(changeSet, new Headers(config.getName(), resourceDir.getAbsolutePath(), DRIFT)); - assertChangeSetContainsDirEntry(changeSet, new DirectoryEntry("conf") + // verify that the drift change set was generated + assertTrue(driftChangeSet.exists(), "Expected to find drift change set " + driftChangeSet.getPath()); + assertHeaderEquals(driftChangeSet, new Headers(config.getName(), resourceDir.getAbsolutePath(), DRIFT)); + assertChangeSetContainsDirEntry(driftChangeSet, new DirectoryEntry("conf") .add(removedFileEntry("server-2.conf", server2ConfHash))); - }
- //@SuppressWarnings("unchecked") + // verify that the coverage change set was updated + File coverageChangeSet = changeSet(config.getName(), COVERAGE);
+ assertHeaderEquals(coverageChangeSet, new Headers(config.getName(), resourceDir.getAbsolutePath(), COVERAGE)); + assertChangeSetContainsDirEntry(coverageChangeSet, new DirectoryEntry("conf") + .add(addedFileEntry("server-1.conf", sha256(server1Conf)))); + }
void assertHeaderEquals(File changeSet, Headers expected) throws Exception { ChangeSetReader reader = new ChangeSetReaderImpl(new BufferedReader(new FileReader(changeSet)));
commit 75983c3b3b86b3b798b209bc001be6399c115311 Author: John Sanda jsanda@redhat.com Date: Wed Jul 6 14:16:15 2011 -0400
First, rough cut at generating drift change sets
Some of the code is particularly rough because it is going to be refactored soon as a new change set file format is introduced. The file format will be more similar to that used by bundles where files are not grouped by directories. I am trying not to do any more than I have to in order to get things working so that I can start the refactoring.
This commit also adds logic in DriftDetector to check whether the schedule pulled off the queue is ready to be processed. If not, it is put back on the queue, and DriftDetector simply returns.
This commit does not include logic for updating the coverage change set when drift is detected.
diff --git a/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/ChangeSetManagerImpl.java b/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/ChangeSetManagerImpl.java index 9fba34e..c412bcb 100644 --- a/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/ChangeSetManagerImpl.java +++ b/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/ChangeSetManagerImpl.java @@ -28,8 +28,11 @@ public class ChangeSetManagerImpl implements ChangeSetManager {
@Override public boolean changeSetExists(int resourceId, Headers headers) throws IOException { - File file = findChangeSet(resourceId, headers.getDriftConfigurationName()); - return file != null && file.exists(); + File changeSetDir = findChangeSetDir(resourceId, headers.getDriftConfigurationName()); + if (changeSetDir == null || !changeSetDir.exists()) { + return false; + } + return new File(changeSetDir, "changeset.txt").exists(); }
@Override @@ -46,7 +49,12 @@ public class ChangeSetManagerImpl implements ChangeSetManager {
@Override public File findChangeSet(int resourceId, String name, DriftChangeSetCategory type) { - File changeSetDir = findChangeSetDir(resourceId, name); + File resourceDir = new File(changeSetsDir, Integer.toString(resourceId)); + File changeSetDir = new File(resourceDir, name); + if (!changeSetDir.exists()) { + changeSetDir.mkdirs(); + } + switch (type) { case COVERAGE: return new File(changeSetDir, "changeset.txt"); @@ -71,15 +79,19 @@ public class ChangeSetManagerImpl implements ChangeSetManager {
@Override public ChangeSetWriter getChangeSetWriter(int resourceId, Headers headers) throws IOException { -// File resourceDir = new File(changeSetsDir, Integer.toString(resourceId)); -// File changeSetDir = new File(resourceDir, headers.getDriftConfigurationName()); -// -// if (!changeSetDir.exists()) { -// changeSetDir.mkdirs(); -// } -// -// return new ChangeSetWriterImpl(new File(changeSetDir, "changeset.txt"), headers); - File changeSet = findChangeSet(resourceId, headers.getDriftConfigurationName(), headers.getType()); + File resourceDir = new File(changeSetsDir, Integer.toString(resourceId)); + File changeSetDir = new File(resourceDir, headers.getDriftConfigurationName()); + + if (!changeSetDir.exists()) { + changeSetDir.mkdirs(); + } + + File changeSet; + if (headers.getType() == COVERAGE) { + changeSet = new File(changeSetDir, "changeset.txt"); + } else { + changeSet = new File(changeSetDir, "drift-changeset.txt"); + } return new ChangeSetWriterImpl(changeSet, headers); }
@@ -97,11 +109,4 @@ public class ChangeSetManagerImpl implements ChangeSetManager {
}
- private String relativePath(File basedir, File file) { - if (basedir.equals(file)) { - return basedir.getPath(); - } - return FilenameUtils.getName(basedir.getAbsolutePath()) + separator + - file.getAbsolutePath().substring(basedir.getAbsolutePath().length() + 1); - } } diff --git a/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DirectoryAnalyzer.java b/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DirectoryAnalyzer.java index 140e777..8f4dfa3 100644 --- a/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DirectoryAnalyzer.java +++ b/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DirectoryAnalyzer.java @@ -14,6 +14,7 @@ import org.rhq.common.drift.FileEntry; import org.rhq.core.util.MessageDigestGenerator;
import static org.rhq.common.drift.FileEntry.addedFileEntry; +import static org.rhq.common.drift.FileEntry.changedFileEntry; import static org.rhq.common.drift.FileEntry.removedFileEntry;
public class DirectoryAnalyzer { @@ -49,22 +50,34 @@ public class DirectoryAnalyzer {
public void run() throws IOException { File dir = new File(basedir, dirEntry.getDirectory()); + // TODO handle directories Set<String> files = fileNames(dir.listFiles()); Map<String, FileEntry> fileEntries = createFileEntriesMap(); + List<String> processed = new ArrayList<String>();
Set<String> dirEntryFileNames = dirEntryFileNames();
for (String file : files) { if (!fileEntries.containsKey(file)) { filesAdded.add(addedFileEntry(file, sha256(new File(dir, file)))); + processed.add(file); } } + files.removeAll(processed);
for (String file : fileEntries.keySet()) { if (!files.contains(file)) { filesRemoved.add(removedFileEntry(file, fileEntries.get(file).getNewSHA())); } } + + for (String file : files) { + FileEntry entry = fileEntries.get(file); + String currentHash = sha256(new File(dir, file)); + if (!currentHash.equals(entry.getNewSHA())) { + filesChanged.add(changedFileEntry(file, entry.getNewSHA(), currentHash)); + } + } }
private Set<String> fileNames(File... files) { diff --git a/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DriftClient.java b/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DriftClient.java index aca1a2f..0f4f7cf 100644 --- a/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DriftClient.java +++ b/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DriftClient.java @@ -2,6 +2,7 @@ package org.rhq.core.pc.drift;
import java.io.File;
+import org.rhq.core.domain.drift.DriftChangeSetCategory; import org.rhq.core.domain.drift.DriftConfiguration;
/** @@ -10,7 +11,7 @@ import org.rhq.core.domain.drift.DriftConfiguration; */ public interface DriftClient {
- void sendChangeSetToServer(int resourceId, DriftConfiguration driftConfiguration); + void sendChangeSetToServer(int resourceId, DriftConfiguration driftConfiguration, DriftChangeSetCategory type);
void sendChangeSetContentToServer(int resourceId, String driftConfigurationName, File contentDir);
diff --git a/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DriftDetectionSchedule.java b/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DriftDetectionSchedule.java index e7d9534..0184782 100644 --- a/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DriftDetectionSchedule.java +++ b/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DriftDetectionSchedule.java @@ -24,6 +24,10 @@ public class DriftDetectionSchedule implements Comparable<DriftDetectionSchedule return driftConfig; }
+ public long getNextScan() { + return nextScan; + } + public void updateShedule() { nextScan = System.currentTimeMillis() + (driftConfig.getInterval() * 60 * 1000); } diff --git a/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DriftDetector.java b/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DriftDetector.java index 653bf6f..e57d763 100644 --- a/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DriftDetector.java +++ b/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DriftDetector.java @@ -3,8 +3,6 @@ package org.rhq.core.pc.drift; import java.io.File; import java.io.IOException; import java.util.Collection; -import java.util.HashSet; -import java.util.Set; import java.util.Stack;
import org.apache.commons.io.DirectoryWalker; @@ -16,6 +14,7 @@ import org.rhq.common.drift.ChangeSetWriter; import org.rhq.common.drift.DirectoryEntry; import org.rhq.common.drift.FileEntry; import org.rhq.common.drift.Headers; +import org.rhq.core.domain.drift.DriftChangeSetCategory; import org.rhq.core.domain.drift.DriftConfiguration; import org.rhq.core.util.MessageDigestGenerator;
@@ -53,17 +52,22 @@ public class DriftDetector implements Runnable { return; }
+ if (schedule.getNextScan() > (System.currentTimeMillis() + 100L)) { + scheduleQueue.enqueue(schedule); + return; + } + DriftConfiguration driftConfig = schedule.getDriftConfiguration(); int resourceId = schedule.getResourceId(); + DriftChangeSetCategory changeSetType = null;
try { - // TODO add logic to determine if there is an existing changeset - // if there is no previous changeset then we need to generate the initial - // coverage changeset if (changeSetMgr.changeSetExists(schedule.getResourceId(), new Headers(driftConfig.getName(), basedir(resourceId, driftConfig), COVERAGE))) { + changeSetType = DRIFT; generateDriftChangeSet(schedule); } else { + changeSetType = COVERAGE; generateCoverageChangeSet(schedule); } } catch (IOException e) { @@ -73,23 +77,34 @@ public class DriftDetector implements Runnable {
schedule.updateShedule(); scheduleQueue.enqueue(schedule); - driftClient.sendChangeSetToServer(schedule.getResourceId(), driftConfig); + driftClient.sendChangeSetToServer(schedule.getResourceId(), driftConfig, changeSetType); }
private void generateDriftChangeSet(DriftDetectionSchedule schedule) throws IOException { -// File basedir = new File(basedir(schedule.getResourceId(), schedule.getDriftConfiguration())); -// -// ChangeSetWriter writer = changeSetMgr.getChangeSetWriter(schedule.getResourceId(), -// new Headers(schedule.getDriftConfiguration().getName(), basedir.getAbsolutePath(), DRIFT)); -// ChangeSetReader reader = changeSetMgr.getChangeSetReader(schedule.getResourceId(), -// schedule.getDriftConfiguration().getName()); -// -// for (DirectoryEntry dirEntry : reader) { -// File dir = new File(basedir, dirEntry.getDirectory()); -// if (dir.list().length > dirEntry.getNumberOfFiles()) { -// -// } -// } + File basedir = new File(basedir(schedule.getResourceId(), schedule.getDriftConfiguration())); + + ChangeSetWriter writer = changeSetMgr.getChangeSetWriter(schedule.getResourceId(), + new Headers(schedule.getDriftConfiguration().getName(), basedir.getAbsolutePath(), DRIFT)); + ChangeSetReader reader = changeSetMgr.getChangeSetReader(schedule.getResourceId(), + schedule.getDriftConfiguration().getName()); + + for (DirectoryEntry dirEntry : reader) { + DirectoryAnalyzer analyzer = new DirectoryAnalyzer(basedir, dirEntry); + analyzer.run(); + + DirectoryEntry newDirEntry = new DirectoryEntry(dirEntry.getDirectory()); + for (FileEntry entry : analyzer.getFilesAdded()) { + newDirEntry.add(entry); + } + for (FileEntry entry : analyzer.getFilesRemoved()) { + newDirEntry.add(entry); + } + for (FileEntry entry : analyzer.getFilesChanged()) { + newDirEntry.add(entry); + } + writer.writeDirectoryEntry(newDirEntry); + } + writer.close(); }
private void generateCoverageChangeSet(DriftDetectionSchedule schedule) throws IOException { diff --git a/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DriftManager.java b/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DriftManager.java index be93ed5..8bd9da1 100644 --- a/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DriftManager.java +++ b/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DriftManager.java @@ -20,6 +20,7 @@ import org.apache.commons.logging.LogFactory; import org.rhq.common.drift.Headers; import org.rhq.core.clientapi.agent.drift.DriftAgentService; import org.rhq.core.clientapi.server.drift.DriftServerService; +import org.rhq.core.domain.drift.DriftChangeSetCategory; import org.rhq.core.domain.drift.DriftConfiguration; import org.rhq.core.domain.drift.DriftFile; import org.rhq.core.domain.resource.Resource; @@ -83,9 +84,10 @@ public class DriftManager extends AgentService implements DriftAgentService, Dri }
@Override - public void sendChangeSetToServer(int resourceId, DriftConfiguration driftConfiguration) { + public void sendChangeSetToServer(int resourceId, DriftConfiguration driftConfiguration, + DriftChangeSetCategory type) { try { - File changeSetFile = changeSetMgr.findChangeSet(resourceId, driftConfiguration.getName()); + File changeSetFile = changeSetMgr.findChangeSet(resourceId, driftConfiguration.getName(), type); if (changeSetFile == null) { log .warn("changeset[resourceId: " + resourceId + ", driftConfiguration: " diff --git a/modules/core/plugin-container/src/test/java/org/rhq/core/pc/drift/DirectoryAnalyzerTest.java b/modules/core/plugin-container/src/test/java/org/rhq/core/pc/drift/DirectoryAnalyzerTest.java index df05a15..8f9588f 100644 --- a/modules/core/plugin-container/src/test/java/org/rhq/core/pc/drift/DirectoryAnalyzerTest.java +++ b/modules/core/plugin-container/src/test/java/org/rhq/core/pc/drift/DirectoryAnalyzerTest.java @@ -1,22 +1,17 @@ package org.rhq.core.pc.drift;
import java.io.File; -import java.util.Arrays; -import java.util.Collections; -import java.util.Set; -import java.util.TreeSet;
import org.testng.annotations.Test;
import org.rhq.common.drift.DirectoryEntry; -import org.rhq.common.drift.FileEntry; import org.rhq.core.domain.drift.DriftConfiguration; -import org.rhq.test.AssertUtils;
import static java.util.Arrays.asList; -import static java.util.Collections.addAll; import static org.apache.commons.io.FileUtils.touch; +import static org.apache.commons.io.FileUtils.writeLines; import static org.rhq.common.drift.FileEntry.addedFileEntry; +import static org.rhq.common.drift.FileEntry.changedFileEntry; import static org.rhq.common.drift.FileEntry.removedFileEntry; import static org.rhq.core.domain.drift.DriftChangeSetCategory.COVERAGE; import static org.rhq.test.AssertUtils.assertCollectionMatchesNoOrder; @@ -91,4 +86,36 @@ public class DirectoryAnalyzerTest extends DriftTest { assertCollectionMatchesNoOrder(asList(removedFileEntry("server-2.conf", server2ConfHash)), analyzer.getFilesRemoved(), "Failed to detect removed file."); } + + @SuppressWarnings("unchecked") + @Test + public void detectChangedFile() throws Exception { + DriftConfiguration config = driftConfiguration("changed-files-test", resourceDir.getAbsolutePath()); + File confDir = mkdir(resourceDir, "conf"); + File server1Conf = new File(confDir, "server-1.conf"); + touch(server1Conf); + + String server1ConfHash = sha256(server1Conf); + File changeSetDir = changeSetDir(config.getName()); + + // generate the initial, coverage changes set + writeChangeSet(changeSetDir, + config.getName(), + resourceDir.getAbsolutePath(), + COVERAGE.code(), + "conf 1", + sha256(server1Conf) + " 0 server-1.conf A", + "" + ); + + // create some drift + writeLines(server1Conf, asList("This is a test", "Testing a changed file")); + + DirectoryAnalyzer analyzer = new DirectoryAnalyzer(resourceDir, new DirectoryEntry("conf") + .add(addedFileEntry("server-1.conf", server1ConfHash))); + analyzer.run(); + + assertCollectionMatchesNoOrder(asList(changedFileEntry("server-1.conf", server1ConfHash, sha256(server1Conf))), + analyzer.getFilesChanged(), "Failed to detect changed files."); + } } diff --git a/modules/core/plugin-container/src/test/java/org/rhq/core/pc/drift/DriftClientTestStub.java b/modules/core/plugin-container/src/test/java/org/rhq/core/pc/drift/DriftClientTestStub.java index 013aaef..758fda8 100644 --- a/modules/core/plugin-container/src/test/java/org/rhq/core/pc/drift/DriftClientTestStub.java +++ b/modules/core/plugin-container/src/test/java/org/rhq/core/pc/drift/DriftClientTestStub.java @@ -2,6 +2,7 @@ package org.rhq.core.pc.drift;
import java.io.File;
+import org.rhq.core.domain.drift.DriftChangeSetCategory; import org.rhq.core.domain.drift.DriftConfiguration;
class DriftClientTestStub implements DriftClient { @@ -9,7 +10,9 @@ class DriftClientTestStub implements DriftClient { private File basedir;
@Override - public void sendChangeSetToServer(int resourceId, DriftConfiguration driftConfiguration) { + public void sendChangeSetToServer(int resourceId, DriftConfiguration driftConfiguration, + DriftChangeSetCategory type) { + }
@Override diff --git a/modules/core/plugin-container/src/test/java/org/rhq/core/pc/drift/DriftDetectorTest.java b/modules/core/plugin-container/src/test/java/org/rhq/core/pc/drift/DriftDetectorTest.java index dca3dc6..9b66324 100644 --- a/modules/core/plugin-container/src/test/java/org/rhq/core/pc/drift/DriftDetectorTest.java +++ b/modules/core/plugin-container/src/test/java/org/rhq/core/pc/drift/DriftDetectorTest.java @@ -17,11 +17,13 @@ import org.rhq.common.drift.DirectoryEntry; import org.rhq.common.drift.FileEntry; import org.rhq.common.drift.Headers; import org.rhq.core.domain.configuration.Configuration; +import org.rhq.core.domain.drift.DriftChangeSetCategory; import org.rhq.core.domain.drift.DriftConfiguration; import org.rhq.test.AssertUtils;
import static org.apache.commons.io.FileUtils.touch; import static org.rhq.common.drift.FileEntry.addedFileEntry; +import static org.rhq.common.drift.FileEntry.removedFileEntry; import static org.rhq.core.domain.drift.DriftChangeSetCategory.COVERAGE; import static org.rhq.core.domain.drift.DriftChangeSetCategory.DRIFT; import static org.rhq.core.domain.drift.DriftConfigurationDefinition.BaseDirValueContext.fileSystem; @@ -196,9 +198,55 @@ public class DriftDetectorTest extends DriftTest { detector.run();
File changeSet = changeSet(config.getName(), DRIFT); + assertTrue(changeSet.exists(), "Expected to find drift change set " + changeSet.getPath()); + assertHeaderEquals(changeSet, new Headers(config.getName(), resourceDir.getAbsolutePath(), DRIFT)); + assertChangeSetContainsDirEntry(changeSet, new DirectoryEntry("conf") + .add(addedFileEntry("server-2.conf", sha256(server2Conf)))); }
+ @SuppressWarnings("unchecked") + @Test + public void includeRemovedFileInDriftChangeSet() throws Exception { + DriftConfiguration config = driftConfiguration("file-removed-drift-test", resourceDir.getAbsolutePath()); + + File confDir = mkdir(resourceDir, "conf"); + File server1Conf = new File(confDir, "server-1.conf"); + touch(server1Conf); + File server2Conf = new File(confDir, "server-2.conf"); + touch(server2Conf); + + String server2ConfHash = sha256(server2Conf); + File changeSetDir = changeSetDir(config.getName()); + + // generate the initial, coverage change set + writeChangeSet(changeSetDir, + config.getName(), + resourceDir.getAbsolutePath(), + COVERAGE.code(), + "conf 2", + sha256(server1Conf) + " 0 server-1.conf A", + server2ConfHash + " 0 server-2.conf A", + "" + ); + + // create some drift + server2Conf.delete(); + + scheduleQueue.enqueue(new DriftDetectionSchedule(resourceId(), config)); + detector.run(); + + File changeSet = changeSet(config.getName(), DRIFT); + + assertTrue(changeSet.exists(), "Expected to find drift change set " + changeSet.getPath()); + assertHeaderEquals(changeSet, new Headers(config.getName(), resourceDir.getAbsolutePath(), DRIFT)); + assertChangeSetContainsDirEntry(changeSet, new DirectoryEntry("conf") + .add(removedFileEntry("server-2.conf", server2ConfHash))); + } + + //@SuppressWarnings("unchecked") + + void assertHeaderEquals(File changeSet, Headers expected) throws Exception { ChangeSetReader reader = new ChangeSetReaderImpl(new BufferedReader(new FileReader(changeSet))); Headers actual = reader.getHeaders(); diff --git a/modules/core/plugin-container/src/test/java/org/rhq/core/pc/drift/DriftTest.java b/modules/core/plugin-container/src/test/java/org/rhq/core/pc/drift/DriftTest.java index b555fef..97bfa45 100644 --- a/modules/core/plugin-container/src/test/java/org/rhq/core/pc/drift/DriftTest.java +++ b/modules/core/plugin-container/src/test/java/org/rhq/core/pc/drift/DriftTest.java @@ -171,8 +171,8 @@ public class DriftTest { }
protected void writeChangeSet(File changeSetDir, String... changeSet) throws Exception { - BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(new File(changeSetDir, - "changeset.txt"))); + File changeSetFile = new File(changeSetDir, "changeset.txt"); + BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(changeSetFile)); writeLines(asList(changeSet), "\n", stream); stream.close(); }
rhq-commits@lists.fedorahosted.org