Search in sources :

Example 6 with ArtifactScope

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);
}
Also used : ArtifactScope(io.cdap.cdap.api.artifact.ArtifactScope) ArtifactSummary(io.cdap.cdap.api.artifact.ArtifactSummary) Table(io.cdap.cdap.cli.util.table.Table) RowMaker(io.cdap.cdap.cli.util.RowMaker)

Example 7 with ArtifactScope

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()));
    }
}
Also used : NotImplementedException(io.cdap.cdap.common.NotImplementedException) ApplicationNotFoundException(io.cdap.cdap.common.ApplicationNotFoundException) NamespaceNotFoundException(io.cdap.cdap.common.NamespaceNotFoundException) NotFoundException(io.cdap.cdap.common.NotFoundException) ArtifactNotFoundException(io.cdap.cdap.common.ArtifactNotFoundException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JsonWriter(com.google.gson.stream.JsonWriter) ApplicationNotFoundException(io.cdap.cdap.common.ApplicationNotFoundException) UnauthorizedException(io.cdap.cdap.security.spi.authorization.UnauthorizedException) NamespaceNotFoundException(io.cdap.cdap.common.NamespaceNotFoundException) WriteConflictException(io.cdap.cdap.internal.app.runtime.artifact.WriteConflictException) DatasetManagementException(io.cdap.cdap.api.dataset.DatasetManagementException) IOException(java.io.IOException) ConflictException(io.cdap.cdap.common.ConflictException) NotImplementedException(io.cdap.cdap.common.NotImplementedException) ExecutionException(java.util.concurrent.ExecutionException) AccessException(io.cdap.cdap.api.security.AccessException) InvalidArtifactException(io.cdap.cdap.common.InvalidArtifactException) ArtifactAlreadyExistsException(io.cdap.cdap.common.ArtifactAlreadyExistsException) NotFoundException(io.cdap.cdap.common.NotFoundException) ServiceException(io.cdap.cdap.common.ServiceException) JsonSyntaxException(com.google.gson.JsonSyntaxException) BadRequestException(io.cdap.cdap.common.BadRequestException) ArtifactNotFoundException(io.cdap.cdap.common.ArtifactNotFoundException) ArtifactScope(io.cdap.cdap.api.artifact.ArtifactScope) ApplicationUpdateDetail(io.cdap.cdap.proto.ApplicationUpdateDetail) ServiceException(io.cdap.cdap.common.ServiceException) OutputStreamWriter(java.io.OutputStreamWriter) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) ChunkResponder(io.cdap.http.ChunkResponder) InvalidArtifactException(io.cdap.cdap.common.InvalidArtifactException) Path(javax.ws.rs.Path) AuditPolicy(io.cdap.cdap.common.security.AuditPolicy) POST(javax.ws.rs.POST)

Example 8 with ArtifactScope

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());
}
Also used : ArtifactScope(io.cdap.cdap.api.artifact.ArtifactScope) ArtifactId(io.cdap.cdap.api.artifact.ArtifactId) HashMap(java.util.HashMap) DefaultScheduleBuilder(io.cdap.cdap.internal.app.runtime.schedule.DefaultScheduleBuilder) DefaultApplicationSpecification(io.cdap.cdap.internal.app.DefaultApplicationSpecification) ScheduleCreationSpec(io.cdap.cdap.internal.schedule.ScheduleCreationSpec) Map(java.util.Map) HashMap(java.util.HashMap)

Example 9 with ArtifactScope

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();
            }
        }
    };
}
Also used : ArtifactScope(io.cdap.cdap.api.artifact.ArtifactScope) FilterInputStream(java.io.FilterInputStream) HttpURLConnection(java.net.HttpURLConnection)

Example 10 with ArtifactScope

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);
}
Also used : ArtifactScope(io.cdap.cdap.api.artifact.ArtifactScope) ArtifactVersionRange(io.cdap.cdap.api.artifact.ArtifactVersionRange) InvalidArtifactRangeException(io.cdap.cdap.api.artifact.InvalidArtifactRangeException)

Aggregations

ArtifactScope (io.cdap.cdap.api.artifact.ArtifactScope)32 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)12 ArtifactId (io.cdap.cdap.api.artifact.ArtifactId)8 ArtifactSummary (io.cdap.cdap.api.artifact.ArtifactSummary)8 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)8 File (java.io.File)8 Map (java.util.Map)8 ArtifactVersion (io.cdap.cdap.api.artifact.ArtifactVersion)6 ArtifactVersionRange (io.cdap.cdap.api.artifact.ArtifactVersionRange)6 AppRequest (io.cdap.cdap.proto.artifact.AppRequest)6 ArtifactId (io.cdap.cdap.proto.id.ArtifactId)6 ArrayList (java.util.ArrayList)6 FileReader (java.io.FileReader)5 Gson (com.google.gson.Gson)4 GsonBuilder (com.google.gson.GsonBuilder)4 JsonObject (com.google.gson.JsonObject)4 JsonSyntaxException (com.google.gson.JsonSyntaxException)4 JsonWriter (com.google.gson.stream.JsonWriter)4 ApplicationConfigUpdateAction (io.cdap.cdap.api.app.ApplicationConfigUpdateAction)4 AccessException (io.cdap.cdap.api.security.AccessException)4