use of co.cask.cdap.app.deploy.ConfigResponse in project cdap by caskdata.
the class InMemoryConfigurator method config.
/**
* Executes the <code>Application.configure</code> within the same JVM.
* <p>
* This method could be dangerous and should be used only in standalone mode.
* </p>
*
* @return A instance of {@link ListenableFuture}.
*/
@Override
public ListenableFuture<ConfigResponse> config() {
SettableFuture<ConfigResponse> result = SettableFuture.create();
try {
Object appMain = artifactClassLoader.loadClass(appClassName).newInstance();
if (!(appMain instanceof Application)) {
throw new IllegalStateException(String.format("Application main class is of invalid type: %s", appMain.getClass().getName()));
}
Application app = (Application) appMain;
ConfigResponse response = createResponse(app);
result.set(response);
return result;
} catch (Throwable t) {
return Futures.immediateFailedFuture(t);
}
}
use of co.cask.cdap.app.deploy.ConfigResponse in project cdap by caskdata.
the class SandboxConfigurator method config.
/**
* Runs the <code>Application.configure()</code> in a sandbox JVM
* with high level of security.
*
* @return An instance of {@link ListenableFuture}
*/
@Override
public ListenableFuture<ConfigResponse> config() {
final SettableFuture<ConfigResponse> result = SettableFuture.create();
final File outputFile;
try {
outputFile = File.createTempFile(PREFIX, EXT);
// Run the command in seperate JVM.
process = Runtime.getRuntime().exec(getCommand(outputFile));
// Add future to handle the case when the future is cancelled.
// OnSuccess, we don't do anything other than cleaning the output.
// onFailure, we make sure that process is destroyed.
Futures.addCallback(result, new FutureCallback<ConfigResponse>() {
private void deleteOutput() {
if (outputFile.exists()) {
outputFile.delete();
}
}
@Override
public void onSuccess(final ConfigResponse result) {
// Delete the output file on delete.
deleteOutput();
}
@Override
public void onFailure(final Throwable t) {
// destroy the process.
if (result.isCancelled()) {
process.destroy();
}
deleteOutput();
}
});
} catch (Exception e) {
// upon construction.
return Futures.immediateFailedFuture(e);
}
// Start a thread that waits for command execution to complete or until it's cancelled.
new Thread() {
@Override
public void run() {
try {
// Wait for process to exit and extract the return. If cancelled the process will
// be shutdown.
process.waitFor();
int exit = process.exitValue();
if (exit == 0) {
result.set(new DefaultConfigResponse(0, Files.newReaderSupplier(outputFile, Charsets.UTF_8)));
} else {
result.set(new DefaultConfigResponse(exit, null));
}
} catch (Exception e) {
result.setException(e);
}
}
}.start();
return result;
}
use of co.cask.cdap.app.deploy.ConfigResponse in project cdap by caskdata.
the class LocalArtifactLoaderStage method process.
/**
* Instantiates the Application class and calls configure() on it to generate the {@link ApplicationSpecification}.
*
* @param deploymentInfo information needed to deploy the application, such as the artifact to create it from
* and the application config to use.
*/
@Override
public void process(AppDeploymentInfo deploymentInfo) throws Exception {
ArtifactId artifactId = deploymentInfo.getArtifactId();
Location artifactLocation = deploymentInfo.getArtifactLocation();
String appClassName = deploymentInfo.getAppClassName();
String appVersion = deploymentInfo.getApplicationVersion();
String configString = deploymentInfo.getConfigString();
EntityImpersonator classLoaderImpersonator = new EntityImpersonator(artifactId, impersonator);
ClassLoader artifactClassLoader = artifactRepository.createArtifactClassLoader(artifactLocation, classLoaderImpersonator);
getContext().setProperty(LocalApplicationManager.ARTIFACT_CLASSLOADER_KEY, artifactClassLoader);
InMemoryConfigurator inMemoryConfigurator = new InMemoryConfigurator(cConf, Id.Namespace.fromEntityId(deploymentInfo.getNamespaceId()), Id.Artifact.fromEntityId(artifactId), appClassName, artifactRepository, artifactClassLoader, deploymentInfo.getApplicationName(), deploymentInfo.getApplicationVersion(), configString);
ListenableFuture<ConfigResponse> result = inMemoryConfigurator.config();
ConfigResponse response = result.get(120, TimeUnit.SECONDS);
if (response.getExitCode() != 0) {
throw new IllegalArgumentException("Failed to configure application: " + deploymentInfo);
}
ApplicationSpecification specification = adapter.fromJson(response.get());
ApplicationId applicationId;
if (appVersion == null) {
applicationId = deploymentInfo.getNamespaceId().app(specification.getName());
} else {
applicationId = deploymentInfo.getNamespaceId().app(specification.getName(), appVersion);
}
authorizationEnforcer.enforce(applicationId, authenticationContext.getPrincipal(), Action.ADMIN);
emit(new ApplicationDeployable(deploymentInfo.getArtifactId(), deploymentInfo.getArtifactLocation(), applicationId, specification, store.getApplication(applicationId), ApplicationDeployScope.USER, deploymentInfo.getOwnerPrincipal(), deploymentInfo.canUpdateSchedules()));
}
use of co.cask.cdap.app.deploy.ConfigResponse in project cdap by caskdata.
the class ConfiguratorTest method testAppWithConfig.
@Test
public void testAppWithConfig() throws Exception {
LocationFactory locationFactory = new LocalLocationFactory(TMP_FOLDER.newFolder());
Location appJar = AppJarHelper.createDeploymentJar(locationFactory, ConfigTestApp.class);
Id.Artifact artifactId = Id.Artifact.from(Id.Namespace.DEFAULT, ConfigTestApp.class.getSimpleName(), "1.0.0");
CConfiguration cConf = CConfiguration.create();
ArtifactRepository baseArtifactRepo = new DefaultArtifactRepository(conf, null, null, new DummyProgramRunnerFactory(), new DefaultImpersonator(cConf, null));
ArtifactRepository artifactRepo = new AuthorizationArtifactRepository(baseArtifactRepo, authEnforcer, authenticationContext);
ConfigTestApp.ConfigClass config = new ConfigTestApp.ConfigClass("myStream", "myTable");
// Create a configurator that is testable. Provide it an application.
try (CloseableClassLoader artifactClassLoader = artifactRepo.createArtifactClassLoader(appJar, new EntityImpersonator(artifactId.getNamespace().toEntityId(), new DefaultImpersonator(cConf, null)))) {
Configurator configuratorWithConfig = new InMemoryConfigurator(conf, Id.Namespace.DEFAULT, artifactId, ConfigTestApp.class.getName(), artifactRepo, artifactClassLoader, null, null, new Gson().toJson(config));
ListenableFuture<ConfigResponse> result = configuratorWithConfig.config();
ConfigResponse response = result.get(10, TimeUnit.SECONDS);
Assert.assertNotNull(response);
ApplicationSpecificationAdapter adapter = ApplicationSpecificationAdapter.create(new ReflectionSchemaGenerator());
ApplicationSpecification specification = adapter.fromJson(response.get());
Assert.assertNotNull(specification);
Assert.assertTrue(specification.getStreams().size() == 1);
Assert.assertTrue(specification.getStreams().containsKey("myStream"));
Assert.assertTrue(specification.getDatasets().size() == 1);
Assert.assertTrue(specification.getDatasets().containsKey("myTable"));
Configurator configuratorWithoutConfig = new InMemoryConfigurator(conf, Id.Namespace.DEFAULT, artifactId, ConfigTestApp.class.getName(), artifactRepo, artifactClassLoader, null, null, null);
result = configuratorWithoutConfig.config();
response = result.get(10, TimeUnit.SECONDS);
Assert.assertNotNull(response);
specification = adapter.fromJson(response.get());
Assert.assertNotNull(specification);
Assert.assertTrue(specification.getStreams().size() == 1);
Assert.assertTrue(specification.getStreams().containsKey(ConfigTestApp.DEFAULT_STREAM));
Assert.assertTrue(specification.getDatasets().size() == 1);
Assert.assertTrue(specification.getDatasets().containsKey(ConfigTestApp.DEFAULT_TABLE));
Assert.assertNotNull(specification.getProgramSchedules().get(ConfigTestApp.SCHEDULE_NAME));
ProgramStatusTrigger trigger = (ProgramStatusTrigger) specification.getProgramSchedules().get(ConfigTestApp.SCHEDULE_NAME).getTrigger();
Assert.assertEquals(trigger.getProgramId().getProgram(), ConfigTestApp.WORKFLOW_NAME);
}
}
use of co.cask.cdap.app.deploy.ConfigResponse in project cdap by caskdata.
the class ConfiguratorTest method testInMemoryConfigurator.
@Test
public void testInMemoryConfigurator() throws Exception {
LocationFactory locationFactory = new LocalLocationFactory(TMP_FOLDER.newFolder());
Location appJar = AppJarHelper.createDeploymentJar(locationFactory, WordCountApp.class);
Id.Artifact artifactId = Id.Artifact.from(Id.Namespace.DEFAULT, WordCountApp.class.getSimpleName(), "1.0.0");
CConfiguration cConf = CConfiguration.create();
ArtifactRepository baseArtifactRepo = new DefaultArtifactRepository(conf, null, null, new DummyProgramRunnerFactory(), new DefaultImpersonator(cConf, null));
ArtifactRepository artifactRepo = new AuthorizationArtifactRepository(baseArtifactRepo, authEnforcer, authenticationContext);
// Create a configurator that is testable. Provide it a application.
try (CloseableClassLoader artifactClassLoader = artifactRepo.createArtifactClassLoader(appJar, new EntityImpersonator(artifactId.getNamespace().toEntityId(), new DefaultImpersonator(cConf, null)))) {
Configurator configurator = new InMemoryConfigurator(conf, Id.Namespace.DEFAULT, artifactId, WordCountApp.class.getName(), artifactRepo, artifactClassLoader, null, null, "");
// Extract response from the configurator.
ListenableFuture<ConfigResponse> result = configurator.config();
ConfigResponse response = result.get(10, TimeUnit.SECONDS);
Assert.assertNotNull(response);
// Deserialize the JSON spec back into Application object.
ApplicationSpecificationAdapter adapter = ApplicationSpecificationAdapter.create(new ReflectionSchemaGenerator());
ApplicationSpecification specification = adapter.fromJson(response.get());
Assert.assertNotNull(specification);
// Simple checks.
Assert.assertTrue(specification.getName().equals("WordCountApp"));
// # of flows.
Assert.assertTrue(specification.getFlows().size() == 1);
}
}
Aggregations