Search in sources :

Example 1 with PatchType

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

the class IdentityPatchRunner method applyPatch.

/**
 * Apply a patch.
 *
 * @param patchId the patch id
 * @param patch   the patch metadata
 * @param context the patch context
 * @throws PatchingException
 * @throws IOException
 * @throws XMLStreamException
 */
private PatchingResult applyPatch(final String patchId, final Patch patch, final IdentityPatchContext context) throws PatchingException, IOException, XMLStreamException {
    final Identity identity = patch.getIdentity();
    final Patch.PatchType patchType = identity.getPatchType();
    final InstallationManager.InstallationModification modification = context.getModification();
    if (patchType == Patch.PatchType.CUMULATIVE) {
        // Invalidate all installed patches (one-off, cumulative) - we never need to invalidate the release base
        final List<String> invalidation = new ArrayList<String>(modification.getPatchIDs());
        if (!invalidation.isEmpty()) {
            try {
                // Before rolling back the one-off patches, validate that the state until that point is consistent
                validateRollbackState(invalidation.get(invalidation.size() - 1), modification.getUnmodifiedInstallationState());
            } catch (PatchingException e) {
                throw e;
            } catch (Exception e) {
                throw new PatchingException(e);
            }
            // Invalidate the installed patches first
            for (final String rollback : invalidation) {
                rollback(rollback, context);
            }
        }
    }
    // Add to installed patches list
    modification.addInstalledPatch(patchId);
    // Then apply the current patch
    for (final PatchElement element : patch.getElements()) {
        // Apply the content modifications
        final IdentityPatchContext.PatchEntry target = context.resolveForElement(element);
        final PatchElementProvider provider = element.getProvider();
        final Patch.PatchType elementPatchType = provider.getPatchType();
        final String elementPatchId = element.getId();
        // See if we can skip this element
        if (target.isApplied(elementPatchId)) {
            // This needs some further testing, maybe we need to compare our history with the patch if they are consistent
            throw PatchLogger.ROOT_LOGGER.alreadyApplied(elementPatchId);
        }
        // Check upgrade conditions
        checkUpgradeConditions(provider, target);
        apply(elementPatchId, element.getModifications(), target);
        target.apply(elementPatchId, elementPatchType);
    }
    // Apply the patch to the identity
    final IdentityPatchContext.PatchEntry identityEntry = context.getIdentityEntry();
    apply(patchId, patch.getModifications(), identityEntry);
    identityEntry.apply(patchId, patchType);
    // Port forward missing module changes
    if (patchType == Patch.PatchType.CUMULATIVE) {
        portForward(patch, context);
    }
    // We need the resulting version for rollback
    if (patchType == Patch.PatchType.CUMULATIVE) {
        final Identity.IdentityUpgrade upgrade = identity.forType(Patch.PatchType.CUMULATIVE, Identity.IdentityUpgrade.class);
        identityEntry.setResultingVersion(upgrade.getResultingVersion());
    }
    // Execute the tasks
    final IdentityApplyCallback callback = new IdentityApplyCallback(patch, identityEntry.getDirectoryStructure());
    try {
        return executeTasks(context, callback);
    } catch (Exception e) {
        context.cancel(callback);
        throw rethrowException(e);
    }
}
Also used : PatchingException(org.jboss.as.patching.PatchingException) PatchElementProvider(org.jboss.as.patching.metadata.PatchElementProvider) ArrayList(java.util.ArrayList) PatchElement(org.jboss.as.patching.metadata.PatchElement) XMLStreamException(javax.xml.stream.XMLStreamException) PatchingException(org.jboss.as.patching.PatchingException) IOException(java.io.IOException) InstallationManager(org.jboss.as.patching.installation.InstallationManager) Identity(org.jboss.as.patching.metadata.Identity) InstalledIdentity(org.jboss.as.patching.installation.InstalledIdentity) PatchType(org.jboss.as.patching.metadata.Patch.PatchType) Patch(org.jboss.as.patching.metadata.Patch) RollbackPatch(org.jboss.as.patching.metadata.RollbackPatch) PatchEntry(org.jboss.as.patching.runner.IdentityPatchContext.PatchEntry)

Example 2 with PatchType

use of org.jboss.as.patching.metadata.Patch.PatchType 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)

Aggregations

IOException (java.io.IOException)2 XMLStreamException (javax.xml.stream.XMLStreamException)2 PatchingException (org.jboss.as.patching.PatchingException)2 InstalledIdentity (org.jboss.as.patching.installation.InstalledIdentity)2 Identity (org.jboss.as.patching.metadata.Identity)2 Patch (org.jboss.as.patching.metadata.Patch)2 PatchType (org.jboss.as.patching.metadata.Patch.PatchType)2 PatchElement (org.jboss.as.patching.metadata.PatchElement)2 PatchElementProvider (org.jboss.as.patching.metadata.PatchElementProvider)2 RollbackPatch (org.jboss.as.patching.metadata.RollbackPatch)2 PatchEntry (org.jboss.as.patching.runner.IdentityPatchContext.PatchEntry)2 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 InstallationManager (org.jboss.as.patching.installation.InstallationManager)1 PatchableTarget (org.jboss.as.patching.installation.PatchableTarget)1 LayerType (org.jboss.as.patching.metadata.LayerType)1