Search in sources :

Example 16 with PatchingResult

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

the class LayerTestCase method patchLayer.

@Test
public void patchLayer() throws Exception {
    // add a layer
    // randomString();
    String layerName = "mylayer";
    installLayers(layerName);
    InstalledIdentity installedIdentity = loadInstalledIdentity();
    // build a one-off patch for the layer with 1 added module
    // and 1 add file
    String patchID = randomString();
    File patchDir = mkdir(tempDir, patchID);
    // randomString();
    String layerPatchId = "mylayerPatchID";
    String moduleName = randomString();
    ContentModification moduleAdded = ContentModificationUtils.addModule(patchDir, layerPatchId, moduleName);
    ContentModification fileAdded = ContentModificationUtils.addMisc(patchDir, patchID, "new file resource", "bin", "my-new-standalone.sh");
    Patch patch = PatchBuilder.create().setPatchId(patchID).oneOffPatchIdentity(installedIdentity.getIdentity().getName(), installedIdentity.getIdentity().getVersion()).getParent().oneOffPatchElement(layerPatchId, layerName, false).addContentModification(moduleAdded).getParent().addContentModification(fileAdded).build();
    createPatchXMLFile(patchDir, patch);
    File zippedPatch = createZippedPatchFile(patchDir, patchID);
    // apply patch
    PatchingResult result = executePatch(zippedPatch);
    assertPatchHasBeenApplied(result, patch);
    InstalledIdentity patchedInstalledIdentity = loadInstalledIdentity();
    assertInstallationIsPatched(patch, patchedInstalledIdentity.getIdentity().loadTargetInfo());
    assertFileExists(env.getInstalledImage().getJbossHome(), "bin", fileAdded.getItem().getName());
    DirectoryStructure layerDirStructure = installedIdentity.getLayers().get(0).loadTargetInfo().getDirectoryStructure();
    File modulesPatchDir = layerDirStructure.getModulePatchDirectory(layerPatchId);
    assertDirExists(modulesPatchDir);
    assertDefinedModule(modulesPatchDir, moduleName, moduleAdded.getItem().getContentHash());
}
Also used : PatchingResult(org.jboss.as.patching.tool.PatchingResult) TestUtils.randomString(org.jboss.as.patching.runner.TestUtils.randomString) IoUtils.newFile(org.jboss.as.patching.IoUtils.newFile) TestUtils.createPatchXMLFile(org.jboss.as.patching.runner.TestUtils.createPatchXMLFile) File(java.io.File) TestUtils.createZippedPatchFile(org.jboss.as.patching.runner.TestUtils.createZippedPatchFile) ContentModification(org.jboss.as.patching.metadata.ContentModification) Patch(org.jboss.as.patching.metadata.Patch) DirectoryStructure(org.jboss.as.patching.DirectoryStructure) Test(org.junit.Test)

Example 17 with PatchingResult

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

the class IdentityPatchContext method finalize.

/**
 * Finalize the patch.
 *
 * @param callback the finalize callback
 * @return the result
 * @throws Exception
 */
