Search in sources :

Example 6 with Identity

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

the class PatchInfoHandler method patchIdInfo.

private ModelNode patchIdInfo(final OperationContext context, final String patchId, final boolean verbose, final PatchingHistory.Iterator i) {
    while (i.hasNext()) {
        final Entry entry = i.next();
        if (patchId.equals(entry.getPatchId())) {
            final ModelNode result = new ModelNode();
            result.get(Constants.PATCH_ID).set(entry.getPatchId());
            result.get(Constants.TYPE).set(entry.getType().getName());
            result.get(Constants.DESCRIPTION).set(entry.getMetadata().getDescription());
            final String link = entry.getMetadata().getLink();
            if (link != null) {
                result.get(Constants.LINK).set(link);
            }
            final Identity identity = entry.getMetadata().getIdentity();
            result.get(Constants.IDENTITY_NAME).set(identity.getName());
            result.get(Constants.IDENTITY_VERSION).set(identity.getVersion());
            if (verbose) {
                final ModelNode list = result.get(Constants.ELEMENTS).setEmptyList();
                final Patch metadata = entry.getMetadata();
                for (PatchElement e : metadata.getElements()) {
                    final ModelNode element = new ModelNode();
                    element.get(Constants.PATCH_ID).set(e.getId());
                    element.get(Constants.TYPE).set(e.getProvider().isAddOn() ? Constants.ADD_ON : Constants.LAYER);
                    element.get(Constants.NAME).set(e.getProvider().getName());
                    element.get(Constants.DESCRIPTION).set(e.getDescription());
                    list.add(element);
                }
            }
            return result;
        }
    }
    return null;
}
Also used : Entry(org.jboss.as.patching.tool.PatchingHistory.Entry) PatchElement(org.jboss.as.patching.metadata.PatchElement) ModelNode(org.jboss.dmr.ModelNode) Identity(org.jboss.as.patching.metadata.Identity) InstalledIdentity(org.jboss.as.patching.installation.InstalledIdentity) Patch(org.jboss.as.patching.metadata.Patch)

Example 7 with Identity

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

the class IdentityPatchRunner method applyPatch.

/**
 * Apply a patch.
 *
 * @param patchResolver   the patch metadata resolver
 * @param contentProvider the patch content provider
 * @param contentPolicy   the content verification policy
 * @param modification    the installation modification
 * @throws PatchingException for any error
 */
public PatchingResult applyPatch(final PatchMetadataResolver patchResolver, final PatchContentProvider contentProvider, final ContentVerificationPolicy contentPolicy, final InstallationManager.InstallationModification modification) throws PatchingException {
    try {
        // Check if we can apply this patch
        final Patch patch = patchResolver.resolvePatch(modification.getName(), modification.getVersion());
        if (patch == null) {
            throw PatchLogger.ROOT_LOGGER.failedToResolvePatch(modification.getName(), modification.getVersion());
        }
        final String patchId = patch.getPatchId();
        final Identity identity = patch.getIdentity();
        final String appliesTo = identity.getVersion();
        if (!appliesTo.equals(modification.getVersion())) {
            throw PatchLogger.ROOT_LOGGER.doesNotApply(appliesTo, modification.getVersion());
        }
        // Cannot apply the same patch twice
        if (modification.isApplied(patchId)) {
            throw PatchLogger.ROOT_LOGGER.alreadyApplied(patchId);
        }
        // See if the prerequisites are met
        checkUpgradeConditions(identity, modification);
        // Apply the patch
        final File backup = installedImage.getPatchHistoryDir(patchId);
        final IdentityPatchContext context = new IdentityPatchContext(backup, contentProvider, contentPolicy, modification, APPLY, installedImage);
        try {
            return applyPatch(patchId, patch, context);
        } catch (Exception e) {
            PatchLogger.ROOT_LOGGER.debugf(e, "failed to apply patch %s", patchId);
            throw rethrowException(e);
        } finally {
            context.cleanup();
        }
    } finally {
        contentProvider.cleanup();
    }
}
Also used : Identity(org.jboss.as.patching.metadata.Identity) InstalledIdentity(org.jboss.as.patching.installation.InstalledIdentity) Patch(org.jboss.as.patching.metadata.Patch) RollbackPatch(org.jboss.as.patching.metadata.RollbackPatch) File(java.io.File) XMLStreamException(javax.xml.stream.XMLStreamException) PatchingException(org.jboss.as.patching.PatchingException) IOException(java.io.IOException)

Example 8 with Identity

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

the class IdentityPatchRunner method rollback.

/**
 * Rollback a patch.
 *
 * @param patchID the patch id
 * @param context the patch context
 * @throws PatchingException
 */
