Search in sources :

Example 16 with PatchingException

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

the class LocalPatchOperationStepHandler method executeRuntime.

private void executeRuntime(final OperationContext context, final ModelNode operation) throws OperationFailedException {
    // Acquire the lock and check the write permissions for this operation
    final ServiceRegistry registry = context.getServiceRegistry(true);
    final InstallationManager installationManager = (InstallationManager) registry.getRequiredService(InstallationManagerService.NAME).getValue();
    if (installationManager.requiresRestart()) {
        throw PatchLogger.ROOT_LOGGER.serverRequiresRestart();
    }
    try {
        final PatchTool runner = PatchTool.Factory.create(installationManager);
        final ContentVerificationPolicy policy = PatchTool.Factory.create(operation);
        final int index = operation.get(ModelDescriptionConstants.INPUT_STREAM_INDEX).asInt(0);
        final InputStream is = context.getAttachmentStream(index);
        installationManager.restartRequired();
        final PatchingResult result = runner.applyPatch(is, policy);
        context.restartRequired();
        context.completeStep(new OperationContext.ResultHandler() {

            @Override
            public void handleResult(OperationContext.ResultAction resultAction, OperationContext context, ModelNode operation) {
                if (resultAction == OperationContext.ResultAction.KEEP) {
                    result.commit();
                } else {
                    installationManager.clearRestartRequired();
                    context.revertRestartRequired();
                    result.rollback();
                }
            }
        });
    } catch (PatchingException e) {
        final ModelNode failureDescription = context.getFailureDescription();
        PatchOperationTarget.formatFailedResponse(e, failureDescription);
        installationManager.clearRestartRequired();
    }
}
Also used : OperationContext(org.jboss.as.controller.OperationContext) PatchingException(org.jboss.as.patching.PatchingException) InputStream(java.io.InputStream) InstallationManager(org.jboss.as.patching.installation.InstallationManager) PatchingResult(org.jboss.as.patching.tool.PatchingResult) PatchTool(org.jboss.as.patching.tool.PatchTool) ContentVerificationPolicy(org.jboss.as.patching.tool.ContentVerificationPolicy) ServiceRegistry(org.jboss.msc.service.ServiceRegistry) ModelNode(org.jboss.dmr.ModelNode)

Example 17 with PatchingException

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

the class PatchIdCompleter method complete.

@Override
public void complete(CLICompleterInvocation completerInvocation) {
    AbstractDistributionCommand cmd = (AbstractDistributionCommand) completerInvocation.getCommand();
    try {
        List<ModelNode> patches = PatchRollbackActivator.getPatches(completerInvocation.getCommandContext(), cmd, cmd.getPatchStream(), cmd.getHost());
        List<String> names = new ArrayList<>();
        for (ModelNode mn : patches) {
            if (mn.hasDefined(Constants.PATCH_ID)) {
                names.add(mn.get(Constants.PATCH_ID).asString());
            }
        }
        String buffer = completerInvocation.getGivenCompleteValue();
        if (buffer == null || buffer.isEmpty()) {
            completerInvocation.addAllCompleterValues(names);
        } else {
            for (String n : names) {
                if (n.startsWith(buffer)) {
                    completerInvocation.addCompleterValue(n);
                    completerInvocation.setOffset(buffer.length());
                }
            }
        }
    } catch (PatchingException | CommandException ex) {
        // OK, will not complete.
        return;
    }
}
Also used : PatchingException(org.jboss.as.patching.PatchingException) ArrayList(java.util.ArrayList) CommandException(org.aesh.command.CommandException) ModelNode(org.jboss.dmr.ModelNode)

Example 18 with PatchingException

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

the class PatchMerger method mergeModifications.