protected PatchingResult finalize(final FinalizeCallback callback) throws Exception {
    assert state == State.NEW;
    final Patch original = callback.getPatch();
    final Patch.PatchType patchType = original.getIdentity().getPatchType();
    final String patchId;
    if (patchType == Patch.PatchType.CUMULATIVE) {
        patchId = modification.getCumulativePatchID();
    } else {
        patchId = original.getPatchId();
    }
    try {
        // The processed patch, based on the recorded changes
        final Patch processedPatch = createProcessedPatch(original);
        // The rollback containing all the recorded rollback actions
        final RollbackPatch rollbackPatch = createRollbackPatch(patchId, patchType);
        callback.finishPatch(processedPatch, rollbackPatch, this);
    } catch (Exception e) {
        if (undoChanges()) {
            callback.operationCancelled(this);
        }
        throw e;
    }
    state = State.PREPARED;
    return new PatchingResult() {

        @Override
        public String getPatchId() {
            return original.getPatchId();
        }

        @Override
        public PatchInfo getPatchInfo() {
            return new PatchInfo() {

                @Override
                public String getVersion() {
                    return identityEntry.getResultingVersion();
                }

                @Override
                public String getCumulativePatchID() {
                    return identityEntry.delegate.getModifiedState().getCumulativePatchID();
                }

                @Override
                public List<String> getPatchIDs() {
                    return identityEntry.delegate.getModifiedState().getPatchIDs();
                }
            };
        }

        @Override
        public void commit() {
            if (state == State.PREPARED) {
                complete(modification, callback);
            } else {
                undoChanges();
                throw new IllegalStateException();
            }
        }

        @Override
        public void rollback() {
            if (undoChanges()) {
                try {
                    callback.operationCancelled(IdentityPatchContext.this);
                } finally {
                    modification.cancel();
                }
            }
        }
    };
}
Also used : PatchInfo(org.jboss.as.patching.PatchInfo) PatchingResult(org.jboss.as.patching.tool.PatchingResult) RollbackPatch(org.jboss.as.patching.metadata.RollbackPatch) Patch(org.jboss.as.patching.metadata.Patch) RollbackPatch(org.jboss.as.patching.metadata.RollbackPatch) XMLStreamException(javax.xml.stream.XMLStreamException) PatchingException(org.jboss.as.patching.PatchingException) IOException(java.io.IOException)

Example 18 with PatchingResult

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

the class PatchToolImpl method applyPatchBundle.

protected PatchingResult applyPatchBundle(final File workDir, final BundledPatch bundledPatch, final ContentVerificationPolicy contentPolicy) throws PatchingException, IOException {
    final List<BundledPatchEntry> patches = bundledPatch.getPatches();
    if (patches.isEmpty()) {
        throw new PatchingException(PatchLogger.ROOT_LOGGER.patchBundleIsEmpty());
    }
    PatchingResult result = null;
    BundledPatchEntry lastCommittedEntry = null;
    final List<BundledPatch.BundledPatchEntry> results = new ArrayList<BundledPatch.BundledPatchEntry>(patches.size());
    final List<InstalledIdentity> installedIdentities = manager.getInstalledIdentities();
    for (BundledPatchEntry entry : patches) {
        // TODO this has to be checked against the specific one targeted by the patch
        boolean alreadyApplied = false;
        for (InstalledIdentity identity : installedIdentities) {
            if (identity.getAllInstalledPatches().contains(entry.getPatchId())) {
                alreadyApplied = true;
                break;
            }
        }
        if (alreadyApplied) {
            continue;
        }
        if (result != null) {
            result.commit();
            results.add(0, lastCommittedEntry);
        }
        final File patch = new File(workDir, entry.getPatchPath());
        final FileInputStream is = new FileInputStream(patch);
        PatchingResult currentResult = null;
        try {
            currentResult = applyPatch(workDir, is, contentPolicy);
        } catch (PatchingException e) {
            // Undo the changes included as part of this patch
            for (BundledPatch.BundledPatchEntry committed : results) {
                try {
                    rollback(committed.getPatchId(), contentPolicy, false, false).commit();
                } catch (PatchingException oe) {
                    PatchLogger.ROOT_LOGGER.debugf(oe, "failed to rollback patch '%s'", committed.getPatchId());
                }
            }
            throw e;
        } finally {
            safeClose(is);
        }
        if (currentResult != null) {
            result = currentResult;
            lastCommittedEntry = entry;
        }
    }
    if (result == null) {
        throw new PatchingException();
    }
    return new WrappedMultiInstallPatch(result, contentPolicy, results);
}
Also used : InstalledIdentity(org.jboss.as.patching.installation.InstalledIdentity) PatchingException(org.jboss.as.patching.PatchingException) ArrayList(java.util.ArrayList) BundledPatch(org.jboss.as.patching.metadata.BundledPatch) FileInputStream(java.io.FileInputStream) BundledPatchEntry(org.jboss.as.patching.metadata.BundledPatch.BundledPatchEntry) PatchingResult(org.jboss.as.patching.tool.PatchingResult) File(java.io.File)

Example 19 with PatchingResult

use of org.jboss.as.patching.tool.PatchingResult 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 20 with PatchingResult

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

the class AddOnTestCase method patchAddOn.

