use of org.ow2.proactive.scripting.ForkEnvironmentScriptResult in project scheduling by ow2-proactive.
the class ForkedProcessBuilderCreator method executeForkEnvironmentScriptAndExtractVariables.
private ScriptResult executeForkEnvironmentScriptAndExtractVariables(TaskContext context, PrintStream outputSink, PrintStream errorSink, OSProcessBuilder processBuilder) throws Exception {
ScriptResult forkEnvironmentScriptResult = null;
ForkEnvironment forkEnvironment = context.getInitializer().getForkEnvironment();
if (forkEnvironment != null) {
if (forkEnvironment.getEnvScript() != null) {
if (!context.getInitializer().isAuthorizedForkEnvironmentScript()) {
throw new SecurityException("Unauthorized fork environment script: " + System.getProperty("line.separator") + forkEnvironment.getEnvScript().fetchScript());
}
forkEnvironmentScriptResult = forkEnvironmentScriptExecutor.executeForkEnvironmentScript(context, outputSink, errorSink);
}
try {
processBuilder.environment().putAll(// by existing environment variables, variables and credentials
taskContextVariableExtractor.extractVariablesThirdPartyCredentialsAndSystemEnvironmentVariables(context));
} catch (IllegalArgumentException processEnvironmentReadOnly) {
throw new IllegalStateException("Cannot use runAsMe mode and set system environment properties", processEnvironmentReadOnly);
}
}
return forkEnvironmentScriptResult;
}
use of org.ow2.proactive.scripting.ForkEnvironmentScriptResult in project scheduling by ow2-proactive.
the class ForkedProcessBuilderCreatorTest method setMocks.
private void setMocks(ForkedProcessBuilderCreator forkedProcessBuilderCreator) throws Exception {
ForkedJvmTaskExecutionCommandCreator forkedJvmTaskExecutionCommandCreator = mock(ForkedJvmTaskExecutionCommandCreator.class);
TaskContextVariableExtractor taskContextVariableExtractor = mock(TaskContextVariableExtractor.class);
ForkEnvironmentScriptExecutor forkEnvironmentScriptExecutor = mock(ForkEnvironmentScriptExecutor.class);
given(forkedJvmTaskExecutionCommandCreator.createForkedJvmTaskExecutionCommand(any(TaskContext.class), any(ScriptResult.class), any(String.class))).willReturn(Arrays.asList(forkEnJavaCommandString));
given(taskContextVariableExtractor.extractVariablesThirdPartyCredentialsAndSystemEnvironmentVariables(any(TaskContext.class))).willReturn(taskContextExtractedVariables);
given(forkEnvironmentScriptExecutor.executeForkEnvironmentScript(any(TaskContext.class), any(PrintStream.class), any(PrintStream.class))).willReturn(new ScriptResult<ForkEnvironmentScriptResult>(new ForkEnvironmentScriptResult()));
setPrivateField(ForkedProcessBuilderCreator.class.getDeclaredField("forkedJvmTaskExecutionCommandCreator"), forkedProcessBuilderCreator, forkedJvmTaskExecutionCommandCreator);
setPrivateField(ForkedProcessBuilderCreator.class.getDeclaredField("taskContextVariableExtractor"), forkedProcessBuilderCreator, taskContextVariableExtractor);
setPrivateField(ForkedProcessBuilderCreator.class.getDeclaredField("forkEnvironmentScriptExecutor"), forkedProcessBuilderCreator, forkEnvironmentScriptExecutor);
}
use of org.ow2.proactive.scripting.ForkEnvironmentScriptResult in project scheduling by ow2-proactive.
the class ForkedProcessBuilderCreator method createForkedProcessBuilder.
/**
* Creates a process builder for a given task context.
*
* @param context The task context to execute.
* @param serializedContext The task context saved to disk.
* @param outputSink Standard output sink.
* @param errorSink Error sink.
* @param workingDir The working directory to execute the process in.
* @return Returns a process builder, ready to execute.
* @throws Exception
*/
public OSProcessBuilder createForkedProcessBuilder(TaskContext context, File serializedContext, PrintStream outputSink, PrintStream errorSink, File workingDir) throws Exception {
String nativeScriptPath = context.getSchedulerHome();
OSProcessBuilder processBuilder = getOsProcessBuilder(context, workingDir, nativeScriptPath);
ScriptResult forkEnvironmentScriptResult = executeForkEnvironmentScriptAndExtractVariables(context, outputSink, errorSink, processBuilder);
processBuilder.command().addAll(forkedJvmTaskExecutionCommandCreator.createForkedJvmTaskExecutionCommand(context, forkEnvironmentScriptResult, serializedContext.getAbsolutePath()));
processBuilder = processBuilder.directory(workingDir);
return processBuilder;
}
use of org.ow2.proactive.scripting.ForkEnvironmentScriptResult in project scheduling by ow2-proactive.
the class ForkedJvmTaskExecutionCommandCreator method createForkedJvmTaskExecutionCommand.
/**
* Creates a command to start a task inside a java virtual machine.
*
* @param taskContext TaskContext object describing the task.
* @param forkEnvironmentScriptResult Result from a running fork environment script. If it is
* of instance {@link ForkEnvironmentScriptResult}, the script return
* variables will be used for the construction of the command.
* @param serializedContextAbsolutePath The serialized TaskContext object which will be read by the virtual
* machine to run the task.
* @return A List, empty if the TaskContext is null, otherwise filled with a command.
* @throws Exception If the {@link TaskContextVariableExtractor} could not extract all variables from the
* TaskContext.
*/
public List<String> createForkedJvmTaskExecutionCommand(TaskContext taskContext, ScriptResult forkEnvironmentScriptResult, String serializedContextAbsolutePath) throws Exception {
if (taskContext == null) {
return new ArrayList<>(0);
}
Map<String, Serializable> variables = taskContextVariableExtractor.getAllVariables(taskContext);
String javaHome = System.getProperty("java.home");
List<String> jvmArguments = new ArrayList<>(1);
ForkEnvironment forkEnvironment = null;
boolean isDockerWindowsToLinuxTmp = false;
if (taskContext.getInitializer() != null) {
forkEnvironment = taskContext.getInitializer().getForkEnvironment();
if (forkEnvironment != null) {
isDockerWindowsToLinuxTmp = forkEnvironment.isDockerWindowsToLinux();
}
}
final boolean isDockerWindowsToLinux = isDockerWindowsToLinuxTmp;
// set the task fork property so that script engines have a mean to know
// if they are running in a forked task or not
jvmArguments.add(PASchedulerProperties.TASK_FORK.getCmdLine() + "true");
if (isDockerWindowsToLinux) {
jvmArguments.add("-D" + DOCKER_FORK_WINDOWS2LINUX + "=true");
}
configureLogging(jvmArguments, variables, isDockerWindowsToLinux);
StringBuilder classpath = new StringBuilder("." + getPathSeparator(isDockerWindowsToLinux));
if (!System.getProperty("java.class.path", "").contains("node.jar")) {
// in case the class path of the node is not built with the node.jar, we
// build the classpath with wildcards to avoid command too long errors on windows
String classPathEntries = getStandardClassPathEntries(variables).toString();
classpath.append(convertToLinuxClassPathIfNeeded(isDockerWindowsToLinux, classPathEntries));
}
for (String classpathEntry : OneJar.getClasspath()) {
classpath.append(getPathSeparator(isDockerWindowsToLinux)).append(convertToLinuxClassPathIfNeeded(isDockerWindowsToLinux, classpathEntry));
}
if (forkEnvironment != null) {
for (String jvmArgument : forkEnvironment.getJVMArguments()) {
jvmArguments.add(VariableSubstitutor.filterAndUpdate(jvmArgument, variables));
}
for (String classpathEntry : forkEnvironment.getAdditionalClasspath()) {
// classpath defined in the fork environment does not need to be converted to linux (as we expect the user to provide the correct path)
VariableSubstitutor.filterAndUpdate(classpathEntry, variables);
classpath.append(getPathSeparator(isDockerWindowsToLinux)).append(VariableSubstitutor.filterAndUpdate(classpathEntry, variables));
}
if (!Strings.isNullOrEmpty(forkEnvironment.getJavaHome())) {
javaHome = VariableSubstitutor.filterAndUpdate(forkEnvironment.getJavaHome(), variables);
}
}
// The following code forwards the PAMR configuration the forked JVM. Though the general use-case involves to
// write a custom fork environment script to configure properly the properties, this avoids a common misconfiguration issues.
forwardProActiveProperties(jvmArguments, PAMRConfig.PA_NET_ROUTER_ADDRESS, PAMRConfig.PA_NET_ROUTER_PORT, PAMRConfig.PA_PAMR_SOCKET_FACTORY, PAMRConfig.PA_PAMRSSH_KEY_DIR, PAMRConfig.PA_PAMRSSH_REMOTE_ADDRESS, PAMRConfig.PA_PAMRSSH_REMOTE_USERNAME, PAMRConfig.PA_PAMRSSH_REMOTE_PORT, CentralPAPropertyRepository.PA_COMMUNICATION_PROTOCOL, CentralPAPropertyRepository.PA_COMMUNICATION_ADDITIONAL_PROTOCOLS, CentralPAPropertyRepository.PA_COMMUNICATION_PROTOCOLS_ORDER, CentralPAPropertyRepository.PA_CLASSLOADING_USEHTTP, CentralPAPropertyRepository.PA_NET_USE_IP_ADDRESS);
forwardOtherProperties(jvmArguments, createNodeSourceProperty());
List<String> prefixes = javaPrefixCommandExtractor.extractJavaPrefixCommandToCommandListFromScriptResult(forkEnvironmentScriptResult);
if (prefixes.isEmpty() && forkEnvironment != null) {
prefixes.addAll(forkEnvironment.getPreJavaCommand());
}
List<String> javaCommand = new ArrayList<>(prefixes.size() + 3 + jvmArguments.size() + 2);
javaCommand.addAll(prefixes);
javaCommand.add(javaHome + (isDockerWindowsToLinux ? JAVA_HOME_LINUX_JAVA_EXECUTABLE : JAVA_HOME_POSTFIX_JAVA_EXECUTABLE));
javaCommand.add("-cp");
javaCommand.add(classpath.toString());
javaCommand.addAll(jvmArguments.stream().map(arg -> isDockerWindowsToLinux ? ForkEnvironment.convertToLinuxPathInJVMArgument(arg) : arg).collect(Collectors.toList()));
javaCommand.add(ExecuteForkedTaskInsideNewJvm.class.getName());
javaCommand.add(convertToLinuxPathIfNeeded(isDockerWindowsToLinux, serializedContextAbsolutePath));
if (logger.isDebugEnabled()) {
logger.debug("Forked JVM command : " + javaCommand);
}
return javaCommand;
}
use of org.ow2.proactive.scripting.ForkEnvironmentScriptResult in project scheduling by ow2-proactive.
the class JavaPrefixCommandExtractor method extractJavaPrefixCommandToCommandListFromScriptResult.
/**
* Extracts a java fork prefix command from a script result.
*
* @param scriptResult ScriptResult object from which the fork environment command is extracted.
* @return Java prefix command, extracted out of the fork environment script variables.
*/
public List<String> extractJavaPrefixCommandToCommandListFromScriptResult(ScriptResult scriptResult) {
List<String> javaPrefixCommand = new ArrayList<>();
if (scriptResult != null && scriptResult.getResult() instanceof ForkEnvironmentScriptResult) {
ForkEnvironmentScriptResult forkEnvResult = (ForkEnvironmentScriptResult) scriptResult.getResult();
javaPrefixCommand.addAll(forkEnvResult.getJavaPrefixCommand());
}
return javaPrefixCommand;
}
Aggregations