use of io.fabric8.patch.management.BundleUpdate in project fabric8 by jboss-fuse.
the class GitPatchManagementServiceImpl method install.
@Override
public void install(String transaction, Patch patch, List<BundleUpdate> bundleUpdatesInThisPatch) {
transactionIsValid(transaction, patch);
Git fork = pendingTransactions.get(transaction);
try {
switch(pendingTransactionsTypes.get(transaction)) {
case ROLLUP:
{
Activator.log2(LogService.LOG_INFO, String.format("Installing rollup patch \"%s\"", patch.getPatchData().getId()));
// we can install only one rollup patch within single transaction
// and it is equal to cherry-picking all user changes on top of transaction branch
// after cherry-picking the commit from the rollup patch branch
// rollup patches do their own update to startup.properties
// we're operating on patch branch, HEAD of the patch branch points to the baseline
ObjectId since = fork.getRepository().resolve("HEAD^{commit}");
// we'll pick all user changes between baseline and main patch branch without P installations
ObjectId to = fork.getRepository().resolve(gitPatchRepository.getMainBranchName() + "^{commit}");
Iterable<RevCommit> mainChanges = fork.log().addRange(since, to).call();
List<RevCommit> userChanges = new LinkedList<>();
// gather lines of HF patches - patches that have *only* bundle updates
// if any of HF patches provide newer version of artifact than currently installed R patch,
// we will leave the relevant line in etc/overrides.properties
List<String> hfChanges = new LinkedList<>();
for (RevCommit rc : mainChanges) {
if (isUserChangeCommit(rc)) {
userChanges.add(rc);
} else {
String hfPatchId = isHfChangeCommit(rc);
if (hfPatchId != null) {
hfChanges.addAll(gatherOverrides(hfPatchId, patch));
}
}
}
String patchRef = patch.getManagedPatch().getCommitId();
if (env == EnvType.STANDALONE_CHILD) {
// we're in a slightly different situation:
// - patch was patch:added in root container
// - its main commit should be used when patching full Fuse/AMQ container
// - it created "side" commits (with tags) for this case of patching admin:create based containers
// - those tags are stored in special patch-info.txt file within patch' commit
String patchInfo = gitPatchRepository.getFileContent(fork, patchRef, "patch-info.txt");
if (patchInfo != null) {
BufferedReader reader = new BufferedReader(new StringReader(patchInfo));
String line = null;
while ((line = reader.readLine()) != null) {
if (line.startsWith("#")) {
continue;
}
Pattern p = Pattern.compile(env.getBaselineTagFormat().replace("%s", "(.*)"));
if (p.matcher(line).matches()) {
// this means we have another commit/tag that we should chery-pick as a patch
// for this standalone child container
patchRef = line.trim();
}
}
} else {
// hmm, we actually can't patch standalone child container then...
Activator.log2(LogService.LOG_WARNING, String.format("Can't install rollup patch \"%s\" in admin container - no information about admin container patch", patch.getPatchData().getId()));
return;
}
}
if (env == EnvType.STANDALONE) {
// pick the rollup patch
fork.cherryPick().include(fork.getRepository().resolve(patchRef)).setNoCommit(true).call();
gitPatchRepository.prepareCommit(fork, String.format(MARKER_R_PATCH_INSTALLATION_PATTERN, patch.getPatchData().getId())).call();
} else if (env == EnvType.STANDALONE_CHILD) {
// rebase on top of rollup patch
fork.reset().setMode(ResetCommand.ResetType.HARD).setRef("refs/tags/" + patchRef + "^{commit}").call();
}
// next commit - reset overrides.properties - this is 2nd step of installing rollup patch
// we are doing it even if the commit is going to be empty - this is the same step as after
// creating initial baseline
resetOverrides(fork.getRepository().getWorkTree(), hfChanges);
fork.add().addFilepattern("etc/overrides.properties").call();
RevCommit c = gitPatchRepository.prepareCommit(fork, String.format(MARKER_R_PATCH_RESET_OVERRIDES_PATTERN, patch.getPatchData().getId())).call();
if (env == EnvType.STANDALONE) {
// tag the new rollup patch as new baseline
String newFuseVersion = determineVersion(fork.getRepository().getWorkTree(), "fuse");
fork.tag().setName(String.format(EnvType.STANDALONE.getBaselineTagFormat(), newFuseVersion)).setObjectId(c).call();
}
// reapply those user changes that are not conflicting
// for each conflicting cherry-pick we do a backup of user files, to be able to restore them
// when rollup patch is rolled back
ListIterator<RevCommit> it = userChanges.listIterator(userChanges.size());
int prefixSize = Integer.toString(userChanges.size()).length();
int count = 1;
while (it.hasPrevious()) {
RevCommit userChange = it.previous();
String prefix = String.format("%0" + prefixSize + "d-%s", count++, userChange.getName());
CherryPickResult result = fork.cherryPick().include(userChange).setNoCommit(true).call();
// ENTESB-5492: remove etc/overrides.properties if there is such file left from old patch
// mechanism
File overrides = new File(fork.getRepository().getWorkTree(), "etc/overrides.properties");
if (overrides.isFile()) {
// version of some bundles, overrides.properties should be kept
if (!(hfChanges.size() > 0 && overrides.length() > 0)) {
overrides.delete();
fork.rm().addFilepattern("etc/overrides.properties").call();
}
}
// if there's conflict here, prefer patch version (which is "ours" (first) in this case)
handleCherryPickConflict(patch.getPatchData().getPatchDirectory(), fork, result, userChange, false, PatchKind.ROLLUP, prefix, true, false);
// always commit even empty changes - to be able to restore user changes when rolling back
// rollup patch.
// commit has the original commit id appended to the message.
// when we rebase on OLDER baseline (rollback) we restore backed up files based on this
// commit id (from patches/patch-id.backup/number-commit directory)
String newMessage = userChange.getFullMessage() + "\n\n";
newMessage += prefix;
gitPatchRepository.prepareCommit(fork, newMessage).call();
// we may have unadded changes - when file mode is changed
fork.reset().setMode(ResetCommand.ResetType.HARD).call();
}
// finally - let's get rid of all the tags related to non-rollup patches installed between
// previous baseline and previous HEAD, because installing rollup patch makes all previous P
// patches obsolete
RevWalk walk = new RevWalk(fork.getRepository());
RevCommit c1 = walk.parseCommit(since);
RevCommit c2 = walk.parseCommit(to);
Map<String, RevTag> tags = gitPatchRepository.findTagsBetween(fork, c1, c2);
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();
}
}
break;
}
case NON_ROLLUP:
{
Activator.log2(LogService.LOG_INFO, String.format("Installing non-rollup patch \"%s\"", patch.getPatchData().getId()));
// simply cherry-pick patch commit to transaction branch
// non-rollup patches require manual change to artifact references in all files
// pick the non-rollup patch
RevCommit commit = new RevWalk(fork.getRepository()).parseCommit(fork.getRepository().resolve(patch.getManagedPatch().getCommitId()));
CherryPickResult result = fork.cherryPick().include(commit).setNoCommit(true).call();
handleCherryPickConflict(patch.getPatchData().getPatchDirectory(), fork, result, commit, true, PatchKind.NON_ROLLUP, null, true, false);
// there are several files in ${karaf.home} that need to be changed together with patch
// commit, to make them reference updated bundles (paths, locations, ...)
updateFileReferences(fork, patch.getPatchData(), bundleUpdatesInThisPatch);
updateOverrides(fork.getRepository().getWorkTree(), patch.getPatchData());
fork.add().addFilepattern(".").call();
// always commit non-rollup patch
RevCommit c = gitPatchRepository.prepareCommit(fork, String.format(MARKER_P_PATCH_INSTALLATION_PATTERN, patch.getPatchData().getId())).call();
// we may have unadded changes - when file mode is changed
fork.reset().setMode(ResetCommand.ResetType.HARD).call();
// tag the installed patch (to easily rollback and to prevent another installation)
String tagName = String.format("patch-%s", patch.getPatchData().getId().replace(' ', '-'));
if (env == EnvType.STANDALONE_CHILD) {
tagName += "-" + gitPatchRepository.getStandaloneChildkarafName();
}
fork.tag().setName(tagName).setObjectId(c).call();
break;
}
}
} catch (IOException | GitAPIException e) {
throw new PatchException(e.getMessage(), e);
}
}
use of io.fabric8.patch.management.BundleUpdate 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.BundleUpdate 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);
}
use of io.fabric8.patch.management.BundleUpdate in project fabric8 by jboss-fuse.
the class ProtectedTest method updateBinAdminReferences.
@Test
public void updateBinAdminReferences() throws IOException {
File binAdmin = new File(git.getRepository().getWorkTree(), "bin/admin");
FileUtils.copyFile(new File("src/test/resources/files/bin/admin"), binAdmin);
List<BundleUpdate> bundleUpdates = new LinkedList<>();
bundleUpdates.add(BundleUpdate.from("mvn:org.apache.karaf.admin/org.apache.karaf.admin.command/2.4.0.redhat-620133").to("mvn:org.apache.karaf.admin/org.apache.karaf.admin.command/2.4.0.redhat-620134"));
bundleUpdates.add(BundleUpdate.from("mvn:org.apache.felix/org.apache.felix.gogo.runtime/0.12.1").to("mvn:org.apache.felix/org.apache.felix.gogo.runtime/1.12.1"));
Map<String, String> updates = Utils.collectLocationUpdates(bundleUpdates);
new GitPatchManagementServiceImpl(context).updateReferences(git, "bin/admin", "system/", updates, false);
String expected = FileUtils.readFileToString(new File("src/test/resources/files/bin/admin.updated"));
String changed = FileUtils.readFileToString(binAdmin);
assertThat(changed, equalTo(expected));
}
use of io.fabric8.patch.management.BundleUpdate in project fabric8 by jboss-fuse.
the class ProtectedTest method updateEtcStartupReferences.
@Test
public void updateEtcStartupReferences() throws IOException {
File etcConfig = new File(git.getRepository().getWorkTree(), "etc/startup.properties");
FileUtils.copyFile(new File("src/test/resources/files/etc/startup.properties"), etcConfig);
List<BundleUpdate> bundleUpdates = new LinkedList<>();
bundleUpdates.add(BundleUpdate.from("mvn:org.ops4j.pax.url/pax-url-wrap/2.4.0/jar/uber").to("mvn:org.ops4j.pax.url/pax-url-wrap/2.4.2/jar/uber"));
bundleUpdates.add(BundleUpdate.from("file:/opt/karaf/system/org/ops4j/pax/url/pax-url-aether/2.4.0/pax-url-aether-2.4.0.jar").to("file:/opt/karaf/system/org/ops4j/pax/url/pax-url-aether/2.4.2/pax-url-aether-2.4.2.jar"));
bundleUpdates.add(BundleUpdate.from("mvn:org.apache.felix/org.apache.felix.bundlerepository/2.0.4").to("mvn:org.apache.felix/org.apache.felix.bundlerepository/2.1.4"));
Map<String, String> updates = Utils.collectLocationUpdates(bundleUpdates);
new GitPatchManagementServiceImpl(context).updateReferences(git, "etc/startup.properties", "", updates, false);
String expected = FileUtils.readFileToString(new File("src/test/resources/files/etc/startup.properties.updated"));
String changed = FileUtils.readFileToString(etcConfig);
assertThat(changed, equalTo(expected));
}
Aggregations