use of io.cdap.cdap.proto.ApplicationDetail 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.proto.ApplicationDetail in project cdap by caskdata.
the class CapabilityManagementServiceTest method reset.
@After
public void reset() throws Exception {
// Reset all relevant stores.
for (ApplicationDetail appDetail : applicationLifecycleService.getApps(NamespaceId.SYSTEM)) {
programLifecycleService.stopAll(new ApplicationId(NamespaceId.SYSTEM.getNamespace(), appDetail.getName(), appDetail.getAppVersion()));
}
for (ApplicationDetail appDetail : applicationLifecycleService.getApps(NamespaceId.DEFAULT)) {
programLifecycleService.stopAll(new ApplicationId(NamespaceId.DEFAULT.getNamespace(), appDetail.getName(), appDetail.getAppVersion()));
}
applicationLifecycleService.removeAll(NamespaceId.SYSTEM);
applicationLifecycleService.removeAll(NamespaceId.DEFAULT);
artifactRepository.clear(NamespaceId.SYSTEM);
artifactRepository.clear(NamespaceId.DEFAULT);
}
use of io.cdap.cdap.proto.ApplicationDetail in project cdap by caskdata.
the class ApplicationDetailFetcherTest method testGetAllApplications.
@Test
public void testGetAllApplications() throws Exception {
ApplicationDetailFetcher fetcher = getApplicationDetailFetcher(fetcherType);
String namespace = TEST_NAMESPACE1;
List<ApplicationDetail> appDetailList = Collections.emptyList();
ApplicationDetail appDetail = null;
// No applications have been deployed
appDetailList = fetcher.list(namespace);
Assert.assertEquals(Collections.emptyList(), appDetailList);
// Deploy the application
deploy(AllProgramsApp.class, 200, Constants.Gateway.API_VERSION_3_TOKEN, namespace);
// Get and validate the application
appDetailList = fetcher.list(namespace);
Assert.assertEquals(1, appDetailList.size());
appDetail = appDetailList.get(0);
assertAllProgramAppDetail(appDetail);
// Deploy another application
deploy(AppWithSchedule.class, 200, Constants.Gateway.API_VERSION_3_TOKEN, namespace);
// Get and validate the application
appDetailList = fetcher.list(namespace);
Assert.assertEquals(2, appDetailList.size());
Assert.assertEquals(AllProgramsApp.NAME, appDetailList.get(0).getName());
Assert.assertEquals(AllProgramsApp.DESC, appDetailList.get(0).getDescription());
Assert.assertEquals(AppWithSchedule.NAME, appDetailList.get(1).getName());
Assert.assertEquals(AppWithSchedule.DESC, appDetailList.get(1).getDescription());
// Delete the application
Assert.assertEquals(200, doDelete(getVersionedAPIPath("apps/", Constants.Gateway.API_VERSION_3_TOKEN, namespace)).getResponseCode());
}
use of io.cdap.cdap.proto.ApplicationDetail in project cdap by caskdata.
the class CapabilityApplierTest method testGetAppsWithCapability.
@Test
public void testGetAppsWithCapability() throws Exception {
// Deploy application with capability
Class<CapabilityAppWithWorkflow> appWithWorkflowClass = CapabilityAppWithWorkflow.class;
Requirements declaredAnnotation = appWithWorkflowClass.getDeclaredAnnotation(Requirements.class);
// verify this app has capabilities
Assert.assertTrue(declaredAnnotation.capabilities().length > 0);
String appNameWithCapabilities = appWithWorkflowClass.getSimpleName() + UUID.randomUUID();
for (String capability : declaredAnnotation.capabilities()) {
CapabilityConfig capabilityConfig = new CapabilityConfig("Enable", CapabilityStatus.ENABLED, capability, Collections.emptyList(), Collections.emptyList(), Collections.emptyList());
capabilityWriter.addOrUpdateCapability(capability, CapabilityStatus.ENABLED, capabilityConfig);
}
deployArtifactAndApp(appWithWorkflowClass, appNameWithCapabilities);
// Deploy application without capability
Class<WorkflowAppWithFork> appNoCapabilityClass = WorkflowAppWithFork.class;
Requirements declaredAnnotation1 = appNoCapabilityClass.getDeclaredAnnotation(Requirements.class);
// verify this app has no capabilities
Assert.assertNull(declaredAnnotation1);
String appNameWithoutCapability = appNoCapabilityClass.getSimpleName() + UUID.randomUUID();
deployArtifactAndApp(appNoCapabilityClass, appNameWithoutCapability);
// verify that list applications return the application tagged with capability only
for (String capability : declaredAnnotation.capabilities()) {
EntityResult<ApplicationId> appsForCapability = capabilityApplier.getApplications(NamespaceId.DEFAULT, capability, null, 0, 10);
Set<ApplicationId> applicationIds = new HashSet<>(appsForCapability.getEntities());
List<ApplicationDetail> appsReturned = new ArrayList<>(applicationLifecycleService.getAppDetails(applicationIds).values());
appsReturned.forEach(applicationDetail -> Assert.assertEquals(appNameWithCapabilities, applicationDetail.getArtifact().getName()));
}
// delete the app and verify nothing is returned.
applicationLifecycleService.removeApplication(NamespaceId.DEFAULT.app(appNameWithCapabilities, TEST_VERSION));
for (String capability : declaredAnnotation.capabilities()) {
Set<ApplicationId> applicationIds = new HashSet<>(capabilityApplier.getApplications(NamespaceId.DEFAULT, capability, null, 0, 10).getEntities());
List<ApplicationDetail> appsReturned = new ArrayList<>(applicationLifecycleService.getAppDetails(applicationIds).values());
Assert.assertTrue(appsReturned.isEmpty());
}
applicationLifecycleService.removeApplication(NamespaceId.DEFAULT.app(appNameWithoutCapability, TEST_VERSION));
artifactRepository.deleteArtifact(Id.Artifact.from(new Id.Namespace(NamespaceId.DEFAULT.getNamespace()), appNameWithoutCapability, TEST_VERSION));
for (String capability : declaredAnnotation.capabilities()) {
capabilityWriter.deleteCapability(capability);
}
}
use of io.cdap.cdap.proto.ApplicationDetail in project cdap by caskdata.
the class ApplicationLifecycleService method createAppDetailsArchive.
/**
* Creates a ZIP archive that contains the {@link ApplicationDetail} for all applications. The archive created will
* contain a directory entry for each of the namespace. Inside each namespace directory, it contains the
* application detail json, the application name as the file name, with {@code ".json"} as the file extension.
* <p/>
* This method requires instance admin permission.
*
* @param zipOut the {@link ZipOutputStream} for writing out the application details
*/
public void createAppDetailsArchive(ZipOutputStream zipOut) throws Exception {
accessEnforcer.enforce(new InstanceId(cConf.get(Constants.INSTANCE_NAME)), authenticationContext.getPrincipal(), StandardPermission.GET);
Set<NamespaceId> namespaces = new HashSet<>();
JsonWriter jsonWriter = new JsonWriter(new OutputStreamWriter(zipOut, StandardCharsets.UTF_8));
store.scanApplications(batchSize, (appId, appSpec) -> {
// Skip the SYSTEM namespace apps
if (NamespaceId.SYSTEM.equals(appId.getParent())) {
return;
}
try {
ApplicationDetail applicationDetail = enforceApplicationDetailAccess(appId, ApplicationDetail.fromSpec(appSpec, null));
// Add a directory for the namespace
if (namespaces.add(appId.getParent())) {
ZipEntry entry = new ZipEntry(appId.getNamespace() + "/");
zipOut.putNextEntry(entry);
zipOut.closeEntry();
}
ZipEntry entry = new ZipEntry(appId.getNamespace() + "/" + appId.getApplication() + ".json");
zipOut.putNextEntry(entry);
GSON.toJson(applicationDetail, ApplicationDetail.class, jsonWriter);
jsonWriter.flush();
zipOut.closeEntry();
} catch (IOException | AccessException e) {
throw new RuntimeException("Failed to add zip entry for application " + appId, e);
}
});
}
Aggregations