Search in sources :

Example 1 with DependencyVersion

use of org.expath.pkg.repo.deps.DependencyVersion in project exist by eXist-db.

the class Deployment method checkProcessorVersion.

private void checkProcessorVersion(final PackageLoader.Version version) throws PackageException {
    final String procVersion = SystemProperties.getInstance().getSystemProperty("product-version", "1.0");
    final DependencyVersion depVersion = version.getDependencyVersion();
    if (!depVersion.isCompatible(procVersion)) {
        throw new PackageException("Package requires eXist-db version " + version.toString() + ". " + "Installed version is " + procVersion);
    }
}
Also used : DependencyVersion(org.expath.pkg.repo.deps.DependencyVersion)

Example 2 with DependencyVersion

use of org.expath.pkg.repo.deps.DependencyVersion in project exist by eXist-db.

the class Deployment method installAndDeploy.

/**
 * Install and deploy a give xar archive. Dependencies are installed from
 * the PackageLoader.
 *
 * @param broker the broker to use
 * @param transaction the transaction for this deployment task
 * @param xar the .xar file to install
 * @param loader package loader to use
 * @param enforceDeps when set to true, the method will throw an exception if a dependency could not be resolved
 *                    or an older version of the required dependency is installed and needs to be replaced.
 * @return the collection path to which the package was deployed or Optional.empty if not deployed
 * @throws PackageException if package installation failed
 * @throws IOException in case of an IO error
 */
public Optional<String> installAndDeploy(final DBBroker broker, final Txn transaction, final XarSource xar, final PackageLoader loader, boolean enforceDeps) throws PackageException, IOException {
    final Optional<DocumentImpl> descriptor = getDescriptor(broker, xar);
    if (!descriptor.isPresent()) {
        throw new PackageException("Missing descriptor from package: " + xar.getURI());
    }
    final DocumentImpl document = descriptor.get();
    final ElementImpl root = (ElementImpl) document.getDocumentElement();
    final String name = root.getAttribute("name");
    final String pkgVersion = root.getAttribute("version");
    final Optional<ExistRepository> repo = broker.getBrokerPool().getExpathRepo();
    if (repo.isPresent()) {
        final Packages packages = repo.get().getParentRepo().getPackages(name);
        if (packages != null && (!enforceDeps || pkgVersion.equals(packages.latest().getVersion()))) {
            LOG.info("Application package {} already installed. Skipping.", name);
            final Package pkg = packages.latest();
            return Optional.of(getTargetCollection(broker, pkg, getPackageDir(pkg)));
        }
        InMemoryNodeSet deps;
        try {
            deps = findElements(root, DEPENDENCY_ELEMENT);
            for (final SequenceIterator i = deps.iterate(); i.hasNext(); ) {
                final Element dependency = (Element) i.nextItem();
                final String pkgName = dependency.getAttribute("package");
                final String processor = dependency.getAttribute("processor");
                final String versionStr = dependency.getAttribute("version");
                final String semVer = dependency.getAttribute("semver");
                final String semVerMin = dependency.getAttribute("semver-min");
                final String semVerMax = dependency.getAttribute("semver-max");
                PackageLoader.Version version = null;
                if (semVer != null) {
                    version = new PackageLoader.Version(semVer, true);
                } else if (semVerMax != null || semVerMin != null) {
                    version = new PackageLoader.Version(semVerMin, semVerMax);
                } else if (pkgVersion != null) {
                    version = new PackageLoader.Version(versionStr, false);
                }
                if (processor != null && processor.equals(PROCESSOR_NAME) && version != null) {
                    checkProcessorVersion(version);
                } else if (pkgName != null) {
                    LOG.info("Package {} depends on {}", name, pkgName);
                    boolean isInstalled = false;
                    if (repo.get().getParentRepo().getPackages(pkgName) != null) {
                        LOG.debug("Package {} already installed", pkgName);
                        Packages pkgs = repo.get().getParentRepo().getPackages(pkgName);
                        // check if installed package matches required version
                        if (pkgs != null) {
                            if (version != null) {
                                Package latest = pkgs.latest();
                                DependencyVersion depVersion = version.getDependencyVersion();
                                if (depVersion.isCompatible(latest.getVersion())) {
                                    isInstalled = true;
                                } else {
                                    LOG.debug("Package {} needs to be upgraded", pkgName);
                                    if (enforceDeps) {
                                        throw new PackageException("Package requires version " + version.toString() + " of package " + pkgName + ". Installed version is " + latest.getVersion() + ". Please upgrade!");
                                    }
                                }
                            } else {
                                isInstalled = true;
                            }
                            if (isInstalled) {
                                LOG.debug("Package {} already installed", pkgName);
                            }
                        }
                    }
                    if (!isInstalled && loader != null) {
                        final XarSource depFile = loader.load(pkgName, version);
                        if (depFile != null) {
                            installAndDeploy(broker, transaction, depFile, loader);
                        } else {
                            if (enforceDeps) {
                                LOG.warn("Missing dependency: package {} could not be resolved. This error is not fatal, but the package may not work as expected", pkgName);
                            } else {
                                throw new PackageException("Missing dependency: package " + pkgName + " could not be resolved.");
                            }
                        }
                    }
                }
            }
        } catch (final XPathException e) {
            throw new PackageException("Invalid descriptor found in " + xar.getURI());
        }
        // installing the xar into the expath repo
        LOG.info("Installing package {}", xar.getURI());
        final UserInteractionStrategy interact = new BatchUserInteraction();
        final org.expath.pkg.repo.Package pkg = repo.get().getParentRepo().installPackage(xar, true, interact);
        final ExistPkgInfo info = (ExistPkgInfo) pkg.getInfo("exist");
        if (info != null && !info.getJars().isEmpty()) {
            ClasspathHelper.updateClasspath(broker.getBrokerPool(), pkg);
        }
        broker.getBrokerPool().getXQueryPool().clear();
        final String pkgName = pkg.getName();
        // signal status
        broker.getBrokerPool().reportStatus("Installing app: " + pkg.getAbbrev());
        repo.get().reportAction(ExistRepository.Action.INSTALL, pkg.getName());
        LOG.info("Deploying package {}", pkgName);
        return deploy(broker, transaction, pkgName, repo, null);
    }
    // Totally unnecessary to do the above if repo is unavailable.
    return Optional.empty();
}
Also used : Element(org.w3c.dom.Element) org.expath.pkg.repo(org.expath.pkg.repo) SequenceIterator(org.exist.xquery.value.SequenceIterator) DependencyVersion(org.expath.pkg.repo.deps.DependencyVersion) DependencyVersion(org.expath.pkg.repo.deps.DependencyVersion) Package(org.expath.pkg.repo.Package) Package(org.expath.pkg.repo.Package) BatchUserInteraction(org.expath.pkg.repo.tui.BatchUserInteraction)

Aggregations

DependencyVersion (org.expath.pkg.repo.deps.DependencyVersion)2 SequenceIterator (org.exist.xquery.value.SequenceIterator)1 org.expath.pkg.repo (org.expath.pkg.repo)1 Package (org.expath.pkg.repo.Package)1 BatchUserInteraction (org.expath.pkg.repo.tui.BatchUserInteraction)1 Element (org.w3c.dom.Element)1