use of io.cdap.cdap.internal.app.runtime.artifact.ArtifactDescriptor in project cdap by caskdata.
the class SystemAppTask method run.
@Override
public void run(RunnableTaskContext context) throws Exception {
ArtifactId systemAppArtifactId = context.getArtifactId();
if (systemAppArtifactId == null) {
throw new IllegalArgumentException("Missing artifactId from the system app task request");
}
LOG.debug("Received system app task for artifact {}", systemAppArtifactId);
Injector injector = createInjector(cConf);
ArtifactRepository artifactRepository = injector.getInstance(ArtifactRepository.class);
Impersonator impersonator = injector.getInstance(Impersonator.class);
String systemAppNamespace = context.getNamespace();
Id.Artifact artifactId = Id.Artifact.from(Id.Namespace.from(systemAppNamespace), systemAppArtifactId.getName(), systemAppArtifactId.getVersion());
ArtifactLocalizerClient localizerClient = injector.getInstance(ArtifactLocalizerClient.class);
File artifactLocation = localizerClient.getUnpackedArtifactLocation(Artifacts.toProtoArtifactId(new NamespaceId(systemAppNamespace), systemAppArtifactId));
EntityImpersonator classLoaderImpersonator = new EntityImpersonator(artifactId.toEntityId(), impersonator);
try (CloseableClassLoader artifactClassLoader = artifactRepository.createArtifactClassLoader(new ArtifactDescriptor(artifactId.getNamespace().getId(), artifactId.toArtifactId(), Locations.toLocation(artifactLocation)), classLoaderImpersonator);
SystemAppTaskContext systemAppTaskContext = buildTaskSystemAppContext(injector, systemAppNamespace, systemAppArtifactId, artifactClassLoader)) {
RunnableTaskRequest taskRequest = context.getEmbeddedRequest();
String taskClassName = taskRequest.getClassName();
if (taskClassName == null) {
LOG.debug("No system app task to execute");
return;
}
LOG.debug("Requested to run system app task {}", taskClassName);
Class<?> clazz = artifactClassLoader.loadClass(taskClassName);
if (!(RunnableTask.class.isAssignableFrom(clazz))) {
throw new ClassCastException(String.format("%s is not a RunnableTask", taskClassName));
}
LOG.debug("Launching system app task {}", taskClassName);
RunnableTask runnableTask = (RunnableTask) injector.getInstance(clazz);
RunnableTaskContext runnableTaskContext = new RunnableTaskContext(taskRequest.getParam().getSimpleParam(), null, null, null, systemAppTaskContext) {
@Override
public void writeResult(byte[] data) throws IOException {
context.writeResult(data);
}
@Override
public void setTerminateOnComplete(boolean terminate) {
context.setTerminateOnComplete(terminate);
}
@Override
public boolean isTerminateOnComplete() {
return context.isTerminateOnComplete();
}
};
runnableTask.run(runnableTaskContext);
LOG.debug("System app task completed {}", taskClassName);
}
}
use of io.cdap.cdap.internal.app.runtime.artifact.ArtifactDescriptor in project cdap by caskdata.
the class DefaultPluginConfigurer method addPlugin.
protected Plugin addPlugin(String pluginType, String pluginName, String pluginId, PluginProperties properties, PluginSelector selector) throws PluginNotExistsException {
validateExistingPlugin(pluginId);
final Class[] callerClasses = CallerClassSecurityManager.getCallerClasses();
if (callerClasses.length < 3) {
// This shouldn't happen as there should be someone calling this method.
throw new IllegalStateException("Invalid call stack.");
}
Set<ArtifactId> parents = new LinkedHashSet<>();
// 0 is the CallerClassSecurityManager, 1 is this class, hence 2 is the actual caller
for (int i = 2; i < callerClasses.length; i++) {
ClassLoader classloader = callerClasses[i].getClassLoader();
if (classloader instanceof PluginClassLoader) {
// if this is the first time we've seen this plugin artifact, it must be a new plugin parent.
io.cdap.cdap.api.artifact.ArtifactId pluginCallerArtifactId = ((PluginClassLoader) classloader).getArtifactId();
parents.add((pluginCallerArtifactId.getScope() == ArtifactScope.SYSTEM ? NamespaceId.SYSTEM : pluginNamespaceId).artifact(pluginCallerArtifactId.getName(), pluginCallerArtifactId.getVersion().getVersion()));
}
}
PluginNotExistsException exception = null;
for (ArtifactId parentId : Iterables.concat(parents, Collections.singleton(artifactId))) {
try {
Map.Entry<ArtifactDescriptor, PluginClass> pluginEntry = pluginFinder.findPlugin(pluginNamespaceId, parentId, pluginType, pluginName, selector);
Plugin plugin = FindPluginHelper.getPlugin(Iterables.transform(parents, ArtifactId::toApiArtifactId), pluginEntry, properties, pluginInstantiator);
plugins.put(pluginId, new PluginWithLocation(plugin, pluginEntry.getKey().getLocation()));
return plugin;
} catch (PluginNotExistsException e) {
// ignore this in case the plugin extends something higher up in the call stack.
// For example, suppose the app uses pluginA, which uses pluginB. However, pluginB is a plugin that
// has the app as its parent and not pluginA as its parent. In this case, we want to keep going up the call
// stack until we get to the app as the parent, which would be able to find plugin B.
exception = e;
}
}
throw exception == null ? new PluginNotExistsException(pluginNamespaceId, pluginType, pluginName) : exception;
}
use of io.cdap.cdap.internal.app.runtime.artifact.ArtifactDescriptor in project cdap by caskdata.
the class DeployDatasetModulesStage method process.
/**
* Deploys dataset modules specified in the given application spec.
*
* @param input An instance of {@link ApplicationDeployable}
*/
@Override
public void process(ApplicationDeployable input) throws Exception {
Map<String, String> datasetModules = input.getSpecification().getDatasetModules();
if (allowCustomModule) {
KerberosPrincipalId ownerPrincipal = input.getOwnerPrincipal();
// get the authorizing user
String authorizingUser = AuthorizationUtil.getAppAuthorizingUser(ownerAdmin, authenticationContext, input.getApplicationId(), ownerPrincipal);
EntityImpersonator classLoaderImpersonator = new EntityImpersonator(input.getArtifactId(), impersonator);
try (CloseableClassLoader classLoader = artifactRepository.createArtifactClassLoader(new ArtifactDescriptor(input.getArtifactId().getNamespace(), input.getArtifactId().toApiArtifactId(), input.getArtifactLocation()), classLoaderImpersonator)) {
deployer.deployModules(input.getApplicationId().getParent(), datasetModules, input.getArtifactLocation(), classLoader, authorizingUser);
}
} else if (deployer.hasNonSystemDatasetModules(datasetModules)) {
throw new IllegalStateException("Custom dataset module is not supported. " + "One of the dataset module is a custom module: " + datasetModules);
}
// Emit the input to next stage.
emit(input);
}
use of io.cdap.cdap.internal.app.runtime.artifact.ArtifactDescriptor in project cdap by caskdata.
the class RemotePluginConfigurer method addPlugin.
@Override
protected Plugin addPlugin(String pluginType, String pluginName, String pluginId, PluginProperties properties, PluginSelector selector) throws PluginNotExistsException {
validateExistingPlugin(pluginId);
// plugin
if (!localPlugins.containsKey(pluginId)) {
return super.addPlugin(pluginType, pluginName, pluginId, properties, selector);
}
Plugin existingPlugin = localPlugins.get(pluginId);
File existingPluginLocation = new File(localPluginDir, Artifacts.getFileName(existingPlugin.getArtifactId()));
// need to regenerate this plugin to ensure the plugin has updated properties with macro resolved, also
// register it to plugin instantiator
io.cdap.cdap.api.artifact.ArtifactId artifactId = existingPlugin.getArtifactId();
String namespace = artifactId.getScope().equals(ArtifactScope.SYSTEM) ? NamespaceId.SYSTEM.getNamespace() : pluginNamespaceId.getNamespace();
Location pluginLocation = Locations.toLocation(existingPluginLocation);
SortedMap<io.cdap.cdap.api.artifact.ArtifactId, PluginClass> selectedPlugin = new TreeMap<>();
selectedPlugin.put(artifactId, existingPlugin.getPluginClass());
selector.select(selectedPlugin);
Plugin plugin = FindPluginHelper.getPlugin(existingPlugin.getParents(), Maps.immutableEntry(new ArtifactDescriptor(namespace, artifactId, pluginLocation), existingPlugin.getPluginClass()), properties, pluginInstantiator);
plugins.put(pluginId, new PluginWithLocation(plugin, pluginLocation));
return plugin;
}
use of io.cdap.cdap.internal.app.runtime.artifact.ArtifactDescriptor in project cdap by caskdata.
the class RemoteConfiguratorTest method testRemoteConfigurator.
@Test
public void testRemoteConfigurator() throws Exception {
LocationFactory locationFactory = new LocalLocationFactory(TEMP_FOLDER.newFolder());
Location appJar = AppJarHelper.createDeploymentJar(locationFactory, AllProgramsApp.class);
ArtifactId artifactId = NamespaceId.DEFAULT.artifact(AllProgramsApp.class.getSimpleName(), "1.0.0");
artifacts.put(artifactId, new ArtifactDetail(new ArtifactDescriptor(artifactId.getNamespace(), artifactId.toApiArtifactId(), appJar), new ArtifactMeta(ArtifactClasses.builder().build())));
AppDeploymentInfo info = new AppDeploymentInfo(artifactId, appJar, NamespaceId.DEFAULT, new ApplicationClass(AllProgramsApp.class.getName(), "", null), null, null, null);
Configurator configurator = new RemoteConfigurator(cConf, metricsCollectionService, info, remoteClientFactory);
// Extract response from the configurator.
ListenableFuture<ConfigResponse> result = configurator.config();
ConfigResponse response = result.get(10, TimeUnit.SECONDS);
Assert.assertNotNull(response);
AppSpecInfo appSpecInfo = response.getAppSpecInfo();
if (appSpecInfo == null) {
throw new IllegalStateException("Failed to deploy application");
}
ApplicationSpecification specification = appSpecInfo.getAppSpec();
Assert.assertNotNull(specification);
// Simple checks.
Assert.assertEquals(AllProgramsApp.NAME, specification.getName());
ApplicationSpecification expectedSpec = Specifications.from(new AllProgramsApp());
for (ProgramType programType : ProgramType.values()) {
Assert.assertEquals(expectedSpec.getProgramsByType(programType), specification.getProgramsByType(programType));
}
Assert.assertEquals(expectedSpec.getDatasets(), specification.getDatasets());
}
Aggregations