Search in sources :

Example 46 with Patch

use of io.fabric8.patch.management.Patch in project fabric8 by jboss-fuse.

the class ApplyPatchMojo method execute.

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    getLog().info("Applyings patches to " + outputDirectory);
    Offline offline = new Offline(outputDirectory, new Offline.Logger() {

        @Override
        public void log(int level, String message) {
            switch(level) {
                case Offline.DEBUG:
                    getLog().debug(message);
                    break;
                case Offline.INFO:
                    getLog().info(message);
                    break;
                case Offline.WARN:
                    getLog().warn(message);
                    break;
                case Offline.ERROR:
                    getLog().error(message);
                    break;
            }
        }
    });
    try {
        for (File patch : patches) {
            getLog().info("Applying patch: " + patch);
            offline.apply(patch);
        }
    } catch (Exception e) {
        throw new MojoFailureException("Error processing patches", e);
    }
}
Also used : MojoFailureException(org.apache.maven.plugin.MojoFailureException) Offline(io.fabric8.patch.impl.Offline) File(java.io.File) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException)

Example 47 with Patch

use of io.fabric8.patch.management.Patch in project fabric8 by jboss-fuse.

the class SimulateAction method doExecute.

@Override
protected void doExecute(Service service) throws Exception {
    Patch patch = service.getPatch(patchId);
    if (patch == null) {
        throw new PatchException("Patch '" + patchId + "' not found");
    }
    if (patch.isInstalled()) {
        throw new PatchException("Patch '" + patchId + "' is already installed");
    }
    PatchResult result = service.install(patch, true);
// display(result);
}
Also used : PatchResult(io.fabric8.patch.management.PatchResult) PatchException(io.fabric8.patch.management.PatchException) Patch(io.fabric8.patch.management.Patch)

Example 48 with Patch

use of io.fabric8.patch.management.Patch in project fabric8 by jboss-fuse.

the class Offline method applyPatch.

