[GitHub] [lucene-solr] janhoy commented on a change in pull request #994: SOLR-13662: Package Manager (CLI)
janhoy commented on a change in pull request #994: SOLR-13662: Package Manager (CLI) URL: https://github.com/apache/lucene-solr/pull/994#discussion_r346436605 ## File path: solr/core/src/java/org/apache/solr/packagemanager/RepositoryManager.java ## @@ -0,0 +1,328 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.solr.packagemanager; + +import static org.apache.solr.packagemanager.PackageUtils.getMapper; + +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.lang.invoke.MethodHandles; +import java.net.MalformedURLException; +import java.net.URL; +import java.nio.ByteBuffer; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; +import org.apache.lucene.util.Version; +import org.apache.solr.client.solrj.SolrRequest; +import org.apache.solr.client.solrj.SolrServerException; +import org.apache.solr.client.solrj.impl.HttpSolrClient; +import org.apache.solr.client.solrj.request.V2Request; +import org.apache.solr.client.solrj.request.beans.Package; +import org.apache.solr.client.solrj.response.V2Response; +import org.apache.solr.common.SolrException; +import org.apache.solr.common.SolrException.ErrorCode; +import org.apache.solr.common.cloud.SolrZkClient; +import org.apache.solr.core.BlobRepository; +import org.apache.solr.packagemanager.SolrPackage.Artifact; +import org.apache.solr.packagemanager.SolrPackage.SolrPackageRelease; +import org.apache.solr.pkg.PackageAPI; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +/** + * Handles most of the management of repositories and packages present in external repositories. + */ +public class RepositoryManager { + + private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + + final private PackageManager packageManager; + + public static final String systemVersion = Version.LATEST.toString(); + + final HttpSolrClient solrClient; + + public RepositoryManager(HttpSolrClient solrClient, PackageManager packageManager) { +this.packageManager = packageManager; +this.solrClient = solrClient; + } + + public List getPackages() { +List list = new ArrayList<>(getPackagesMap().values()); +Collections.sort(list); +return list; + } + + /** + * Get a map of package name to {@link SolrPackage} objects + */ + public Map getPackagesMap() { +Map packagesMap = new HashMap<>(); +for (PackageRepository repository: getRepositories()) { + packagesMap.putAll(repository.getPackages()); +} + +return packagesMap; + } + + /** + * List of added repositories + */ + public List getRepositories() { +// TODO: Instead of fetching again and again, we should look for caching this +PackageRepository items[]; +try { + items = getMapper().readValue(getRepositoriesJson(packageManager.zkClient), DefaultPackageRepository[].class); +} catch (IOException | KeeperException | InterruptedException e) { + throw new SolrException(ErrorCode.SERVER_ERROR, e); +} +List repositories = Arrays.asList(items); + +for (PackageRepository updateRepository: repositories) { + updateRepository.refresh(); +} + +return repositories; + } + + /** + * Add a repository to Solr + */ + public void addRepository(String name, String uri) throws KeeperException, InterruptedException, MalformedURLException, IOException { +String existingRepositoriesJson = getRepositoriesJson(packageManager.zkClient); +log.info(existingRepositoriesJson); + +List repos = getMapper().readValue(existingRepositoriesJson, List.class); +repos.add(new DefaultPackageRepository(name, uri)); +if (packageManager.zkClient.exists("/repositories.json", true) == false) { + packageManager.zkClient.create("/repositories.json", getMapper().writeValueAsString(repos).getBytes("UTF-8"), CreateMode.PERSISTENT,
[GitHub] [lucene-solr] janhoy commented on a change in pull request #994: SOLR-13662: Package Manager (CLI)
janhoy commented on a change in pull request #994: SOLR-13662: Package Manager (CLI) URL: https://github.com/apache/lucene-solr/pull/994#discussion_r346427550 ## File path: solr/core/src/java/org/apache/solr/packagemanager/RepositoryManager.java ## @@ -0,0 +1,328 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.solr.packagemanager; + +import static org.apache.solr.packagemanager.PackageUtils.getMapper; + +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.lang.invoke.MethodHandles; +import java.net.MalformedURLException; +import java.net.URL; +import java.nio.ByteBuffer; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; +import org.apache.lucene.util.Version; +import org.apache.solr.client.solrj.SolrRequest; +import org.apache.solr.client.solrj.SolrServerException; +import org.apache.solr.client.solrj.impl.HttpSolrClient; +import org.apache.solr.client.solrj.request.V2Request; +import org.apache.solr.client.solrj.request.beans.Package; +import org.apache.solr.client.solrj.response.V2Response; +import org.apache.solr.common.SolrException; +import org.apache.solr.common.SolrException.ErrorCode; +import org.apache.solr.common.cloud.SolrZkClient; +import org.apache.solr.core.BlobRepository; +import org.apache.solr.packagemanager.SolrPackage.Artifact; +import org.apache.solr.packagemanager.SolrPackage.SolrPackageRelease; +import org.apache.solr.pkg.PackageAPI; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +/** + * Handles most of the management of repositories and packages present in external repositories. + */ +public class RepositoryManager { + + private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + + final private PackageManager packageManager; + + public static final String systemVersion = Version.LATEST.toString(); + + final HttpSolrClient solrClient; + + public RepositoryManager(HttpSolrClient solrClient, PackageManager packageManager) { +this.packageManager = packageManager; +this.solrClient = solrClient; + } + + public List getPackages() { +List list = new ArrayList<>(getPackagesMap().values()); +Collections.sort(list); +return list; + } + + /** + * Get a map of package name to {@link SolrPackage} objects + */ + public Map getPackagesMap() { +Map packagesMap = new HashMap<>(); +for (PackageRepository repository: getRepositories()) { + packagesMap.putAll(repository.getPackages()); +} + +return packagesMap; + } + + /** + * List of added repositories + */ + public List getRepositories() { +// TODO: Instead of fetching again and again, we should look for caching this +PackageRepository items[]; +try { + items = getMapper().readValue(getRepositoriesJson(packageManager.zkClient), DefaultPackageRepository[].class); +} catch (IOException | KeeperException | InterruptedException e) { + throw new SolrException(ErrorCode.SERVER_ERROR, e); +} +List repositories = Arrays.asList(items); + +for (PackageRepository updateRepository: repositories) { + updateRepository.refresh(); +} + +return repositories; + } + + /** + * Add a repository to Solr + */ + public void addRepository(String name, String uri) throws KeeperException, InterruptedException, MalformedURLException, IOException { +String existingRepositoriesJson = getRepositoriesJson(packageManager.zkClient); +log.info(existingRepositoriesJson); + +List repos = getMapper().readValue(existingRepositoriesJson, List.class); +repos.add(new DefaultPackageRepository(name, uri)); +if (packageManager.zkClient.exists("/repositories.json", true) == false) { + packageManager.zkClient.create("/repositories.json", getMapper().writeValueAsString(repos).getBytes("UTF-8"), CreateMode.PERSISTENT,
[GitHub] [lucene-solr] janhoy commented on a change in pull request #994: SOLR-13662: Package Manager (CLI)
janhoy commented on a change in pull request #994: SOLR-13662: Package Manager (CLI) URL: https://github.com/apache/lucene-solr/pull/994#discussion_r345644978 ## File path: solr/core/src/test-files/solr/question-answer-repository/repository.json ## @@ -0,0 +1,57 @@ +[ + { +"name": "question-answer", +"description": "A natural language question answering plugin", +"versions": [ + { +"version": "1.0.0", +"date": "2019-01-01", +"artifacts": [ + { +"url": "question-answer-request-handler-1.0.jar", +"sig": "C9UWKkucmY3UNzqn0VLneVMe9kCbJjw7Urc76vGenoRwp32xvNn5ZIGZ7G34xZP7cVjqn/ltDlLWBZ/C3eAtuw==" + } +], +"manifest": { + "min-solr-version": "8.0", + "max-solr-version": "9.99", + "plugins": [ +{ + "name": "request-handler", + "setup-command": { +"path": "/api/collections/${collection}/config", Review comment: Will you allow a package to run any HTTP command against the cluster? What privilege does the pkg manager run under? Imagine someone posts a malicious package "superduper-solr-package" somewhere that attempts to run a setup-command `/api/authentication/disable` :-) or `/api/c/.system/update/foo` or some other arbitrary command? That's one reason I think we should turn this whole deploy upside down and let Solr plugin points fetch info from the plugin instead of the plugin author hardcoding some literal api commands against the cluster. ยด This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services - To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org For additional commands, e-mail: issues-h...@lucene.apache.org
[GitHub] [lucene-solr] janhoy commented on a change in pull request #994: SOLR-13662: Package Manager (CLI)
janhoy commented on a change in pull request #994: SOLR-13662: Package Manager (CLI) URL: https://github.com/apache/lucene-solr/pull/994#discussion_r345639359 ## File path: solr/core/src/java/org/apache/solr/util/PackageTool.java ## @@ -0,0 +1,255 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.solr.util; + +import java.lang.invoke.MethodHandles; +import java.util.Map; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.Option; +import org.apache.commons.cli.OptionBuilder; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.core.config.Configurator; +import org.apache.lucene.util.SuppressForbidden; +import org.apache.solr.client.solrj.impl.HttpClientUtil; +import org.apache.solr.client.solrj.impl.HttpSolrClient; +import org.apache.solr.packagemanager.PackageManager; +import org.apache.solr.packagemanager.PackageUtils; +import org.apache.solr.packagemanager.RepositoryManager; +import org.apache.solr.packagemanager.SolrPackageInstance; +import org.apache.solr.util.SolrCLI.StatusTool; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +@SuppressForbidden(reason = "Need to use System.out.println() instead of log4j/slf4j for cleaner output") +public class PackageTool extends SolrCLI.ToolBase { + + private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + + @SuppressForbidden(reason = "Need to turn off logging, and SLF4J doesn't seem to provide for a way.") + public PackageTool() { +// Need a logging free, clean output going through to the user. +Configurator.setRootLevel(Level.OFF); + } + + @Override + public String getName() { +return "package"; + } + + public static String solrUrl = null; + public static String solrBaseUrl = null; + public PackageManager packageManager; + public RepositoryManager repositoryManager; + + @Override + protected void runImpl(CommandLine cli) throws Exception { +try { + solrUrl = cli.getOptionValues("solrUrl")[cli.getOptionValues("solrUrl").length-1]; + solrBaseUrl = solrUrl.replaceAll("\\/solr$", ""); // strip out ending "/solr" + log.info("Solr url: "+solrUrl+", solr base url: "+solrBaseUrl); + String zkHost = getZkHost(cli); + + log.info("ZK: "+zkHost); + String cmd = cli.getArgList().size() == 0? "help": cli.getArgs()[0]; + + try (HttpSolrClient solrClient = new HttpSolrClient.Builder(solrBaseUrl).build()) { +if (cmd != null) { + packageManager = new PackageManager(solrClient, solrBaseUrl, zkHost); + try { +repositoryManager = new RepositoryManager(solrClient, packageManager); + +switch (cmd) { + case "add-repo": +repositoryManager.addRepository(cli.getArgs()[1], cli.getArgs()[2]); +break; + case "list-installed": +packageManager.listInstalled(); +break; + case "list-available": +repositoryManager.listAvailable(); +break; + case "list-deployed": +if (cli.hasOption('c')) { + String collection = cli.getArgs()[1]; + Map packages = packageManager.getPackagesDeployed(collection); + PackageUtils.printGreen("Packages deployed on " + collection + ":"); + for (String packageName: packages.keySet()) { +PackageUtils.printGreen("\t" + packages.get(packageName)); + } +} else { + String packageName = cli.getArgs()[1]; + Map deployedCollections = packageManager.getDeployedCollections(packageName); + PackageUtils.printGreen("Collections on which package " + packageName + " was deployed:"); + for (String collection: deployedCollections.keySet()) { +PackageUtils.printGreen("\t" + collection + "("+packageName+":"+deployedCollections.get(collection)+")"); + } +} +break; +
[GitHub] [lucene-solr] janhoy commented on a change in pull request #994: SOLR-13662: Package Manager (CLI)
janhoy commented on a change in pull request #994: SOLR-13662: Package Manager (CLI) URL: https://github.com/apache/lucene-solr/pull/994#discussion_r345641481 ## File path: solr/core/src/java/org/apache/solr/util/PackageTool.java ## @@ -0,0 +1,255 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.solr.util; + +import java.lang.invoke.MethodHandles; +import java.util.Map; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.Option; +import org.apache.commons.cli.OptionBuilder; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.core.config.Configurator; +import org.apache.lucene.util.SuppressForbidden; +import org.apache.solr.client.solrj.impl.HttpClientUtil; +import org.apache.solr.client.solrj.impl.HttpSolrClient; +import org.apache.solr.packagemanager.PackageManager; +import org.apache.solr.packagemanager.PackageUtils; +import org.apache.solr.packagemanager.RepositoryManager; +import org.apache.solr.packagemanager.SolrPackageInstance; +import org.apache.solr.util.SolrCLI.StatusTool; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +@SuppressForbidden(reason = "Need to use System.out.println() instead of log4j/slf4j for cleaner output") +public class PackageTool extends SolrCLI.ToolBase { + + private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + + @SuppressForbidden(reason = "Need to turn off logging, and SLF4J doesn't seem to provide for a way.") + public PackageTool() { +// Need a logging free, clean output going through to the user. +Configurator.setRootLevel(Level.OFF); + } + + @Override + public String getName() { +return "package"; + } + + public static String solrUrl = null; + public static String solrBaseUrl = null; + public PackageManager packageManager; + public RepositoryManager repositoryManager; + + @Override + protected void runImpl(CommandLine cli) throws Exception { +try { + solrUrl = cli.getOptionValues("solrUrl")[cli.getOptionValues("solrUrl").length-1]; + solrBaseUrl = solrUrl.replaceAll("\\/solr$", ""); // strip out ending "/solr" + log.info("Solr url: "+solrUrl+", solr base url: "+solrBaseUrl); + String zkHost = getZkHost(cli); + + log.info("ZK: "+zkHost); + String cmd = cli.getArgList().size() == 0? "help": cli.getArgs()[0]; + + try (HttpSolrClient solrClient = new HttpSolrClient.Builder(solrBaseUrl).build()) { +if (cmd != null) { + packageManager = new PackageManager(solrClient, solrBaseUrl, zkHost); + try { +repositoryManager = new RepositoryManager(solrClient, packageManager); + +switch (cmd) { + case "add-repo": +repositoryManager.addRepository(cli.getArgs()[1], cli.getArgs()[2]); +break; + case "list-installed": +packageManager.listInstalled(); +break; + case "list-available": +repositoryManager.listAvailable(); +break; + case "list-deployed": +if (cli.hasOption('c')) { + String collection = cli.getArgs()[1]; + Map packages = packageManager.getPackagesDeployed(collection); + PackageUtils.printGreen("Packages deployed on " + collection + ":"); + for (String packageName: packages.keySet()) { +PackageUtils.printGreen("\t" + packages.get(packageName)); + } +} else { + String packageName = cli.getArgs()[1]; + Map deployedCollections = packageManager.getDeployedCollections(packageName); + PackageUtils.printGreen("Collections on which package " + packageName + " was deployed:"); + for (String collection: deployedCollections.keySet()) { +PackageUtils.printGreen("\t" + collection + "("+packageName+":"+deployedCollections.get(collection)+")"); + } +} +break; +
[GitHub] [lucene-solr] janhoy commented on a change in pull request #994: SOLR-13662: Package Manager (CLI)
janhoy commented on a change in pull request #994: SOLR-13662: Package Manager (CLI) URL: https://github.com/apache/lucene-solr/pull/994#discussion_r345633566 ## File path: solr/core/src/java/org/apache/solr/packagemanager/RepositoryManager.java ## @@ -0,0 +1,328 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.solr.packagemanager; + +import static org.apache.solr.packagemanager.PackageUtils.getMapper; + +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.lang.invoke.MethodHandles; +import java.net.MalformedURLException; +import java.net.URL; +import java.nio.ByteBuffer; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; +import org.apache.lucene.util.Version; +import org.apache.solr.client.solrj.SolrRequest; +import org.apache.solr.client.solrj.SolrServerException; +import org.apache.solr.client.solrj.impl.HttpSolrClient; +import org.apache.solr.client.solrj.request.V2Request; +import org.apache.solr.client.solrj.request.beans.Package; +import org.apache.solr.client.solrj.response.V2Response; +import org.apache.solr.common.SolrException; +import org.apache.solr.common.SolrException.ErrorCode; +import org.apache.solr.common.cloud.SolrZkClient; +import org.apache.solr.core.BlobRepository; +import org.apache.solr.packagemanager.SolrPackage.Artifact; +import org.apache.solr.packagemanager.SolrPackage.SolrPackageRelease; +import org.apache.solr.pkg.PackageAPI; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +/** + * Handles most of the management of repositories and packages present in external repositories. + */ +public class RepositoryManager { + + private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + + final private PackageManager packageManager; + + public static final String systemVersion = Version.LATEST.toString(); + + final HttpSolrClient solrClient; + + public RepositoryManager(HttpSolrClient solrClient, PackageManager packageManager) { +this.packageManager = packageManager; +this.solrClient = solrClient; + } + + public List getPackages() { +List list = new ArrayList<>(getPackagesMap().values()); +Collections.sort(list); +return list; + } + + /** + * Get a map of package name to {@link SolrPackage} objects + */ + public Map getPackagesMap() { +Map packagesMap = new HashMap<>(); +for (PackageRepository repository: getRepositories()) { + packagesMap.putAll(repository.getPackages()); +} + +return packagesMap; + } + + /** + * List of added repositories + */ + public List getRepositories() { +// TODO: Instead of fetching again and again, we should look for caching this +PackageRepository items[]; +try { + items = getMapper().readValue(getRepositoriesJson(packageManager.zkClient), DefaultPackageRepository[].class); +} catch (IOException | KeeperException | InterruptedException e) { + throw new SolrException(ErrorCode.SERVER_ERROR, e); +} +List repositories = Arrays.asList(items); + +for (PackageRepository updateRepository: repositories) { + updateRepository.refresh(); +} + +return repositories; + } + + /** + * Add a repository to Solr + */ + public void addRepository(String name, String uri) throws KeeperException, InterruptedException, MalformedURLException, IOException { +String existingRepositoriesJson = getRepositoriesJson(packageManager.zkClient); +log.info(existingRepositoriesJson); + +List repos = getMapper().readValue(existingRepositoriesJson, List.class); +repos.add(new DefaultPackageRepository(name, uri)); +if (packageManager.zkClient.exists("/repositories.json", true) == false) { + packageManager.zkClient.create("/repositories.json", getMapper().writeValueAsString(repos).getBytes("UTF-8"), CreateMode.PERSISTENT,
[GitHub] [lucene-solr] janhoy commented on a change in pull request #994: SOLR-13662: Package Manager (CLI)
janhoy commented on a change in pull request #994: SOLR-13662: Package Manager (CLI) URL: https://github.com/apache/lucene-solr/pull/994#discussion_r345632143 ## File path: solr/core/src/java/org/apache/solr/packagemanager/RepositoryManager.java ## @@ -0,0 +1,328 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.solr.packagemanager; + +import static org.apache.solr.packagemanager.PackageUtils.getMapper; + +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.lang.invoke.MethodHandles; +import java.net.MalformedURLException; +import java.net.URL; +import java.nio.ByteBuffer; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; +import org.apache.lucene.util.Version; +import org.apache.solr.client.solrj.SolrRequest; +import org.apache.solr.client.solrj.SolrServerException; +import org.apache.solr.client.solrj.impl.HttpSolrClient; +import org.apache.solr.client.solrj.request.V2Request; +import org.apache.solr.client.solrj.request.beans.Package; +import org.apache.solr.client.solrj.response.V2Response; +import org.apache.solr.common.SolrException; +import org.apache.solr.common.SolrException.ErrorCode; +import org.apache.solr.common.cloud.SolrZkClient; +import org.apache.solr.core.BlobRepository; +import org.apache.solr.packagemanager.SolrPackage.Artifact; +import org.apache.solr.packagemanager.SolrPackage.SolrPackageRelease; +import org.apache.solr.pkg.PackageAPI; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +/** + * Handles most of the management of repositories and packages present in external repositories. + */ +public class RepositoryManager { + + private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + + final private PackageManager packageManager; + + public static final String systemVersion = Version.LATEST.toString(); + + final HttpSolrClient solrClient; + + public RepositoryManager(HttpSolrClient solrClient, PackageManager packageManager) { +this.packageManager = packageManager; +this.solrClient = solrClient; + } + + public List getPackages() { +List list = new ArrayList<>(getPackagesMap().values()); +Collections.sort(list); +return list; + } + + /** + * Get a map of package name to {@link SolrPackage} objects + */ + public Map getPackagesMap() { +Map packagesMap = new HashMap<>(); +for (PackageRepository repository: getRepositories()) { + packagesMap.putAll(repository.getPackages()); +} + +return packagesMap; + } + + /** + * List of added repositories + */ + public List getRepositories() { +// TODO: Instead of fetching again and again, we should look for caching this +PackageRepository items[]; +try { + items = getMapper().readValue(getRepositoriesJson(packageManager.zkClient), DefaultPackageRepository[].class); +} catch (IOException | KeeperException | InterruptedException e) { + throw new SolrException(ErrorCode.SERVER_ERROR, e); +} +List repositories = Arrays.asList(items); + +for (PackageRepository updateRepository: repositories) { + updateRepository.refresh(); +} + +return repositories; + } + + /** + * Add a repository to Solr + */ + public void addRepository(String name, String uri) throws KeeperException, InterruptedException, MalformedURLException, IOException { +String existingRepositoriesJson = getRepositoriesJson(packageManager.zkClient); +log.info(existingRepositoriesJson); + +List repos = getMapper().readValue(existingRepositoriesJson, List.class); +repos.add(new DefaultPackageRepository(name, uri)); +if (packageManager.zkClient.exists("/repositories.json", true) == false) { + packageManager.zkClient.create("/repositories.json", getMapper().writeValueAsString(repos).getBytes("UTF-8"), CreateMode.PERSISTENT,
[GitHub] [lucene-solr] janhoy commented on a change in pull request #994: SOLR-13662: Package Manager (CLI)
janhoy commented on a change in pull request #994: SOLR-13662: Package Manager (CLI) URL: https://github.com/apache/lucene-solr/pull/994#discussion_r345634253 ## File path: solr/core/src/java/org/apache/solr/packagemanager/RepositoryManager.java ## @@ -0,0 +1,328 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.solr.packagemanager; + +import static org.apache.solr.packagemanager.PackageUtils.getMapper; + +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.lang.invoke.MethodHandles; +import java.net.MalformedURLException; +import java.net.URL; +import java.nio.ByteBuffer; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; +import org.apache.lucene.util.Version; +import org.apache.solr.client.solrj.SolrRequest; +import org.apache.solr.client.solrj.SolrServerException; +import org.apache.solr.client.solrj.impl.HttpSolrClient; +import org.apache.solr.client.solrj.request.V2Request; +import org.apache.solr.client.solrj.request.beans.Package; +import org.apache.solr.client.solrj.response.V2Response; +import org.apache.solr.common.SolrException; +import org.apache.solr.common.SolrException.ErrorCode; +import org.apache.solr.common.cloud.SolrZkClient; +import org.apache.solr.core.BlobRepository; +import org.apache.solr.packagemanager.SolrPackage.Artifact; +import org.apache.solr.packagemanager.SolrPackage.SolrPackageRelease; +import org.apache.solr.pkg.PackageAPI; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +/** + * Handles most of the management of repositories and packages present in external repositories. + */ +public class RepositoryManager { + + private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + + final private PackageManager packageManager; + + public static final String systemVersion = Version.LATEST.toString(); + + final HttpSolrClient solrClient; + + public RepositoryManager(HttpSolrClient solrClient, PackageManager packageManager) { +this.packageManager = packageManager; +this.solrClient = solrClient; + } + + public List getPackages() { +List list = new ArrayList<>(getPackagesMap().values()); +Collections.sort(list); +return list; + } + + /** + * Get a map of package name to {@link SolrPackage} objects + */ + public Map getPackagesMap() { +Map packagesMap = new HashMap<>(); +for (PackageRepository repository: getRepositories()) { + packagesMap.putAll(repository.getPackages()); +} + +return packagesMap; + } + + /** + * List of added repositories + */ + public List getRepositories() { +// TODO: Instead of fetching again and again, we should look for caching this +PackageRepository items[]; +try { + items = getMapper().readValue(getRepositoriesJson(packageManager.zkClient), DefaultPackageRepository[].class); +} catch (IOException | KeeperException | InterruptedException e) { + throw new SolrException(ErrorCode.SERVER_ERROR, e); +} +List repositories = Arrays.asList(items); + +for (PackageRepository updateRepository: repositories) { + updateRepository.refresh(); +} + +return repositories; + } + + /** + * Add a repository to Solr + */ + public void addRepository(String name, String uri) throws KeeperException, InterruptedException, MalformedURLException, IOException { +String existingRepositoriesJson = getRepositoriesJson(packageManager.zkClient); +log.info(existingRepositoriesJson); + +List repos = getMapper().readValue(existingRepositoriesJson, List.class); +repos.add(new DefaultPackageRepository(name, uri)); +if (packageManager.zkClient.exists("/repositories.json", true) == false) { + packageManager.zkClient.create("/repositories.json", getMapper().writeValueAsString(repos).getBytes("UTF-8"), CreateMode.PERSISTENT,
[GitHub] [lucene-solr] janhoy commented on a change in pull request #994: SOLR-13662: Package Manager (CLI)
janhoy commented on a change in pull request #994: SOLR-13662: Package Manager (CLI) URL: https://github.com/apache/lucene-solr/pull/994#discussion_r345636779 ## File path: solr/core/src/java/org/apache/solr/packagemanager/SolrPackage.java ## @@ -0,0 +1,143 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.solr.packagemanager; + + +import java.util.Date; +import java.util.List; +import java.util.Map; + +import org.apache.solr.common.annotation.JsonProperty; +import org.apache.solr.common.util.ReflectMapWriter; + +/** + * Describes a package (along with all released versions) as it appears in a repository. + */ +public class SolrPackage implements Comparable, ReflectMapWriter { + + @JsonProperty("name") + public String name; + + @JsonProperty("description") + public String description; + + @JsonProperty("versions") + public List versions; + + @JsonProperty("repository") + private String repository; + + @Override + public String toString() { +return jsonStr(); + } + + public static class SolrPackageRelease implements ReflectMapWriter { +@JsonProperty("version") +public String version; + +@JsonProperty("date") +public Date date; + +@JsonProperty("artifacts") +public List artifacts; + +@JsonProperty("manifest") Review comment: Won't this class represent the content of `manifest.json`? In that case it could be misleading to have a property `manifest` inside? Perhaps I misunderstand? This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services - To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org For additional commands, e-mail: issues-h...@lucene.apache.org
[GitHub] [lucene-solr] janhoy commented on a change in pull request #994: SOLR-13662: Package Manager (CLI)
janhoy commented on a change in pull request #994: SOLR-13662: Package Manager (CLI) URL: https://github.com/apache/lucene-solr/pull/994#discussion_r345633116 ## File path: solr/core/src/java/org/apache/solr/packagemanager/RepositoryManager.java ## @@ -0,0 +1,328 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.solr.packagemanager; + +import static org.apache.solr.packagemanager.PackageUtils.getMapper; + +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.lang.invoke.MethodHandles; +import java.net.MalformedURLException; +import java.net.URL; +import java.nio.ByteBuffer; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; +import org.apache.lucene.util.Version; +import org.apache.solr.client.solrj.SolrRequest; +import org.apache.solr.client.solrj.SolrServerException; +import org.apache.solr.client.solrj.impl.HttpSolrClient; +import org.apache.solr.client.solrj.request.V2Request; +import org.apache.solr.client.solrj.request.beans.Package; +import org.apache.solr.client.solrj.response.V2Response; +import org.apache.solr.common.SolrException; +import org.apache.solr.common.SolrException.ErrorCode; +import org.apache.solr.common.cloud.SolrZkClient; +import org.apache.solr.core.BlobRepository; +import org.apache.solr.packagemanager.SolrPackage.Artifact; +import org.apache.solr.packagemanager.SolrPackage.SolrPackageRelease; +import org.apache.solr.pkg.PackageAPI; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +/** + * Handles most of the management of repositories and packages present in external repositories. + */ +public class RepositoryManager { + + private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + + final private PackageManager packageManager; + + public static final String systemVersion = Version.LATEST.toString(); + + final HttpSolrClient solrClient; + + public RepositoryManager(HttpSolrClient solrClient, PackageManager packageManager) { +this.packageManager = packageManager; +this.solrClient = solrClient; + } + + public List getPackages() { +List list = new ArrayList<>(getPackagesMap().values()); +Collections.sort(list); +return list; + } + + /** + * Get a map of package name to {@link SolrPackage} objects + */ + public Map getPackagesMap() { +Map packagesMap = new HashMap<>(); +for (PackageRepository repository: getRepositories()) { + packagesMap.putAll(repository.getPackages()); +} + +return packagesMap; + } + + /** + * List of added repositories + */ + public List getRepositories() { +// TODO: Instead of fetching again and again, we should look for caching this +PackageRepository items[]; +try { + items = getMapper().readValue(getRepositoriesJson(packageManager.zkClient), DefaultPackageRepository[].class); +} catch (IOException | KeeperException | InterruptedException e) { + throw new SolrException(ErrorCode.SERVER_ERROR, e); +} +List repositories = Arrays.asList(items); + +for (PackageRepository updateRepository: repositories) { + updateRepository.refresh(); +} + +return repositories; + } + + /** + * Add a repository to Solr + */ + public void addRepository(String name, String uri) throws KeeperException, InterruptedException, MalformedURLException, IOException { +String existingRepositoriesJson = getRepositoriesJson(packageManager.zkClient); +log.info(existingRepositoriesJson); + +List repos = getMapper().readValue(existingRepositoriesJson, List.class); +repos.add(new DefaultPackageRepository(name, uri)); +if (packageManager.zkClient.exists("/repositories.json", true) == false) { + packageManager.zkClient.create("/repositories.json", getMapper().writeValueAsString(repos).getBytes("UTF-8"), CreateMode.PERSISTENT,
[GitHub] [lucene-solr] janhoy commented on a change in pull request #994: SOLR-13662: Package Manager (CLI)
janhoy commented on a change in pull request #994: SOLR-13662: Package Manager (CLI) URL: https://github.com/apache/lucene-solr/pull/994#discussion_r345629730 ## File path: solr/core/src/java/org/apache/solr/packagemanager/SolrPackage.java ## @@ -0,0 +1,143 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.solr.packagemanager; + + +import java.util.Date; +import java.util.List; +import java.util.Map; + +import org.apache.solr.common.annotation.JsonProperty; +import org.apache.solr.common.util.ReflectMapWriter; + +/** + * Describes a package (along with all released versions) as it appears in a repository. + */ +public class SolrPackage implements Comparable, ReflectMapWriter { + + @JsonProperty("name") + public String name; + + @JsonProperty("description") + public String description; + + @JsonProperty("versions") + public List versions; + + @JsonProperty("repository") + private String repository; + + @Override + public String toString() { +return jsonStr(); + } + + public static class SolrPackageRelease implements ReflectMapWriter { +@JsonProperty("version") +public String version; + +@JsonProperty("date") +public Date date; + +@JsonProperty("artifacts") +public List artifacts; + +@JsonProperty("manifest") +public Manifest manifest; + +@Override +public String toString() { + return jsonStr(); +} + } + + public static class Artifact implements ReflectMapWriter { +@JsonProperty("url") +public String url; + +@JsonProperty("sig") +public String sig; + } + + public static class Manifest implements ReflectMapWriter { +@JsonProperty("min-solr-version") Review comment: `min-solr-version` and `max-solr-version` can be replaced by a SemVer expression (https://devhints.io/semver) which would allow a simple `8` to say you are compatible with all 8.x, or `*` to allow all etc. This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services - To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org For additional commands, e-mail: issues-h...@lucene.apache.org
[GitHub] [lucene-solr] janhoy commented on a change in pull request #994: SOLR-13662: Package Manager (CLI)
janhoy commented on a change in pull request #994: SOLR-13662: Package Manager (CLI) URL: https://github.com/apache/lucene-solr/pull/994#discussion_r345631027 ## File path: solr/core/src/java/org/apache/solr/packagemanager/RepositoryManager.java ## @@ -0,0 +1,328 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.solr.packagemanager; + +import static org.apache.solr.packagemanager.PackageUtils.getMapper; + +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.lang.invoke.MethodHandles; +import java.net.MalformedURLException; +import java.net.URL; +import java.nio.ByteBuffer; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; +import org.apache.lucene.util.Version; +import org.apache.solr.client.solrj.SolrRequest; +import org.apache.solr.client.solrj.SolrServerException; +import org.apache.solr.client.solrj.impl.HttpSolrClient; +import org.apache.solr.client.solrj.request.V2Request; +import org.apache.solr.client.solrj.request.beans.Package; +import org.apache.solr.client.solrj.response.V2Response; +import org.apache.solr.common.SolrException; +import org.apache.solr.common.SolrException.ErrorCode; +import org.apache.solr.common.cloud.SolrZkClient; +import org.apache.solr.core.BlobRepository; +import org.apache.solr.packagemanager.SolrPackage.Artifact; +import org.apache.solr.packagemanager.SolrPackage.SolrPackageRelease; +import org.apache.solr.pkg.PackageAPI; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +/** + * Handles most of the management of repositories and packages present in external repositories. + */ +public class RepositoryManager { + + private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + + final private PackageManager packageManager; + + public static final String systemVersion = Version.LATEST.toString(); + + final HttpSolrClient solrClient; + + public RepositoryManager(HttpSolrClient solrClient, PackageManager packageManager) { +this.packageManager = packageManager; +this.solrClient = solrClient; + } + + public List getPackages() { +List list = new ArrayList<>(getPackagesMap().values()); +Collections.sort(list); +return list; + } + + /** + * Get a map of package name to {@link SolrPackage} objects + */ + public Map getPackagesMap() { +Map packagesMap = new HashMap<>(); +for (PackageRepository repository: getRepositories()) { + packagesMap.putAll(repository.getPackages()); +} + +return packagesMap; + } + + /** + * List of added repositories + */ + public List getRepositories() { +// TODO: Instead of fetching again and again, we should look for caching this +PackageRepository items[]; +try { + items = getMapper().readValue(getRepositoriesJson(packageManager.zkClient), DefaultPackageRepository[].class); +} catch (IOException | KeeperException | InterruptedException e) { + throw new SolrException(ErrorCode.SERVER_ERROR, e); +} +List repositories = Arrays.asList(items); + +for (PackageRepository updateRepository: repositories) { + updateRepository.refresh(); +} + +return repositories; + } + + /** + * Add a repository to Solr + */ + public void addRepository(String name, String uri) throws KeeperException, InterruptedException, MalformedURLException, IOException { +String existingRepositoriesJson = getRepositoriesJson(packageManager.zkClient); +log.info(existingRepositoriesJson); + +List repos = getMapper().readValue(existingRepositoriesJson, List.class); +repos.add(new DefaultPackageRepository(name, uri)); +if (packageManager.zkClient.exists("/repositories.json", true) == false) { + packageManager.zkClient.create("/repositories.json", getMapper().writeValueAsString(repos).getBytes("UTF-8"), CreateMode.PERSISTENT,
[GitHub] [lucene-solr] janhoy commented on a change in pull request #994: SOLR-13662: Package Manager (CLI)
janhoy commented on a change in pull request #994: SOLR-13662: Package Manager (CLI) URL: https://github.com/apache/lucene-solr/pull/994#discussion_r345635689 ## File path: solr/core/src/java/org/apache/solr/packagemanager/RepositoryManager.java ## @@ -0,0 +1,328 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.solr.packagemanager; + +import static org.apache.solr.packagemanager.PackageUtils.getMapper; + +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.lang.invoke.MethodHandles; +import java.net.MalformedURLException; +import java.net.URL; +import java.nio.ByteBuffer; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; +import org.apache.lucene.util.Version; +import org.apache.solr.client.solrj.SolrRequest; +import org.apache.solr.client.solrj.SolrServerException; +import org.apache.solr.client.solrj.impl.HttpSolrClient; +import org.apache.solr.client.solrj.request.V2Request; +import org.apache.solr.client.solrj.request.beans.Package; +import org.apache.solr.client.solrj.response.V2Response; +import org.apache.solr.common.SolrException; +import org.apache.solr.common.SolrException.ErrorCode; +import org.apache.solr.common.cloud.SolrZkClient; +import org.apache.solr.core.BlobRepository; +import org.apache.solr.packagemanager.SolrPackage.Artifact; +import org.apache.solr.packagemanager.SolrPackage.SolrPackageRelease; +import org.apache.solr.pkg.PackageAPI; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +/** + * Handles most of the management of repositories and packages present in external repositories. + */ +public class RepositoryManager { + + private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + + final private PackageManager packageManager; + + public static final String systemVersion = Version.LATEST.toString(); + + final HttpSolrClient solrClient; + + public RepositoryManager(HttpSolrClient solrClient, PackageManager packageManager) { +this.packageManager = packageManager; +this.solrClient = solrClient; + } + + public List getPackages() { +List list = new ArrayList<>(getPackagesMap().values()); +Collections.sort(list); +return list; + } + + /** + * Get a map of package name to {@link SolrPackage} objects + */ + public Map getPackagesMap() { +Map packagesMap = new HashMap<>(); +for (PackageRepository repository: getRepositories()) { + packagesMap.putAll(repository.getPackages()); +} + +return packagesMap; + } + + /** + * List of added repositories + */ + public List getRepositories() { +// TODO: Instead of fetching again and again, we should look for caching this +PackageRepository items[]; +try { + items = getMapper().readValue(getRepositoriesJson(packageManager.zkClient), DefaultPackageRepository[].class); +} catch (IOException | KeeperException | InterruptedException e) { + throw new SolrException(ErrorCode.SERVER_ERROR, e); +} +List repositories = Arrays.asList(items); + +for (PackageRepository updateRepository: repositories) { + updateRepository.refresh(); +} + +return repositories; + } + + /** + * Add a repository to Solr + */ + public void addRepository(String name, String uri) throws KeeperException, InterruptedException, MalformedURLException, IOException { +String existingRepositoriesJson = getRepositoriesJson(packageManager.zkClient); +log.info(existingRepositoriesJson); + +List repos = getMapper().readValue(existingRepositoriesJson, List.class); +repos.add(new DefaultPackageRepository(name, uri)); +if (packageManager.zkClient.exists("/repositories.json", true) == false) { + packageManager.zkClient.create("/repositories.json", getMapper().writeValueAsString(repos).getBytes("UTF-8"), CreateMode.PERSISTENT,