private static void mergeModifications(final ModificationBuilderTarget<?> elementBuilder, Collection<ContentModification> cp1Modifications, Collection<ContentModification> cp2Modifications, Patch cp1, Patch cp2) throws PatchingException {
    final ContentModifications cp2Mods = new ContentModifications(cp2Modifications);
    for (ContentModification cp1Mod : cp1Modifications) {
        final ContentModification cp2Mod = cp2Mods.remove(cp1Mod.getItem());
        if (cp2Mod == null) {
            elementBuilder.addContentModification(cp1Mod);
        } else {
            final ModificationType cp1Type = cp1Mod.getType();
            final ModificationType cp2Type = cp2Mod.getType();
            final ModificationType modType;
            if (cp1Type.equals(ModificationType.ADD)) {
                if (cp2Type.equals(ModificationType.ADD)) {
                    throw new PatchingException("Patch " + cp2.getPatchId() + " adds " + cp1Mod.getItem().getRelativePath() + " already added by patch " + cp1.getPatchId());
                }
                if (cp2Type.equals(ModificationType.MODIFY)) {
                    modType = ModificationType.ADD;
                } else {
                    // remove cancels add
                    if (cp1Mod.getItem().getContentType().equals(ContentType.MODULE)) {
                        // but not for modules where remove is effectively modify (resulting in module.xml indicating an absent module)
                        // so add becomes an add of an absent module
                        modType = ModificationType.ADD;
                    } else {
                        modType = null;
                        continue;
                    }
                }
            } else if (cp1Type.equals(ModificationType.REMOVE)) {
                if (cp2Type.equals(ModificationType.REMOVE)) {
                    throw new PatchingException("Patch " + cp2.getPatchId() + " removes " + cp1Mod.getItem().getRelativePath() + " already removed by patch " + cp1.getPatchId());
                }
                /*if (cp2Type.equals(ModificationType.MODIFY)) {
                        throw new PatchingException("Patch " + cp2.getPatchId() + " modifies "
                                + cp1Mod.getItem().getRelativePath() + " removed by patch " + cp1.getPatchId());
                        this could happen since the REMOVE will leave a module.xml indicating the module is absent
                        so, to re-add the module, it has to be MODIFY, since ADD will fail
                    }*/
                // add after remove makes it modify
                modType = ModificationType.MODIFY;
            } else {
                // modify
                if (cp2Type.equals(ModificationType.ADD)) {
                    throw new PatchingException("Patch " + cp2.getPatchId() + " adds " + cp1Mod.getItem().getRelativePath() + " modified by patch " + cp1.getPatchId());
                }
                if (cp2Type.equals(ModificationType.REMOVE)) {
                    modType = ModificationType.REMOVE;
                } else {
                    modType = ModificationType.MODIFY;
                }
            }
            if (ModificationType.ADD.equals(modType)) {
                final ContentItem cp2Item = cp2Mod.getItem();
                if (cp2Item.getContentType().equals(ContentType.MODULE)) {
                    final ModuleItem module = (ModuleItem) cp2Item;
                    if (cp2Type.equals(ModificationType.REMOVE)) {
                        try {
                            elementBuilder.addModule(module.getName(), module.getSlot(), PatchUtils.getAbsentModuleContentHash(module));
                        } catch (IOException e) {
                            throw new PatchingException("Failed to calculate hash for the removed module " + module.getName(), e);
                        }
                    } else {
                        elementBuilder.addModule(module.getName(), module.getSlot(), module.getContentHash());
                    }
                } else if (cp2Item.getContentType().equals(ContentType.MISC)) {
                    final MiscContentItem misc = (MiscContentItem) cp2Item;
                    elementBuilder.addFile(misc.getName(), Arrays.asList(misc.getPath()), misc.getContentHash(), misc.isDirectory());
                } else {
                    // bundle
                    final BundleItem bundle = (BundleItem) cp2Item;
                    elementBuilder.addBundle(bundle.getName(), bundle.getSlot(), bundle.getContentHash());
                }
            } else if (ModificationType.REMOVE.equals(modType)) {
                final ContentItem cp1Item = cp1Mod.getItem();
                if (cp1Item.getContentType().equals(ContentType.MODULE)) {
                    final ModuleItem module = (ModuleItem) cp2Mod.getItem();
                    elementBuilder.removeModule(module.getName(), module.getSlot(), cp1Mod.getTargetHash());
                } else if (cp1Item.getContentType().equals(ContentType.MISC)) {
                    final MiscContentItem misc = (MiscContentItem) cp2Mod.getItem();
                    elementBuilder.removeFile(misc.getName(), Arrays.asList(misc.getPath()), cp1Mod.getTargetHash(), misc.isDirectory());
                } else {
                    // bundle
                    final BundleItem bundle = (BundleItem) cp2Mod.getItem();
                    elementBuilder.removeBundle(bundle.getName(), bundle.getSlot(), cp1Mod.getTargetHash());
                }
            } else {
                // modify
                final ContentItem cp1Item = cp1Mod.getItem();
                if (cp1Item.getContentType().equals(ContentType.MODULE)) {
                    final ModuleItem module = (ModuleItem) cp2Mod.getItem();
                    elementBuilder.modifyModule(module.getName(), module.getSlot(), cp1Mod.getTargetHash(), module.getContentHash());
                } else if (cp1Item.getContentType().equals(ContentType.MISC)) {
                    final MiscContentItem misc = (MiscContentItem) cp2Mod.getItem();
                    elementBuilder.modifyFile(misc.getName(), Arrays.asList(misc.getPath()), cp1Mod.getTargetHash(), misc.getContentHash(), misc.isDirectory());
                } else {
                    // bundle
                    final BundleItem bundle = (BundleItem) cp2Mod.getItem();
                    elementBuilder.modifyBundle(bundle.getName(), bundle.getSlot(), cp1Mod.getTargetHash(), bundle.getContentHash());
                }
            }
        }
    }
    for (ContentModification cp2Mod : cp2Mods.getModifications()) {
        elementBuilder.addContentModification(cp2Mod);
    }
}
Also used : PatchingException(org.jboss.as.patching.PatchingException) IOException(java.io.IOException)

Example 19 with PatchingException

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

the class PatchMerger method merge.

