use of io.cdap.cdap.internal.capability.autoinstall.HubPackage in project cdap by caskdata.
the class CapabilityApplier method autoInstallResources.
@VisibleForTesting
void autoInstallResources(String capability, @Nullable List<URL> hubs) {
ExecutorService executor = Executors.newFixedThreadPool(autoInstallThreads, Threads.createDaemonThreadFactory("installer-" + capability + "-%d"));
try {
for (URL hub : Optional.ofNullable(hubs).orElse(Collections.emptyList())) {
HubPackage[] hubPackages;
try {
URL url = new URL(hub.getProtocol(), hub.getHost(), hub.getPort(), hub.getPath() + "/packages.json");
String packagesJson = HttpClients.doGetAsString(url);
// Deserialize packages.json from hub
// See https://cdap.atlassian.net/wiki/spaces/DOCS/pages/554401840/Hub+API?src=search#Get-Hub-Catalog
hubPackages = GSON.fromJson(packagesJson, HubPackage[].class);
} catch (Exception e) {
LOG.warn("Failed to get packages.json from {} for capability {}. Ignoring error.", hub, capability, e);
continue;
}
String currentVersion = getCurrentVersion();
// Get the latest compatible version of each plugin from hub and install it
List<Future<?>> futures = Arrays.stream(hubPackages).filter(p -> p.versionIsInRange(currentVersion)).collect(Collectors.groupingBy(HubPackage::getName, Collectors.maxBy(Comparator.comparing(HubPackage::getArtifactVersion)))).values().stream().filter(Optional::isPresent).map(Optional::get).map(p -> executor.submit(() -> {
try {
LOG.debug("Installing plugins {} for capability {} from hub {}", p.getName(), capability, hub);
p.installPlugin(hub, artifactRepository, tmpDir);
} catch (Exception e) {
LOG.warn("Failed to install plugin {} for capability {} from hub {}. Ignoring error", p.getName(), capability, hub, e);
}
})).collect(Collectors.toList());
for (Future<?> future : futures) {
try {
future.get();
} catch (InterruptedException | ExecutionException e) {
LOG.warn("Ignoring error during plugin install", e);
}
}
}
} finally {
executor.shutdownNow();
}
}
Aggregations