Search in sources :

Example 16 with ScriptResult

use of org.ow2.proactive.scripting.ScriptResult in project scheduling by ow2-proactive.

the class RMRest method executeNodeScript.

@Override
@POST
@GZIP
@Path("node/script")
@Produces("application/json")
public ScriptResult<Object> executeNodeScript(@HeaderParam("sessionid") String sessionId, @FormParam("nodeurl") String nodeUrl, @FormParam("script") String script, @FormParam("scriptEngine") String scriptEngine) throws Throwable {
    RMProxyUserInterface rm = checkAccess(sessionId);
    List<ScriptResult<Object>> results = rm.executeScript(script, scriptEngine, TargetType.NODE_URL.name(), Collections.singleton(nodeUrl));
    if (results.isEmpty()) {
        throw new IllegalStateException("Empty results from script execution");
    }
    return results.get(0);
}
Also used : ScriptResult(org.ow2.proactive.scripting.ScriptResult) RMProxyUserInterface(org.ow2.proactive.resourcemanager.common.util.RMProxyUserInterface) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) GZIP(org.jboss.resteasy.annotations.GZIP)

Example 17 with ScriptResult

use of org.ow2.proactive.scripting.ScriptResult in project scheduling by ow2-proactive.

the class FlowScript method getResult.

@Override
protected ScriptResult<FlowAction> getResult(Object evalResult, Bindings bindings) {
    try {
        FlowAction act = new FlowAction();
        /*
             * no action defined
             */
        if (this.actionType == null || this.actionType.equals(FlowActionType.CONTINUE.toString())) {
            act.setType(FlowActionType.CONTINUE);
        } else /*
             * loop
             */
        if (this.actionType.equals(FlowActionType.LOOP.toString())) {
            if (this.target == null) {
                String msg = "LOOP control flow action requires a target";
                logger.error(msg);
                return new ScriptResult<FlowAction>(new Exception(msg));
            } else {
                if (bindings.containsKey(loopVariable)) {
                    Boolean enabled;
                    String loopValue = bindings.get(loopVariable).toString();
                    if ("true".equalsIgnoreCase(loopValue)) {
                        enabled = Boolean.TRUE;
                    } else if ("false".equalsIgnoreCase(loopValue)) {
                        enabled = Boolean.FALSE;
                    } else {
                        try {
                            (new Predictor(loopValue)).nextMatchingDate();
                            enabled = Boolean.TRUE;
                            act.setCronExpr(loopValue);
                        } catch (InvalidPatternException e) {
                            enabled = Boolean.FALSE;
                        }
                    }
                    if (enabled) {
                        act.setType(FlowActionType.LOOP);
                        act.setTarget(this.target);
                    } else {
                        act.setType(FlowActionType.CONTINUE);
                    }
                } else {
                    String msg = "Script environment for LOOP action needs to define variable " + loopVariable;
                    logger.error(msg);
                    return new ScriptResult<FlowAction>(new Exception(msg));
                }
            }
        } else /*
             * replicate
             */
        if (this.actionType.equals(FlowActionType.REPLICATE.toString())) {
            if (bindings.containsKey(replicateRunsVariable)) {
                act.setType(FlowActionType.REPLICATE);
                int args = 1;
                Object o = bindings.get(replicateRunsVariable);
                try {
                    args = Integer.parseInt("" + o);
                } catch (NumberFormatException e) {
                    try {
                        args = (int) Math.floor(Double.parseDouble("" + o));
                    } catch (Exception e2) {
                        String msg = "REPLICATE action: could not parse value for variable " + replicateRunsVariable;
                        logger.error(msg);
                        return new ScriptResult<FlowAction>(new Exception(msg, e2));
                    }
                }
                if (args < 0) {
                    String msg = "REPLICATE action: value of variable " + replicateRunsVariable + " cannot be negative";
                    logger.error(msg);
                    return new ScriptResult<FlowAction>(new Exception(msg));
                }
                act.setDupNumber(args);
            } else {
                String msg = "Script environment for REPLICATE action needs to define variable " + replicateRunsVariable;
                logger.error(msg);
                return new ScriptResult<FlowAction>(new Exception(msg));
            }
        } else /*
             * if
             */
        if (this.actionType.equals(FlowActionType.IF.toString())) {
            if (this.target == null) {
                String msg = "IF action requires a target ";
                logger.error(msg);
                return new ScriptResult<FlowAction>(new Exception(msg));
            } else if (this.targetElse == null) {
                String msg = "IF action requires an ELSE target ";
                logger.error(msg);
                return new ScriptResult<FlowAction>(new Exception(msg));
            } else {
                act.setType(FlowActionType.IF);
                if (bindings.containsKey(branchSelectionVariable)) {
                    String val = new String((String) bindings.get(branchSelectionVariable));
                    if (val.toLowerCase().equals(ifBranchSelectedVariable)) {
                        act.setTarget(this.target);
                        act.setTargetElse(this.targetElse);
                    } else if (val.toLowerCase().equals(elseBranchSelectedVariable)) {
                        act.setTarget(this.targetElse);
                        act.setTargetElse(this.target);
                    } else {
                        String msg = "IF action: value for " + branchSelectionVariable + " needs to be one of " + ifBranchSelectedVariable + " or " + elseBranchSelectedVariable;
                        logger.error(msg);
                        return new ScriptResult<FlowAction>(new Exception(msg));
                    }
                } else {
                    String msg = "Environment for IF action needs to define variable " + branchSelectionVariable;
                    logger.error(msg);
                    return new ScriptResult<FlowAction>(new Exception(msg));
                }
                if (this.targetContinuation != null) {
                    act.setTargetContinuation(this.targetContinuation);
                }
            }
        } else /*
             * unknown action
             */
        {
            String msg = actionType + " action type unknown";
            logger.error(msg);
            return new ScriptResult<FlowAction>(new Exception(msg));
        }
        return new ScriptResult<FlowAction>(act);
    } catch (Throwable th) {
        return new ScriptResult<FlowAction>(th);
    }
}
Also used : ScriptResult(org.ow2.proactive.scripting.ScriptResult) InvalidPatternException(it.sauronsoftware.cron4j.InvalidPatternException) Predictor(it.sauronsoftware.cron4j.Predictor) InvalidScriptException(org.ow2.proactive.scripting.InvalidScriptException) InvalidPatternException(it.sauronsoftware.cron4j.InvalidPatternException)

