Search in sources :

Example 6 with PatchDetailsRequest

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();
                }
            }
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) BundleStartLevel(org.osgi.framework.startlevel.BundleStartLevel) PatchData(io.fabric8.patch.management.PatchData) Bundle(org.osgi.framework.Bundle) PatchDetailsRequest(io.fabric8.patch.management.PatchDetailsRequest) FileInputStream(java.io.FileInputStream) URISyntaxException(java.net.URISyntaxException) PatchException(io.fabric8.patch.management.PatchException) BundleException(org.osgi.framework.BundleException) IOException(java.io.IOException) PatchResult(io.fabric8.patch.management.PatchResult) BundleException(org.osgi.framework.BundleException) FileFilter(java.io.FileFilter) File(java.io.File) Patch(io.fabric8.patch.management.Patch) Pending(io.fabric8.patch.management.Pending) BundleUpdate(io.fabric8.patch.management.BundleUpdate) FeatureUpdate(io.fabric8.patch.management.FeatureUpdate)

Example 7 with PatchDetailsRequest

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);
}
Also used : GitPatchManagementServiceImpl(io.fabric8.patch.management.impl.GitPatchManagementServiceImpl) Git(org.eclipse.jgit.api.Git) GitPatchRepository(io.fabric8.patch.management.impl.GitPatchRepository) File(java.io.File) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Aggregations

Patch (io.fabric8.patch.management.Patch)6 ManagedPatch (io.fabric8.patch.management.ManagedPatch)5 PatchDetailsRequest (io.fabric8.patch.management.PatchDetailsRequest)5 PatchException (io.fabric8.patch.management.PatchException)5 File (java.io.File)4 IOException (java.io.IOException)4 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)3 PatchData (io.fabric8.patch.management.PatchData)2 Pending (io.fabric8.patch.management.Pending)2 FileFilter (java.io.FileFilter)2 FileInputStream (java.io.FileInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 LinkedList (java.util.LinkedList)2 ZipFile (org.apache.commons.compress.archivers.zip.ZipFile)2 Git (org.eclipse.jgit.api.Git)2 Bundle (org.osgi.framework.Bundle)2 Artifact (io.fabric8.patch.management.Artifact)1 Artifact.isSameButVersion (io.fabric8.patch.management.Artifact.isSameButVersion)1 BundleUpdate (io.fabric8.patch.management.BundleUpdate)1 FeatureUpdate (io.fabric8.patch.management.FeatureUpdate)1