public static File merge(File patch1, File patch2, File merged) throws PatchingException {
    final File workDir = createTempDir();
    final File patch1Dir = expandContent(patch1, workDir, "patch1");
    final File patch2Dir = expandContent(patch2, workDir, "patch2");
    final File mergedDir = new File(workDir, "merged");
    final Patch patch1Metadata = parsePatchXml(patch1Dir, patch1);
    final Patch patch2Metadata = parsePatchXml(patch2Dir, patch2);
    final Patch mergedMetadata = merge(patch1Metadata, patch2Metadata);
    if (!mergedDir.mkdirs()) {
        throw new PatchingException("Failed to create directory " + mergedDir.getAbsolutePath());
    }
    // merge with the previous versions
    for (File f : patch1Dir.listFiles(new FilenameFilter() {

        @Override
        public boolean accept(File dir, String name) {
            return name.endsWith(PATCH_XML_SUFFIX);
        }
    })) {
        Patch patch;
        try {
            patch = PatchXml.parse(f).resolvePatch(null, null);
        } catch (Exception e) {
            throw new PatchingException("Failed to parse " + f.getAbsolutePath(), e);
        }
        patch = merge(patch, patch2Metadata);
        try (Writer writer = Files.newBufferedWriter(new File(mergedDir, f.getName()).toPath(), StandardCharsets.UTF_8)) {
            PatchXml.marshal(writer, patch);
        } catch (Exception e) {
            throw new PatchingException("Failed to marshal merged metadata into " + f.getName(), e);
        }
    }
    // the latest patch.xml
    copyFile(new File(patch2Dir, PatchXml.PATCH_XML), new File(mergedDir, patch2Metadata.getIdentity().getVersion() + PATCH_XML_SUFFIX));
    // merged patch.xml is the metadata from the earliest version to the latest
    try (Writer writer = Files.newBufferedWriter(new File(mergedDir, PatchXml.PATCH_XML).toPath(), StandardCharsets.UTF_8)) {
        PatchXml.marshal(writer, mergedMetadata);
    } catch (Exception e) {
        throw new PatchingException("Failed to marshal merged metadata into " + PatchXml.PATCH_XML, e);
    }
    try {
        mergeRootContent(new File(patch1Dir, patch1Metadata.getPatchId()), new File(patch2Dir, patch2Metadata.getPatchId()), new File(mergedDir, patch2Metadata.getPatchId()));
    } catch (IOException e) {
        throw new PatchingException("Failed to merge root modifications", e);
    }
    try {
        mergeElementContent(patch1Dir, patch2Dir, mergedDir, patch1Metadata, patch2Metadata);
    } catch (IOException e) {
        throw new PatchingException("Failed to merge element modifications", e);
    }
    // list(mergedDir);
    ZipUtils.zip(mergedDir, merged);
    IoUtils.recursiveDelete(workDir);
    return merged;
}
Also used : FilenameFilter(java.io.FilenameFilter) PatchingException(org.jboss.as.patching.PatchingException) IOException(java.io.IOException) File(java.io.File) IOException(java.io.IOException) PatchingException(org.jboss.as.patching.PatchingException) Writer(java.io.Writer)

Example 20 with PatchingException

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

the class PatchMerger method createTempDir.

static File createTempDir(final File parent) throws PatchingException {
    File workDir = null;
    int count = 0;
    while (workDir == null || workDir.exists()) {
        count++;
        workDir = new File(parent == null ? TEMP_DIR : parent, DIRECTORY_PREFIX + count);
    }
    if (!workDir.mkdirs()) {
        throw new PatchingException(PatchLogger.ROOT_LOGGER.cannotCreateDirectory(workDir.getAbsolutePath()));
    }
    return workDir;
}
Also used : PatchingException(org.jboss.as.patching.PatchingException) File(java.io.File)

Aggregations

PatchingException (org.jboss.as.patching.PatchingException)39 File (java.io.File)20 IOException (java.io.IOException)19 InstalledIdentity (org.jboss.as.patching.installation.InstalledIdentity)17 XMLStreamException (javax.xml.stream.XMLStreamException)9 Patch (org.jboss.as.patching.metadata.Patch)9 PatchTool (org.jboss.as.patching.tool.PatchTool)8 PatchingResult (org.jboss.as.patching.tool.PatchingResult)8 InstallationManager (org.jboss.as.patching.installation.InstallationManager)7 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)5 PatchableTarget (org.jboss.as.patching.installation.PatchableTarget)5 InputStream (java.io.InputStream)4 BundledPatch (org.jboss.as.patching.metadata.BundledPatch)4 PatchElement (org.jboss.as.patching.metadata.PatchElement)4 PatchElementProvider (org.jboss.as.patching.metadata.PatchElementProvider)4 ModelNode (org.jboss.dmr.ModelNode)4 OperationContext (org.jboss.as.controller.OperationContext)3 ContentModification (org.jboss.as.patching.metadata.ContentModification)3 Identity (org.jboss.as.patching.metadata.Identity)3