protected void applyPatch(PatchData patch, ZipFile zipFile, File storage) throws IOException {
    log(DEBUG, "Applying patch: " + patch.getId() + " / " + patch.getDescription());
    File startupFile = new File(karafBase, "etc/startup.properties");
    File overridesFile = new File(karafBase, "etc/overrides.properties");
    List<String> startup = readLines(new File(karafBase, "etc/startup.properties"));
    List<String> overrides = readLines(overridesFile);
    List<Artifact> toExtract = new ArrayList<Artifact>();
    List<Artifact> toDelete = new ArrayList<Artifact>();
    for (String bundle : patch.getBundles()) {
        Artifact artifact = mvnurlToArtifact(bundle, true);
        if (artifact == null) {
            continue;
        }
        // Compute patch bundle version and range
        VersionRange range;
        Version oVer = VersionTable.getVersion(artifact.getVersion());
        String vr = patch.getVersionRange(bundle);
        String override;
        if (vr != null && !vr.isEmpty()) {
            override = bundle + OVERRIDE_RANGE + vr;
            range = VersionRange.parseVersionRange(vr);
        } else {
            override = bundle;
            Version v1 = new Version(oVer.getMajor(), oVer.getMinor(), 0);
            Version v2 = new Version(oVer.getMajor(), oVer.getMinor() + 1, 0);
            range = new VersionRange(false, v1, v2, true);
        }
        // Process overrides.properties
        boolean matching = false;
        boolean added = false;
        for (int i = 0; i < overrides.size(); i++) {
            String line = overrides.get(i).trim();
            if (!line.isEmpty() && !line.startsWith("#")) {
                Artifact overrideArtifact = mvnurlToArtifact(line, true);
                if (overrideArtifact != null) {
                    Version ver = VersionTable.getVersion(overrideArtifact.getVersion());
                    if (isSameButVersion(artifact, overrideArtifact) && range.contains(ver)) {
                        matching = true;
                        if (ver.compareTo(oVer) < 0) {
                            // Replace old override with the new one
                            overrides.set(i, override);
                            if (!added) {
                                log(DEBUG, "Replacing with artifact: " + override);
                                added = true;
                            }
                            // Remove old file
                            toDelete.add(overrideArtifact);
                            toExtract.remove(overrideArtifact);
                        }
                    }
                } else {
                    log(WARN, "Unable to convert to artifact: " + line);
                }
            }
        }
        // If there was not matching bundles, add it
        if (!matching) {
            overrides.add(override);
            log(DEBUG, "Adding artifact: " + override);
        }
        // Process startup.properties
        for (int i = 0; i < startup.size(); i++) {
            String line = startup.get(i).trim();
            if (!line.isEmpty() && !line.startsWith("#")) {
                int index = line.indexOf('=');
                String mvnUrl = Utils.pathToMvnurl(line.substring(0, index));
                if (mvnUrl != null) {
                    Artifact startupArtifact = mvnurlToArtifact(mvnUrl, true);
                    if (startupArtifact != null) {
                        Version ver = VersionTable.getVersion(startupArtifact.getVersion());
                        if (isSameButVersion(artifact, startupArtifact) && range.contains(ver)) {
                            matching = true;
                            // Now check versions
                            if (ver.compareTo(oVer) < 0) {
                                line = artifact.getPath() + line.substring(index);
                                startup.set(i, line);
                                log(DEBUG, "Overwriting startup.properties with: " + artifact);
                                added = true;
                            }
                        }
                    }
                }
            }
        }
        // Extract artifact
        if (!matching || added) {
            toExtract.add(artifact);
        }
    }
    // Extract / delete artifacts if needed
    if (zipFile != null) {
        for (Artifact artifact : toExtract) {
            log(DEBUG, "Extracting artifact: " + artifact);
            ZipEntry entry = zipFile.getEntry("repository/" + artifact.getPath());
            if (entry == null) {
                log(ERROR, "Could not find artifact in patch zip: " + artifact);
                continue;
            }
            File f = new File(karafBase, "system/" + artifact.getPath());
            if (!f.isFile()) {
                f.getParentFile().mkdirs();
                InputStream fis = zipFile.getInputStream(entry);
                FileOutputStream fos = new FileOutputStream(f);
                try {
                    IOUtils.copy(fis, fos);
                } finally {
                    IOUtils.closeQuietly(fis);
                    IOUtils.closeQuietly(fos);
                }
            }
        }
        for (Artifact artifact : toDelete) {
            String fileName = artifact.getPath();
            File file = new File(karafBase, "system/" + fileName);
            if (file.exists()) {
                log(DEBUG, "Removing old artifact " + artifact);
                file.delete();
            } else {
                log(WARN, "Could not find: " + file);
            }
        }
    }
    overrides = new ArrayList<String>(new HashSet<String>(overrides));
    Collections.sort(overrides);
    writeLines(overridesFile, overrides);
    writeLines(startupFile, startup);
    // update the remaining patch files (using either the patch ZIP file or the patch storage location)
    if (zipFile != null) {
        patchFiles(patch, zipFile);
    } else if (storage != null) {
        patchFiles(patch, storage);
    } else {
        throw new PatchException("Unable to update patch files: no access to patch ZIP file or patch storage location");
    }
    if (patch.getMigratorBundle() != null) {
        Artifact artifact = mvnurlToArtifact(patch.getMigratorBundle(), true);
        if (artifact != null) {
            // Copy it to the deploy dir
            File src = new File(karafBase, "system/" + artifact.getPath());
            File target = new File(new File(karafBase, "deploy"), artifact.getArtifactId() + ".jar");
            copy(src, target);
        }
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) ArrayList(java.util.ArrayList) VersionRange(org.apache.felix.utils.version.VersionRange) Utils.mvnurlToArtifact(io.fabric8.patch.management.Utils.mvnurlToArtifact) Artifact(io.fabric8.patch.management.Artifact) Artifact.isSameButVersion(io.fabric8.patch.management.Artifact.isSameButVersion) Version(org.osgi.framework.Version) FileOutputStream(java.io.FileOutputStream) PatchException(io.fabric8.patch.management.PatchException) ZipFile(java.util.zip.ZipFile) File(java.io.File) HashSet(java.util.HashSet)

Example 49 with Patch

use of io.fabric8.patch.management.Patch in project fabric8 by jboss-fuse.

the class Offline method extractPatch.

protected List<PatchData> extractPatch(ZipFile zipFile) throws IOException {
    List<PatchData> patches = new ArrayList<PatchData>();
    Enumeration<? extends ZipEntry> entries = zipFile.entries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        if (!entry.isDirectory()) {
            String entryName = entry.getName();
            if (entryName.endsWith(".patch") && !entryName.contains("/")) {
                InputStream fis = zipFile.getInputStream(entry);
                try {
                    PatchData patch = PatchData.load(fis);
                    patches.add(patch);
                } finally {
                    IOUtils.closeQuietly(fis);
                }
            }
        }
    }
    return patches;
}
Also used : PatchData(io.fabric8.patch.management.PatchData) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) ArrayList(java.util.ArrayList)

