Search in sources :

Example 11 with PatchElement

use of org.jboss.as.patching.metadata.PatchElement 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 12 with PatchElement

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

the class IdentityPatchContext method createPatchElement.

/**
 * Copy a patch element
 *
 * @param entry         the patch entry
 * @param patchId       the patch id for the element
 * @param modifications the element modifications
 * @return the new patch element
 */
protected static PatchElement createPatchElement(final PatchEntry entry, String patchId, final List<ContentModification> modifications) {
    final PatchElement patchElement = entry.element;
    final PatchElementImpl element = new PatchElementImpl(patchId);
    element.setProvider(patchElement.getProvider());
    // Add all the rollback actions
    element.getModifications().addAll(modifications);
    element.setDescription(patchElement.getDescription());
    return element;
}
Also used : PatchElementImpl(org.jboss.as.patching.metadata.impl.PatchElementImpl) PatchElement(org.jboss.as.patching.metadata.PatchElement)

Example 13 with PatchElement

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

the class IdentityPatchContext method createRollbackElement.

/**
 * Create a patch element for the rollback patch.
 *
 * @param entry the entry
 * @return the new patch element
 */
protected static PatchElement createRollbackElement(final PatchEntry entry) {
    final PatchElement patchElement = entry.element;
    final String patchId;
    final Patch.PatchType patchType = patchElement.getProvider().getPatchType();
    if (patchType == Patch.PatchType.CUMULATIVE) {
        patchId = entry.getCumulativePatchID();
    } else {
        patchId = patchElement.getId();
    }
    return createPatchElement(entry, patchId, entry.rollbackActions);
}
Also used : PatchElement(org.jboss.as.patching.metadata.PatchElement) Patch(org.jboss.as.patching.metadata.Patch) RollbackPatch(org.jboss.as.patching.metadata.RollbackPatch)

Example 14 with PatchElement

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

the class IdentityPatchContext method createRollbackPatch.

/**
 * Create a rollback patch based on the recorded actions.
 *
 * @param patchId   the new patch id, depending on release or one-off
 * @param patchType the current patch identity
 * @return the rollback patch
 */
protected RollbackPatch createRollbackPatch(final String patchId, final Patch.PatchType patchType) {
    // Process elements
    final List<PatchElement> elements = new ArrayList<PatchElement>();
    // Process layers
    for (final PatchEntry entry : getLayers()) {
        final PatchElement element = createRollbackElement(entry);
        elements.add(element);
    }
    // Process add-ons
    for (final PatchEntry entry : getAddOns()) {
        final PatchElement element = createRollbackElement(entry);
        elements.add(element);
    }
    final InstalledIdentity installedIdentity = modification.getUnmodifiedInstallationState();
    final String name = installedIdentity.getIdentity().getName();
    final IdentityImpl identity = new IdentityImpl(name, modification.getVersion());
    if (patchType == Patch.PatchType.CUMULATIVE) {
        identity.setPatchType(Patch.PatchType.CUMULATIVE);
        identity.setResultingVersion(installedIdentity.getIdentity().getVersion());
    } else if (patchType == Patch.PatchType.ONE_OFF) {
        identity.setPatchType(Patch.PatchType.ONE_OFF);
    }
    final List<ContentModification> modifications = identityEntry.rollbackActions;
    final Patch delegate = new PatchImpl(patchId, "rollback patch", identity, elements, modifications);
    return new PatchImpl.RollbackPatchImpl(delegate, installedIdentity);
}
Also used : InstalledIdentity(org.jboss.as.patching.installation.InstalledIdentity) ArrayList(java.util.ArrayList) IdentityImpl(org.jboss.as.patching.metadata.impl.IdentityImpl) PatchElement(org.jboss.as.patching.metadata.PatchElement) PatchImpl(org.jboss.as.patching.metadata.PatchImpl) ContentModification(org.jboss.as.patching.metadata.ContentModification) Patch(org.jboss.as.patching.metadata.Patch) RollbackPatch(org.jboss.as.patching.metadata.RollbackPatch)

Example 15 with PatchElement

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

the class PatchableTargetsArtifact method process.

@Override
public boolean process(PatchingXmlArtifact.XmlArtifactState<Patch> parent, PatchingArtifactProcessor processor) {
    final InstalledIdentity identity = processor.getValidationContext().getInstalledIdentity();
    final Patch patch = parent.getPatch();
    if (Constants.BASE.equals(patch.getPatchId())) {
        return true;
    }
    final List<PatchElement> elements = patch.getElements();
    boolean valid = true;
    if (elements != null && !elements.isEmpty()) {
        for (final PatchElement element : elements) {
            final String patchID = element.getId();
            final PatchElementProvider provider = element.getProvider();
            final String layerName = provider.getName();
            final PatchableTarget target = provider.isAddOn() ? identity.getAddOn(layerName) : identity.getLayer(layerName);
            boolean modules = false;
            boolean bundles = false;
            for (final ContentModification modification : element.getModifications()) {
                if (modules && bundles) {
                    break;
                }
                if (modification.getItem().getContentType() == ContentType.BUNDLE) {
                    bundles = true;
                } else if (modification.getItem().getContentType() == ContentType.MODULE) {
                    modules = true;
                }
            }
            final PatchableTargetState state = new PatchableTargetState(patchID, layerName, target, bundles, modules);
            if (!processor.process(PatchingArtifacts.LAYER, state)) {
                valid = false;
            }
        }
    }
    // In case there are misc content modifications also validate that misc backup exists
    for (final ContentModification modification : patch.getModifications()) {
        if (modification.getType() != ModificationType.ADD && modification.getItem().getContentType() == ContentType.MISC) {
            final PatchingHistoryDirArtifact.DirectoryArtifactState history = processor.getParentArtifact(PatchingArtifacts.HISTORY_DIR);
            PatchingArtifacts.MISC_BACKUP.process(history, processor);
            break;
        }
    }
    return valid;
}
Also used : InstalledIdentity(org.jboss.as.patching.installation.InstalledIdentity) PatchableTarget(org.jboss.as.patching.installation.PatchableTarget) PatchElementProvider(org.jboss.as.patching.metadata.PatchElementProvider) PatchElement(org.jboss.as.patching.metadata.PatchElement) Patch(org.jboss.as.patching.metadata.Patch) ContentModification(org.jboss.as.patching.metadata.ContentModification)

Aggregations

PatchElement (org.jboss.as.patching.metadata.PatchElement)20 Patch (org.jboss.as.patching.metadata.Patch)13 PatchElementProvider (org.jboss.as.patching.metadata.PatchElementProvider)11 Identity (org.jboss.as.patching.metadata.Identity)10 ContentModification (org.jboss.as.patching.metadata.ContentModification)8 InstalledIdentity (org.jboss.as.patching.installation.InstalledIdentity)7 RollbackPatch (org.jboss.as.patching.metadata.RollbackPatch)6 IdentityUpgrade (org.jboss.as.patching.metadata.Identity.IdentityUpgrade)5 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)4 PatchingException (org.jboss.as.patching.PatchingException)4 PatchableTarget (org.jboss.as.patching.installation.PatchableTarget)4 ContentItem (org.jboss.as.patching.metadata.ContentItem)4 PatchEntry (org.jboss.as.patching.runner.IdentityPatchContext.PatchEntry)4 Entry (org.jboss.as.patching.tool.PatchingHistory.Entry)3 File (java.io.File)2 IOException (java.io.IOException)2 LinkedHashMap (java.util.LinkedHashMap)2 XMLStreamException (javax.xml.stream.XMLStreamException)2 SimpleTable (org.jboss.as.cli.util.SimpleTable)2