Search in sources :

Example 1 with PackageException

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

the class AppRestoreUtils method checkApps.

/**
 * Inspects the apps contained in the backup against installed apps in the database
 * and return a set of symbolic backup paths pointing to the collection of those
 * apps for which newer versions are installed within the database. The returned
 * paths may then be ignored during a restore.
 *
 * The method attempts to be fail safe to make sure even bad backups can be restored. Errors
 * reading package descriptors are thus only logged and should not abort the process.
 *
 * @param broker the broker used for reading the backup and retrieving the expath repo
 * @param descriptors a queue of backup descriptors to inspect
 * @return a set of paths for which newer versions exist in the database. may be empty.
 */
public static Set<String> checkApps(final DBBroker broker, final Deque<BackupDescriptor> descriptors) {
    final List<AppDetail> apps = getAppsFromBackup(broker, descriptors);
    final Set<String> paths = new HashSet<>();
    final Optional<ExistRepository> repo = broker.getBrokerPool().getExpathRepo();
    if (repo.isPresent()) {
        for (final AppDetail app : apps) {
            final Packages packages = repo.get().getParentRepo().getPackages(app.name);
            if (packages != null) {
                final Package latest = packages.latest();
                try {
                    final Semver version = Semver.parse(latest.getVersion());
                    if (version.compareTo(app.version) > 0) {
                        paths.add(app.path);
                    }
                } catch (PackageException e) {
                    LOG.warn("Invalid semver in expath repository for {}", app.name, e);
                }
            }
        }
    }
    return paths;
}
Also used : Packages(org.expath.pkg.repo.Packages) PackageException(org.expath.pkg.repo.PackageException) Package(org.expath.pkg.repo.Package) Semver(org.expath.pkg.repo.deps.Semver) ExistRepository(org.exist.repo.ExistRepository)

Example 2 with PackageException

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

the class AppRestoreUtils method getAppsFromSubColl.

private static void getAppsFromSubColl(final List<AppDetail> result, final XMLReaderPool parserPool, final BackupDescriptor descriptor) {
    final List<String> collections = getSubcollectionNames(parserPool, descriptor);
    for (final String collection : collections) {
        final BackupDescriptor app = descriptor.getChildBackupDescriptor(collection);
        final InputSource is = app.getInputSource("expath-pkg.xml");
        if (is != null) {
            XMLReader reader = null;
            try {
                reader = parserPool.borrowXMLReader();
                reader.setContentHandler(new DefaultHandler() {

                    @Override
                    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
                        if (PKG_NAMESPACE.equals(uri) && "package".equals(localName)) {
                            final String version = attributes.getValue("version");
                            final String name = attributes.getValue("name");
                            if (StringUtils.isEmpty(version) || StringUtils.isEmpty(name)) {
                                LOG.warn("Invalid package descriptor for {}", app.getSymbolicPath());
                                return;
                            }
                            try {
                                final AppDetail detail = new AppDetail(app.getSymbolicPath(), name, Semver.parse(version));
                                result.add(detail);
                            } catch (PackageException e) {
                                LOG.warn("Invalid semver found while parsing {}", app.getSymbolicPath());
                            }
                        }
                    }
                });
                reader.parse(is);
            } catch (IOException | SAXException e) {
                LOG.warn("Parse exception while parsing {}", app.getSymbolicPath("expath-pkg.xml", false));
            } finally {
                if (reader != null) {
                    parserPool.returnXMLReader(reader);
                }
            }
        }
    }
}
Also used : InputSource(org.xml.sax.InputSource) Attributes(org.xml.sax.Attributes) IOException(java.io.IOException) BackupDescriptor(org.exist.backup.BackupDescriptor) DefaultHandler(org.xml.sax.helpers.DefaultHandler) SAXException(org.xml.sax.SAXException) PackageException(org.expath.pkg.repo.PackageException) XMLReader(org.xml.sax.XMLReader)

Example 3 with PackageException

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

the class Deploy method eval.

@Override
public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
    if (!context.getSubject().hasDbaRole())
        throw new XPathException(this, EXPathErrorCode.EXPDY003, "Permission denied. You need to be a member " + "of the dba group to use repo:deploy/undeploy");
    final String pkgName = args[0].getStringValue();
    try {
        Deployment deployment = new Deployment();
        final Optional<String> target;
        if (isCalledAs("deploy")) {
            String userTarget = null;
            if (getArgumentCount() == 2) {
                userTarget = args[1].getStringValue();
            }
            try (final Txn transaction = context.getBroker().continueOrBeginTransaction()) {
                target = deployment.deploy(context.getBroker(), transaction, pkgName, context.getRepository(), userTarget);
                transaction.commit();
            }
        } else if (isCalledAs("install-and-deploy")) {
            String version = null;
            final String repoURI;
            if (getArgumentCount() == 3) {
                version = args[1].getStringValue();
                repoURI = args[2].getStringValue();
            } else {
                repoURI = args[1].getStringValue();
            }
            try (final Txn transaction = context.getBroker().continueOrBeginTransaction()) {
                target = installAndDeploy(transaction, pkgName, version, repoURI);
                transaction.commit();
            }
        } else if (isCalledAs("install-and-deploy-from-db")) {
            String repoURI = null;
            if (getArgumentCount() == 2) {
                repoURI = args[1].getStringValue();
            }
            try (final Txn transaction = context.getBroker().continueOrBeginTransaction()) {
                target = installAndDeployFromDb(transaction, pkgName, repoURI);
                transaction.commit();
            }
        } else {
            try (final Txn transaction = context.getBroker().continueOrBeginTransaction()) {
                target = deployment.undeploy(context.getBroker(), transaction, pkgName, context.getRepository());
                transaction.commit();
            }
        }
        target.orElseThrow(() -> new XPathException("expath repository is not available."));
        return statusReport(target);
    } catch (PackageException e) {
        throw new XPathException(this, EXPathErrorCode.EXPDY001, e.getMessage(), args[0], e);
    } catch (IOException e) {
        throw new XPathException(this, ErrorCodes.FOER0000, "Caught IO error while deploying expath archive", args[0], e);
    } catch (TransactionException e) {
        throw new XPathException(this, ErrorCodes.FOER0000, "Caught transaction error while deploying expath archive", args[0], e);
    }
}
Also used : TransactionException(org.exist.storage.txn.TransactionException) Deployment(org.exist.repo.Deployment) PackageException(org.expath.pkg.repo.PackageException) Txn(org.exist.storage.txn.Txn) IOException(java.io.IOException)

