Search in sources :

Example 46 with ArtifactSummary

use of co.cask.cdap.api.artifact.ArtifactSummary in project cdap by caskdata.

the class DefaultPreviewRunner method startPreview.

@Override
public void startPreview(PreviewRequest<?> previewRequest) throws Exception {
    namespaceAdmin.create(new NamespaceMeta.Builder().setName(previewRequest.getProgram().getNamespaceId()).build());
    programId = previewRequest.getProgram();
    AppRequest<?> request = previewRequest.getAppRequest();
    ArtifactSummary artifactSummary = request.getArtifact();
    ApplicationId preview = programId.getParent();
    DataTracerFactoryProvider.setDataTracerFactory(preview, dataTracerFactory);
    String config = request.getConfig() == null ? null : GSON.toJson(request.getConfig());
    try {
        applicationLifecycleService.deployApp(preview.getParent(), preview.getApplication(), preview.getVersion(), artifactSummary, config, NOOP_PROGRAM_TERMINATOR, null, request.canUpdateSchedules());
    } catch (Exception e) {
        this.status = new PreviewStatus(PreviewStatus.Status.DEPLOY_FAILED, new BasicThrowable(e), null, null);
        throw e;
    }
    final PreviewConfig previewConfig = previewRequest.getAppRequest().getPreview();
    ProgramController controller = programLifecycleService.start(programId, previewConfig == null ? Collections.<String, String>emptyMap() : previewConfig.getRuntimeArgs(), false);
    controller.addListener(new AbstractListener() {

        @Override
        public void init(ProgramController.State currentState, @Nullable Throwable cause) {
            setStatus(new PreviewStatus(PreviewStatus.Status.RUNNING, null, System.currentTimeMillis(), null));
            // Only have timer if there is a timeout setting.
            if (previewConfig.getTimeout() != null) {
                timer = new Timer();
                final int timeOutMinutes = previewConfig.getTimeout();
                timer.schedule(new TimerTask() {

                    @Override
                    public void run() {
                        try {
                            LOG.info("Stopping the preview since it has reached running time: {} mins.", timeOutMinutes);
                            stopPreview();
                            killedByTimer = true;
                        } catch (Exception e) {
                            LOG.debug("Error shutting down the preview run with id: {}", programId);
                        }
                    }
                }, timeOutMinutes * 60 * 1000);
            }
        }

        @Override
        public void completed() {
            setStatus(new PreviewStatus(PreviewStatus.Status.COMPLETED, null, status.getStartTime(), System.currentTimeMillis()));
            shutDownUnrequiredServices();
        }

        @Override
        public void killed() {
            if (!killedByTimer) {
                setStatus(new PreviewStatus(PreviewStatus.Status.KILLED, null, status.getStartTime(), System.currentTimeMillis()));
            } else {
                setStatus(new PreviewStatus(PreviewStatus.Status.KILLED_BY_TIMER, null, status.getStartTime(), System.currentTimeMillis()));
            }
            shutDownUnrequiredServices();
        }

        @Override
        public void error(Throwable cause) {
            setStatus(new PreviewStatus(PreviewStatus.Status.RUN_FAILED, new BasicThrowable(cause), status.getStartTime(), System.currentTimeMillis()));
            shutDownUnrequiredServices();
        }
    }, Threads.SAME_THREAD_EXECUTOR);
    runId = controller.getProgramRunId();
}
Also used : ProgramController(co.cask.cdap.app.runtime.ProgramController) ArtifactSummary(co.cask.cdap.api.artifact.ArtifactSummary) Timer(java.util.Timer) TimerTask(java.util.TimerTask) NamespaceMeta(co.cask.cdap.proto.NamespaceMeta) PreviewStatus(co.cask.cdap.app.preview.PreviewStatus) BasicThrowable(co.cask.cdap.proto.BasicThrowable) AbstractListener(co.cask.cdap.internal.app.runtime.AbstractListener) ApplicationId(co.cask.cdap.proto.id.ApplicationId) BasicThrowable(co.cask.cdap.proto.BasicThrowable) PreviewConfig(co.cask.cdap.proto.artifact.preview.PreviewConfig)

Example 47 with ArtifactSummary

use of co.cask.cdap.api.artifact.ArtifactSummary in project cdap by caskdata.

the class AuthorizationArtifactRepository method clear.

@Override
public void clear(NamespaceId namespace) throws Exception {
    List<ArtifactSummary> artifacts = delegate.getArtifactSummaries(namespace, false);
    for (ArtifactSummary artifactSummary : artifacts) {
        authorizationEnforcer.enforce(namespace.artifact(artifactSummary.getName(), artifactSummary.getVersion()), authenticationContext.getPrincipal(), Action.ADMIN);
    }
    delegate.clear(namespace);
}
Also used : ArtifactSummary(co.cask.cdap.api.artifact.ArtifactSummary)

