Search in sources :

Example 6 with ApplicationDetail

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());
}
Also used : ApplicationDetail(io.cdap.cdap.proto.ApplicationDetail) ArtifactSummary(io.cdap.cdap.api.artifact.ArtifactSummary) ArtifactId(io.cdap.cdap.api.artifact.ArtifactId) ApplicationNotFoundException(io.cdap.cdap.common.ApplicationNotFoundException) ArtifactRange(io.cdap.cdap.api.artifact.ArtifactRange) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) ArtifactNotFoundException(io.cdap.cdap.common.ArtifactNotFoundException) ArtifactDetail(io.cdap.cdap.internal.app.runtime.artifact.ArtifactDetail)

Example 7 with ApplicationDetail

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);
}
Also used : ApplicationDetail(io.cdap.cdap.proto.ApplicationDetail) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) After(org.junit.After)

Example 8 with ApplicationDetail

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());
}
Also used : ApplicationDetail(io.cdap.cdap.proto.ApplicationDetail) Test(org.junit.Test)

Example 9 with ApplicationDetail

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);
    }
}
Also used : WorkflowAppWithFork(io.cdap.cdap.WorkflowAppWithFork) ArrayList(java.util.ArrayList) Requirements(io.cdap.cdap.api.annotation.Requirements) ApplicationDetail(io.cdap.cdap.proto.ApplicationDetail) CapabilityAppWithWorkflow(io.cdap.cdap.CapabilityAppWithWorkflow) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 10 with ApplicationDetail

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);
        }
    });
}
Also used : ApplicationDetail(io.cdap.cdap.proto.ApplicationDetail) AccessException(io.cdap.cdap.api.security.AccessException) InstanceId(io.cdap.cdap.proto.id.InstanceId) ZipEntry(java.util.zip.ZipEntry) OutputStreamWriter(java.io.OutputStreamWriter) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) IOException(java.io.IOException) JsonIOException(com.google.gson.JsonIOException) JsonWriter(com.google.gson.stream.JsonWriter) HashSet(java.util.HashSet)

Aggregations

ApplicationDetail (io.cdap.cdap.proto.ApplicationDetail)52 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)30 Test (org.junit.Test)30 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)26 File (java.io.File)20 ArrayList (java.util.ArrayList)20 ArtifactSummary (io.cdap.cdap.api.artifact.ArtifactSummary)16 HttpResponse (io.cdap.common.http.HttpResponse)16 HashSet (java.util.HashSet)16 Gson (com.google.gson.Gson)14 Constants (io.cdap.cdap.common.conf.Constants)14 ProgramId (io.cdap.cdap.proto.id.ProgramId)14 HashMap (java.util.HashMap)14 List (java.util.List)14 Map (java.util.Map)14 Assert (org.junit.Assert)14 ImmutableMap (com.google.common.collect.ImmutableMap)12 ImmutableSet (com.google.common.collect.ImmutableSet)12 RunRecord (io.cdap.cdap.proto.RunRecord)12 AppRequest (io.cdap.cdap.proto.artifact.AppRequest)12