Search in sources :

Example 1 with BundledPatch

use of org.jboss.as.patching.metadata.BundledPatch in project wildfly-core by wildfly.

the class PatchBundleUnitTestCase method prepare.

/**
 * Prepare the multi patch bundle.
 *
 * @param root  the temp dir root
 * @param steps the individual steps
 * @return the prepared content
 * @throws PatchingException
 */
protected File prepare(File root, PatchingTestStepBuilder... steps) throws PatchingException {
    final File tempDir = new File(root, randomString());
    tempDir.mkdir();
    final List<BundledPatch.BundledPatchEntry> entries = new ArrayList<BundledPatch.BundledPatchEntry>();
    for (final PatchingTestStepBuilder step : steps) {
        // Prepare the patches.
        final Patch patch = step.build();
        writePatch(step.getPatchDir(), patch);
        final String patchId = patch.getPatchId();
        final String path = patchId + ".zip";
        final File patchOutput = new File(tempDir, path);
        ZipUtils.zip(step.getPatchDir(), patchOutput);
        entries.add(new BundledPatch.BundledPatchEntry(patchId, path));
    }
    final File multiPatchXml = new File(tempDir, PatchBundleXml.MULTI_PATCH_XML);
    try {
        final OutputStream os = new FileOutputStream(multiPatchXml);
        try {
            PatchBundleXml.marshal(os, new BundledPatch() {

                @Override
                public List<BundledPatchEntry> getPatches() {
                    return entries;
                }
            });
        } finally {
            safeClose(os);
        }
    } catch (Exception e) {
        throw new PatchingException(e);
    }
    final File result = new File(root, "multi-step-contents.zip");
    ZipUtils.zip(tempDir, result);
    return result;
}
Also used : PatchingException(org.jboss.as.patching.PatchingException) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) ArrayList(java.util.ArrayList) TestUtils.randomString(org.jboss.as.patching.runner.TestUtils.randomString) BundledPatch(org.jboss.as.patching.metadata.BundledPatch) IOException(java.io.IOException) PatchingException(org.jboss.as.patching.PatchingException) FileOutputStream(java.io.FileOutputStream) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File) BundledPatch(org.jboss.as.patching.metadata.BundledPatch) Patch(org.jboss.as.patching.metadata.Patch)

Example 2 with BundledPatch

use of org.jboss.as.patching.metadata.BundledPatch in project wildfly-core by wildfly.

the class PatchToolImpl method applyPatchBundle.

protected PatchingResult applyPatchBundle(final File workDir, final BundledPatch bundledPatch, final ContentVerificationPolicy contentPolicy) throws PatchingException, IOException {
    final List<BundledPatchEntry> patches = bundledPatch.getPatches();
    if (patches.isEmpty()) {
        throw new PatchingException(PatchLogger.ROOT_LOGGER.patchBundleIsEmpty());
    }
    PatchingResult result = null;
    BundledPatchEntry lastCommittedEntry = null;
    final List<BundledPatch.BundledPatchEntry> results = new ArrayList<BundledPatch.BundledPatchEntry>(patches.size());
    final List<InstalledIdentity> installedIdentities = manager.getInstalledIdentities();
    for (BundledPatchEntry entry : patches) {
        // TODO this has to be checked against the specific one targeted by the patch
        boolean alreadyApplied = false;
        for (InstalledIdentity identity : installedIdentities) {
            if (identity.getAllInstalledPatches().contains(entry.getPatchId())) {
                alreadyApplied = true;
                break;
            }
        }
        if (alreadyApplied) {
            continue;
        }
        if (result != null) {
            result.commit();
            results.add(0, lastCommittedEntry);
        }
        final File patch = new File(workDir, entry.getPatchPath());
        final FileInputStream is = new FileInputStream(patch);
        PatchingResult currentResult = null;
        try {
            currentResult = applyPatch(workDir, is, contentPolicy);
        } catch (PatchingException e) {
            // Undo the changes included as part of this patch
            for (BundledPatch.BundledPatchEntry committed : results) {
                try {
                    rollback(committed.getPatchId(), contentPolicy, false, false).commit();
                } catch (PatchingException oe) {
                    PatchLogger.ROOT_LOGGER.debugf(oe, "failed to rollback patch '%s'", committed.getPatchId());
                }
            }
            throw e;
        } finally {
            safeClose(is);
        }
        if (currentResult != null) {
            result = currentResult;
            lastCommittedEntry = entry;
        }
    }
    if (result == null) {
        throw new PatchingException();
    }
    return new WrappedMultiInstallPatch(result, contentPolicy, results);
}
Also used : InstalledIdentity(org.jboss.as.patching.installation.InstalledIdentity) PatchingException(org.jboss.as.patching.PatchingException) ArrayList(java.util.ArrayList) BundledPatch(org.jboss.as.patching.metadata.BundledPatch) FileInputStream(java.io.FileInputStream) BundledPatchEntry(org.jboss.as.patching.metadata.BundledPatch.BundledPatchEntry) PatchingResult(org.jboss.as.patching.tool.PatchingResult) File(java.io.File)

