Search in sources :

Example 6 with Task

use of com.walmartlabs.concord.runtime.v2.sdk.Task in project concord-plugins by walmartlabs.

the class PuppetTaskV2Test method testQuery.

@Test
public void testQuery() throws Exception {
    // -- Task in-vars
    variables.put("action", "pql");
    variables.put("queryString", "inventory[certname]{ limit 10 }");
    // -- Execute
    TaskResult.SimpleResult result = task.execute(input);
    // -- Validate
    assertNotNull(result);
    assertTrue(result.ok());
    List data = (List) result.values().get("data");
    assertEquals(10, data.size());
}
Also used : TaskResult(com.walmartlabs.concord.runtime.v2.sdk.TaskResult) List(java.util.List) Test(org.junit.Test)

Example 7 with Task

use of com.walmartlabs.concord.runtime.v2.sdk.Task in project concord by walmartlabs.

the class RunPlaybookTask2 method run.

private void run(Context ctx, Path workDir, Map<String, Object> args) throws Exception {
    ApiClient apiClient = apiClientFactory.create(ApiClientConfiguration.builder().context(context).build());
    AnsibleSecretServiceV1 ansibleSecretService = new AnsibleSecretServiceV1(context, secretService);
    AnsibleTask task = new AnsibleTask(apiClient, new AnsibleAuthFactory(ansibleSecretService), ansibleSecretService);
    Map<String, Object> projectInfo = getMap(context, Constants.Request.PROJECT_INFO_KEY, null);
    String orgName = projectInfo != null ? (String) projectInfo.get("orgName") : null;
    AnsibleContext context = AnsibleContext.builder().apiBaseUrl(apiClient.getBasePath()).instanceId(UUID.fromString(txId)).workDir(workDir).tmpDir(createTmpDir(workDir)).defaults(defaults != null ? defaults : Collections.emptyMap()).args(args).sessionToken(apiCfg.getSessionToken(ctx)).eventCorrelationId(ctx.getEventCorrelationId()).orgName(orgName).retryCount((Integer) ctx.getVariable(Constants.Context.CURRENT_RETRY_COUNTER)).build();
    PlaybookProcessRunner runner = new PlaybookProcessRunnerFactory(new AnsibleDockerServiceV1(ctx, dockerService), workDir).create(args);
    TaskResult.SimpleResult result = task.run(context, runner);
    result.values().forEach(ctx::setVariable);
    if (!result.ok()) {
        throw new IllegalStateException("Process finished with exit code " + result.values().get("exitCode"));
    }
}
Also used : ApiClient(com.walmartlabs.concord.ApiClient) TaskResult(com.walmartlabs.concord.runtime.v2.sdk.TaskResult)

Example 8 with Task

use of com.walmartlabs.concord.runtime.v2.sdk.Task in project concord by walmartlabs.

the class AnsibleTaskV2 method execute.

@Override
public TaskResult execute(Variables input) throws Exception {
    Map<String, Object> in = input.toMap();
    Path workDir = context.workingDirectory();
    PlaybookProcessRunner runner = new PlaybookProcessRunnerFactory(new AnsibleDockerServiceV2(context.dockerService()), workDir).create(in);
    AnsibleSecretService secretService = new AnsibleSecretServiceV2(context.secretService());
    AnsibleTask task = new AnsibleTask(apiClient, new AnsibleAuthFactory(secretService), secretService);
    UUID instanceId = Objects.requireNonNull(context.processInstanceId());
    Path tmpDir = context.fileService().createTempDirectory("ansible");
    ProjectInfo projectInfo = context.processConfiguration().projectInfo();
    AnsibleContext ctx = AnsibleContext.builder().apiBaseUrl(apiClient.getBasePath()).instanceId(instanceId).workDir(workDir).tmpDir(tmpDir).defaults(defaults).args(in).sessionToken(context.processConfiguration().processInfo().sessionToken()).eventCorrelationId(context.execution().correlationId()).orgName(projectInfo != null ? projectInfo.orgName() : null).retryCount(ContextUtils.getCurrentRetryAttemptNumber(context)).build();
    TaskResult.SimpleResult result = task.run(ctx, runner);
    if (!result.ok()) {
        throw new IllegalStateException("Process finished with exit code " + result.values().get("exitCode"));
    }
    return result;
}
Also used : Path(java.nio.file.Path) AnsibleSecretService(com.walmartlabs.concord.plugins.ansible.secrets.AnsibleSecretService) ProjectInfo(com.walmartlabs.concord.runtime.v2.sdk.ProjectInfo) UUID(java.util.UUID)

Example 9 with Task

use of com.walmartlabs.concord.runtime.v2.sdk.Task in project concord by walmartlabs.

the class TaskMethodResolver method invoke.