Example 4 with PackageException

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

the class Deploy method installAndDeploy.

private Optional<String> installAndDeploy(final Txn transaction, final String pkgName, final String version, final String repoURI) throws XPathException {
    try {
        final RepoPackageLoader loader = new RepoPackageLoader(repoURI);
        final Deployment deployment = new Deployment();
        final XarSource xar = loader.load(pkgName, new PackageLoader.Version(version, false));
        if (xar != null) {
            return deployment.installAndDeploy(context.getBroker(), transaction, xar, loader);
        }
        return Optional.empty();
    } catch (final MalformedURLException e) {
        throw new XPathException(this, EXPathErrorCode.EXPDY005, "Malformed URL: " + repoURI);
    } catch (final PackageException | IOException e) {
        LOG.error(e.getMessage(), e);
        throw new XPathException(this, EXPathErrorCode.EXPDY007, e.getMessage());
    }
}
Also used : PackageLoader(org.exist.repo.PackageLoader) XarSource(org.expath.pkg.repo.XarSource) Deployment(org.exist.repo.Deployment) PackageException(org.expath.pkg.repo.PackageException) IOException(java.io.IOException)

Example 5 with PackageException

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

the class Deploy method installAndDeployFromDb.

private Optional<String> installAndDeployFromDb(final Txn transaction, final String path, final String repoURI) throws XPathException {
    final XmldbURI docPath = XmldbURI.createInternal(path);
    try (final LockedDocument lockedDoc = context.getBroker().getXMLResource(docPath, LockMode.READ_LOCK)) {
        if (lockedDoc == null) {
            throw new XPathException(this, EXPathErrorCode.EXPDY001, path + " no such .xar", new StringValue(path));
        }
        final DocumentImpl doc = lockedDoc.getDocument();
        if (doc.getResourceType() != DocumentImpl.BINARY_FILE) {
            throw new XPathException(this, EXPathErrorCode.EXPDY001, path + " is not a valid .xar", new StringValue(path));
        }
        RepoPackageLoader loader = null;
        if (repoURI != null) {
            loader = new RepoPackageLoader(repoURI);
        }
        final XarSource xarSource = new BinaryDocumentXarSource(context.getBroker().getBrokerPool(), transaction, (BinaryDocument) doc);
        final Deployment deployment = new Deployment();
        return deployment.installAndDeploy(context.getBroker(), transaction, xarSource, loader);
    } catch (PackageException | IOException | PermissionDeniedException e) {
        LOG.error(e.getMessage(), e);
        throw new XPathException(this, EXPathErrorCode.EXPDY007, "Package installation failed: " + e.getMessage(), new StringValue(e.getMessage()));
    }
}
Also used : Deployment(org.exist.repo.Deployment) IOException(java.io.IOException) DocumentImpl(org.exist.dom.persistent.DocumentImpl) XarSource(org.expath.pkg.repo.XarSource) LockedDocument(org.exist.dom.persistent.LockedDocument) PackageException(org.expath.pkg.repo.PackageException) PermissionDeniedException(org.exist.security.PermissionDeniedException) XmldbURI(org.exist.xmldb.XmldbURI)

Aggregations

PackageException (org.expath.pkg.repo.PackageException)12 IOException (java.io.IOException)6 URI (java.net.URI)3 Deployment (org.exist.repo.Deployment)3 ExistRepository (org.exist.repo.ExistRepository)3 URISyntaxException (java.net.URISyntaxException)2 Path (java.nio.file.Path)2 StreamSource (javax.xml.transform.stream.StreamSource)2 Txn (org.exist.storage.txn.Txn)2 XPathException (org.exist.xquery.XPathException)2 Package (org.expath.pkg.repo.Package)2 Packages (org.expath.pkg.repo.Packages)2 NotExistException (org.expath.pkg.repo.Storage.NotExistException)2 XarSource (org.expath.pkg.repo.XarSource)2 Writer (java.io.Writer)1 Files (java.nio.file.Files)1 Paths (java.nio.file.Paths)1 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1 List (java.util.List)1