Search in sources :

Example 1 with ApplicationClient

use of io.cdap.cdap.client.ApplicationClient in project cdap by caskdata.

the class IntegrationTestBase method assertNoApps.

private void assertNoApps(NamespaceId namespace) throws Exception {
    ApplicationClient applicationClient = getApplicationClient();
    List<ApplicationRecord> applicationRecords = applicationClient.list(namespace);
    List<String> applicationIds = Lists.newArrayList();
    for (ApplicationRecord applicationRecord : applicationRecords) {
        applicationIds.add(applicationRecord.getName());
    }
    Assert.assertTrue("Must have no deployed apps, but found the following apps: " + Joiner.on(", ").join(applicationIds), applicationRecords.isEmpty());
}
Also used : ApplicationClient(io.cdap.cdap.client.ApplicationClient) ApplicationRecord(io.cdap.cdap.proto.ApplicationRecord)

Example 2 with ApplicationClient

use of io.cdap.cdap.client.ApplicationClient in project cdap by caskdata.

the class IntegrationTestBaseTest method testDeployApplicationInNamespace.

@Test
public void testDeployApplicationInNamespace() throws Exception {
    NamespaceId namespace = new NamespaceId("Test1");
    NamespaceMeta namespaceMeta = new NamespaceMeta.Builder().setName(namespace).build();
    getNamespaceClient().create(namespaceMeta);
    ClientConfig clientConfig = new ClientConfig.Builder(getClientConfig()).build();
    deployApplication(namespace, AllProgramsApp.class);
    // Check the default namespaces applications to see whether the application wasn't made in the default namespace
    ClientConfig defaultClientConfig = new ClientConfig.Builder(getClientConfig()).build();
    Assert.assertTrue(new ApplicationClient(defaultClientConfig).list(NamespaceId.DEFAULT).isEmpty());
    ApplicationClient applicationClient = new ApplicationClient(clientConfig);
    Assert.assertEquals(AllProgramsApp.NAME, applicationClient.list(namespace).get(0).getName());
    applicationClient.delete(namespace.app(AllProgramsApp.NAME));
    Assert.assertTrue(new ApplicationClient(clientConfig).list(namespace).isEmpty());
}
Also used : ApplicationClient(io.cdap.cdap.client.ApplicationClient) NamespaceMeta(io.cdap.cdap.proto.NamespaceMeta) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) ClientConfig(io.cdap.cdap.client.config.ClientConfig) Test(org.junit.Test)

Example 3 with ApplicationClient

use of io.cdap.cdap.client.ApplicationClient in project cdap by caskdata.

the class PostUpgradeJobMain method restartPipelinesAndSchedules.

private static void restartPipelinesAndSchedules(ClientConfig clientConfig, long startTimeMillis, boolean restartSystemApps) throws Exception {
    long endTimeMillis = System.currentTimeMillis();
    ApplicationClient applicationClient = new ApplicationClient(clientConfig);
    ScheduleClient scheduleClient = new ScheduleClient(clientConfig);
    ProgramClient programClient = new ProgramClient(clientConfig);
    NamespaceClient namespaceClient = new NamespaceClient(clientConfig);
    List<NamespaceId> namespaceIdList = namespaceClient.list().stream().map(NamespaceMeta::getNamespaceId).collect(Collectors.toList());
    if (restartSystemApps) {
        namespaceIdList.add(NamespaceId.SYSTEM);
    }
    for (NamespaceId namespaceId : namespaceIdList) {
        for (ApplicationRecord record : applicationClient.list(namespaceId)) {
            ApplicationId applicationId = new ApplicationId(namespaceId.getNamespace(), record.getName(), record.getAppVersion());
            programClient.restart(applicationId, TimeUnit.MILLISECONDS.toSeconds(startTimeMillis), TimeUnit.MILLISECONDS.toSeconds(endTimeMillis));
        }
        // Re-enable schedules in a namespace AFTER programs have been restarted.
        scheduleClient.reEnableSuspendedSchedules(namespaceId, startTimeMillis, endTimeMillis);
    }
}
Also used : ApplicationClient(io.cdap.cdap.client.ApplicationClient) ProgramClient(io.cdap.cdap.client.ProgramClient) NamespaceClient(io.cdap.cdap.client.NamespaceClient) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) ScheduleClient(io.cdap.cdap.client.ScheduleClient) ApplicationRecord(io.cdap.cdap.proto.ApplicationRecord)