Example 48 with ArtifactSummary

use of co.cask.cdap.api.artifact.ArtifactSummary in project cdap by caskdata.

the class DefaultArtifactRepository method getApplicationClasses.

@Override
public List<ApplicationClassInfo> getApplicationClasses(NamespaceId namespace, String className) throws IOException {
    List<ApplicationClassInfo> infos = Lists.newArrayList();
    for (Map.Entry<ArtifactDescriptor, ApplicationClass> entry : artifactStore.getApplicationClasses(namespace, className).entrySet()) {
        ArtifactSummary artifactSummary = ArtifactSummary.from(entry.getKey().getArtifactId());
        ApplicationClass appClass = entry.getValue();
        infos.add(new ApplicationClassInfo(artifactSummary, appClass.getClassName(), appClass.getConfigSchema()));
    }
    return Collections.unmodifiableList(infos);
}
Also used : ArtifactSummary(co.cask.cdap.api.artifact.ArtifactSummary) ApplicationClassInfo(co.cask.cdap.proto.artifact.ApplicationClassInfo) ApplicationClass(co.cask.cdap.api.artifact.ApplicationClass) Map(java.util.Map) SortedMap(java.util.SortedMap) HashMap(java.util.HashMap)

Example 49 with ArtifactSummary

use of co.cask.cdap.api.artifact.ArtifactSummary in project cdap by caskdata.

the class ApplicationLifecycleService method getApps.

/**
 * Get all applications in the specified namespace that satisfy the specified predicate.
 *
 * @param namespace the namespace to get apps from
 * @param predicate the predicate that must be satisfied in order to be returned
 * @return list of all applications in the namespace that satisfy the specified predicate
 */
public List<ApplicationRecord> getApps(final NamespaceId namespace, com.google.common.base.Predicate<ApplicationRecord> predicate) throws Exception {
    List<ApplicationRecord> appRecords = new ArrayList<>();
    Map<ApplicationId, ApplicationSpecification> appSpecs = new HashMap<>();
    for (ApplicationSpecification appSpec : store.getAllApplications(namespace)) {
        appSpecs.put(namespace.app(appSpec.getName(), appSpec.getAppVersion()), appSpec);
    }
    appSpecs.keySet().retainAll(authorizationEnforcer.isVisible(appSpecs.keySet(), authenticationContext.getPrincipal()));
    for (ApplicationId appId : appSpecs.keySet()) {
        ApplicationSpecification appSpec = appSpecs.get(appId);
        if (appSpec == null) {
            continue;
        }
        // possible if this particular app was deploy prior to v3.2 and upgrade failed for some reason.
        ArtifactId artifactId = appSpec.getArtifactId();
        ArtifactSummary artifactSummary = artifactId == null ? new ArtifactSummary(appSpec.getName(), null) : ArtifactSummary.from(artifactId);
        ApplicationRecord record = new ApplicationRecord(artifactSummary, appId, appSpec.getDescription(), ownerAdmin.getOwnerPrincipal(appId));
        if (predicate.apply(record)) {
            appRecords.add(record);
        }
    }
    return appRecords;
}
Also used : ApplicationSpecification(co.cask.cdap.api.app.ApplicationSpecification) ArtifactSummary(co.cask.cdap.api.artifact.ArtifactSummary) ArtifactId(co.cask.cdap.api.artifact.ArtifactId) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ApplicationId(co.cask.cdap.proto.id.ApplicationId) ApplicationRecord(co.cask.cdap.proto.ApplicationRecord)

Example 50 with ArtifactSummary

use of co.cask.cdap.api.artifact.ArtifactSummary in project cdap by caskdata.

the class AppLifecycleHttpHandlerTest method testOwnerUsingArtifact.