@Test
public void patchAddOn() throws Exception {
    // start from a base installation
    // add an add-on
    String addOnName = randomString();
    installAddOn(env.getModuleRoot(), addOnName);
    InstalledIdentity installedIdentity = loadInstalledIdentity();
    // build a one-off patch for the add-on with 1 added module
    // and 1 add file
    String patchID = randomString();
    File patchDir = mkdir(tempDir, patchID);
    String addOnPatchID = randomString();
    String moduleName = randomString();
    ContentModification moduleAdded = ContentModificationUtils.addModule(patchDir, addOnPatchID, moduleName);
    ContentModification fileAdded = ContentModificationUtils.addMisc(patchDir, patchID, "new file resource", "bin", "my-new-standalone.sh");
    Patch patch = PatchBuilder.create().setPatchId(patchID).oneOffPatchIdentity(installedIdentity.getIdentity().getName(), installedIdentity.getIdentity().getVersion()).getParent().oneOffPatchElement(addOnPatchID, addOnName, true).addContentModification(moduleAdded).getParent().addContentModification(fileAdded).build();
    createPatchXMLFile(patchDir, patch);
    File zippedPatch = createZippedPatchFile(patchDir, patchID);
    // apply patch
    PatchingResult result = executePatch(zippedPatch);
    assertPatchHasBeenApplied(result, patch);
    InstalledIdentity patchedInstalledIdentity = InstalledIdentity.load(env.getInstalledImage().getJbossHome(), productConfig, env.getInstalledImage().getModulesDir());
    assertInstallationIsPatched(patch, patchedInstalledIdentity.getIdentity().loadTargetInfo());
    assertFileExists(env.getInstalledImage().getJbossHome(), "bin", fileAdded.getItem().getName());
    DirectoryStructure addOnStructure = installedIdentity.getAddOns().iterator().next().loadTargetInfo().getDirectoryStructure();
    File modulesPatchDir = addOnStructure.getModulePatchDirectory(addOnPatchID);
    assertDirExists(modulesPatchDir);
    assertDefinedModule(modulesPatchDir, moduleName, moduleAdded.getItem().getContentHash());
}
Also used : PatchingResult(org.jboss.as.patching.tool.PatchingResult) TestUtils.randomString(org.jboss.as.patching.runner.TestUtils.randomString) IoUtils.newFile(org.jboss.as.patching.IoUtils.newFile) TestUtils.createPatchXMLFile(org.jboss.as.patching.runner.TestUtils.createPatchXMLFile) File(java.io.File) TestUtils.createZippedPatchFile(org.jboss.as.patching.runner.TestUtils.createZippedPatchFile) ContentModification(org.jboss.as.patching.metadata.ContentModification) Patch(org.jboss.as.patching.metadata.Patch) DirectoryStructure(org.jboss.as.patching.DirectoryStructure) Test(org.junit.Test)

Aggregations

PatchingResult (org.jboss.as.patching.tool.PatchingResult)37 File (java.io.File)31 Test (org.junit.Test)27 TestUtils.createPatchXMLFile (org.jboss.as.patching.runner.TestUtils.createPatchXMLFile)25 TestUtils.createZippedPatchFile (org.jboss.as.patching.runner.TestUtils.createZippedPatchFile)25 Patch (org.jboss.as.patching.metadata.Patch)24 TestUtils.randomString (org.jboss.as.patching.runner.TestUtils.randomString)23 ContentModification (org.jboss.as.patching.metadata.ContentModification)20 InstalledIdentity (org.jboss.as.patching.installation.InstalledIdentity)17 IoUtils.newFile (org.jboss.as.patching.IoUtils.newFile)13 HashUtils.hashFile (org.jboss.as.patching.HashUtils.hashFile)12 PatchingException (org.jboss.as.patching.PatchingException)9 PatchTool (org.jboss.as.patching.tool.PatchTool)8 IOException (java.io.IOException)5 DirectoryStructure (org.jboss.as.patching.DirectoryStructure)5 Identity (org.jboss.as.patching.installation.Identity)5 InstallationManager (org.jboss.as.patching.installation.InstallationManager)4 OperationContext (org.jboss.as.controller.OperationContext)3 ContentVerificationPolicy (org.jboss.as.patching.tool.ContentVerificationPolicy)3 ModelNode (org.jboss.dmr.ModelNode)3