@Override
public Object invoke(ELContext elContext, Object base, Object method, Class<?>[] paramTypes, Object[] params) {
    Step step = context.execution().currentStep();
    if (!(step instanceof Expression) || !(base instanceof Task) || !(method instanceof String)) {
        return null;
    }
    String taskName = getName(base);
    if (taskName == null) {
        return null;
    }
    CallContext callContext = TaskCallInterceptor.CallContext.builder().taskName(taskName).correlationId(context.execution().correlationId()).currentStep(step).processDefinition(context.execution().processDefinition()).build();
    TaskCallInterceptor interceptor = context.execution().runtime().getService(TaskCallInterceptor.class);
    try {
        return interceptor.invoke(callContext, Method.of(base, (String) method, Arrays.asList(params)), () -> super.invoke(elContext, base, method, paramTypes, params));
    } catch (javax.el.MethodNotFoundException e) {
        throw new MethodNotFoundException(base, method, paramTypes);
    } catch (javax.el.ELException e) {
        if (e.getCause() instanceof RuntimeException) {
            throw (RuntimeException) e.getCause();
        }
        throw e;
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : Task(com.walmartlabs.concord.runtime.v2.sdk.Task) Expression(com.walmartlabs.concord.runtime.v2.model.Expression) Step(com.walmartlabs.concord.runtime.v2.model.Step) CallContext(com.walmartlabs.concord.runtime.v2.runner.tasks.TaskCallInterceptor.CallContext) MethodNotFoundException(com.walmartlabs.concord.runtime.v2.runner.el.MethodNotFoundException) TaskCallInterceptor(com.walmartlabs.concord.runtime.v2.runner.tasks.TaskCallInterceptor) MethodNotFoundException(com.walmartlabs.concord.runtime.v2.runner.el.MethodNotFoundException)

Example 10 with Task

use of com.walmartlabs.concord.runtime.v2.sdk.Task in project concord by walmartlabs.

the class ExpressionEvaluatorTest method testEval4.

@Test
public void testEval4() {
    /*
         * configuration:
         *   arguments:
         *     x: ${y}
         *     z: ${y.y1}
         *     y:
         *     	y1: ${task(y.y2)}
         *     	y2: "asdasd"
         *     	y3: ${z}
         */
    Map<Object, Object> input = map("x", "${y}", "z", "${y.y1}", "y", map("y1", "${task.foo(y.y2)}", "y2", "abc", "y3", "${z}"));
    TaskProviders providers = mock(TaskProviders.class);
    TestTask2 task = spy(new TestTask2());
    when(providers.createTask(any(), eq("task"))).thenReturn(task);
    ExpressionEvaluator ee = new DefaultExpressionEvaluator(providers);
    Map<String, Object> vars = Collections.emptyMap();
    // scope:
    // ---
    Map<Object, Object> output = ee.evalAsMap(scope(vars), input);
    Map<Object, Object> y = map("y1", "${abc}", "y2", "abc", "y3", "${abc}");
    assertThat(output, is(map("x", y, "z", "${abc}", "y", y)));
    verify(task, times(1)).foo(anyString());
}
Also used : TaskProviders(com.walmartlabs.concord.runtime.v2.runner.tasks.TaskProviders) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.jupiter.api.Test)

Aggregations

TaskResult (com.walmartlabs.concord.runtime.v2.sdk.TaskResult)7 Test (org.junit.Test)7 TaskProviders (com.walmartlabs.concord.runtime.v2.runner.tasks.TaskProviders)6 HashMap (java.util.HashMap)5 List (java.util.List)5 MissingParameterException (com.walmartlabs.concord.plugins.puppet.model.exception.MissingParameterException)4 MapBackedVariables (com.walmartlabs.concord.runtime.v2.sdk.MapBackedVariables)4 SSLHandshakeException (javax.net.ssl.SSLHandshakeException)4 Step (com.walmartlabs.concord.runtime.v2.model.Step)3 TaskCallInterceptor (com.walmartlabs.concord.runtime.v2.runner.tasks.TaskCallInterceptor)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3 Test (org.junit.jupiter.api.Test)3 TaskCall (com.walmartlabs.concord.runtime.v2.model.TaskCall)2 TaskCallOptions (com.walmartlabs.concord.runtime.v2.model.TaskCallOptions)2 CallContext (com.walmartlabs.concord.runtime.v2.runner.tasks.TaskCallInterceptor.CallContext)2 TaskException (com.walmartlabs.concord.runtime.v2.runner.tasks.TaskException)2 Task (com.walmartlabs.concord.runtime.v2.sdk.Task)2 Variables (com.walmartlabs.concord.runtime.v2.sdk.Variables)2 Frame (com.walmartlabs.concord.svm.Frame)2 AbstractModule (com.google.inject.AbstractModule)1