Search in sources :

Example 1 with Deployment

use of org.exist.repo.Deployment 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 2 with Deployment

use of org.exist.repo.Deployment 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 3 with Deployment

use of org.exist.repo.Deployment 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)

Example 4 with Deployment

use of org.exist.repo.Deployment in project exist by eXist-db.

the class RestoreAppsTest method createAndInstallApp.

private void createAndInstallApp(String version, String repoDescriptor) throws IOException, PackageException, EXistException {
    String descriptor = "<package xmlns=\"http://expath.org/ns/pkg\" name=\"http://existsolutions.com/apps/backup-test\"\n" + "   abbrev=\"backup-test\" version=\"" + version + "\" spec=\"1.0\">\n" + "   <title>Backup Test App</title>\n" + "   <dependency processor=\"http://exist-db.org\" semver-min=\"5.0.0-RC8\"/>\n" + "</package>";
    Path xarFile = temporaryFolder.newFile().toPath();
    try (ZipOutputStream zos = new ZipOutputStream(Files.newOutputStream(xarFile, StandardOpenOption.WRITE))) {
        ZipEntry entry = new ZipEntry("expath-pkg.xml");
        zos.putNextEntry(entry);
        byte[] bytes = descriptor.getBytes(StandardCharsets.UTF_8);
        zos.write(bytes);
        zos.closeEntry();
        entry = new ZipEntry("repo.xml");
        zos.putNextEntry(entry);
        bytes = repoDescriptor.getBytes(StandardCharsets.UTF_8);
        zos.write(bytes);
        zos.closeEntry();
    }
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    Optional<ExistRepository> repo = pool.getExpathRepo();
    if (!repo.isPresent()) {
        throw new EXistException("expath repository not available for test");
    }
    XarSource xar = new XarFileSource(xarFile);
    Deployment deployment = new Deployment();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
        final Txn transaction = pool.getTransactionManager().beginTransaction()) {
        deployment.installAndDeploy(broker, transaction, xar, null);
        transaction.commit();
    }
    Packages pkgs = repo.get().getParentRepo().getPackages("http://existsolutions.com/apps/backup-test");
    assertEquals(1, pkgs.packages().size());
}
Also used : Path(java.nio.file.Path) ZipEntry(java.util.zip.ZipEntry) Deployment(org.exist.repo.Deployment) EXistException(org.exist.EXistException) Txn(org.exist.storage.txn.Txn) DBBroker(org.exist.storage.DBBroker) ZipOutputStream(java.util.zip.ZipOutputStream) BrokerPool(org.exist.storage.BrokerPool) ExistRepository(org.exist.repo.ExistRepository)

Aggregations

Deployment (org.exist.repo.Deployment)4 IOException (java.io.IOException)3 PackageException (org.expath.pkg.repo.PackageException)3 Txn (org.exist.storage.txn.Txn)2 XarSource (org.expath.pkg.repo.XarSource)2 Path (java.nio.file.Path)1 ZipEntry (java.util.zip.ZipEntry)1 ZipOutputStream (java.util.zip.ZipOutputStream)1 EXistException (org.exist.EXistException)1 DocumentImpl (org.exist.dom.persistent.DocumentImpl)1 LockedDocument (org.exist.dom.persistent.LockedDocument)1 ExistRepository (org.exist.repo.ExistRepository)1 PackageLoader (org.exist.repo.PackageLoader)1 PermissionDeniedException (org.exist.security.PermissionDeniedException)1 BrokerPool (org.exist.storage.BrokerPool)1 DBBroker (org.exist.storage.DBBroker)1 TransactionException (org.exist.storage.txn.TransactionException)1 XmldbURI (org.exist.xmldb.XmldbURI)1