Search in sources :

Example 26 with Command

use of org.ow2.proactive_grid_cloud_portal.cli.cmd.Command in project scheduling by ow2-proactive.

the class FlatJobFactory method createNativeJobFromCommand.

/**
 * Creates a job from a String representing a native command to launch. So job in result is made
 * of one native task.
 *
 * @param command a string representing an executable command to launch.
 * @param jobName A String representing a name to give to the job, if null. default job name is made of
 * {link FlatJobFactory#JOB_DEFAULT_NAME_PREFIX} + userName parameter.
 * @param selectionScriptPath A Path to a file containing a selection script, or null if
 * no script is needed.
 * @param userName name of connected user that asked job creation, null otherwise. This parameter
 * is just used for default job's name creation.
 * @return a job object representing created job and ready-to-schedule job.
 * @throws JobCreationException with a relevant error message if an error occurs.
 */
public Job createNativeJobFromCommand(String command, String jobName, String selectionScriptPath, String userName) throws JobCreationException {
    if (command == null || "".equalsIgnoreCase(command)) {
        throw new JobCreationException("Error, command cannot be null");
    }
    if (jobName == null) {
        jobName = JOB_DEFAULT_NAME_PREFIX + userName;
    }
    Job nativeJob = new TaskFlowJob();
    nativeJob.setName(jobName);
    logger.debug("Job : " + nativeJob.getName());
    try {
        NativeTask t = createNativeTaskFromCommandString(command, "task1", selectionScriptPath);
        t.setPreciousResult(true);
        ((TaskFlowJob) nativeJob).addTask(t);
        logger.debug("-> Task Name = " + t.getName());
        logger.debug("-> command = " + t.getCommandLine() + "\n");
    } catch (Exception e) {
        throw new JobCreationException(e);
    }
    return nativeJob;
}
Also used : TaskFlowJob(org.ow2.proactive.scheduler.common.job.TaskFlowJob) JobCreationException(org.ow2.proactive.scheduler.common.exception.JobCreationException) NativeTask(org.ow2.proactive.scheduler.common.task.NativeTask) Job(org.ow2.proactive.scheduler.common.job.Job) TaskFlowJob(org.ow2.proactive.scheduler.common.job.TaskFlowJob) JobCreationException(org.ow2.proactive.scheduler.common.exception.JobCreationException) InvalidScriptException(org.ow2.proactive.scripting.InvalidScriptException)

Example 27 with Command

use of org.ow2.proactive_grid_cloud_portal.cli.cmd.Command in project scheduling by ow2-proactive.

the class FlatJobFactory method createNativeJobFromCommandsFile.

/**
 * Create a job from a String representing file path, this text file contains native commands to launch
 * Every line of the text file is taken and considered as a native command from which a native task is built,
 * except lines beginning with {@link FlatJobFactory#JOB_DEFAULT_NAME_PREFIX} and empty lines.
 * So job in result is made of several native tasks without dependencies.
 *
 * @param commandFilePath a string representing a text file containing native commands.
 * @param jobName A String representing a name to give to the job. If null, default job name is made of
 * {@link FlatJobFactory#JOB_DEFAULT_NAME_PREFIX} + userName parameter.
 * @param selectionScriptPath a Path to a file containing a selection script, or null if
 * no script is needed.
 * @param userName name of connected user that asked job creation, null otherwise. This parameter
 * is only used for default job's name creation.
 * @return a job object representing created job and ready-to-schedule job.
 * @throws JobCreationException with a relevant error message if an error occurs.
 */
