use of org.jboss.fuse.patch.management.PatchReport in project fuse-karaf by jboss-fuse.
the class PatchServiceImpl method install.
/**
* <p>Main installation method. Installing a patch in standalone mode is a matter of correct merge (cherry-pick, merge,
* rebase) of patch branch into <code>master</code> branch.</p>
* <p>Static changes are handled by git, runtime changes (bundles, features) are handled depending on patch type:<ul>
* <li>Rollup: clear OSGi bundle cache, reinstall features that were installed after restart</li>
* <li>Non-Rollup: update bundles, generate overrides.properties and update scripts to reference new versions</li>
* </ul></p>
* <p>For Rollup patches we don't update bundles - we clear the bundle cache instead.</p>
* @param patches
* @param simulate
* @param synchronous
* @return
*/
private Map<String, PatchResult> install(final Collection<Patch> patches, final boolean simulate, boolean synchronous) {
PatchKind kind = checkConsistency(patches);
checkPrerequisites(patches);
checkStandaloneChild(patches);
String transaction = null;
try {
// Compute individual patch results (patchId -> Result)
final Map<String, PatchResult> results = new LinkedHashMap<String, PatchResult>();
// current state of the framework
Bundle[] allBundles = bundleContext.getBundles();
// bundle -> url to update the bundle from (used for non-rollup patch)
final Map<Bundle, String> bundleUpdateLocations = new HashMap<>();
/* A "key" is name + "update'able version". Such version is current version with micro version == 0 */
// [symbolic name|updateable-version] -> newest update for the bundle out of all installed patches
final Map<String, BundleUpdate> updatesForBundleKeys = new LinkedHashMap<>();
// [feature name|updateable-version] -> newest update for the feature out of all installed patches
final Map<String, FeatureUpdate> updatesForFeatureKeys = new LinkedHashMap<>();
final List<String> overridesForFeatureKeys = new LinkedList<>();
// symbolic name -> version -> location
final BundleVersionHistory history = createBundleVersionHistory();
// beginning installation transaction = creating of temporary branch in git
transaction = this.patchManagement.beginInstallation(kind);
// bundles from etc/startup.properties + felix.framework = all bundles not managed by features
// these bundles will be treated in special way
// symbolic name -> Bundle
final Map<String, Bundle> coreBundles = helper.getCoreBundles(allBundles);
// runtime info is prepared to apply runtime changes and static info is prepared to update KARAF_HOME files
for (Patch patch : patches) {
List<FeatureUpdate> featureUpdatesInThisPatch = null;
List<String> featureOverridesInThisPatch = null;
if (kind == PatchKind.ROLLUP) {
// list of feature updates for the current patch
featureUpdatesInThisPatch = featureUpdatesInPatch(patch, updatesForFeatureKeys, kind);
helper.sortFeatureUpdates(featureUpdatesInThisPatch);
} else {
// list of feature overrides (new Karaf 4.2 feature override mechanism)
// this is collected for the purpose of summary, not to collect information needed
// for actual override
featureOverridesInThisPatch = featureOverridesInPatch(patch, kind);
overridesForFeatureKeys.addAll(featureOverridesInThisPatch);
}
// list of bundle updates for the current patch - for ROLLUP patch, we minimize the list of bundles
// to "restore" (install after clearing data/cache) by not including bundles that are
// already updated as part of fueatures update
List<BundleUpdate> bundleUpdatesInThisPatch = bundleUpdatesInPatch(patch, allBundles, bundleUpdateLocations, history, updatesForBundleKeys, kind, coreBundles, featureUpdatesInThisPatch);
// prepare patch result before doing runtime changes
PatchResult result = null;
if (patch.getResult() != null) {
result = patch.getResult();
if (patchManagement.isStandaloneChild()) {
// ENTESB-5120: "result" is actually a result of patch installation in root container
// we need dedicated result for admin:create based child container
PatchResult childResult = new PatchResult(patch.getPatchData(), simulate, System.currentTimeMillis(), bundleUpdatesInThisPatch, featureUpdatesInThisPatch, featureOverridesInThisPatch, result);
result.addChildResult(System.getProperty("karaf.name"), childResult);
}
} else {
result = new PatchResult(patch.getPatchData(), simulate, System.currentTimeMillis(), bundleUpdatesInThisPatch, featureUpdatesInThisPatch, featureOverridesInThisPatch);
}
result.getKarafBases().add(String.format("%s | %s", System.getProperty("karaf.name"), System.getProperty("karaf.base")));
results.put(patch.getPatchData().getId(), result);
patch.setResult(result);
// each patch may change files, we're not updating the main files yet - it'll be done when
// install transaction is committed
patchManagement.install(transaction, patch, bundleUpdatesInThisPatch);
}
// One special case
if (kind == PatchKind.NON_ROLLUP) {
// for rollup patch, this bundle will be installed from scratch
for (Map.Entry<Bundle, String> entry : bundleUpdateLocations.entrySet()) {
Bundle bundle = entry.getKey();
if (bundle.getSymbolicName() != null && "org.ops4j.pax.url.mvn".equals(stripSymbolicName(bundle.getSymbolicName()))) {
// handle this bundle specially - update it here
URL location = new URL(entry.getValue());
System.out.printf("Special update of bundle \"%s\" from \"%s\"%n", bundle.getSymbolicName(), location);
if (!simulate) {
update(bundle, location);
bundle.start();
}
// replace location - to be stored in result
bundleUpdateLocations.put(bundle, location.toString());
}
}
}
if (kind == PatchKind.ROLLUP) {
Presentation.displayFeatureUpdates(updatesForFeatureKeys.values(), true);
} else {
Presentation.displayFeatureOverrides(overridesForFeatureKeys, true);
}
// effectively, we will update all the bundles from this list - even if some bundles will be "updated"
// as part of feature installation
Presentation.displayBundleUpdates(updatesForBundleKeys.values(), true);
// then required repositories, features and bundles will be reinstalled
if (kind == PatchKind.ROLLUP) {
if (!simulate) {
if (patches.size() == 1) {
Patch patch = patches.iterator().next();
PatchResult result = results.get(patch.getPatchData().getId());
// single shot
if (patchManagement.isStandaloneChild()) {
backupService.backupDataFiles(result.getChildPatches().get(System.getProperty("karaf.name")), Pending.ROLLUP_INSTALLATION);
} else {
backupService.backupDataFiles(result, Pending.ROLLUP_INSTALLATION);
}
for (Bundle b : coreBundles.values()) {
if (b.getSymbolicName() != null && Utils.stripSymbolicName(b.getSymbolicName()).equals("org.apache.felix.fileinstall")) {
b.stop(Bundle.STOP_TRANSIENT);
break;
}
}
// update KARAF_HOME
patchManagement.commitInstallation(transaction);
if (patchManagement.isStandaloneChild()) {
result.getChildPatches().get(System.getProperty("karaf.name")).setPending(Pending.ROLLUP_INSTALLATION);
} else {
result.setPending(Pending.ROLLUP_INSTALLATION);
}
result.store();
// Some updates need a full JVM restart.
if (isJvmRestartNeeded(results)) {
boolean handlesFullRestart = Boolean.getBoolean("karaf.restart.jvm.supported");
if (handlesFullRestart) {
System.out.println("Rollup patch " + patch.getPatchData().getId() + " installed. Restarting Karaf..");
// KARAF-5179 - we need both properties set to true
System.setProperty("karaf.restart", "true");
System.setProperty("karaf.restart.jvm", "true");
} else {
System.out.println("Rollup patch " + patch.getPatchData().getId() + " installed. Shutting down Karaf, please restart...");
}
} else {
// We don't need a JVM restart, so lets just do a OSGi framework restart
System.setProperty("karaf.restart", "true");
}
File karafData = new File(bundleContext.getProperty("karaf.data"));
File cleanCache = new File(karafData, "clean_cache");
cleanCache.createNewFile();
Thread.currentThread().setContextClassLoader(bundleContext.getBundle(0L).adapt(BundleWiring.class).getClassLoader());
bundleContext.getBundle(0L).stop();
// stop/shutdown occurs on another thread
}
} else {
System.out.println("Simulation only - no files and runtime data will be modified.");
patchManagement.rollbackInstallation(transaction);
}
return results;
}
// update KARAF_HOME
if (!simulate) {
patchManagement.commitInstallation(transaction);
} else {
patchManagement.rollbackInstallation(transaction);
}
if (!simulate) {
Runnable task = () -> {
try {
// update bundles
applyChanges(bundleUpdateLocations);
for (String featureOverride : overridesForFeatureKeys) {
System.out.println("overriding feature: " + featureOverride);
}
if (overridesForFeatureKeys.size() > 0) {
System.out.println("refreshing features");
featuresService.refreshFeatures(EnumSet.noneOf(FeaturesService.Option.class));
}
// persist results of all installed patches
for (Patch patch : patches) {
PatchResult result = results.get(patch.getPatchData().getId());
System.out.printf("Summary of patch %s:%n", patch.getPatchData().getId());
PatchReport report = patch.getResult().getReport();
System.out.printf(" - Bundles updated: %d%n", report.getUpdatedBundles());
System.out.printf(" - Features updated: %d%n", report.getUpdatedFeatures());
System.out.printf(" - Features overriden: %d%n", report.getOverridenFeatures());
System.out.flush();
result.store();
}
} catch (Exception e) {
e.printStackTrace(System.err);
System.err.flush();
}
};
if (synchronous) {
task.run();
} else {
new Thread(task).start();
}
} else {
System.out.println("Simulation only - no files and runtime data will be modified.");
}
return results;
} catch (Exception e) {
e.printStackTrace(System.err);
System.err.flush();
if (transaction != null && patchManagement != null) {
patchManagement.rollbackInstallation(transaction);
}
throw new PatchException(e.getMessage(), e);
} finally {
System.out.flush();
}
}
use of org.jboss.fuse.patch.management.PatchReport in project fuse-karaf by jboss-fuse.
the class PatchServiceImpl method resumePendingPatchTasks.
/**
* Upon startup (activation), we check if there are any *.patch.pending files. if yes, we're finishing the
* installation
*/
private void resumePendingPatchTasks() {
LOG.info("Performing \"resume pending patch tasks\"");
try {
File[] pendingPatches = patchDir.listFiles(pathname -> pathname.exists() && pathname.getName().endsWith(".pending"));
if (pendingPatches == null || pendingPatches.length == 0) {
return;
}
for (File pending : pendingPatches) {
Pending what = Pending.valueOf(FileUtils.readFileToString(pending, "UTF-8"));
String name = pending.getName().replaceFirst("\\.pending$", "");
if (patchManagement.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()) {
System.out.println("Ignoring patch result file: " + patchFile.getName());
continue;
}
PatchData patchData = PatchData.load(new FileInputStream(patchFile));
Patch patch = patchManagement.loadPatch(new PatchDetailsRequest(patchData.getId()));
System.out.printf("Resume %s of %spatch \"%s\"%n", what == Pending.ROLLUP_INSTALLATION ? "installation" : "rollback", patch.getPatchData().isRollupPatch() ? "rollup " : "", patch.getPatchData().getId());
PatchResult result = patch.getResult();
if (patchManagement.isStandaloneChild()) {
result = result.getChildPatches().get(System.getProperty("karaf.name"));
if (result == null) {
System.out.println("Ignoring patch result file: " + patchFile.getName());
continue;
}
}
// feature time
Set<String> newRepositories = new LinkedHashSet<>();
Set<String> features = new LinkedHashSet<>();
for (FeatureUpdate featureUpdate : result.getFeatureUpdates()) {
if (featureUpdate.getName() == null && featureUpdate.getPreviousRepository() != null) {
// feature was not shipped by patch
newRepositories.add(featureUpdate.getPreviousRepository());
} else if (featureUpdate.getNewRepository() == null) {
// feature was not changed by patch
newRepositories.add(featureUpdate.getPreviousRepository());
features.add(String.format("%s|%s", featureUpdate.getName(), featureUpdate.getPreviousVersion()));
} else {
// feature was shipped by patch
if (what == Pending.ROLLUP_INSTALLATION) {
newRepositories.add(featureUpdate.getNewRepository());
features.add(String.format("%s|%s", featureUpdate.getName(), featureUpdate.getNewVersion()));
} else {
newRepositories.add(featureUpdate.getPreviousRepository());
features.add(String.format("%s|%s", featureUpdate.getName(), featureUpdate.getPreviousVersion()));
}
}
}
System.out.println("Restoring feature repositories");
for (String repo : newRepositories) {
try {
URI repositoryUri = URI.create(repo);
if (featuresService.getRepository(repositoryUri) == null) {
System.out.println("Restoring feature repository: " + repo);
featuresService.addRepository(repositoryUri);
}
} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace(System.err);
System.err.flush();
}
}
Set<String> installedFeatures = null;
try {
installedFeatures = Arrays.stream(featuresService.listInstalledFeatures()).map(f -> String.format("%s|%s", f.getName(), f.getVersion())).collect(Collectors.toSet());
} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace(System.err);
System.err.flush();
}
EnumSet<FeaturesService.Option> options = EnumSet.noneOf(FeaturesService.Option.class);
Set<String> toInstall = new LinkedHashSet<>();
System.out.println("Restoring features");
for (String f : features) {
if (installedFeatures == null || !installedFeatures.contains(f)) {
String[] fv = f.split("\\|");
String fid = String.format("%s/%s", fv[0], fv[1]);
System.out.printf("Restoring feature %s%n", fid);
toInstall.add(fid);
}
}
try {
if (!toInstall.isEmpty()) {
featuresService.installFeatures(toInstall, options);
}
System.out.println("Refreshing features service");
featuresService.refreshFeatures(options);
} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace(System.err);
System.err.flush();
}
for (BundleUpdate update : result.getBundleUpdates()) {
if (!update.isIndependent()) {
continue;
}
String location = null;
if (update.getNewVersion() == null) {
System.out.printf("Restoring bundle %s from %s%n", update.getSymbolicName(), update.getPreviousLocation());
location = update.getPreviousLocation();
} else {
if (what == Pending.ROLLUP_INSTALLATION) {
System.out.printf("Updating bundle %s from %s%n", update.getSymbolicName(), update.getNewLocation());
location = update.getNewLocation();
} else {
System.out.printf("Downgrading bundle %s from %s%n", update.getSymbolicName(), update.getPreviousLocation());
location = update.getPreviousLocation();
}
}
try {
Bundle b = bundleContext.installBundle(location);
if (update.getStartLevel() > -1) {
b.adapt(BundleStartLevel.class).setStartLevel(update.getStartLevel());
}
switch(update.getState()) {
// ?
case Bundle.UNINSTALLED:
case Bundle.INSTALLED:
case Bundle.STARTING:
case Bundle.STOPPING:
break;
case Bundle.RESOLVED:
// ?bundleContext.getBundle(0L).adapt(org.osgi.framework.wiring.FrameworkWiring.class).resolveBundles(...);
break;
case Bundle.ACTIVE:
b.start();
break;
}
} catch (BundleException e) {
System.err.println(" - " + e.getMessage());
// e.printStackTrace(System.err);
System.err.flush();
}
}
pending.delete();
System.out.printf("%spatch \"%s\" %s successfully%n", patch.getPatchData().isRollupPatch() ? "Rollup " : "", patchData.getId(), what == Pending.ROLLUP_INSTALLATION ? "installed" : "rolled back");
if (what == Pending.ROLLUP_INSTALLATION) {
System.out.printf("Summary of patch %s:%n", patch.getPatchData().getId());
PatchReport report = patch.getResult().getReport();
System.out.printf(" - Bundles updated: %d%n", report.getUpdatedBundles());
System.out.printf(" - Features updated: %d%n", report.getUpdatedFeatures());
System.out.printf(" - Features overriden: %d%n", report.getOverridenFeatures());
System.out.printf("Detailed report: %s%n", new File(patch.getPatchData().getPatchLocation(), patch.getPatchData().getId() + ".patch.result.html").getCanonicalPath());
System.out.flush();
}
if (what == Pending.ROLLUP_ROLLBACK) {
List<String> bases = patch.getResult().getKarafBases();
bases.removeIf(s -> s.startsWith(System.getProperty("karaf.name")));
result.setPending(null);
patch.getResult().store();
if (patch.getResult().getKarafBases().size() == 0) {
File file = new File(patchDir, patchData.getId() + ".patch.result");
file.delete();
}
if (patchManagement.isStandaloneChild()) {
File file = new File(patchDir, patchData.getId() + "." + System.getProperty("karaf.name") + ".patch.result");
if (file.isFile()) {
file.delete();
}
}
}
}
} catch (IOException e) {
LOG.error("Error resuming a patch: " + e.getMessage(), e);
}
}
use of org.jboss.fuse.patch.management.PatchReport in project fuse-karaf by jboss-fuse.
the class DiffUtils method generateDiffReport.
/**
* <p>Having four commits, generate single, HTML report about all modified files</p>
* <p>Please excuse inline html code.</p>
* @param patch
* @param git
* @param conflicts
* @param base
* @param ours
* @param theirs
* @param resolved
* @param result
*/
public static void generateDiffReport(Patch patch, PatchResult patchResult, Git git, Set<String> conflicts, RevCommit base, RevCommit ours, RevCommit theirs, RevCommit resolved, Writer result) throws IOException {
ObjectReader reader = git.getRepository().newObjectReader();
CanonicalTreeParser ctpBase = new CanonicalTreeParser();
CanonicalTreeParser ctpOurs = new CanonicalTreeParser();
CanonicalTreeParser ctpTheirs = new CanonicalTreeParser();
CanonicalTreeParser ctpResolved = new CanonicalTreeParser();
ctpBase.reset(reader, base.getTree());
ctpOurs.reset(reader, ours.getTree());
ctpTheirs.reset(reader, theirs.getTree());
ctpResolved.reset(reader, resolved.getTree());
// this map will contain 3 diffs for each file/path:
// 0 - diff between base and "ours" ("ours" depends on patch kind and it's really "ours" in P-Patch,
// because patch change is cherry-picked on top of custom change. In R-Patch, custom changes come after
// patch, so they're called "theirs" in diff/git terminology)
// 1 - diff between base and "theirs" (see above)
// 2 - diff between base and resolved, effective and final state of history
Map<String, DiffEntry[]> report = new LinkedHashMap<>();
// 1. base -> ours
TreeWalk walk = new TreeWalk(reader);
walk.addTree(ctpBase);
walk.addTree(ctpOurs);
walk.setRecursive(true);
List<DiffEntry> diffs = DiffEntry.scan(walk);
diffs.forEach(de -> collect(report, de, 0));
// 2. base -> theirs
walk.reset();
ctpBase.reset(reader, base.getTree());
walk.addTree(ctpBase);
walk.addTree(ctpTheirs);
walk.setRecursive(true);
diffs = DiffEntry.scan(walk);
diffs.forEach(de -> collect(report, de, 1));
// 3. base -> resolved
walk.reset();
ctpBase.reset(reader, base.getTree());
walk.addTree(ctpBase);
walk.addTree(ctpResolved);
walk.setRecursive(true);
diffs = DiffEntry.scan(walk);
diffs.forEach(de -> collect(report, de, 2));
// report generation
PatchData pd = patchResult.getPatchData();
result.write(reportHeader.replace("@PATCH_ID@", pd.getId()));
PatchReport pr = patchResult.getReport();
result.write("<table class=\"summary\">\n" + " <tr>\n" + " <td class=\"f\">Patch ID:</td><td>" + pr.getId() + "</td>\n" + " </tr>\n" + " <tr>\n" + " <td class=\"f\">Patch type:</td><td>" + (pr.isRollup() ? "rollup" : "non-rollup") + "</td>\n" + " </tr>\n" + " <tr>\n" + " <td class=\"f\">Installation date:</td><td>" + DATE.format(pr.getTimestamp()) + "</td>\n" + " </tr>\n" + " <tr>\n" + " <td class=\"f\">Bundles updated</td><td>" + pr.getUpdatedBundles() + "</td>\n" + " </tr>\n" + " <tr>\n" + " <td class=\"f\">Features updated</td><td>" + pr.getUpdatedFeatures() + "</td>\n" + " </tr>\n" + " <tr>\n" + " <td class=\"f\">Features overriden</td><td>" + pr.getOverridenFeatures() + "</td>\n" + " </tr>\n" + " <tr>\n" + " <td class=\"f\">File conflicts</td><td>" + conflicts.size() + "</td>\n" + " </tr>\n" + " </table>\n" + "</div>\n");
if (conflicts.size() > 0) {
result.write("<h1 class=\"header\">\n" + " <div>Conflicting files</div>\n" + "</h1>\n");
}
for (Map.Entry<String, DiffEntry[]> e : report.entrySet()) {
if (!conflicts.contains(e.getKey())) {
// we don't care about diffs that aren't really conflicts
continue;
}
result.write(fileHeader1);
result.write(e.getKey());
result.write(fileHeader2);
// we have max 3 entries (not all entries may be present
result.write("<td class=\"side\">\n" + " <div class=\"header\">Custom version</div>\n" + " <div class=\"content" + (e.getValue()[0] != null ? "" : " empty") + "\">");
if (e.getValue()[0] != null) {
// custom change
diff(git, reader, e.getValue()[0], result);
} else {
result.write("No change");
}
result.write("</div>\n" + " </td>");
result.write("<td class=\"side\">\n" + " <div class=\"header\">Patch</div>\n" + " <div class=\"content" + (e.getValue()[1] != null ? "" : " empty") + "\">");
if (e.getValue()[1] != null) {
// patch change
diff(git, reader, e.getValue()[1], result);
} else {
result.write("No change");
}
result.write("</div>\n" + " </td>");
result.write("<td class=\"side\">\n" + " <div class=\"header\">Final version</div>\n" + " <div class=\"content" + (e.getValue()[2] != null ? "" : " empty") + "\">");
if (e.getValue()[2] != null) {
// effective change - should always be available
// or maybe not when both patch and user removed the file?
diff(git, reader, e.getValue()[2], result);
} else {
result.write("No change");
}
result.write("</div>\n" + " </td>");
result.write(fileFooter);
}
result.write(reportFooter);
}
Aggregations