Example 3 with BundledPatch

use of org.jboss.as.patching.metadata.BundledPatch in project wildfly-core by wildfly.

the class PatchInspect method displayPatchBundleXml.

private void displayPatchBundleXml(CommandContext ctx, List<BundledPatch.BundledPatchEntry> patches, ZipFile patchZip) throws CommandException {
    if (patches.isEmpty()) {
        return;
    }
    for (BundledPatch.BundledPatchEntry bundledPatch : patches) {
        final ZipEntry bundledZip = patchZip.getEntry(bundledPatch.getPatchPath());
        if (bundledZip == null) {
            throw new CommandException("Patch file not found in the bundle: " + bundledPatch.getPatchPath());
        }
        InputStream is = null;
        ZipInputStream bundledPatchIs = null;
        try {
            is = patchZip.getInputStream(bundledZip);
            bundledPatchIs = new ZipInputStream(is);
            ZipEntry bundledPatchXml = bundledPatchIs.getNextEntry();
            while (bundledPatchXml != null) {
                if (PatchXml.PATCH_XML.equals(bundledPatchXml.getName())) {
                    break;
                }
                bundledPatchXml = bundledPatchIs.getNextEntry();
            }
            if (bundledPatchXml == null) {
                throw new CommandLineException("Failed to locate " + PatchXml.PATCH_XML + " in bundled patch " + bundledPatch.getPatchPath());
            }
            final Patch patch = PatchXml.parse(bundledPatchIs).resolvePatch(null, null);
            if (verbose) {
                // to make the separation better in the verbose mode
                ctx.printLine("CONTENT OF " + bundledPatch.getPatchPath() + ':' + Util.LINE_SEPARATOR);
            }
            displayPatchXml(ctx, patch);
            ctx.printLine("");
        } catch (Exception e) {
            throw new CommandException("Failed to inspect " + bundledPatch.getPatchPath(), e);
        } finally {
            IoUtils.safeClose(bundledPatchIs);
            IoUtils.safeClose(is);
        }
    }
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ZipInputStream(java.util.zip.ZipInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) CommandException(org.aesh.command.CommandException) BundledPatch(org.jboss.as.patching.metadata.BundledPatch) Patch(org.jboss.as.patching.metadata.Patch) BundledPatch(org.jboss.as.patching.metadata.BundledPatch) CommandLineException(org.jboss.as.cli.CommandLineException) XMLStreamException(javax.xml.stream.XMLStreamException) PatchingException(org.jboss.as.patching.PatchingException) ZipException(java.util.zip.ZipException) IOException(java.io.IOException) CommandException(org.aesh.command.CommandException) CommandLineException(org.jboss.as.cli.CommandLineException)

Example 4 with BundledPatch

use of org.jboss.as.patching.metadata.BundledPatch in project wildfly-core by wildfly.

the class PatchToolImpl method execute.

protected PatchingResult execute(final File workDir, final ContentVerificationPolicy contentPolicy) throws PatchingException, IOException, XMLStreamException {
    final File patchBundleXml = new File(workDir, PatchBundleXml.MULTI_PATCH_XML);
    if (patchBundleXml.exists()) {
        final InputStream patchIs = new FileInputStream(patchBundleXml);
        try {
            // Handle multi patch installs
            final BundledPatch bundledPatch = PatchBundleXml.parse(patchIs);
            return applyPatchBundle(workDir, bundledPatch, contentPolicy);
        } finally {
            safeClose(patchIs);
        }
    } else {
        // Parse the xml
        File patchXml = new File(workDir, PatchXml.PATCH_XML);
        PatchMetadataResolver patchResolver = parsePatchXml(patchXml);
        Patch patch = patchResolver.resolvePatch(null, null);
        final InstalledIdentity installedIdentity = manager.getInstalledIdentity(patch.getIdentity().getName(), null);
        final String currentVersion = installedIdentity.getIdentity().getVersion();
        if (!Constants.UNKNOWN.equals(currentVersion) && !patch.getIdentity().getVersion().equals(currentVersion)) {
            patchXml = new File(workDir, currentVersion + PatchMerger.PATCH_XML_SUFFIX);
            if (!patchXml.exists()) {
                throw new PatchingException("The patch does not contain metadata for currently installed " + patch.getIdentity().getName() + " version " + currentVersion);
            }
            patchResolver = parsePatchXml(patchXml);
            patch = patchResolver.resolvePatch(null, null);
        }
        return apply(patchResolver, PatchContentProvider.DefaultContentProvider.create(workDir), contentPolicy);
    }
}
Also used : InstalledIdentity(org.jboss.as.patching.installation.InstalledIdentity) PatchingException(org.jboss.as.patching.PatchingException) PatchMetadataResolver(org.jboss.as.patching.metadata.PatchMetadataResolver) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) BundledPatch(org.jboss.as.patching.metadata.BundledPatch) File(java.io.File) Patch(org.jboss.as.patching.metadata.Patch) BundledPatch(org.jboss.as.patching.metadata.BundledPatch) FileInputStream(java.io.FileInputStream)

