Search in sources :

Example 11 with PatchingException

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

the class PatchToolImpl method applyPatch.

private PatchingResult applyPatch(final File parentWorkDir, final InputStream is, final ContentVerificationPolicy contentPolicy) throws PatchingException {
    File workDir = null;
    try {
        // Create a working dir
        workDir = parentWorkDir == null ? IdentityPatchRunner.createTempDir() : IdentityPatchRunner.createTempDir(parentWorkDir);
        try {
            // Save the content
            Path cachedContent = workDir.toPath().resolve("content");
            Files.copy(is, cachedContent);
            // Unpack to the work dir
            ZipUtils.unzip(cachedContent.toFile(), workDir);
        } catch (IOException e) {
            // add info that temp dir is involved
            throw PatchLogger.ROOT_LOGGER.cannotCopyFilesToTempDir(workDir.getAbsolutePath(), e.getMessage(), e);
        }
        // Execute
        return execute(workDir, contentPolicy);
    } catch (Exception e) {
        throw rethrowException(e);
    } finally {
        if (workDir != null && !IoUtils.recursiveDelete(workDir)) {
            PatchLogger.ROOT_LOGGER.cannotDeleteFile(workDir.getAbsolutePath());
        }
    }
}
Also used : Path(java.nio.file.Path) IOException(java.io.IOException) File(java.io.File) XMLStreamException(javax.xml.stream.XMLStreamException) PatchingException(org.jboss.as.patching.PatchingException) IOException(java.io.IOException)

Example 12 with PatchingException

use of org.jboss.as.patching.PatchingException 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 13 with PatchingException

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

the class PatchXmlUtils method parseMiscModification.

static ContentModification parseMiscModification(final XMLExtendedStreamReader reader, ModificationType type) throws XMLStreamException {
    String path = null;
    byte[] hash = NO_CONTENT;
    boolean directory = false;
    boolean affectsRuntime = false;
    byte[] targetHash = NO_CONTENT;
    ModificationCondition condition = null;
    final int count = reader.getAttributeCount();
    for (int i = 0; i < count; i++) {
        final Attribute attribute = Attribute.forName(reader.getAttributeLocalName(i));
        switch(attribute) {
            case DIRECTORY:
                directory = Boolean.parseBoolean(reader.getAttributeValue(i));
                break;
            case PATH:
                path = reader.getAttributeValue(i);
                break;
            case HASH:
                if (type == ModificationType.REMOVE) {
                    targetHash = hexStringToByteArray(reader.getAttributeValue(i));
                } else {
                    hash = hexStringToByteArray(reader.getAttributeValue(i));
                }
                break;
            case NEW_HASH:
                if (type == ModificationType.REMOVE) {
                    hash = hexStringToByteArray(reader.getAttributeValue(i));
                } else {
                    targetHash = hexStringToByteArray(reader.getAttributeValue(i));
                }
                break;
            case IN_RUNTIME_USE:
                affectsRuntime = Boolean.parseBoolean(reader.getAttributeValue(i));
                break;
            case CONDITION:
                try {
                    condition = ModificationCondition.Factory.fromString(reader.getAttributeValue(i));
                } catch (PatchingException e) {
                    throw ControllerLogger.ROOT_LOGGER.invalidAttributeValue(reader.getAttributeValue(i), new QName(attribute.name), reader.getLocation());
                }
                break;
            default:
                throw unexpectedAttribute(reader, i);
        }
    }
    requireNoContent(reader);
    final MiscContentItem item = createMiscItem(path, hash, directory, affectsRuntime);
    return new ContentModification(item, targetHash, type, condition);
}
Also used : PatchingException(org.jboss.as.patching.PatchingException) ParseUtils.unexpectedAttribute(org.jboss.as.controller.parsing.ParseUtils.unexpectedAttribute) QName(javax.xml.namespace.QName) HashUtils.bytesToHexString(org.jboss.as.patching.HashUtils.bytesToHexString)

Example 14 with PatchingException

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

the class InstallationManagerImpl method getInstalledIdentity.

/**
 * This method returns the installed identity with the requested name and version.
 * If the product name is null, the default identity will be returned.
 *
 * If the product name was recognized and the requested version was not null,
 * the version comparison will take place. If the version of the currently installed product
 * doesn't match the requested one, the exception will be thrown.
 * If the requested version is null, the currently installed identity with the requested name
 * will be returned.
 *
 * If the product name was not recognized among the registered ones, a new installed identity
 * with the requested name will be created and returned. (This is because the patching system
 * is not aware of how many and what the patching streams there are expected).
 *
 * @param productName
 * @param productVersion
 * @return
 * @throws PatchingException
 */
