Search in sources :

Example 11 with Command

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

the class TestExecRemote method processBuilderWithDSScript.

private void processBuilderWithDSScript(String miscDir, boolean isLinux, String valueToEcho, HashSet<String> nodesUrls) throws Exception {
    File sFile = new File(miscDir + "processBuilderWithDS.groovy");
    RMTHelper.log("Test 5 - Test " + sFile);
    // Create test temporary file
    File tempDir = tmpFolder.newFolder("testExecRemote");
    String testFilename = "test.txt";
    FileUtils.write(new File(tempDir, testFilename), valueToEcho);
    // Generate the remote command execute in the remote localspace
    DSHelper dsHelper = new DSHelper();
    try {
        // Start DS
        String dsurl = dsHelper.startDS(tempDir.getAbsolutePath());
        String[] cmd = (isLinux) ? new String[] { dsurl, "/bin/cat", testFilename } : new String[] { dsurl, "cmd.exe", "/c", "more", testFilename };
        // Execute the script
        SimpleScript script = new SimpleScript(sFile, cmd);
        List<ScriptResult<Object>> results = rmHelper.getResourceManager().executeScript(script, TargetType.NODE_URL.toString(), nodesUrls);
        assertFalse("The results must not be empty", results.size() == 0);
        for (ScriptResult<Object> res : results) {
            Throwable exception = res.getException();
            if (exception != null) {
                RMTHelper.log("An exception occured while executing the script remotely:");
                exception.printStackTrace(System.out);
            }
            String output = res.getOutput();
            assertTrue("The script output must contains " + valueToEcho, output.contains(valueToEcho));
        }
    } finally {
        dsHelper.stopDS();
    }
}
Also used : ScriptResult(org.ow2.proactive.scripting.ScriptResult) SimpleScript(org.ow2.proactive.scripting.SimpleScript) File(java.io.File)

Example 12 with Command

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

the class GetJobOutputCommandTest method executeCommandWithArgs.

@Override
protected void executeCommandWithArgs(Object... args) {
    Command command = null;
    if (args.length == 1) {
        command = new GetJobOutputCommand((String) args[0]);
    } else {
        command = new GetJobOutputCommand((String) args[0], (String) args[1]);
    }
    command.execute(context);
}
Also used : Command(org.ow2.proactive_grid_cloud_portal.cli.cmd.Command) GetJobOutputCommand(org.ow2.proactive_grid_cloud_portal.cli.cmd.sched.GetJobOutputCommand) GetJobOutputCommand(org.ow2.proactive_grid_cloud_portal.cli.cmd.sched.GetJobOutputCommand) Matchers.anyString(org.mockito.Matchers.anyString)

Example 13 with Command

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

the class ForkedJvmTaskExecutionCommandCreatorTest method replaceJavaPrefixCommandCreatorWithMock.

/**
 * Replaces the java prefix command creator service with a mock
 *
 * @param forkedJvmTaskExecutionCommandCreator Instance which will have the service replaced.
 * @throws IllegalAccessException
 * @throws NoSuchFieldException
 */
private void replaceJavaPrefixCommandCreatorWithMock(ForkedJvmTaskExecutionCommandCreator forkedJvmTaskExecutionCommandCreator) throws IllegalAccessException, NoSuchFieldException {
    JavaPrefixCommandExtractor javaPrefixCommandExtractor = mock(JavaPrefixCommandExtractor.class);
    given(javaPrefixCommandExtractor.extractJavaPrefixCommandToCommandListFromScriptResult(any(ScriptResult.class))).willReturn(Arrays.asList(testPreJaveCommandString));
    setPrivateField(ForkedJvmTaskExecutionCommandCreator.class.getDeclaredField("javaPrefixCommandExtractor"), forkedJvmTaskExecutionCommandCreator, javaPrefixCommandExtractor);
}
Also used : ScriptResult(org.ow2.proactive.scripting.ScriptResult) JavaPrefixCommandExtractor(org.ow2.proactive.scheduler.task.executors.forked.env.command.JavaPrefixCommandExtractor)

