use of org.jboss.as.patching.PatchingException in project wildfly-core by wildfly.
the class PatchStreamResourceOperationStepHandler method execute.
protected void execute(OperationContext context, ModelNode operation, InstallationManager instMgr, String patchStream) throws OperationFailedException {
final InstalledIdentity installedIdentity;
if (patchStream != null) {
try {
installedIdentity = instMgr.getInstalledIdentity(patchStream, null);
} catch (PatchingException e) {
throw new OperationFailedException(PatchLogger.ROOT_LOGGER.failedToLoadInfo(patchStream), e);
}
} else {
installedIdentity = instMgr.getDefaultIdentity();
}
execute(context, operation, installedIdentity);
}
use of org.jboss.as.patching.PatchingException 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();
}
}
use of org.jboss.as.patching.PatchingException in project wildfly-core by wildfly.
the class IdentityPatchRunner method rollbackPatch.
/**
* Rollback a patch.
*
* @param patchId the patch id
* @param contentPolicy the content policy
* @param rollbackTo rollback multiple one off patches
* @param resetConfiguration whether to reset the configuration
* @param modification the installation modification
* @return the patching result
* @throws PatchingException
*/
public PatchingResult rollbackPatch(final String patchId, final ContentVerificationPolicy contentPolicy, final boolean rollbackTo, final boolean resetConfiguration, InstallationManager.InstallationModification modification) throws PatchingException {
if (Constants.BASE.equals(patchId)) {
throw PatchLogger.ROOT_LOGGER.cannotRollbackPatch(patchId);
}
try {
// Before rolling back the patch, validate that the state until that point is consistent
validateRollbackState(patchId, modification.getUnmodifiedInstallationState());
} catch (PatchingException e) {
throw e;
} catch (Exception e) {
throw new PatchingException(e);
}
// Figure out what to do
final List<String> patches = new ArrayList<String>();
final List<String> oneOffs = modification.getPatchIDs();
final int index = oneOffs.indexOf(patchId);
if (index == -1) {
if (patchId.equals(modification.getCumulativePatchID())) {
// Rollback all active
patches.addAll(oneOffs);
patches.add(modification.getCumulativePatchID());
} else {
throw PatchLogger.ROOT_LOGGER.cannotRollbackPatch(patchId);
}
} else if (index == 0) {
patches.add(patchId);
} else {
if (rollbackTo) {
// rollback one-offs up to the given patchId
for (int i = 0; i <= index; i++) {
patches.add(oneOffs.get(i));
}
} else {
// TODO perhaps we can allow this as well?
throw PatchLogger.ROOT_LOGGER.cannotRollbackPatch(patchId);
}
}
final File historyDir = installedImage.getPatchHistoryDir(patchId);
assertExists(historyDir);
final File rollbackXml = new File(historyDir, Constants.ROLLBACK_XML);
assertExists(rollbackXml);
final File workDir = createTempDir();
final PatchContentProvider provider = PatchContentProvider.ROLLBACK_PROVIDER;
final IdentityPatchContext context = new IdentityPatchContext(workDir, provider, contentPolicy, modification, ROLLBACK, installedImage);
try {
// Rollback patches
for (final String rollback : patches) {
if (!Constants.BASE.equals(rollback)) {
rollback(rollback, context);
modification.removeInstalledPatch(rollback);
}
}
// Execute the tasks
final IdentityPatchContext.PatchEntry identity = context.getIdentityEntry();
final IdentityRollbackCallback callback = new IdentityRollbackCallback(patchId, patches, resetConfiguration, identity.getDirectoryStructure());
try {
return executeTasks(context, callback);
} catch (Exception e) {
context.cancel(callback);
PatchLogger.ROOT_LOGGER.debugf(e, "failed to rollback patch %s", patchId);
throw rethrowException(e);
}
} finally {
if (workDir != null && !IoUtils.recursiveDelete(workDir)) {
PatchLogger.ROOT_LOGGER.cannotDeleteFile(workDir.getAbsolutePath());
}
context.cleanup();
}
}
use of org.jboss.as.patching.PatchingException 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);
}
}
use of org.jboss.as.patching.PatchingException in project wildfly-core by wildfly.
the class IdentityPatchRunner 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_SUFFIX + count);
}
if (!workDir.mkdirs()) {
throw new PatchingException(PatchLogger.ROOT_LOGGER.cannotCreateDirectory(workDir.getAbsolutePath()));
}
return workDir;
}
Aggregations