Example 50 with Patch

use of io.fabric8.patch.management.Patch in project fabric8 by jboss-fuse.

the class Presentation method displayBundleUpdates.

/**
 * Displays a table with installed (<code>install == true</code>) or rolledback bundles
 * @param updatesForBundleKeys
 * @param install is this displayed during patch install (<code>true</code>) or rollback (<code>false</code>)
 */
public static void displayBundleUpdates(Collection<BundleUpdate> updatesForBundleKeys, boolean install) {
    int l1 = "[symbolic name]".length();
    int l2 = "[version]".length();
    int l3 = install ? "[new location]".length() : "[previous location]".length();
    // updates
    int tu = 0;
    // updates as part of features
    int tuf = 0;
    // reinstalls
    int tk = 0;
    // reinstalls as part of features
    int tkf = 0;
    Map<String, BundleUpdate> map = new TreeMap<>();
    for (BundleUpdate be : updatesForBundleKeys) {
        String sn = be.getSymbolicName() == null ? "" : stripSymbolicName(be.getSymbolicName());
        if (sn.length() > l1) {
            l1 = sn.length();
        }
        if (install) {
            String version = be.getPreviousVersion() == null ? "<update>" : be.getPreviousVersion();
            if (version.length() > l2) {
                l2 = version.length();
            }
        } else {
            String version = be.getNewVersion() == null ? be.getPreviousVersion() : be.getNewVersion();
            if (version.length() > l2) {
                l2 = version.length();
            }
        }
        if (install) {
            String newLocation = be.getNewLocation() == null ? "<reinstall>" : be.getNewLocation();
            if (newLocation.length() > l3) {
                l3 = newLocation.length();
            }
        } else {
            String previousLocation = be.getPreviousLocation();
            if (previousLocation.length() > l3) {
                l3 = previousLocation.length();
            }
        }
        if (be.getNewLocation() != null) {
            if (be.isIndependent()) {
                tu++;
            } else {
                tuf++;
            }
        } else {
            if (be.isIndependent()) {
                tk++;
            } else {
                tkf++;
            }
        }
        map.put(be.getSymbolicName(), be);
    }
    if (tu > 0) {
        System.out.printf("========== Bundles to %s (%d):%n", install ? "update" : "downgrade", tu);
        System.out.printf("%-" + l1 + "s   %-" + l2 + "s   %-" + l3 + "s%n", "[symbolic name]", "[version]", install ? "[new location]" : "[previous location]");
        for (Map.Entry<String, BundleUpdate> e : map.entrySet()) {
            BundleUpdate be = e.getValue();
            if (be.isIndependent() && be.getNewLocation() != null) {
                System.out.printf("%-" + l1 + "s   %-" + l2 + "s   %-" + l3 + "s%n", be.getSymbolicName() == null ? "" : stripSymbolicName(be.getSymbolicName()), install ? (be.getPreviousVersion() == null ? "<update>" : be.getPreviousVersion()) : be.getNewVersion(), install ? be.getNewLocation() : be.getPreviousLocation());
            }
        }
    }
    if (tuf > 0) {
        System.out.printf("========== Bundles to %s as part of features or core bundles (%d):%n", install ? "update" : "downgrade", tuf);
        System.out.printf("%-" + l1 + "s   %-" + l2 + "s   %-" + l3 + "s%n", "[symbolic name]", "[version]", install ? "[new location]" : "[previous location]");
        for (Map.Entry<String, BundleUpdate> e : map.entrySet()) {
            BundleUpdate be = e.getValue();
            if (!be.isIndependent() && be.getNewLocation() != null) {
                System.out.printf("%-" + l1 + "s   %-" + l2 + "s   %-" + l3 + "s%n", be.getSymbolicName() == null ? "" : stripSymbolicName(be.getSymbolicName()), install ? be.getPreviousVersion() : be.getNewVersion(), install ? be.getNewLocation() : be.getPreviousLocation());
            }
        }
    }
    if (tk > 0) {
        System.out.printf("========== Bundles to reinstall (%d):%n", tk);
        System.out.printf("%-" + l1 + "s   %-" + l2 + "s   %-" + l3 + "s%n", "[symbolic name]", "[version]", "[location]");
        for (Map.Entry<String, BundleUpdate> e : map.entrySet()) {
            BundleUpdate be = e.getValue();
            if (be.isIndependent() && be.getNewLocation() == null) {
                System.out.printf("%-" + l1 + "s   %-" + l2 + "s   %-" + l3 + "s%n", be.getSymbolicName() == null ? "" : stripSymbolicName(be.getSymbolicName()), be.getPreviousVersion(), be.getPreviousLocation());
            }
        }
    }
    if (tkf > 0) {
        System.out.printf("========== Bundles to reinstall as part of features or core bundles (%d):%n", tkf);
        System.out.printf("%-" + l1 + "s   %-" + l2 + "s   %-" + l3 + "s%n", "[symbolic name]", "[version]", "[location]");
        for (Map.Entry<String, BundleUpdate> e : map.entrySet()) {
            BundleUpdate be = e.getValue();
            if (!be.isIndependent() && be.getNewLocation() == null) {
                System.out.printf("%-" + l1 + "s   %-" + l2 + "s   %-" + l3 + "s%n", be.getSymbolicName() == null ? "" : stripSymbolicName(be.getSymbolicName()), be.getPreviousVersion(), be.getPreviousLocation());
            }
        }
    }
    System.out.flush();
}
Also used : TreeMap(java.util.TreeMap) TreeMap(java.util.TreeMap) Map(java.util.Map) BundleUpdate(io.fabric8.patch.management.BundleUpdate)

Aggregations

File (java.io.File)54 Test (org.junit.Test)43 Git (org.eclipse.jgit.api.Git)35 PatchException (io.fabric8.patch.management.PatchException)34 Patch (io.fabric8.patch.management.Patch)30 IOException (java.io.IOException)28 GitPatchManagementServiceImpl (io.fabric8.patch.management.impl.GitPatchManagementServiceImpl)27 GitPatchRepository (io.fabric8.patch.management.impl.GitPatchRepository)26 HashMap (java.util.HashMap)18 RevCommit (org.eclipse.jgit.revwalk.RevCommit)18 LinkedList (java.util.LinkedList)17 ZipFile (org.apache.commons.compress.archivers.zip.ZipFile)17 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)17 ObjectId (org.eclipse.jgit.lib.ObjectId)17 Bundle (org.osgi.framework.Bundle)17 Version (org.osgi.framework.Version)15 PatchResult (io.fabric8.patch.management.PatchResult)13 BundleUpdate (io.fabric8.patch.management.BundleUpdate)11 PatchData (io.fabric8.patch.management.PatchData)11 ArrayList (java.util.ArrayList)11