Example 4 with ApplicationClient

use of io.cdap.cdap.client.ApplicationClient in project cdap by caskdata.

the class UpgradeJobMain method suspendSchedulesAndStopPipelines.

private static void suspendSchedulesAndStopPipelines(ClientConfig clientConfig) throws Exception {
    ApplicationClient applicationClient = new ApplicationClient(clientConfig);
    ScheduleClient scheduleClient = new ScheduleClient(clientConfig);
    ProgramClient programClient = new ProgramClient(clientConfig);
    NamespaceClient namespaceClient = new NamespaceClient(clientConfig);
    boolean shouldRetry = false;
    List<NamespaceId> namespaceIdList = namespaceClient.list().stream().map(NamespaceMeta::getNamespaceId).collect(Collectors.toList());
    namespaceIdList.add(NamespaceId.SYSTEM);
    for (NamespaceId namespaceId : namespaceIdList) {
        for (ApplicationRecord record : applicationClient.list(namespaceId)) {
            ApplicationId applicationId = new ApplicationId(namespaceId.getNamespace(), record.getName(), record.getAppVersion());
            LOG.debug("Trying to stop schedule and workflows for application " + applicationId);
            List<WorkflowId> workflowIds = applicationClient.get(applicationId).getPrograms().stream().filter(programRecord -> programRecord.getType().equals(ProgramType.WORKFLOW)).map(programRecord -> new WorkflowId(applicationId, programRecord.getName())).collect(Collectors.toList());
            for (WorkflowId workflowId : workflowIds) {
                List<ScheduleId> scheduleIds = scheduleClient.listSchedules(workflowId).stream().map(scheduleDetail -> new ScheduleId(namespaceId.getNamespace(), record.getName(), record.getAppVersion(), scheduleDetail.getName())).collect(Collectors.toList());
                for (ScheduleId scheduleId : scheduleIds) {
                    if (scheduleClient.getStatus(scheduleId).equals(SCHEDULED)) {
                        scheduleClient.suspend(scheduleId);
                    }
                }
                // Need to stop workflows first or else the program will fail to stop below
                if (!programClient.getStatus(workflowId).equals(ProgramStatus.STOPPED.toString())) {
                    try {
                        programClient.stop(workflowId);
                    } catch (BadRequestException e) {
                        // transitioned to stop state since it was checked earlier or not.
                        if (!programClient.getStatus(workflowId).equals(ProgramStatus.STOPPED.toString())) {
                            // Pipeline still in running state. Continue with stopping rest of the pipelines in this namespace and
                            // next retry should try to stop/verify status for this pipeline.
                            shouldRetry = true;
                        }
                    }
                }
            }
        }
        // At least one pipeline is still in running state so retry to verify pipeline status .
        if (shouldRetry) {
            throw new RetryableException("At least one pipeline in namespace " + namespaceId + " is still running.");
        }
        // All schedules are stopped, now stop all programs
        programClient.stopAll(namespaceId);
    }
}
Also used : Retries(io.cdap.cdap.common.service.Retries) RetryStrategy(io.cdap.cdap.common.service.RetryStrategy) WorkflowId(io.cdap.cdap.proto.id.WorkflowId) ScheduleClient(io.cdap.cdap.client.ScheduleClient) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) LoggerFactory(org.slf4j.LoggerFactory) NamespaceClient(io.cdap.cdap.client.NamespaceClient) RetryStrategies(io.cdap.cdap.common.service.RetryStrategies) ProgramType(io.cdap.cdap.proto.ProgramType) ApplicationClient(io.cdap.cdap.client.ApplicationClient) ScheduleId(io.cdap.cdap.proto.id.ScheduleId) ConnectionConfig(io.cdap.cdap.client.config.ConnectionConfig) ProgramStatus(io.cdap.cdap.proto.ProgramStatus) ProgramClient(io.cdap.cdap.client.ProgramClient) Logger(org.slf4j.Logger) RetryableException(io.cdap.cdap.api.retry.RetryableException) IOException(java.io.IOException) BadRequestException(io.cdap.cdap.common.BadRequestException) Collectors(java.util.stream.Collectors) TimeUnit(java.util.concurrent.TimeUnit) NamespaceMeta(io.cdap.cdap.proto.NamespaceMeta) List(java.util.List) CConfiguration(io.cdap.cdap.common.conf.CConfiguration) ClientConfig(io.cdap.cdap.client.config.ClientConfig) Constants(io.cdap.cdap.common.conf.Constants) ApplicationRecord(io.cdap.cdap.proto.ApplicationRecord) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) NotFoundException(io.cdap.cdap.common.NotFoundException) SecurityUtil(io.cdap.cdap.security.impersonation.SecurityUtil) ApplicationClient(io.cdap.cdap.client.ApplicationClient) ProgramClient(io.cdap.cdap.client.ProgramClient) WorkflowId(io.cdap.cdap.proto.id.WorkflowId) ScheduleId(io.cdap.cdap.proto.id.ScheduleId) ApplicationRecord(io.cdap.cdap.proto.ApplicationRecord) RetryableException(io.cdap.cdap.api.retry.RetryableException) NamespaceClient(io.cdap.cdap.client.NamespaceClient) BadRequestException(io.cdap.cdap.common.BadRequestException) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) ScheduleClient(io.cdap.cdap.client.ScheduleClient)

