use of io.cdap.cdap.internal.app.runtime.artifact.ArtifactDetail in project cdap by caskdata.
the class ArtifactHttpHandler method getProperties.
@GET
@Path("/namespaces/{namespace-id}/artifacts/{artifact-name}/versions/{artifact-version}/properties")
public void getProperties(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("artifact-name") String artifactName, @PathParam("artifact-version") String artifactVersion, @QueryParam("scope") @DefaultValue("user") String scope, @QueryParam("keys") @Nullable String keys) throws Exception {
NamespaceId namespace = validateAndGetScopedNamespace(Ids.namespace(namespaceId), scope);
ArtifactId artifactId = validateAndGetArtifactId(namespace, artifactName, artifactVersion);
try {
ArtifactDetail artifactDetail = artifactRepository.getArtifact(Id.Artifact.fromEntityId(artifactId));
Map<String, String> properties = artifactDetail.getMeta().getProperties();
Map<String, String> result;
if (keys != null && !keys.isEmpty()) {
result = new HashMap<>();
for (String key : Splitter.on(',').trimResults().split(keys)) {
result.put(key, properties.get(key));
}
} else {
result = properties;
}
responder.sendJson(HttpResponseStatus.OK, GSON.toJson(result));
} catch (IOException e) {
LOG.error("Exception reading artifacts named {} for namespace {} from the store.", artifactName, namespaceId, e);
responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR, "Error reading artifact properties from the store.");
}
}
use of io.cdap.cdap.internal.app.runtime.artifact.ArtifactDetail in project cdap by caskdata.
the class ArtifactHttpHandler method getArtifactProperties.
@POST
@Path("/namespaces/{namespace-id}/artifactproperties")
public void getArtifactProperties(FullHttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @QueryParam("order") @DefaultValue("DESC") String order) throws Exception {
NamespaceId namespace = validateAndGetNamespace(namespaceId);
ArtifactSortOrder sortOrder = ArtifactSortOrder.valueOf(order);
List<ArtifactPropertiesRequest> propertyRequests;
try (Reader reader = new InputStreamReader(new ByteBufInputStream(request.content()), StandardCharsets.UTF_8)) {
propertyRequests = GSON.fromJson(reader, BATCH_ARTIFACT_PROPERTIES_REQUEST);
} catch (JsonSyntaxException e) {
throw new BadRequestException("Unable to parse request: " + e.getMessage(), e);
}
List<ArtifactSummaryProperties> result = new ArrayList<>(propertyRequests.size());
for (ArtifactPropertiesRequest propertiesRequest : propertyRequests) {
NamespaceId requestNamespace = propertiesRequest.getScope() == ArtifactScope.SYSTEM ? NamespaceId.SYSTEM : namespace;
ArtifactRange range = new ArtifactRange(requestNamespace.getNamespace(), propertiesRequest.getName(), ArtifactVersionRange.parse(propertiesRequest.getVersion()));
List<ArtifactDetail> artifactDetails = artifactRepository.getArtifactDetails(range, 1, sortOrder);
for (ArtifactDetail artifactDetail : artifactDetails) {
Map<String, String> properties = artifactDetail.getMeta().getProperties();
Map<String, String> filteredProperties = new HashMap<>(propertiesRequest.getProperties().size());
for (String propertyKey : propertiesRequest.getProperties()) {
if (properties.containsKey(propertyKey)) {
filteredProperties.put(propertyKey, properties.get(propertyKey));
}
}
String artifactVersion = artifactDetail.getDescriptor().getArtifactId().getVersion().getVersion();
result.add(new ArtifactSummaryProperties(propertiesRequest.getName(), artifactVersion, propertiesRequest.getScope(), filteredProperties));
}
}
responder.sendJson(HttpResponseStatus.OK, GSON.toJson(result, BATCH_ARTIFACT_PROPERTIES_RESPONSE));
}
use of io.cdap.cdap.internal.app.runtime.artifact.ArtifactDetail in project cdap by caskdata.
the class AbstractProgramRuntimeService method run.
@Override
public final RuntimeInfo run(ProgramDescriptor programDescriptor, ProgramOptions options, RunId runId) {
ProgramId programId = programDescriptor.getProgramId();
ProgramRunId programRunId = programId.run(runId);
ClusterMode clusterMode = ProgramRunners.getClusterMode(options);
// Creates the ProgramRunner based on the cluster mode
ProgramRunner runner = (clusterMode == ClusterMode.ON_PREMISE ? programRunnerFactory : Optional.ofNullable(remoteProgramRunnerFactory).orElseThrow(UnsupportedOperationException::new)).create(programId.getType());
File tempDir = createTempDirectory(programId, runId);
AtomicReference<Runnable> cleanUpTaskRef = new AtomicReference<>(createCleanupTask(tempDir, runner));
DelayedProgramController controller = new DelayedProgramController(programRunId);
RuntimeInfo runtimeInfo = createRuntimeInfo(controller, programId, () -> cleanUpTaskRef.get().run());
updateRuntimeInfo(runtimeInfo);
executor.execute(() -> {
try {
// Get the artifact details and save it into the program options.
ArtifactId artifactId = programDescriptor.getArtifactId();
ArtifactDetail artifactDetail = getArtifactDetail(artifactId);
ApplicationSpecification appSpec = programDescriptor.getApplicationSpecification();
ProgramDescriptor newProgramDescriptor = programDescriptor;
boolean isPreview = Boolean.valueOf(options.getArguments().getOption(ProgramOptionConstants.IS_PREVIEW, "false"));
// for preview we already have a resolved app spec, so no need to regenerate the app spec again
if (!isPreview && appSpec != null && ClusterMode.ON_PREMISE.equals(clusterMode)) {
try {
ApplicationSpecification generatedAppSpec = regenerateAppSpec(artifactDetail, programId, artifactId, appSpec, options);
appSpec = generatedAppSpec != null ? generatedAppSpec : appSpec;
newProgramDescriptor = new ProgramDescriptor(programDescriptor.getProgramId(), appSpec);
} catch (Exception e) {
LOG.warn("Failed to regenerate the app spec for program {}, using the existing app spec", programId);
}
}
ProgramOptions runtimeProgramOptions = updateProgramOptions(artifactId, programId, options, runId, clusterMode, Iterables.getFirst(artifactDetail.getMeta().getClasses().getApps(), null));
// Take a snapshot of all the plugin artifacts used by the program
ProgramOptions optionsWithPlugins = createPluginSnapshot(runtimeProgramOptions, programId, tempDir, newProgramDescriptor.getApplicationSpecification());
// Create and run the program
Program executableProgram = createProgram(cConf, runner, newProgramDescriptor, artifactDetail, tempDir);
cleanUpTaskRef.set(createCleanupTask(cleanUpTaskRef.get(), executableProgram));
controller.setProgramController(runner.run(executableProgram, optionsWithPlugins));
} catch (Exception e) {
controller.failed(e);
programStateWriter.error(programRunId, e);
LOG.error("Exception while trying to run program", e);
}
});
return runtimeInfo;
}
use of io.cdap.cdap.internal.app.runtime.artifact.ArtifactDetail in project cdap by caskdata.
the class CapabilityApplier method shouldDeployApp.
// Returns true if capability applier should try to deploy this application. 2 conditions when it returns true:
// 1. Either the application is not deployed before.
// 2. If application is deployed before then the app artifact of the deployed application is not the latest one
// available.
private boolean shouldDeployApp(ApplicationId applicationId, SystemApplication application) throws Exception {
ApplicationDetail currAppDetail;
try {
currAppDetail = applicationLifecycleService.getAppDetail(applicationId);
} catch (ApplicationNotFoundException exception) {
return true;
}
// Compare if the app artifact version of currently deployed application with highest version of app artifact
// available. If it's not same, capability applier should redeploy application.
ArtifactSummary summary = application.getArtifact();
NamespaceId artifactNamespace = ArtifactScope.SYSTEM.equals(summary.getScope()) ? NamespaceId.SYSTEM : applicationId.getParent();
ArtifactRange range = new ArtifactRange(artifactNamespace.getNamespace(), summary.getName(), ArtifactVersionRange.parse(summary.getVersion()));
// this method will not throw ArtifactNotFoundException, if no artifacts in the range, we are expecting an empty
// collection returned.
List<ArtifactDetail> artifactDetail = artifactRepository.getArtifactDetails(range, 1, ArtifactSortOrder.DESC);
if (artifactDetail.isEmpty()) {
throw new ArtifactNotFoundException(range.getNamespace(), range.getName());
}
ArtifactId latestArtifactId = artifactDetail.get(0).getDescriptor().getArtifactId();
// same artifact. If same means no need to deploy the application again.
return !currAppDetail.getArtifact().getVersion().equals(latestArtifactId.getVersion().getVersion());
}
use of io.cdap.cdap.internal.app.runtime.artifact.ArtifactDetail in project cdap by caskdata.
the class ApplicationLifecycleService method upgradeApplication.
/**
* Upgrades an existing application by upgrading application artifact versions and plugin artifact versions.
*
* @param appId the id of the application to upgrade.
* @param allowedArtifactScopes artifact scopes allowed while looking for latest artifacts for upgrade.
* @param allowSnapshot whether to consider snapshot version of artifacts or not for upgrade.
* @throws IllegalStateException if something unexpected happened during upgrade.
* @throws IOException if there was an IO error during initializing application class from artifact.
* @throws JsonIOException if there was an error in serializing or deserializing app config.
* @throws UnsupportedOperationException if application does not support upgrade operation.
* @throws InvalidArtifactException if candidate application artifact is invalid for upgrade purpose.
* @throws NotFoundException if any object related to upgrade is not found like application/artifact.
* @throws Exception if there was an exception during the upgrade of application. This exception will often wrap
* the actual exception
*/
public void upgradeApplication(ApplicationId appId, Set<ArtifactScope> allowedArtifactScopes, boolean allowSnapshot) throws Exception {
// Check if the current user has admin privileges on it before updating.
accessEnforcer.enforce(appId, authenticationContext.getPrincipal(), StandardPermission.UPDATE);
// check that app exists
ApplicationSpecification currentSpec = store.getApplication(appId);
if (currentSpec == null) {
LOG.info("Application {} not found for upgrade.", appId);
throw new NotFoundException(appId);
}
ArtifactId currentArtifact = currentSpec.getArtifactId();
ArtifactSummary candidateArtifact = getLatestAppArtifactForUpgrade(appId, currentArtifact, allowedArtifactScopes, allowSnapshot);
ArtifactVersion candidateArtifactVersion = new ArtifactVersion(candidateArtifact.getVersion());
// Current artifact should not have higher version than candidate artifact.
if (currentArtifact.getVersion().compareTo(candidateArtifactVersion) > 0) {
String error = String.format("The current artifact has a version higher %s than any existing artifact.", currentArtifact.getVersion());
throw new InvalidArtifactException(error);
}
ArtifactId newArtifactId = new ArtifactId(candidateArtifact.getName(), candidateArtifactVersion, candidateArtifact.getScope());
Id.Artifact newArtifact = Id.Artifact.fromEntityId(Artifacts.toProtoArtifactId(appId.getParent(), newArtifactId));
ArtifactDetail newArtifactDetail = artifactRepository.getArtifact(newArtifact);
updateApplicationInternal(appId, currentSpec.getConfiguration(), programId -> {
}, newArtifactDetail, Collections.singletonList(ApplicationConfigUpdateAction.UPGRADE_ARTIFACT), allowedArtifactScopes, allowSnapshot, ownerAdmin.getOwner(appId), false);
}
Aggregations