use of org.jboss.fuse.patch.management.PatchDetailsRequest in project fuse-karaf by jboss-fuse.
the class ShowCommand method doExecute.
@Override
protected void doExecute(PatchService service) throws Exception {
Patch patch = patchManagement.loadPatch(new PatchDetailsRequest(patchId, bundles, files, diff));
if (patch == null) {
throw new PatchException("Patch '" + patchId + "' not found");
}
System.out.println(String.format("Patch ID: %s", patch.getPatchData().getId()));
if (patch.getManagedPatch() != null) {
System.out.println(String.format("Patch Commit ID: %s", patch.getManagedPatch().getCommitId()));
}
if (bundles) {
System.out.println(String.format("#### %d Bundles%s", patch.getPatchData().getBundles().size(), patch.getPatchData().getBundles().size() == 0 ? "" : ":"));
iterate(patch.getPatchData().getBundles());
}
if (files) {
ManagedPatch details = patch.getManagedPatch();
System.out.println(String.format("#### %d Files added%s", details.getFilesAdded().size(), details.getFilesAdded().size() == 0 ? "" : ":"));
iterate(details.getFilesAdded());
System.out.println(String.format("#### %d Files modified%s", details.getFilesModified().size(), details.getFilesModified().size() == 0 ? "" : ":"));
iterate(details.getFilesModified());
System.out.println(String.format("#### %d Files removed%s", details.getFilesRemoved().size(), details.getFilesRemoved().size() == 0 ? "" : ":"));
iterate(details.getFilesRemoved());
}
if (diff) {
System.out.println("#### Patch changes:\n" + patch.getManagedPatch().getUnifiedDiff());
}
}
use of org.jboss.fuse.patch.management.PatchDetailsRequest in project fuse-karaf by jboss-fuse.
the class GitPatchManagementServiceImpl method checkPendingPatches.
@Override
public void checkPendingPatches() {
File[] pendingPatches = patchesDir.listFiles(pathname -> pathname.exists() && pathname.getName().endsWith(".pending"));
if (pendingPatches == null || pendingPatches.length == 0) {
return;
}
final String dataCache = systemContext.getProperty("org.osgi.framework.storage");
for (File pending : pendingPatches) {
try {
Pending what = Pending.valueOf(FileUtils.readFileToString(pending, "UTF-8"));
final String prefix = what == Pending.ROLLUP_INSTALLATION ? "install" : "rollback";
String name = pending.getName().replaceFirst("\\.pending$", "");
if (isStandaloneChild()) {
if (name.endsWith("." + System.getProperty("karaf.name") + ".patch")) {
name = name.replaceFirst("\\." + System.getProperty("karaf.name"), "");
} else {
continue;
}
}
File patchFile = new File(pending.getParentFile(), name);
if (!patchFile.isFile()) {
Activator.log(LogService.LOG_INFO, "Ignoring patch result file: " + patchFile.getName());
continue;
}
PatchData patchData = PatchData.load(new FileInputStream(patchFile));
Patch patch = loadPatch(new PatchDetailsRequest(patchData.getId()));
String dataFilesName = patchData.getId() + ".datafiles";
if (isStandaloneChild()) {
dataFilesName = patchData.getId() + "." + System.getProperty("karaf.name") + ".datafiles";
}
final File dataFilesBackupDir = new File(pending.getParentFile(), dataFilesName);
final Properties backupProperties = new Properties();
FileInputStream inStream = new FileInputStream(new File(dataFilesBackupDir, "backup-" + prefix + ".properties"));
backupProperties.load(inStream);
Utils.closeQuietly(inStream);
// maybe one of those bundles has data directory to restore?
for (Bundle b : systemContext.getBundles()) {
if (b.getSymbolicName() != null) {
String key = String.format("%s$$%s", stripSymbolicName(b.getSymbolicName()), b.getVersion().toString());
if (backupProperties.containsKey(key)) {
String backupDirName = backupProperties.getProperty(key);
File backupDir = new File(dataFilesBackupDir, prefix + "/" + backupDirName + "/data");
restoreDataDirectory(dataCache, b, backupDir);
// we no longer want to restore this dir
backupProperties.remove(key);
}
}
}
// 2. We can however have more bundle data backups - we'll restore them after each bundle
// is INSTALLED and we'll use listener for this
BundleListener bundleListener = new SynchronousBundleListener() {
@Override
public void bundleChanged(BundleEvent event) {
Bundle b = event.getBundle();
if (event.getType() == BundleEvent.INSTALLED && b.getSymbolicName() != null) {
String key = String.format("%s$$%s", stripSymbolicName(b.getSymbolicName()), b.getVersion().toString());
if (backupProperties.containsKey(key)) {
String backupDirName = backupProperties.getProperty(key);
File backupDir = new File(dataFilesBackupDir, prefix + "/" + backupDirName + "/data");
restoreDataDirectory(dataCache, b, backupDir);
}
}
}
};
systemContext.addBundleListener(bundleListener);
pendingPatchesListeners.put(patchData.getId(), bundleListener);
} catch (Exception e) {
Activator.log(LogService.LOG_ERROR, null, e.getMessage(), e, true);
}
}
}
use of org.jboss.fuse.patch.management.PatchDetailsRequest in project fuse-karaf by jboss-fuse.
the class DeleteCommand method doExecute.
@Override
protected void doExecute(PatchService service) throws Exception {
Patch patch = patchManagement.loadPatch(new PatchDetailsRequest(patchId));
if (patch == null) {
throw new PatchException("Patch '" + patchId + "' not found");
}
if (patch.getResult() != null && patch.getResult().getKarafBases().size() > 0) {
throw new PatchException("Patch '" + patchId + "' can't be deleted, as it's installed in these containers: " + patch.getResult().getKarafBases().stream().map(kb -> kb.contains("|") ? kb.split("\\s*\\|\\s*")[0] : kb).collect(Collectors.joining(", ")));
}
patchManagement.delete(patch);
System.out.println("Patch '" + patchId + "' was successfully deleted");
}
use of org.jboss.fuse.patch.management.PatchDetailsRequest in project fuse-karaf by jboss-fuse.
the class GitPatchManagementServiceImpl method rollback.
@Override
public void rollback(PatchData patchData) {
Git fork = null;
try {
fork = gitPatchRepository.cloneRepository(gitPatchRepository.findOrCreateMainGitRepository(), true);
Ref installationBranch = null;
PatchKind kind = patchData.isRollupPatch() ? PatchKind.ROLLUP : PatchKind.NON_ROLLUP;
switch(kind) {
case ROLLUP:
{
Activator.log2(LogService.LOG_INFO, String.format("Rolling back rollup patch \"%s\"", patchData.getId()));
// rolling back a rollup patch should rebase (cherry-pick) all user commits done after current baseline
// to previous baseline
RevTag currentBaseline = gitPatchRepository.findCurrentBaseline(fork);
RevCommit c1 = new RevWalk(fork.getRepository()).parseCommit(fork.getRepository().resolve(currentBaseline.getTagName() + "^{commit}"));
// remember the commit to discover P patch tags installed on top of rolledback baseline
RevCommit since = c1;
RevCommit c2 = new RevWalk(fork.getRepository()).parseCommit(fork.getRepository().resolve("HEAD"));
RevCommit to = c2;
Iterable<RevCommit> mainChangesSinceRollupPatch = fork.log().addRange(c1, c2).call();
List<RevCommit> userChanges = new LinkedList<>();
for (RevCommit rc : mainChangesSinceRollupPatch) {
if (isUserChangeCommit(rc)) {
userChanges.add(rc);
}
}
if (env == EnvType.STANDALONE) {
// remove the tag
fork.tagDelete().setTags(currentBaseline.getTagName()).call();
}
// baselines are stacked on each other
RevTag previousBaseline = gitPatchRepository.findNthPreviousBaseline(fork, env == EnvType.STANDALONE ? 0 : 1);
c1 = new RevWalk(fork.getRepository()).parseCommit(fork.getRepository().resolve(previousBaseline.getTagName() + "^{commit}"));
// hard reset of main patch branch - to point to other branch, originating from previous baseline
fork.reset().setMode(ResetCommand.ResetType.HARD).setRef(previousBaseline.getTagName() + "^{commit}").call();
// reapply those user changes that are not conflicting
ListIterator<RevCommit> it = userChanges.listIterator(userChanges.size());
Status status = fork.status().call();
if (!status.isClean()) {
// unstage any garbage
fork.reset().setMode(ResetCommand.ResetType.MIXED).call();
for (String p : status.getModified()) {
gitPatchRepository.checkout(fork).addPath(p).call();
}
}
while (it.hasPrevious()) {
RevCommit userChange = it.previous();
CherryPickResult cpr = fork.cherryPick().include(userChange.getId()).setNoCommit(true).call();
// this time prefer user change on top of previous baseline - this change shouldn't be
// conflicting, because when rolling back, patch change was preferred over user change
handleCherryPickConflict(patchData.getPatchDirectory(), fork, cpr, userChange, true, PatchKind.ROLLUP, null, false, true);
// restore backed up content from the reapplied user change
String[] commitMessage = userChange.getFullMessage().split("\n\n");
if (commitMessage.length > 1) {
// we have original commit (that had conflicts) stored in this commit's full message
String ref = commitMessage[commitMessage.length - 1];
File backupDir = new File(patchesDir, patchData.getId() + ".backup");
if (isStandaloneChild()) {
backupDir = new File(patchesDir, patchData.getId() + "." + System.getProperty("karaf.name") + ".backup");
}
backupDir = new File(backupDir, ref);
if (backupDir.exists() && backupDir.isDirectory()) {
Activator.log2(LogService.LOG_DEBUG, String.format("Restoring content of %s", backupDir.getCanonicalPath()));
copyManagedDirectories(backupDir, karafBase, false, false, false);
}
}
gitPatchRepository.prepareCommit(fork, userChange.getFullMessage()).call();
}
// rollback should not lead to restoration of old patch management features
String productVersion = determineVersion(fork.getRepository().getWorkTree());
File featuresCfg = new File(fork.getRepository().getWorkTree(), "etc/org.apache.karaf.features.cfg");
if (featuresCfg.isFile()) {
if (setCurrentPatchManagementVersion(featuresCfg, productVersion)) {
// artificial updates to etc/startup.properties
String pmNew = String.format("mvn:org.jboss.fuse.modules.patch/patch-management/%s", bundleContext.getBundle().getVersion().toString());
String pmOld = String.format("mvn:org.jboss.fuse.modules.patch/patch-management/%s", productVersion);
BundleUpdate update = new BundleUpdate(null, null, pmNew, null, pmOld);
List<BundleUpdate> patchManagementUpdates = Collections.singletonList(update);
updateReferences(fork, "etc/startup.properties", "", Utils.collectLocationUpdates(patchManagementUpdates));
fork.add().addFilepattern("etc/org.apache.karaf.features.cfg").addFilepattern("etc/startup.properties").call();
gitPatchRepository.prepareCommit(fork, String.format(MARKER_BASELINE_REPLACE_PATCH_FEATURE_PATTERN, productVersion, bundleContext.getBundle().getVersion().toString())).call();
}
}
gitPatchRepository.push(fork);
if (env == EnvType.STANDALONE) {
// remove remote tag
fork.push().setRefSpecs(new RefSpec().setSource(null).setDestination("refs/tags/" + currentBaseline.getTagName())).call();
}
// remove tags related to non-rollup patches installed between
// rolled back baseline and previous HEAD, because rolling back to previous rollup patch
// (previous baseline) equal effectively to starting from fresh baseline
RevWalk walk = new RevWalk(fork.getRepository());
Map<String, RevTag> tags = gitPatchRepository.findTagsBetween(fork, since, to);
for (Map.Entry<String, RevTag> entry : tags.entrySet()) {
if (entry.getKey().startsWith("patch-")) {
fork.tagDelete().setTags(entry.getKey()).call();
fork.push().setRefSpecs(new RefSpec().setSource(null).setDestination("refs/tags/" + entry.getKey())).call();
// and remove karaf base from tracked patch result, or even remove the result itself
String patchId = entry.getKey().substring("patch-".length());
Patch patch = loadPatch(new PatchDetailsRequest(patchId));
if (patch != null && patch.getResult() != null) {
boolean removed = false;
for (Iterator<String> iterator = patch.getResult().getKarafBases().iterator(); iterator.hasNext(); ) {
String base = iterator.next();
if (base.contains("|")) {
String[] kb = base.split("\\s*\\|\\s*");
String containerId = kb[0];
if (System.getProperty("karaf.name", "").equals(containerId)) {
iterator.remove();
removed = true;
break;
}
}
}
if (removed) {
if (patch.getResult().getKarafBases().size() == 0) {
// just remove the result entirely
new File(patch.getPatchData().getPatchLocation(), patchId + ".patch.result").delete();
} else {
patch.getResult().store();
}
if (isStandaloneChild()) {
File file = new File(patch.getPatchData().getPatchLocation(), patchId + "." + System.getProperty("karaf.name") + ".patch.result");
if (file.isFile()) {
file.delete();
}
}
}
}
}
}
// HEAD of main patch branch after reset and cherry-picks
c2 = new RevWalk(fork.getRepository()).parseCommit(fork.getRepository().resolve("HEAD"));
// applyChanges(fork, c1, c2);
applyChanges(fork, false);
break;
}
case NON_ROLLUP:
{
Activator.log2(LogService.LOG_INFO, String.format("Rolling back non-rollup patch \"%s\"", patchData.getId()));
// rolling back a non-rollup patch is a revert of the patch commit and removal of patch tag
String patchTagName = String.format("patch-%s", env == EnvType.STANDALONE ? patchData.getId() : patchData.getId() + "-" + gitPatchRepository.getStandaloneChildkarafName());
ObjectId oid = fork.getRepository().resolve(patchTagName);
if (oid == null) {
throw new PatchException(String.format("Can't find installed patch (tag %s is missing)", patchTagName));
}
RevCommit commit = new RevWalk(fork.getRepository()).parseCommit(oid);
RevertCommand revertCommand = fork.revert().include(commit);
RevCommit reverted = revertCommand.call();
if (reverted == null) {
List<String> unmerged = revertCommand.getUnmergedPaths();
Activator.log2(LogService.LOG_WARNING, "Problem rolling back patch \"" + patchData.getId() + "\". The following files where updated later:");
for (String path : unmerged) {
Activator.log2(LogService.LOG_WARNING, " - " + path);
}
RevWalk walk = new RevWalk(fork.getRepository());
RevCommit head = walk.parseCommit(fork.getRepository().resolve("HEAD"));
Map<String, RevTag> tags = gitPatchRepository.findTagsBetween(fork, commit, head);
List<RevTag> laterPatches = new LinkedList<>();
if (tags.size() > 0) {
for (Map.Entry<String, RevTag> tag : tags.entrySet()) {
if (tag.getKey().startsWith("patch-")) {
laterPatches.add(tag.getValue());
}
}
Activator.log2(LogService.LOG_INFO, "The following patches were installed after \"" + patchData.getId() + "\":");
for (RevTag t : laterPatches) {
String message = " - " + t.getTagName().substring("patch-".length());
RevObject object = walk.peel(t);
if (object != null) {
RevCommit c = walk.parseCommit(object.getId());
String date = GitPatchRepository.FULL_DATE.format(new Date(c.getCommitTime() * 1000L));
message += " (" + date + ")";
}
Activator.log2(LogService.LOG_INFO, message);
}
}
return;
}
// TODO: should we restore the backup possibly created when instalilng P patch?
// remove the tag
fork.tagDelete().setTags(patchTagName).call();
gitPatchRepository.push(fork);
// remove remote tag
fork.push().setRefSpecs(new RefSpec().setSource(null).setDestination(String.format("refs/tags/%s", patchTagName))).call();
// HEAD of main patch branch after reset and cherry-picks
RevCommit c = new RevWalk(fork.getRepository()).parseCommit(fork.getRepository().resolve("HEAD"));
applyChanges(fork, c.getParent(0), c);
break;
}
}
} catch (IOException | GitAPIException e) {
throw new PatchException(e.getMessage(), e);
} finally {
if (fork != null) {
gitPatchRepository.closeRepository(fork, true);
}
}
}
use of org.jboss.fuse.patch.management.PatchDetailsRequest in project fuse-karaf by jboss-fuse.
the class GitPatchManagementServiceImpl method gatherOverrides.
/**
* Returns list of bundle updates (maven coordinates) from HF/P patch that should be preserved during
* installation of R patch
* @param hfPatchId ID of patch that was detected to be HF patch installed previously (before R patch just being installed)
* @param patch R patch which is currently being installed
* @return an artificial {@link PatchData} with a list of maven URIs for bundles that are newer in previous P-patches than the ones in currently installed R-patch
*/
private PatchData gatherOverrides(String hfPatchId, Patch patch) {
Patch hf = loadPatch(new PatchDetailsRequest(hfPatchId));
List<String> bundles = new LinkedList<>();
Map<String, String> ranges = new LinkedHashMap<>();
if (hf != null && hf.getPatchData() != null) {
for (String bundle : hf.getPatchData().getBundles()) {
bundles.add(bundle);
String versionRange = hf.getPatchData().getVersionRange(bundle);
if (versionRange != null && !versionRange.trim().equals("")) {
ranges.put(bundle, versionRange);
}
}
// leave only these artifacts that are in newer version than in R patch being installed
if (patch != null && patch.getPatchData() != null) {
Map<String, Artifact> cache = new HashMap<>();
for (String bu : patch.getPatchData().getBundles()) {
Artifact rPatchArtifact = Utils.mvnurlToArtifact(bu, true);
if (rPatchArtifact != null) {
cache.put(String.format("%s:%s", rPatchArtifact.getGroupId(), rPatchArtifact.getArtifactId()), rPatchArtifact);
}
}
for (String bu : hf.getPatchData().getBundles()) {
Artifact hfPatchArtifact = Utils.mvnurlToArtifact(bu, true);
if (hfPatchArtifact != null) {
String key = String.format("%s:%s", hfPatchArtifact.getGroupId(), hfPatchArtifact.getArtifactId());
if (cache.containsKey(key)) {
Version hfVersion = Utils.getOsgiVersion(hfPatchArtifact.getVersion());
Version rVersion = Utils.getOsgiVersion(cache.get(key).getVersion());
if (rVersion.compareTo(hfVersion) >= 0) {
bundles.remove(bu);
ranges.remove(bu);
}
}
}
}
}
}
return new PatchData(null, null, bundles, null, ranges, null, null);
}
Aggregations