use of io.cdap.cdap.api.artifact.ArtifactRange in project cdap by caskdata.
the class IntegrationTestManager method addPluginArtifact.
@Override
public ArtifactManager addPluginArtifact(ArtifactId artifactId, ArtifactId parent, Class<?> pluginClass, Class<?>... pluginClasses) throws Exception {
Set<ArtifactRange> parents = new HashSet<>();
parents.add(new ArtifactRange(parent.getParent().getNamespace(), parent.getArtifact(), new ArtifactVersion(parent.getVersion()), true, new ArtifactVersion(parent.getVersion()), true));
addPluginArtifact(artifactId, parents, pluginClass, pluginClasses);
return new RemoteArtifactManager(clientConfig, restClient, artifactId);
}
use of io.cdap.cdap.api.artifact.ArtifactRange in project cdap by caskdata.
the class UnitTestManager method addPluginArtifact.
@Override
public ArtifactManager addPluginArtifact(ArtifactId artifactId, ArtifactId parent, Class<?> pluginClass, Class<?>... pluginClasses) throws Exception {
Set<ArtifactRange> parents = new HashSet<>();
parents.add(new ArtifactRange(parent.getParent().getNamespace(), parent.getArtifact(), new ArtifactVersion(parent.getVersion()), true, new ArtifactVersion(parent.getVersion()), true));
addPluginArtifact(artifactId, parents, pluginClass, pluginClasses);
return artifactManagerFactory.create(artifactId);
}
use of io.cdap.cdap.api.artifact.ArtifactRange in project cdap by cdapio.
the class HubPackage method installPlugin.
/**
* Downloads the plugin from the given URL and installs it in the artifact repository
*
* @param url URL that points to the plugin directory on the hub
* @param artifactRepository {@link ArtifactRepository} in which the plugin will be installed
* @param tmpDir temporary directory where plugin jar is downloaded from the hub
*/
public void installPlugin(URL url, ArtifactRepository artifactRepository, File tmpDir) throws Exception {
// Deserialize spec.json
URL specURL = new URL(url.getProtocol(), url.getHost(), url.getPort(), url.getPath() + "/packages/" + name + "/" + version + "/spec.json");
Spec spec = GSON.fromJson(HttpClients.doGetAsString(specURL), Spec.class);
for (Spec.Action action : spec.getActions()) {
// See https://cdap.atlassian.net/wiki/spaces/DOCS/pages/554401840/Hub+API?src=search#one_step_deploy_plugin
if (!action.getType().equals("one_step_deploy_plugin")) {
continue;
}
String configFilename = action.getConfigFilename();
if (configFilename == null) {
LOG.warn("Ignoring plugin {} due to missing config", name);
continue;
}
URL configURL = new URL(url.getProtocol(), url.getHost(), url.getPort(), url.getPath() + Joiner.on("/").join(Arrays.asList("/packages", name, version, configFilename)));
// Download plugin json from hub
JsonObject jsonObj = GSON.fromJson(HttpClients.doGetAsString(configURL), JsonObject.class);
List<String> parents = GSON.fromJson(jsonObj.get("parents"), new TypeToken<List<String>>() {
}.getType());
String jarName = action.getJarName();
if (jarName == null) {
LOG.warn("Ignoring plugin {} due to missing jar", name);
continue;
}
// Download plugin jar from hub
File destination = File.createTempFile("artifact-", ".jar", tmpDir);
FileChannel channel = new FileOutputStream(destination, false).getChannel();
URL jarURL = new URL(url.getProtocol(), url.getHost(), url.getPort(), url.getPath() + Joiner.on("/").join(Arrays.asList("/packages", name, version, jarName)));
HttpRequest request = HttpRequest.get(jarURL).withContentConsumer(new HttpContentConsumer() {
@Override
public boolean onReceived(ByteBuffer buffer) {
try {
channel.write(buffer);
} catch (IOException e) {
LOG.error("Failed write to file {}", destination);
return false;
}
return true;
}
@Override
public void onFinished() {
Closeables.closeQuietly(channel);
}
}).build();
HttpClients.executeStreamingRequest(request);
Set<ArtifactRange> parentArtifacts = new HashSet<>();
for (String parent : parents) {
try {
// try parsing it as a namespaced range like system:cdap-data-pipeline[6.3 1.1,7.0.0)
parentArtifacts.add(ArtifactRanges.parseArtifactRange(parent));
} catch (InvalidArtifactRangeException e) {
// if this failed, try parsing as a non-namespaced range like cdap-data-pipeline[6.3 1.1,7.0.0)
parentArtifacts.add(ArtifactRanges.parseArtifactRange(NamespaceId.DEFAULT.getNamespace(), parent));
}
}
// add the artifact to the repo
io.cdap.cdap.proto.id.ArtifactId artifactId = NamespaceId.DEFAULT.artifact(name, version);
Id.Artifact artifact = Id.Artifact.fromEntityId(artifactId);
try {
artifactRepository.addArtifact(artifact, destination, parentArtifacts, ImmutableSet.of());
} catch (ArtifactAlreadyExistsException e) {
LOG.debug("Artifact artifact {}-{} already exists", name, version);
}
Map<String, String> properties = GSON.fromJson(jsonObj.get("properties"), new TypeToken<Map<String, String>>() {
}.getType());
artifactRepository.writeArtifactProperties(Id.Artifact.fromEntityId(artifactId), properties);
if (!java.nio.file.Files.deleteIfExists(Paths.get(destination.getPath()))) {
LOG.warn("Failed to cleanup file {}", destination);
}
}
}
use of io.cdap.cdap.api.artifact.ArtifactRange in project cdap by cdapio.
the class ArtifactRangeTest method testWhitespace.
@Test
public void testWhitespace() throws InvalidArtifactRangeException {
ArtifactRange range = ArtifactRanges.parseArtifactRange(NamespaceId.DEFAULT.getNamespace(), "name[ 1.0.0 , 2.0.0 )");
Assert.assertEquals(new ArtifactRange(NamespaceId.DEFAULT.getNamespace(), "name", new ArtifactVersion("1.0.0"), true, new ArtifactVersion("2.0.0"), false), range);
}
use of io.cdap.cdap.api.artifact.ArtifactRange in project cdap by cdapio.
the class ArtifactRangeTest method testVersionParse.
@Test
public void testVersionParse() throws InvalidArtifactRangeException {
ArtifactRange expected = new ArtifactRange(NamespaceId.DEFAULT.getNamespace(), "test", new ArtifactVersion("1.0.0"), true, new ArtifactVersion("2.0.0-SNAPSHOT"), false);
ArtifactRange actual = ArtifactRanges.parseArtifactRange(NamespaceId.DEFAULT.getNamespace(), "test[1.0.0,2.0.0-SNAPSHOT)");
Assert.assertEquals(expected, actual);
expected = new ArtifactRange(NamespaceId.DEFAULT.getNamespace(), "test", new ArtifactVersion("0.1.0-SNAPSHOT"), false, new ArtifactVersion("1.0.0"), true);
actual = ArtifactRanges.parseArtifactRange(NamespaceId.DEFAULT.getNamespace(), "test(0.1.0-SNAPSHOT,1.0.0]");
Assert.assertEquals(expected, actual);
// test compatible with toString
Assert.assertEquals(expected, ArtifactRanges.parseArtifactRange(expected.toString()));
}
Aggregations