@Override
public InstalledIdentity getInstalledIdentity(String productName, String productVersion) throws PatchingException {
    final String defaultIdentityName = defaultIdentity.getIdentity().getName();
    if (productName == null) {
        productName = defaultIdentityName;
    }
    final File productConf = new File(installedImage.getInstallationMetadata(), productName + Constants.DOT_CONF);
    final String recordedProductVersion;
    if (!productConf.exists()) {
        recordedProductVersion = null;
    } else {
        final Properties props = loadProductConf(productConf);
        recordedProductVersion = props.getProperty(Constants.CURRENT_VERSION);
    }
    if (defaultIdentityName.equals(productName)) {
        if (recordedProductVersion != null && !recordedProductVersion.equals(defaultIdentity.getIdentity().getVersion())) {
            // this means the patching history indicates that the current version is different from the one specified in the server's version module,
            // which could happen in case:
            // - the last applied CP didn't include the new version module or
            // - the version module version included in the last CP didn't match the version specified in the CP's metadata, or
            // - the version module was updated from a one-off, or
            // - the patching history was edited somehow
            // In any case, here I decided to rely on the patching history.
            defaultIdentity = loadIdentity(productName, recordedProductVersion);
        }
        if (productVersion != null && !defaultIdentity.getIdentity().getVersion().equals(productVersion)) {
            throw new PatchingException(PatchLogger.ROOT_LOGGER.productVersionDidNotMatchInstalled(productName, productVersion, defaultIdentity.getIdentity().getVersion()));
        }
        return defaultIdentity;
    }
    if (recordedProductVersion != null && !Constants.UNKNOWN.equals(recordedProductVersion)) {
        if (productVersion != null) {
            if (!productVersion.equals(recordedProductVersion)) {
                throw new PatchingException(PatchLogger.ROOT_LOGGER.productVersionDidNotMatchInstalled(productName, productVersion, recordedProductVersion));
            }
        } else {
            productVersion = recordedProductVersion;
        }
    }
    return loadIdentity(productName, productVersion);
}
Also used : PatchingException(org.jboss.as.patching.PatchingException) Properties(java.util.Properties) File(java.io.File)

Example 15 with PatchingException

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

the class InstallationManagerImpl method getInstalledIdentities.

/**
 * This method will return a list of installed identities for which
 * the corresponding .conf file exists under .installation directory.
 * The list will also include the default identity even if the .conf
 * file has not been created for it.
 */
@Override
public List<InstalledIdentity> getInstalledIdentities() throws PatchingException {
    List<InstalledIdentity> installedIdentities;
    final File metadataDir = installedImage.getInstallationMetadata();
    if (!metadataDir.exists()) {
        installedIdentities = Collections.singletonList(defaultIdentity);
    } else {
        final String defaultConf = defaultIdentity.getIdentity().getName() + Constants.DOT_CONF;
        final File[] identityConfs = metadataDir.listFiles(new FileFilter() {

            @Override
            public boolean accept(File pathname) {
                return pathname.isFile() && pathname.getName().endsWith(Constants.DOT_CONF) && !pathname.getName().equals(defaultConf);
            }
        });
        if (identityConfs == null || identityConfs.length == 0) {
            installedIdentities = Collections.singletonList(defaultIdentity);
        } else {
            installedIdentities = new ArrayList<InstalledIdentity>(identityConfs.length + 1);
            installedIdentities.add(defaultIdentity);
            for (File conf : identityConfs) {
                final Properties props = loadProductConf(conf);
                String productName = conf.getName();
                productName = productName.substring(0, productName.length() - Constants.DOT_CONF.length());
                final String productVersion = props.getProperty(Constants.CURRENT_VERSION);
                InstalledIdentity identity;
                try {
                    identity = LayersFactory.load(installedImage, new ProductConfig(productName, productVersion, null), moduleRoots, bundleRoots);
                } catch (IOException e) {
                    throw new PatchingException(PatchLogger.ROOT_LOGGER.failedToLoadInfo(productName), e);
                }
                installedIdentities.add(identity);
            }
        }
    }
    return installedIdentities;
}
Also used : PatchingException(org.jboss.as.patching.PatchingException) ProductConfig(org.jboss.as.version.ProductConfig) IOException(java.io.IOException) FileFilter(java.io.FileFilter) Properties(java.util.Properties) File(java.io.File)

Aggregations

PatchingException (org.jboss.as.patching.PatchingException)39 File (java.io.File)20 IOException (java.io.IOException)19 InstalledIdentity (org.jboss.as.patching.installation.InstalledIdentity)17 XMLStreamException (javax.xml.stream.XMLStreamException)9 Patch (org.jboss.as.patching.metadata.Patch)9 PatchTool (org.jboss.as.patching.tool.PatchTool)8 PatchingResult (org.jboss.as.patching.tool.PatchingResult)8 InstallationManager (org.jboss.as.patching.installation.InstallationManager)7 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)5 PatchableTarget (org.jboss.as.patching.installation.PatchableTarget)5 InputStream (java.io.InputStream)4 BundledPatch (org.jboss.as.patching.metadata.BundledPatch)4 PatchElement (org.jboss.as.patching.metadata.PatchElement)4 PatchElementProvider (org.jboss.as.patching.metadata.PatchElementProvider)4 ModelNode (org.jboss.dmr.ModelNode)4 OperationContext (org.jboss.as.controller.OperationContext)3 ContentModification (org.jboss.as.patching.metadata.ContentModification)3 Identity (org.jboss.as.patching.metadata.Identity)3