@Test
public void testOwnerUsingArtifact() throws Exception {
    ArtifactId artifactId = new ArtifactId(NamespaceId.DEFAULT.getNamespace(), "wordCountArtifact", "1.0.0");
    addAppArtifact(Id.Artifact.fromEntityId(artifactId), WordCountApp.class);
    ApplicationId applicationId = new ApplicationId(NamespaceId.DEFAULT.getNamespace(), "WordCountApp");
    // deploy an app with a owner
    String ownerPrincipal = "alice/somehost.net@somekdc.net";
    AppRequest<ConfigTestApp.ConfigClass> appRequest = new AppRequest<>(new ArtifactSummary(artifactId.getArtifact(), artifactId.getVersion()), null, ownerPrincipal);
    Assert.assertEquals(HttpResponseCodes.SC_OK, deploy(applicationId, appRequest).getStatusLine().getStatusCode());
    // should be able to retrieve the owner information of the app
    JsonObject appDetails = getAppDetails(NamespaceId.DEFAULT.getNamespace(), applicationId.getApplication());
    Assert.assertEquals(ownerPrincipal, appDetails.get(Constants.Security.PRINCIPAL).getAsString());
    // the stream created by the app should have the app owner too
    Assert.assertEquals(ownerPrincipal, getStreamConfig(applicationId.getNamespaceId().stream("text")).getOwnerPrincipal());
    // the dataset created by the app should have the app owner too
    Assert.assertEquals(ownerPrincipal, getDatasetMeta(applicationId.getNamespaceId().dataset("mydataset")).getOwnerPrincipal());
    // trying to deploy the same app with another owner should fail
    String bobPrincipal = "bob/somehost.net@somekdc.net";
    appRequest = new AppRequest<>(new ArtifactSummary(artifactId.getArtifact(), artifactId.getVersion()), null, bobPrincipal);
    Assert.assertEquals(HttpResponseCodes.SC_FORBIDDEN, deploy(applicationId, appRequest).getStatusLine().getStatusCode());
    // trying to deploy the same app with different version and another owner should fail too
    appRequest = new AppRequest<>(new ArtifactSummary(artifactId.getArtifact(), artifactId.getVersion()), null, bobPrincipal);
    Assert.assertEquals(HttpResponseCodes.SC_FORBIDDEN, deploy(new ApplicationId(applicationId.getNamespace(), applicationId.getApplication(), "1.0"), appRequest).getStatusLine().getStatusCode());
    // trying to re-deploy the same app with same owner should pass
    appRequest = new AppRequest<>(new ArtifactSummary(artifactId.getArtifact(), artifactId.getVersion()), null, ownerPrincipal);
    Assert.assertEquals(HttpResponseCodes.SC_OK, deploy(applicationId, appRequest).getStatusLine().getStatusCode());
    // trying to re-deploy the same app with different version but same owner should pass
    appRequest = new AppRequest<>(new ArtifactSummary(artifactId.getArtifact(), artifactId.getVersion()), null, ownerPrincipal);
    Assert.assertEquals(HttpResponseCodes.SC_OK, deploy(new ApplicationId(applicationId.getNamespace(), applicationId.getApplication(), "1.0"), appRequest).getStatusLine().getStatusCode());
    // clean up the app
    Assert.assertEquals(200, doDelete(getVersionedAPIPath("apps/" + applicationId.getApplication(), applicationId.getNamespace())).getStatusLine().getStatusCode());
    // deletion of app should delete the stream/dataset owner information as they themselves are not deleted
    Assert.assertEquals(ownerPrincipal, getStreamConfig(applicationId.getNamespaceId().stream("text")).getOwnerPrincipal());
    Assert.assertEquals(ownerPrincipal, getDatasetMeta(applicationId.getNamespaceId().dataset("mydataset")).getOwnerPrincipal());
    // cleanup
    deleteNamespace(NamespaceId.DEFAULT.getNamespace());
}
Also used : ArtifactSummary(co.cask.cdap.api.artifact.ArtifactSummary) ArtifactId(co.cask.cdap.proto.id.ArtifactId) JsonObject(com.google.gson.JsonObject) ApplicationId(co.cask.cdap.proto.id.ApplicationId) AppRequest(co.cask.cdap.proto.artifact.AppRequest) Test(org.junit.Test)

Aggregations

ArtifactSummary (co.cask.cdap.api.artifact.ArtifactSummary)64 Test (org.junit.Test)33 ApplicationId (co.cask.cdap.proto.id.ApplicationId)32 AppRequest (co.cask.cdap.proto.artifact.AppRequest)31 ArtifactId (co.cask.cdap.proto.id.ArtifactId)25 NamespaceId (co.cask.cdap.proto.id.NamespaceId)17 Id (co.cask.cdap.common.id.Id)13 IOException (java.io.IOException)13 ProgramId (co.cask.cdap.proto.id.ProgramId)12 HashMap (java.util.HashMap)10 ArtifactVersion (co.cask.cdap.api.artifact.ArtifactVersion)9 NotFoundException (co.cask.cdap.common.NotFoundException)8 HashSet (java.util.HashSet)8 PluginInfo (co.cask.cdap.proto.artifact.PluginInfo)7 Map (java.util.Map)7 Set (java.util.Set)7 JsonObject (com.google.gson.JsonObject)6 URL (java.net.URL)6 ConfigTestApp (co.cask.cdap.ConfigTestApp)5 ArtifactRange (co.cask.cdap.api.artifact.ArtifactRange)5