use of io.fabric8.patch.management.PatchDetailsRequest in project fabric8 by jboss-fuse.
the class ServiceImpl method resumePendingPatchTasks.
/**
* Upon startup (activation), we check if there are any *.patch.pending files. if yes, we're finishing the
* installation
*/
private void resumePendingPatchTasks() throws IOException {
File[] pendingPatches = patchDir.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
return pathname.exists() && pathname.getName().endsWith(".pending");
}
});
if (pendingPatches == null || pendingPatches.length == 0) {
return;
}
for (File pending : pendingPatches) {
Pending what = Pending.valueOf(FileUtils.readFileToString(pending));
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()));
}
}
}
for (String repo : newRepositories) {
System.out.println("Restoring feature repository: " + repo);
try {
featuresService.addRepository(URI.create(repo));
} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace(System.err);
System.err.flush();
}
}
for (String f : features) {
String[] fv = f.split("\\|");
System.out.printf("Restoring feature %s/%s%n", fv[0], fv[1]);
try {
featuresService.installFeature(fv[0], fv[1]);
} 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_ROLLBACK) {
List<String> bases = patch.getResult().getKarafBases();
for (Iterator<String> iterator = bases.iterator(); iterator.hasNext(); ) {
String s = iterator.next();
if (s.startsWith(System.getProperty("karaf.name"))) {
iterator.remove();
}
}
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();
}
}
}
}
}
use of io.fabric8.patch.management.PatchDetailsRequest in project fabric8 by jboss-fuse.
the class GitPatchManagementServiceIT method installThreeNonRollupPatches.
@Test
public void installThreeNonRollupPatches() throws IOException, GitAPIException {
initializationPerformedBaselineDistributionFoundInSystem();
// prepare some ZIP patches
preparePatchZip("src/test/resources/content/patch1", "target/karaf/patches/source/patch-1.zip", false);
preparePatchZip("src/test/resources/content/patch5", "target/karaf/patches/source/patch-5.zip", false);
preparePatchZip("src/test/resources/content/patch6", "target/karaf/patches/source/patch-6.zip", false);
PatchManagement service = (PatchManagement) pm;
PatchData patchData1 = service.fetchPatches(new File("target/karaf/patches/source/patch-1.zip").toURI().toURL()).get(0);
Patch patch1 = service.trackPatch(patchData1);
PatchData patchData5 = service.fetchPatches(new File("target/karaf/patches/source/patch-5.zip").toURI().toURL()).get(0);
Patch patch5 = service.trackPatch(patchData5);
PatchData patchData6 = service.fetchPatches(new File("target/karaf/patches/source/patch-6.zip").toURI().toURL()).get(0);
Patch patch6 = service.trackPatch(patchData6);
GitPatchRepository repository = ((GitPatchManagementServiceImpl) pm).getGitPatchRepository();
Git fork = repository.cloneRepository(repository.findOrCreateMainGitRepository(), true);
String tx = service.beginInstallation(PatchKind.NON_ROLLUP);
List<BundleUpdate> patch1Updates = new LinkedList<>();
patch1Updates.add(BundleUpdate.from("mvn:io.fabric8/fabric-tranquility/1.2.0").to("mvn:io.fabric8/fabric-tranquility/1.2.3"));
service.install(tx, patch1, patch1Updates);
List<BundleUpdate> patch5Updates = new LinkedList<>();
patch5Updates.add(BundleUpdate.from("mvn:io.fabric8/fabric-zen/1.1.44/war").to("mvn:io.fabric8/fabric-zen/1.2.0/war"));
service.install(tx, patch5, patch5Updates);
List<BundleUpdate> patch6Updates = new LinkedList<>();
patch5Updates.add(BundleUpdate.from("mvn:io.fabric8/fabric-zen/1.2.4/war").to("mvn:io.fabric8/fabric-zen/1.3.0/war"));
service.install(tx, patch6, patch6Updates);
service.commitInstallation(tx);
String binAdmin = FileUtils.readFileToString(new File(karafHome, "bin/admin"));
assertTrue(binAdmin.contains("system/io/fabric8/fabric-tranquility/1.2.3/fabric-tranquility-1.2.3.jar"));
String etcStartupProperties = FileUtils.readFileToString(new File(karafHome, "etc/startup.properties"));
// version from patch-5 should be chosen, because there's 1.1.44->1.2.0
assertTrue(etcStartupProperties.contains("io/fabric8/fabric-zen/1.2.0/fabric-zen-1.2.0.war=42"));
assertTrue(etcStartupProperties.contains("io/fabric8/fabric-tranquility/1.2.3/fabric-tranquility-1.2.3.jar=42"));
String etcOverridesProperties = FileUtils.readFileToString(new File(karafHome, "etc/overrides.properties"));
assertTrue(etcOverridesProperties.contains("mvn:io.fabric8/fabric-tranquility/1.2.3\n"));
assertTrue(etcOverridesProperties.contains("mvn:io.fabric8/fabric-zen/1.2.0/war;range=[1.1,1.2)\n"));
assertTrue(etcOverridesProperties.contains("mvn:io.fabric8/fabric-zen/1.3.3/war\n"));
/* rollback time! */
Patch p5 = service.loadPatch(new PatchDetailsRequest("my-patch-5"));
service.rollback(p5.getPatchData());
binAdmin = FileUtils.readFileToString(new File(karafHome, "bin/admin"));
assertTrue(binAdmin.contains("system/io/fabric8/fabric-tranquility/1.2.3/fabric-tranquility-1.2.3.jar"));
etcStartupProperties = FileUtils.readFileToString(new File(karafHome, "etc/startup.properties"));
// rollback wasn't successful
assertTrue(etcStartupProperties.contains("io/fabric8/fabric-zen/1.2.0/fabric-zen-1.2.0.war=42"));
assertFalse(etcStartupProperties.contains("io/fabric8/fabric-zen/1.1.44/fabric-zen-1.1.44.war=42"));
assertTrue(etcStartupProperties.contains("io/fabric8/fabric-tranquility/1.2.3/fabric-tranquility-1.2.3.jar=42"));
etcOverridesProperties = FileUtils.readFileToString(new File(karafHome, "etc/overrides.properties"));
assertTrue(etcOverridesProperties.contains("mvn:io.fabric8/fabric-tranquility/1.2.3\n"));
assertTrue(etcOverridesProperties.contains("mvn:io.fabric8/fabric-zen/1.2.0/war;range=[1.1,1.2)\n"));
assertTrue(etcOverridesProperties.contains("mvn:io.fabric8/fabric-zen/1.3.3/war\n"));
Patch p6 = service.loadPatch(new PatchDetailsRequest("my-patch-6"));
service.rollback(p6.getPatchData());
binAdmin = FileUtils.readFileToString(new File(karafHome, "bin/admin"));
assertTrue(binAdmin.contains("system/io/fabric8/fabric-tranquility/1.2.3/fabric-tranquility-1.2.3.jar"));
etcStartupProperties = FileUtils.readFileToString(new File(karafHome, "etc/startup.properties"));
assertTrue(etcStartupProperties.contains("io/fabric8/fabric-zen/1.2.0/fabric-zen-1.2.0.war=42"));
assertTrue(etcStartupProperties.contains("io/fabric8/fabric-tranquility/1.2.3/fabric-tranquility-1.2.3.jar=42"));
etcOverridesProperties = FileUtils.readFileToString(new File(karafHome, "etc/overrides.properties"));
assertFalse(etcOverridesProperties.contains("mvn:io.fabric8/fabric-zen/1.3.3/war\n"));
repository.closeRepository(fork, true);
}
Aggregations