public Job createNativeJobFromCommandsFile(String commandFilePath, String jobName, String selectionScriptPath, String userName) throws JobCreationException {
    if (jobName == null) {
        jobName = JOB_DEFAULT_NAME_PREFIX + userName;
    }
    Job nativeJob = new TaskFlowJob();
    nativeJob.setName(jobName);
    logger.debug("Job : " + nativeJob.getName());
    try {
        File commandFile = new File(commandFilePath);
        if (!commandFile.isFile()) {
            throw new JobCreationException("Error occured during Job creation, " + "check that file " + commandFilePath + " exists and is a readable file");
        }
        String commandLine;
        int task_number = 0;
        ArrayList<String> commandList = new ArrayList<>();
        try (BufferedReader reader = new BufferedReader(new FileReader(commandFile))) {
            while ((commandLine = reader.readLine()) != null) {
                commandLine = commandLine.trim();
                if (!commandLine.startsWith(CMD_FILE_COMMENT_CHAR, 0) && !"".equals(commandLine)) {
                    commandList.add(commandLine);
                }
            }
        }
        if (commandList.size() == 0) {
            throw new JobCreationException("Error occured during Job creation, " + "No any valid command line has been built from" + commandFilePath + "");
        }
        // compute padding for task number
        int numberOfDigit = Integer.toString(commandList.size()).length();
        NumberFormat nf = NumberFormat.getInstance();
        nf.setMaximumIntegerDigits(numberOfDigit);
        nf.setMinimumIntegerDigits(numberOfDigit);
        for (String command : commandList) {
            NativeTask t = createNativeTaskFromCommandString(command, "task_" + (nf.format(++task_number)), selectionScriptPath);
            t.setPreciousResult(true);
            ((TaskFlowJob) nativeJob).addTask(t);
            logger.debug("-> Task Name = " + t.getName());
            logger.debug("-> command = " + t.getCommandLine() + "\n");
        }
    } catch (Exception e) {
        throw new JobCreationException(e);
    }
    return nativeJob;
}
Also used : TaskFlowJob(org.ow2.proactive.scheduler.common.job.TaskFlowJob) ArrayList(java.util.ArrayList) JobCreationException(org.ow2.proactive.scheduler.common.exception.JobCreationException) NativeTask(org.ow2.proactive.scheduler.common.task.NativeTask) JobCreationException(org.ow2.proactive.scheduler.common.exception.JobCreationException) InvalidScriptException(org.ow2.proactive.scripting.InvalidScriptException) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) Job(org.ow2.proactive.scheduler.common.job.Job) TaskFlowJob(org.ow2.proactive.scheduler.common.job.TaskFlowJob) File(java.io.File) NumberFormat(java.text.NumberFormat)

Example 28 with Command

use of org.ow2.proactive_grid_cloud_portal.cli.cmd.Command in project scheduling by ow2-proactive.

the class FlatJobFactory method createNativeTaskFromCommandString.

/**
 * Creates a native task from a string representing a native command to execute.
 * @param command a String representing a native command.
 * @param taskName an eventual name for the task.
 * @param selectionScriptPath path to an existing file containing a selection script code.
 * @return a NativeTask object that can be put in a Job Object.
 * @throws InvalidScriptException if an error occurs in definition of selection script
 * from file path specified.
 */
private NativeTask createNativeTaskFromCommandString(String command, String taskName, String selectionScriptPath) throws InvalidScriptException {
    NativeTask desc = new NativeTask();
    desc.setCommandLine(Tools.parseCommandLine(command));
    desc.setName(taskName);
    if (selectionScriptPath != null) {
        SelectionScript script = new SelectionScript(new SimpleScript(new File(selectionScriptPath), null), true);
        desc.addSelectionScript(script);
    }
    return desc;
}
Also used : SelectionScript(org.ow2.proactive.scripting.SelectionScript) SimpleScript(org.ow2.proactive.scripting.SimpleScript) NativeTask(org.ow2.proactive.scheduler.common.task.NativeTask) File(java.io.File)

Example 29 with Command

use of org.ow2.proactive_grid_cloud_portal.cli.cmd.Command in project scheduling by ow2-proactive.

the class RMCoreTest method testGetNodeByUrlIncludingDeployingNodesUnknownNodeUrl.

