Search in sources :

Example 6 with PatchDetailsRequest

use of org.jboss.fuse.patch.management.PatchDetailsRequest in project fuse-karaf by jboss-fuse.

the class GitPatchManagementServiceImpl method isHfChangeCommit.

/**
 * Checks whether the commit is related to HotFix patch installation.
 * Such patches are P patches that update <strong>only</strong> bundles.
 * @param rc
 * @return patchId of HF patch if one is detected
 */
private String isHfChangeCommit(RevCommit rc) {
    String msg = rc.getShortMessage();
    boolean pPatch = msg != null && msg.startsWith(MARKER_P_PATCH_INSTALLATION_PREFIX);
    if (pPatch) {
        String patchId = msg.length() > MARKER_P_PATCH_INSTALLATION_PREFIX.length() ? msg.substring(MARKER_P_PATCH_INSTALLATION_PREFIX.length()) : null;
        if (patchId != null) {
            Patch p = loadPatch(new PatchDetailsRequest(patchId));
            if (p != null && p.getPatchData() != null) {
                try {
                    boolean hfPatch = p.getPatchData().getBundles().size() > 0;
                    hfPatch &= p.getPatchData().getFeatureFiles().size() == 0;
                    hfPatch &= p.getPatchData().getFiles().size() == 0;
                    hfPatch &= p.getPatchData().getOtherArtifacts().size() == 0;
                    return patchId;
                } catch (Exception e) {
                    return null;
                }
            }
        }
    }
    return null;
}
Also used : Patch(org.jboss.fuse.patch.management.Patch) ManagedPatch(org.jboss.fuse.patch.management.ManagedPatch) PatchDetailsRequest(org.jboss.fuse.patch.management.PatchDetailsRequest) PatchException(org.jboss.fuse.patch.management.PatchException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) IOException(java.io.IOException)

Example 7 with PatchDetailsRequest

use of org.jboss.fuse.patch.management.PatchDetailsRequest 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);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) BundleStartLevel(org.osgi.framework.startlevel.BundleStartLevel) PatchDetailsRequest(org.jboss.fuse.patch.management.PatchDetailsRequest) URI(java.net.URI) FeaturesService(org.apache.karaf.features.FeaturesService) BundleException(org.osgi.framework.BundleException) BundleUpdate(org.jboss.fuse.patch.management.BundleUpdate) FeatureUpdate(org.jboss.fuse.patch.management.FeatureUpdate) PatchData(org.jboss.fuse.patch.management.PatchData) Bundle(org.osgi.framework.Bundle) PatchReport(org.jboss.fuse.patch.management.PatchReport) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) URISyntaxException(java.net.URISyntaxException) PatchException(org.jboss.fuse.patch.management.PatchException) BundleException(org.osgi.framework.BundleException) IOException(java.io.IOException) PatchResult(org.jboss.fuse.patch.management.PatchResult) File(java.io.File) Patch(org.jboss.fuse.patch.management.Patch) Pending(org.jboss.fuse.patch.management.Pending)

Aggregations

Patch (org.jboss.fuse.patch.management.Patch)7 PatchDetailsRequest (org.jboss.fuse.patch.management.PatchDetailsRequest)7 PatchException (org.jboss.fuse.patch.management.PatchException)6 ManagedPatch (org.jboss.fuse.patch.management.ManagedPatch)5 IOException (java.io.IOException)4 File (java.io.File)3 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)3 PatchData (org.jboss.fuse.patch.management.PatchData)3 FileInputStream (java.io.FileInputStream)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 LinkedList (java.util.LinkedList)2 ZipFile (org.apache.commons.compress.archivers.zip.ZipFile)2 BundleUpdate (org.jboss.fuse.patch.management.BundleUpdate)2 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 Iterator (java.util.Iterator)1 LinkedHashSet (java.util.LinkedHashSet)1