private void rollback(final String patchID, final IdentityPatchContext context) throws PatchingException {
    try {
        // Load the patch history
        final PatchingTaskContext.Mode mode = context.getMode();
        final Patch originalPatch = loadPatchInformation(patchID, installedImage);
        final RollbackPatch rollbackPatch = loadRollbackInformation(patchID, installedImage);
        final Patch.PatchType patchType = rollbackPatch.getIdentity().getPatchType();
        final InstalledIdentity history = rollbackPatch.getIdentityState();
        // Process originals by type first
        final LinkedHashMap<String, PatchElement> originalLayers = new LinkedHashMap<String, PatchElement>();
        final LinkedHashMap<String, PatchElement> originalAddOns = new LinkedHashMap<String, PatchElement>();
        for (final PatchElement patchElement : originalPatch.getElements()) {
            final PatchElementProvider provider = patchElement.getProvider();
            final String layerName = provider.getName();
            final LayerType layerType = provider.getLayerType();
            final Map<String, PatchElement> originals;
            switch(layerType) {
                case Layer:
                    originals = originalLayers;
                    break;
                case AddOn:
                    originals = originalAddOns;
                    break;
                default:
                    throw new IllegalStateException();
            }
            if (!originals.containsKey(layerName)) {
                originals.put(layerName, patchElement);
            } else {
                throw PatchLogger.ROOT_LOGGER.installationDuplicateLayer(layerType.toString(), layerName);
            }
        }
        // Process the rollback xml
        for (final PatchElement patchElement : rollbackPatch.getElements()) {
            final String elementPatchId = patchElement.getId();
            final PatchElementProvider provider = patchElement.getProvider();
            final String layerName = provider.getName();
            final LayerType layerType = provider.getLayerType();
            final LinkedHashMap<String, PatchElement> originals;
            switch(layerType) {
                case Layer:
                    originals = originalLayers;
                    break;
                case AddOn:
                    originals = originalAddOns;
                    break;
                default:
                    throw new IllegalStateException();
            }
            final PatchElement original = originals.remove(layerName);
            if (original == null) {
                throw PatchLogger.ROOT_LOGGER.noSuchLayer(layerName);
            }
            final IdentityPatchContext.PatchEntry entry = context.resolveForElement(patchElement);
            // Create the rollback
            PatchingTasks.rollback(elementPatchId, original.getModifications(), patchElement.getModifications(), entry, ContentItemFilter.ALL_BUT_MISC, mode);
            entry.rollback(original.getId());
            // We need to restore the previous state
            final Patch.PatchType elementPatchType = provider.getPatchType();
            final PatchableTarget.TargetInfo info;
            if (layerType == LayerType.AddOn) {
                info = history.getAddOn(layerName).loadTargetInfo();
            } else {
                info = history.getLayer(layerName).loadTargetInfo();
            }
            if (mode == ROLLBACK) {
                restoreFromHistory(entry, elementPatchId, elementPatchType, info);
            }
        }
        if (!originalLayers.isEmpty() || !originalAddOns.isEmpty()) {
            throw PatchLogger.ROOT_LOGGER.invalidRollbackInformation();
        }
        // Rollback the patch
        final IdentityPatchContext.PatchEntry identity = context.getIdentityEntry();
        PatchingTasks.rollback(patchID, originalPatch.getModifications(), rollbackPatch.getModifications(), identity, ContentItemFilter.MISC_ONLY, mode);
        identity.rollback(patchID);
        // Restore previous state
        if (mode == ROLLBACK) {
            final PatchableTarget.TargetInfo identityHistory = history.getIdentity().loadTargetInfo();
            restoreFromHistory(identity, rollbackPatch.getPatchId(), patchType, identityHistory);
            if (patchType == Patch.PatchType.CUMULATIVE) {
                reenableNotOverridenModules(rollbackPatch, context);
            }
        }
        if (patchType == Patch.PatchType.CUMULATIVE) {
            final Identity.IdentityUpgrade upgrade = rollbackPatch.getIdentity().forType(Patch.PatchType.CUMULATIVE, Identity.IdentityUpgrade.class);
            identity.setResultingVersion(upgrade.getResultingVersion());
        }
    } catch (Exception e) {
        throw rethrowException(e);
    }
}
Also used : InstalledIdentity(org.jboss.as.patching.installation.InstalledIdentity) PatchableTarget(org.jboss.as.patching.installation.PatchableTarget) PatchElementProvider(org.jboss.as.patching.metadata.PatchElementProvider) RollbackPatch(org.jboss.as.patching.metadata.RollbackPatch) PatchElement(org.jboss.as.patching.metadata.PatchElement) XMLStreamException(javax.xml.stream.XMLStreamException) PatchingException(org.jboss.as.patching.PatchingException) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap) LayerType(org.jboss.as.patching.metadata.LayerType) PatchType(org.jboss.as.patching.metadata.Patch.PatchType) Identity(org.jboss.as.patching.metadata.Identity) InstalledIdentity(org.jboss.as.patching.installation.InstalledIdentity) Patch(org.jboss.as.patching.metadata.Patch) RollbackPatch(org.jboss.as.patching.metadata.RollbackPatch) PatchEntry(org.jboss.as.patching.runner.IdentityPatchContext.PatchEntry)

Example 9 with Identity

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

the class PatchHandler method displayPatchXml.