Example 5 with BundledPatch

use of org.jboss.as.patching.metadata.BundledPatch in project wildfly-core by wildfly.

the class TestUtils method createPatchBundleXMLFile.

public static void createPatchBundleXMLFile(File dir, final List<BundledPatch.BundledPatchEntry> patches) throws Exception {
    File bundleXMLFile = new File(dir, "patches.xml");
    FileOutputStream fos = new FileOutputStream(bundleXMLFile);
    try {
        PatchBundleXml.marshal(fos, new BundledPatch() {

            @Override
            public List<BundledPatchEntry> getPatches() {
                return patches;
            }
        });
    } finally {
        safeClose(fos);
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) List(java.util.List) BundledPatch(org.jboss.as.patching.metadata.BundledPatch) IoUtils.newFile(org.jboss.as.patching.IoUtils.newFile) File(java.io.File)

Aggregations

BundledPatch (org.jboss.as.patching.metadata.BundledPatch)6 File (java.io.File)5 PatchingException (org.jboss.as.patching.PatchingException)4 FileOutputStream (java.io.FileOutputStream)3 List (java.util.List)3 Patch (org.jboss.as.patching.metadata.Patch)3 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 IoUtils.newFile (org.jboss.as.patching.IoUtils.newFile)2 InstalledIdentity (org.jboss.as.patching.installation.InstalledIdentity)2 OutputStream (java.io.OutputStream)1 ZipEntry (java.util.zip.ZipEntry)1 ZipException (java.util.zip.ZipException)1 ZipInputStream (java.util.zip.ZipInputStream)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 CommandException (org.aesh.command.CommandException)1 CommandLineException (org.jboss.as.cli.CommandLineException)1 BundledPatchEntry (org.jboss.as.patching.metadata.BundledPatch.BundledPatchEntry)1