Example 5 with ApplicationClient

use of io.cdap.cdap.client.ApplicationClient in project cdap by caskdata.

the class GenerateClientUsageExample method applicationClient.

public void applicationClient() throws Exception {
    // Construct the client used to interact with CDAP
    ApplicationClient appClient = new ApplicationClient(clientConfig);
    // Fetch the list of applications
    List<ApplicationRecord> apps = appClient.list(NamespaceId.DEFAULT);
    // Deploy an application
    File appJarFile = new File("your-app.jar");
    appClient.deploy(NamespaceId.DEFAULT, appJarFile);
    // Delete an application
    appClient.delete(NamespaceId.DEFAULT.app("Purchase"));
    // List programs belonging to an application
    appClient.listPrograms(NamespaceId.DEFAULT.app("Purchase"));
}
Also used : ApplicationClient(io.cdap.cdap.client.ApplicationClient) File(java.io.File) ApplicationRecord(io.cdap.cdap.proto.ApplicationRecord)

Aggregations

ApplicationClient (io.cdap.cdap.client.ApplicationClient)5 ApplicationRecord (io.cdap.cdap.proto.ApplicationRecord)4 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)3 NamespaceClient (io.cdap.cdap.client.NamespaceClient)2 ProgramClient (io.cdap.cdap.client.ProgramClient)2 ScheduleClient (io.cdap.cdap.client.ScheduleClient)2 ClientConfig (io.cdap.cdap.client.config.ClientConfig)2 NamespaceMeta (io.cdap.cdap.proto.NamespaceMeta)2 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)2 RetryableException (io.cdap.cdap.api.retry.RetryableException)1 ConnectionConfig (io.cdap.cdap.client.config.ConnectionConfig)1 BadRequestException (io.cdap.cdap.common.BadRequestException)1 NotFoundException (io.cdap.cdap.common.NotFoundException)1 CConfiguration (io.cdap.cdap.common.conf.CConfiguration)1 Constants (io.cdap.cdap.common.conf.Constants)1 Retries (io.cdap.cdap.common.service.Retries)1 RetryStrategies (io.cdap.cdap.common.service.RetryStrategies)1 RetryStrategy (io.cdap.cdap.common.service.RetryStrategy)1 ProgramStatus (io.cdap.cdap.proto.ProgramStatus)1 ProgramType (io.cdap.cdap.proto.ProgramType)1