Example 14 with Command

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

the class CommandFactory method getCommandList.

/**
 * Returns an ordered {@link Command} list for specified user arguments.
 *
 * @param cli the command-line arguments
 * @return an ordered {@link Command} list.
 */
protected List<Command> getCommandList(CommandLine cli, Map<String, Command> map, ApplicationContext currentContext) {
    LinkedList<Command> list = new LinkedList<>();
    if (map.containsKey(opt(COMMON_HELP))) {
        list.add(map.remove(opt(COMMON_HELP)));
        return list;
    }
    if (map.containsKey(opt(RM_HELP))) {
        list.add(map.remove(opt(RM_HELP)));
        return list;
    }
    if (map.containsKey(opt(SCHEDULER_HELP))) {
        list.add(map.remove(opt(SCHEDULER_HELP)));
        return list;
    }
    if (map.containsKey(opt(SILENT))) {
        list.add(map.remove(opt(SILENT)));
    }
    if (map.containsKey(opt(DEBUG))) {
        list.add(map.remove(opt(DEBUG)));
    }
    if (map.containsKey(opt(URL))) {
        list.addFirst(map.remove(opt(URL)));
    }
    if (map.containsKey(opt(INSECURE))) {
        list.add(map.remove(opt(INSECURE)));
    } else if (map.containsKey(opt(CA_CERTS))) {
        list.add(map.remove(opt(CA_CERTS)));
        if (map.containsKey(opt(CA_CERTS_PASSWORD))) {
            list.add(map.remove(opt(CA_CERTS_PASSWORD)));
        }
    }
    if (map.containsKey(opt(SESSION_ID))) {
        list.add(map.remove(opt(SESSION_ID)));
    } else if (map.containsKey(opt(SESSION_ID_FILE))) {
        list.add(map.remove(opt(SESSION_ID_FILE)));
    }
    if (map.containsKey(opt(PASSWORD))) {
        list.add(map.remove(opt(PASSWORD)));
    }
    if (map.containsKey(opt(LOGIN))) {
        list.add(map.remove(opt(LOGIN)));
    } else if (map.containsKey(opt(CREDENTIALS))) {
        list.add(map.remove(opt(CREDENTIALS)));
    } else {
        // auto login
        String resourceType = currentContext.getResourceType();
        String filename = resourceType + ".cc";
        File credFile = new File(DFLT_SESSION_DIR, filename);
        if (credFile.exists()) {
            list.add(new LoginWithCredentialsCommand(credFile.getAbsolutePath(), true));
        } else {
            String schedulerHome = ClasspathUtils.findSchedulerHome();
            File defaultCredentials = new File(schedulerHome, DEFAULT_CREDENTIALS_PATH);
            if (defaultCredentials.exists()) {
                list.add(new LoginWithCredentialsCommand(defaultCredentials.getAbsolutePath(), true));
            }
        }
    }
    if (map.containsKey(opt(INFRASTRUCTURE))) {
        list.add(map.remove(opt(INFRASTRUCTURE)));
    }
    if (map.containsKey(opt(POLICY))) {
        list.add(map.remove(opt(POLICY)));
    }
    if (map.isEmpty()) {
        list.add(new ImodeCommand());
    } else {
        Command output = map.remove(opt(OUTPUT));
        list.addAll(map.values());
        if (output != null) {
            list.add(output);
        }
    }
    return list;
}
Also used : Command(org.ow2.proactive_grid_cloud_portal.cli.cmd.Command) LoginWithCredentialsCommand(org.ow2.proactive_grid_cloud_portal.cli.cmd.LoginWithCredentialsCommand) ImodeCommand(org.ow2.proactive_grid_cloud_portal.cli.cmd.ImodeCommand) File(java.io.File) LinkedList(java.util.LinkedList) LoginWithCredentialsCommand(org.ow2.proactive_grid_cloud_portal.cli.cmd.LoginWithCredentialsCommand) ImodeCommand(org.ow2.proactive_grid_cloud_portal.cli.cmd.ImodeCommand)

