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);
}
}
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());
}
}
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()));
}
}
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());
}
Aggregations