Example 18 with ScriptResult

use of org.ow2.proactive.scripting.ScriptResult 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;
}
Also used : ScriptResult(org.ow2.proactive.scripting.ScriptResult) ForkEnvironment(org.ow2.proactive.scheduler.common.task.ForkEnvironment)

Example 19 with ScriptResult

use of org.ow2.proactive.scripting.ScriptResult 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);
}
Also used : ScriptResult(org.ow2.proactive.scripting.ScriptResult) ForkEnvironmentScriptResult(org.ow2.proactive.scripting.ForkEnvironmentScriptResult) ForkedJvmTaskExecutionCommandCreator(org.ow2.proactive.scheduler.task.executors.forked.env.ForkedJvmTaskExecutionCommandCreator) PrintStream(java.io.PrintStream) TaskContextVariableExtractor(org.ow2.proactive.scheduler.task.context.TaskContextVariableExtractor) TaskContext(org.ow2.proactive.scheduler.task.context.TaskContext) ForkEnvironmentScriptResult(org.ow2.proactive.scripting.ForkEnvironmentScriptResult)

Example 20 with ScriptResult

use of org.ow2.proactive.scripting.ScriptResult in project scheduling by ow2-proactive.

the class SelectionScriptEvaluator method evaluateScript.

private static void evaluateScript(String selectionScript) {
    try {
        final ScriptHandler localHandler = ScriptLoader.createLocalHandler();
        final ScriptResult<Boolean> result = localHandler.handle(new SelectionScript(new File(selectionScript), null));
        if (result.errorOccured()) {
            result.getException().printStackTrace(System.err);
            System.exit(3);
        }
    } catch (InvalidScriptException e) {
        System.exit(2);
    }
}
Also used : SelectionScript(org.ow2.proactive.scripting.SelectionScript) InvalidScriptException(org.ow2.proactive.scripting.InvalidScriptException) File(java.io.File) ScriptHandler(org.ow2.proactive.scripting.ScriptHandler)

Aggregations

ScriptResult (org.ow2.proactive.scripting.ScriptResult)18 SelectionScript (org.ow2.proactive.scripting.SelectionScript)10 RMNode (org.ow2.proactive.resourcemanager.rmnode.RMNode)8 Test (org.junit.Test)7 SimpleScript (org.ow2.proactive.scripting.SimpleScript)7 SelectionManager (org.ow2.proactive.resourcemanager.selection.SelectionManager)6 SelectionManagerTest (org.ow2.proactive.resourcemanager.selection.SelectionManagerTest)6 File (java.io.File)5 Serializable (java.io.Serializable)4 InvalidScriptException (org.ow2.proactive.scripting.InvalidScriptException)4 ScriptHandler (org.ow2.proactive.scripting.ScriptHandler)4 IOException (java.io.IOException)3 PrintStream (java.io.PrintStream)3 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 LoginException (javax.security.auth.login.LoginException)2 ForkEnvironment (org.ow2.proactive.scheduler.common.task.ForkEnvironment)2 RemoteSpace (org.ow2.proactive.scheduler.common.task.dataspaces.RemoteSpace)2