Example 15 with Command

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

the class EntryPoint method run.

protected int run(String... args) {
    CommandFactory commandFactory = null;
    CommandLine cli = null;
    AbstractDevice console;
    ApplicationContext currentContext = new ApplicationContextImpl().currentContext();
    // Cannot rely on AbstractCommand#isDebugModeEnabled
    // because at this step SetDebugModeCommand#execute has not yet been executed
    // Consequently, SetDebugModeCommand.PROP_DEBUG_MODE is not set even if debug mode is enabled.
    boolean isDebugModeEnabled = isDebugModeEnabled(args);
    try {
        commandFactory = getCommandFactory(currentContext);
        Options options = commandFactory.supportedOptions();
        cli = parseArgs(options, args);
    } catch (IOException ioe) {
        System.err.println("An error occurred.");
        ioe.printStackTrace(System.err);
        return 1;
    } catch (ParseException pe) {
        writeError(currentContext, pe.getMessage(), pe, isDebugModeEnabled);
        // print usage
        Command help = commandFactory.commandForOption(new Option("h", null));
        if (help != null) {
            help.execute(currentContext);
        }
        return 1;
    }
    currentContext.setObjectMapper(new ObjectMapper().configure(FAIL_ON_UNKNOWN_PROPERTIES, false));
    currentContext.setRestServerUrl(DFLT_REST_SCHEDULER_URL);
    currentContext.setResourceType(resourceType());
    // retrieve the (ordered) command list corresponding to command-line
    // arguments
    List<Command> commands;
    try {
        commands = commandFactory.getCommandList(cli, currentContext);
    } catch (CLIException e) {
        writeError(currentContext, "An error occurred.", e, isDebugModeEnabled);
        return 1;
    }
    boolean retryLogin = false;
    try {
        executeCommandList(commands, currentContext);
    } catch (CLIException error) {
        if (REASON_UNAUTHORIZED_ACCESS == error.reason() && hasLoginCommand(commands)) {
            retryLogin = true;
        } else {
            writeError(currentContext, "An error occurred.", error, isDebugModeEnabled);
            return 1;
        }
    } catch (Throwable e) {
        writeError(currentContext, "An error occurred.", e, isDebugModeEnabled);
        return 1;
    }
    /*
         * in case of an existing session-id, the REST CLI reuses it without
         * obtaining a new session-id even if a login with credentials
         * specified. However if the REST server responds with an authorization
         * error (e.g. due to session timeout), it re-executes the commands list
         * with AbstractLoginCommand.PROP_RENEW_SESSION property set to 'true'.
         * This will effectively re-execute the user command with a new
         * session-id from server.
         */
    if (retryLogin && currentContext.getProperty(PROP_PERSISTED_SESSION, Boolean.TYPE, false)) {
        try {
            currentContext.setProperty(PROP_RENEW_SESSION, true);
            executeCommandList(commands, currentContext);
        } catch (Throwable error) {
            writeError(currentContext, "An error occurred while execution.", error, isDebugModeEnabled);
            return 1;
        }
    }
    return 0;
}
Also used : Options(org.apache.commons.cli.Options) IOException(java.io.IOException) CommandLine(org.apache.commons.cli.CommandLine) Command(org.ow2.proactive_grid_cloud_portal.cli.cmd.Command) AbstractLoginCommand(org.ow2.proactive_grid_cloud_portal.cli.cmd.AbstractLoginCommand) AbstractDevice(org.ow2.proactive_grid_cloud_portal.cli.console.AbstractDevice) Option(org.apache.commons.cli.Option) ParseException(org.apache.commons.cli.ParseException) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

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