@Test
public void testGetNodeByUrlIncludingDeployingNodesUnknownNodeUrl() {
    RMDeployingNode rmNode = new RMDeployingNode("node", mockedNodeSource, "command", new Client());
    doReturn(rmNode).when(mockedNodeSource).getNodeInDeployingOrLostNodes(rmNode.getNodeURL());
    RMNode rmNodeFound = rmCore.getNodeByUrlIncludingDeployingNodes(rmNode.getNodeURL());
    assertThat(rmNodeFound).isSameAs(rmNode);
}
Also used : RMNode(org.ow2.proactive.resourcemanager.rmnode.RMNode) RMDeployingNode(org.ow2.proactive.resourcemanager.rmnode.RMDeployingNode) Client(org.ow2.proactive.resourcemanager.authentication.Client) Test(org.junit.Test)

Example 30 with Command

use of org.ow2.proactive_grid_cloud_portal.cli.cmd.Command 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");
    ArrayList<String> jvmArguments = new ArrayList<>(1);
    ForkEnvironment forkEnvironment = null;
    if (taskContext.getInitializer() != null) {
        forkEnvironment = taskContext.getInitializer().getForkEnvironment();
    }
    // 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");
    configureLogging(jvmArguments, variables);
    StringBuilder classpath = new StringBuilder("." + File.pathSeparatorChar);
    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
        classpath.append(getStandardClassPathEntries(variables));
    }
    for (String classpathEntry : OneJar.getClasspath()) {
        classpath.append(File.pathSeparatorChar).append(classpathEntry);
    }
    if (forkEnvironment != null) {
        for (String jvmArgument : forkEnvironment.getJVMArguments()) {
            jvmArguments.add(VariableSubstitutor.filterAndUpdate(jvmArgument, variables));
        }
        for (String classpathEntry : forkEnvironment.getAdditionalClasspath()) {
            classpath.append(File.pathSeparatorChar).append(VariableSubstitutor.filterAndUpdate(classpathEntry, variables));
        }
        if (!Strings.isNullOrEmpty(forkEnvironment.getJavaHome())) {
            javaHome = VariableSubstitutor.filterAndUpdate(forkEnvironment.getJavaHome(), variables);
        }
    }
    List<String> prefixes = javaPrefixCommandExtractor.extractJavaPrefixCommandToCommandListFromScriptResult(forkEnvironmentScriptResult);
    List<String> javaCommand = new ArrayList<>(prefixes.size() + 3 + jvmArguments.size() + 2);
    javaCommand.addAll(prefixes);
    javaCommand.add(javaHome + javaHomePostfixJavaExecutable);
    javaCommand.add("-cp");
    javaCommand.add(classpath.toString());
    javaCommand.addAll(jvmArguments);
    javaCommand.add(ExecuteForkedTaskInsideNewJvm.class.getName());
    javaCommand.add(serializedContextAbsolutePath);
    return javaCommand;
}
Also used : Serializable(java.io.Serializable) ArrayList(java.util.ArrayList) ForkEnvironment(org.ow2.proactive.scheduler.common.task.ForkEnvironment)

Aggregations

Test (org.junit.Test)12 IOException (java.io.IOException)11 RMDeployingNode (org.ow2.proactive.resourcemanager.rmnode.RMDeployingNode)10 Throwables.getStackTraceAsString (com.google.common.base.Throwables.getStackTraceAsString)9 ArrayList (java.util.ArrayList)9 RMException (org.ow2.proactive.resourcemanager.exception.RMException)9 TaskFlowJob (org.ow2.proactive.scheduler.common.job.TaskFlowJob)9 NativeTask (org.ow2.proactive.scheduler.common.task.NativeTask)9 KeyException (java.security.KeyException)8 File (java.io.File)7 ForkEnvironment (org.ow2.proactive.scheduler.common.task.ForkEnvironment)6 JobCreationException (org.ow2.proactive.scheduler.common.exception.JobCreationException)5 CommandLineBuilder (org.ow2.proactive.resourcemanager.utils.CommandLineBuilder)4 JavaTask (org.ow2.proactive.scheduler.common.task.JavaTask)4 UnknownHostException (java.net.UnknownHostException)3 Client (org.ow2.proactive.resourcemanager.authentication.Client)3 Command (org.ow2.proactive_grid_cloud_portal.cli.cmd.Command)3 InetAddress (java.net.InetAddress)2 Properties (java.util.Properties)2 ExecutionException (java.util.concurrent.ExecutionException)2