use of io.cdap.cdap.api.artifact.ArtifactScope in project cdap by caskdata.
the class ListArtifactVersionsCommand method perform.
@Override
public void perform(Arguments arguments, PrintStream output) throws Exception {
String scopeStr = arguments.getOptional(ArgumentName.SCOPE.toString());
String artifactName = arguments.get(ArgumentName.ARTIFACT_NAME.toString());
List<ArtifactSummary> artifactSummaries;
if (scopeStr == null) {
artifactSummaries = artifactClient.listVersions(cliConfig.getCurrentNamespace(), artifactName);
} else {
ArtifactScope scope = ArtifactScope.valueOf(scopeStr.toUpperCase());
artifactSummaries = artifactClient.listVersions(cliConfig.getCurrentNamespace(), artifactName, scope);
}
Table table = Table.builder().setHeader("name", "version", "scope").setRows(artifactSummaries, new RowMaker<ArtifactSummary>() {
@Override
public List<?> makeRow(ArtifactSummary object) {
return Lists.newArrayList(object.getName(), object.getVersion(), object.getScope().name());
}
}).build();
cliConfig.getTableRenderer().render(cliConfig, output, table);
}
use of io.cdap.cdap.api.artifact.ArtifactScope in project cdap by caskdata.
the class AppLifecycleHttpHandler method upgradeApplications.
/**
* Upgrades a lis of existing application to use latest version of application artifact and plugin artifacts.
*
* <pre>
* {@code
* [
* {"name":"XYZ"},
* {"name":"ABC"},
* {"name":"FOO"},
* ]
* }
* </pre>
* The response will be an array of {@link ApplicationUpdateDetail} object, which either indicates a success (200) or
* failure for each of the requested application in the same order as the request. The failure also indicates reason
* for the error. The response will be sent via ChunkResponder to continuously stream upgrade result per application.
*/
@POST
@Path("/upgrade")
@AuditPolicy(AuditDetail.REQUEST_BODY)
public void upgradeApplications(FullHttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @QueryParam("artifactScope") Set<String> artifactScopes, @QueryParam("allowSnapshot") boolean allowSnapshot) throws Exception {
// TODO: (CDAP-16910) Improve batch API performance as each application upgrade is an event independent of each
// other.
List<ApplicationId> appIds = decodeAndValidateBatchApplicationRecord(validateNamespace(namespaceId), request);
Set<ArtifactScope> allowedArtifactScopes = getArtifactScopes(artifactScopes);
try (ChunkResponder chunkResponder = responder.sendChunkStart(HttpResponseStatus.OK)) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try (JsonWriter jsonWriter = new JsonWriter(new OutputStreamWriter(outputStream, StandardCharsets.UTF_8))) {
jsonWriter.beginArray();
for (ApplicationId appId : appIds) {
ApplicationUpdateDetail updateDetail;
try {
applicationLifecycleService.upgradeApplication(appId, allowedArtifactScopes, allowSnapshot);
updateDetail = new ApplicationUpdateDetail(appId);
} catch (UnsupportedOperationException e) {
String errorMessage = String.format("Application %s does not support upgrade.", appId);
updateDetail = new ApplicationUpdateDetail(appId, new NotImplementedException(errorMessage));
} catch (InvalidArtifactException | NotFoundException e) {
updateDetail = new ApplicationUpdateDetail(appId, e);
} catch (Exception e) {
updateDetail = new ApplicationUpdateDetail(appId, new ServiceException("Upgrade failed due to internal error.", e, HttpResponseStatus.INTERNAL_SERVER_ERROR));
LOG.error("Application upgrade failed with exception", e);
}
GSON.toJson(updateDetail, ApplicationUpdateDetail.class, jsonWriter);
jsonWriter.flush();
chunkResponder.sendChunk(Unpooled.wrappedBuffer(outputStream.toByteArray()));
outputStream.reset();
chunkResponder.flush();
}
jsonWriter.endArray();
}
chunkResponder.sendChunk(Unpooled.wrappedBuffer(outputStream.toByteArray()));
}
}
use of io.cdap.cdap.api.artifact.ArtifactScope in project cdap by cdapio.
the class DefaultAppConfigurer method createSpecification.
public ApplicationSpecification createSpecification(@Nullable String applicationName, @Nullable String applicationVersion) {
// applicationName can be null only for apps before 3.2 that were not upgraded
ArtifactScope scope = artifactId.getNamespace().equals(Id.Namespace.SYSTEM) ? ArtifactScope.SYSTEM : ArtifactScope.USER;
ArtifactId artifactId = new ArtifactId(this.artifactId.getName(), this.artifactId.getVersion(), scope);
String namespace = deployNamespace.toEntityId().getNamespace();
String appName = applicationName == null ? name : applicationName;
String appVersion = applicationVersion == null ? ApplicationId.DEFAULT_VERSION : applicationVersion;
Map<String, ScheduleCreationSpec> builtScheduleSpecs = new HashMap<>();
for (Map.Entry<String, ScheduleCreationSpec> entry : scheduleSpecs.entrySet()) {
// If the ScheduleCreationSpec is really a builder, then build the ScheduleCreationSpec
if (entry.getValue() instanceof DefaultScheduleBuilder.ScheduleCreationBuilder) {
DefaultScheduleBuilder.ScheduleCreationBuilder builder = (DefaultScheduleBuilder.ScheduleCreationBuilder) entry.getValue();
builtScheduleSpecs.put(entry.getKey(), builder.build(namespace, appName, appVersion));
} else {
builtScheduleSpecs.put(entry.getKey(), entry.getValue());
}
}
return new DefaultApplicationSpecification(appName, appVersion, ProjectInfo.getVersion().toString(), description, configuration, artifactId, getDatasetModules(), getDatasetSpecs(), mapReduces, sparks, workflows, services, builtScheduleSpecs, workers, getPlugins());
}
use of io.cdap.cdap.api.artifact.ArtifactScope in project cdap by cdapio.
the class RemoteArtifactRepositoryReader method newInputStream.
/**
* Returns an input stream for reading the artifact bytes. If no such artifact exists, or an error occurs during
* reading, an exception is thrown.
*
* @param artifactId the id of the artifact to get
* @return an InputStream for the artifact bytes
* @throws IOException if there as an exception reading from the store.
* @throws NotFoundException if the given artifact does not exist
*/
@Override
public InputStream newInputStream(Id.Artifact artifactId) throws IOException, NotFoundException {
String namespaceId = artifactId.getNamespace().getId();
ArtifactScope scope = ArtifactScope.USER;
// as long as it exists. Using default because it will always be there
if (NamespaceId.SYSTEM.getNamespace().equalsIgnoreCase(namespaceId)) {
namespaceId = NamespaceId.DEFAULT.getNamespace();
scope = ArtifactScope.SYSTEM;
}
String url = String.format("namespaces/%s/artifacts/%s/versions/%s/download?scope=%s", namespaceId, artifactId.getName(), artifactId.getVersion(), scope);
HttpURLConnection urlConn = remoteClient.openConnection(HttpMethod.GET, url);
throwIfError(artifactId, urlConn);
// Use FilterInputStream and override close to ensure the connection is closed once the input stream is closed
return new FilterInputStream(urlConn.getInputStream()) {
@Override
public void close() throws IOException {
try {
super.close();
} finally {
urlConn.disconnect();
}
}
};
}
use of io.cdap.cdap.api.artifact.ArtifactScope in project cdap by cdapio.
the class ArtifactSelectorProvider method getArtifactSelector.
/**
* Gets the corresponding {@link ArtifactSelector} for this config.
* Validates that any given scope, name, and version are all valid or null. The scope must be an
* {@link ArtifactScope}, the version must be an {@link ArtifactVersion}, and the name only contains
* alphanumeric, '-', or '_'. Also checks that at least one field is non-null.
*
* @return an {@link ArtifactSelector} using these config settings
* @throws IllegalArgumentException if any one of the fields are invalid
*/
private ArtifactSelector getArtifactSelector(ArtifactSelectorConfig config) {
String name = config.getName();
if (name != null && !nameMatcher.matchesAllOf(name)) {
throw new IllegalArgumentException(String.format("'%s' is an invalid artifact name. " + "Must contain only alphanumeric, '-', '.', or '_' characters.", name));
}
String version = config.getVersion();
ArtifactVersionRange range;
try {
range = version == null ? null : ArtifactVersionRange.parse(version);
} catch (InvalidArtifactRangeException e) {
throw new IllegalArgumentException(String.format("%s is an invalid artifact version." + "Must be an exact version or a version range " + "with a lower and upper bound.", version));
}
String scope = config.getScope();
ArtifactScope artifactScope = scope == null ? null : ArtifactScope.valueOf(scope.toUpperCase());
return new ArtifactSelector(artifactScope, name, range);
}
Aggregations