private void displayPatchXml(CommandContext ctx, Patch patch) throws CommandLineException {
    final Identity identity = patch.getIdentity();
    SimpleTable table = new SimpleTable(2, ctx.getTerminalWidth());
    table.addLine(new String[] { "Patch ID:", patch.getPatchId() });
    table.addLine(new String[] { "Type:", identity.getPatchType().getName() });
    table.addLine(new String[] { "Identity name:", identity.getName() });
    table.addLine(new String[] { "Identity version:", identity.getVersion() });
    table.addLine(new String[] { "Description:", patch.getDescription() == null ? "n/a" : patch.getDescription() });
    if (patch.getLink() != null) {
        table.addLine(new String[] { "Link:", patch.getLink() });
    }
    ctx.printLine(table.toString(false));
    if (verbose.isPresent(ctx.getParsedCommandLine())) {
        ctx.printLine("");
        ctx.printLine("ELEMENTS");
        for (PatchElement e : patch.getElements()) {
            table = new SimpleTable(2, ctx.getTerminalWidth());
            table.addLine(new String[] { "Patch ID:", e.getId() });
            table.addLine(new String[] { "Name:", e.getProvider().getName() });
            table.addLine(new String[] { "Type:", e.getProvider().isAddOn() ? Constants.ADD_ON : Constants.LAYER });
            table.addLine(new String[] { "Description:", e.getDescription() });
            ctx.printLine("");
            ctx.printLine(table.toString(false));
        }
    }
}
Also used : SimpleTable(org.jboss.as.cli.util.SimpleTable) PatchElement(org.jboss.as.patching.metadata.PatchElement) Identity(org.jboss.as.patching.metadata.Identity)

Example 10 with Identity

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

the class MergingPatchMetadataTestCase method testAddRemove.

@Test
public void testAddRemove() throws Exception {
    final Patch cp1 = generateCP("base", "cp1", ModificationType.ADD);
    final Patch cp2 = generateCP("cp1", "cp2", ModificationType.REMOVE);
    final Patch merged = PatchMerger.merge(cp1, cp2);
    assertEquals("cp2", merged.getPatchId());
    assertEquals("cp2" + " description", merged.getDescription());
    final IdentityUpgrade identity = merged.getIdentity().forType(PatchType.CUMULATIVE, Identity.IdentityUpgrade.class);
    assertEquals("base", identity.getVersion());
    assertEquals("cp2", identity.getResultingVersion());
    assertEquals(PatchType.CUMULATIVE, identity.getPatchType());
    final List<PatchElement> elements = merged.getElements();
    assertEquals(1, elements.size());
    final PatchElement e = elements.get(0);
    assertEquals("base-" + "cp2", e.getId());
    final PatchElementProvider provider = e.getProvider();
    assertEquals("base", provider.getName());
    assertEquals(PatchType.CUMULATIVE, provider.getPatchType());
    assertEquals(LayerType.Layer, provider.getLayerType());
    // assertEquals(0, e.getModifications().size());
    // for modules remove is effectively a modify which changes the module xml to indicate an absent module
    // so, it will remain an add of an absent module
    assertEquals(1, e.getModifications().size());
    final ContentModification mod = e.getModifications().iterator().next();
    assertEquals(ModificationType.ADD, mod.getType());
    Assert.assertArrayEquals(PatchUtils.getAbsentModuleContentHash((ModuleItem) mod.getItem()), mod.getItem().getContentHash());
}
Also used : ModuleItem(org.jboss.as.patching.metadata.ModuleItem) IdentityUpgrade(org.jboss.as.patching.metadata.Identity.IdentityUpgrade) PatchElementProvider(org.jboss.as.patching.metadata.PatchElementProvider) PatchElement(org.jboss.as.patching.metadata.PatchElement) Identity(org.jboss.as.patching.metadata.Identity) Patch(org.jboss.as.patching.metadata.Patch) ContentModification(org.jboss.as.patching.metadata.ContentModification) Test(org.junit.Test)

Aggregations

Identity (org.jboss.as.patching.metadata.Identity)11 PatchElement (org.jboss.as.patching.metadata.PatchElement)10 Patch (org.jboss.as.patching.metadata.Patch)9 PatchElementProvider (org.jboss.as.patching.metadata.PatchElementProvider)7 ContentModification (org.jboss.as.patching.metadata.ContentModification)5 IdentityUpgrade (org.jboss.as.patching.metadata.Identity.IdentityUpgrade)5 Test (org.junit.Test)5 InstalledIdentity (org.jboss.as.patching.installation.InstalledIdentity)4 ContentItem (org.jboss.as.patching.metadata.ContentItem)4 IOException (java.io.IOException)3 XMLStreamException (javax.xml.stream.XMLStreamException)3 PatchingException (org.jboss.as.patching.PatchingException)3 RollbackPatch (org.jboss.as.patching.metadata.RollbackPatch)3 SimpleTable (org.jboss.as.cli.util.SimpleTable)2 PatchType (org.jboss.as.patching.metadata.Patch.PatchType)2 PatchEntry (org.jboss.as.patching.runner.IdentityPatchContext.PatchEntry)2 File (java.io.File)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 InstallationManager (org.jboss.as.patching.installation.InstallationManager)1