Search in sources :

Example 1 with Runner

use of com.walmartlabs.concord.runtime.v2.runner.Runner 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 2 with Runner

use of com.walmartlabs.concord.runtime.v2.runner.Runner 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 3 with Runner

use of com.walmartlabs.concord.runtime.v2.runner.Runner in project concord by walmartlabs.

the class Main method start.

private static ProcessSnapshot start(Runner runner, ProcessConfiguration cfg, Path workDir, Map<String, Object> args) throws Exception {
    // assume all imports were processed by the agent
    ProjectLoaderV2 loader = new ProjectLoaderV2(new NoopImportManager());
    ProcessDefinition processDefinition = loader.load(workDir, new NoopImportsNormalizer(), ImportsListener.NOP_LISTENER).getProjectDefinition();
    Map<String, Object> initiator = cfg.initiator();
    if (initiator != null) {
        // when the process starts the process' initiator and the current user are the same
        args.put(Constants.Request.INITIATOR_KEY, initiator);
        args.put(Constants.Request.CURRENT_USER_KEY, initiator);
    }
    return runner.start(cfg, processDefinition, args);
}
Also used : NoopImportManager(com.walmartlabs.concord.imports.NoopImportManager) ProjectLoaderV2(com.walmartlabs.concord.runtime.v2.ProjectLoaderV2) NoopImportsNormalizer(com.walmartlabs.concord.runtime.v2.NoopImportsNormalizer) ProcessDefinition(com.walmartlabs.concord.runtime.v2.model.ProcessDefinition)

Example 4 with Runner

use of com.walmartlabs.concord.runtime.v2.runner.Runner in project concord by walmartlabs.

the class Run method call.

@Override
public Integer call() throws Exception {
    sourceDir = sourceDir.normalize().toAbsolutePath();
    Path targetDir;
    if (Files.isRegularFile(sourceDir)) {
        Path src = sourceDir.toAbsolutePath();
        System.out.println("Running a single Concord file: " + src);
        targetDir = Files.createTempDirectory("payload");
        Files.copy(src, targetDir.resolve("concord.yml"), StandardCopyOption.REPLACE_EXISTING);
    } else if (Files.isDirectory(sourceDir)) {
        targetDir = sourceDir.resolve("target");
        if (cleanup && Files.exists(targetDir)) {
            if (verbose) {
                System.out.println("Cleaning target directory");
            }
            IOUtils.deleteRecursively(targetDir);
        }
        // copy everything into target except target
        IOUtils.copy(sourceDir, targetDir, "^target$", new CopyNotifier(verbose ? 0 : 100), StandardCopyOption.REPLACE_EXISTING);
    } else {
        throw new IllegalArgumentException("Not a directory or single Concord YAML file: " + sourceDir);
    }
    DependencyManager dependencyManager = initDependencyManager();
    ImportManager importManager = new ImportManagerFactory(dependencyManager, new CliRepositoryExporter(repoCacheDir), Collections.emptySet()).create();
    ProjectLoaderV2.Result loadResult;
    try {
        loadResult = new ProjectLoaderV2(importManager).load(targetDir, new CliImportsNormalizer(importsSource, verbose, defaultVersion), verbose ? new CliImportsListener() : null);
    } catch (ImportProcessingException e) {
        ObjectMapper om = new ObjectMapper();
        System.err.println("Error while processing import " + om.writeValueAsString(e.getImport()) + ": " + e.getMessage());
        return -1;
    } catch (Exception e) {
        System.err.println("Error while loading " + targetDir);
        e.printStackTrace();
        return -1;
    }
    ProcessDefinition processDefinition = loadResult.getProjectDefinition();
    UUID instanceId = UUID.randomUUID();
    if (verbose && !extraVars.isEmpty()) {
        System.out.println("Additional variables: " + extraVars);
    }
    if (verbose && !profiles.isEmpty()) {
        System.out.println("Active profiles: " + profiles);
    }
    ProcessConfiguration cfg = from(processDefinition.configuration()).entryPoint(entryPoint).instanceId(instanceId).build();
    RunnerConfiguration runnerCfg = RunnerConfiguration.builder().dependencies(new DependencyResolver(dependencyManager, verbose).resolveDeps(processDefinition)).debug(cfg.debug()).build();
    Map<String, Object> profileArgs = getProfilesArguments(processDefinition, profiles);
    Map<String, Object> args = ConfigurationUtils.deepMerge(cfg.arguments(), profileArgs, extraVars);
    if (verbose) {
        System.out.println("Process arguments: " + args);
    }
    args.put(Constants.Context.TX_ID_KEY, instanceId.toString());
    args.put(Constants.Context.WORK_DIR_KEY, targetDir.toAbsolutePath().toString());
    if (effectiveYaml) {
        Map<String, List<Step>> flows = new HashMap<>(processDefinition.flows());
        for (String ap : profiles) {
            Profile p = processDefinition.profiles().get(ap);
            if (p != null) {
                flows.putAll(p.flows());
            }
        }
        ProcessDefinition pd = ProcessDefinition.builder().from(processDefinition).configuration(ProcessDefinitionConfiguration.builder().from(processDefinition.configuration()).arguments(args).build()).flows(flows).imports(Imports.builder().build()).profiles(Collections.emptyMap()).build();
        ProjectSerializerV2 serializer = new ProjectSerializerV2();
        serializer.write(pd, System.out);
        return 0;
    }
    System.out.println("Starting...");
    Injector injector = new InjectorFactory(new WorkingDirectory(targetDir), runnerCfg, () -> cfg, new ProcessDependenciesModule(targetDir, runnerCfg.dependencies(), cfg.debug()), new CliServicesModule(secretStoreDir, targetDir, new VaultProvider(vaultDir, vaultId), dependencyManager)).create();
    Runner runner = injector.getInstance(Runner.class);
    if (cfg.debug()) {
        System.out.println("Available tasks: " + injector.getInstance(TaskProviders.class).names());
    }
    try {
        runner.start(cfg, processDefinition, args);
    } catch (Exception e) {
        if (verbose) {
            System.err.print("Error: ");
            e.printStackTrace(System.err);
        } else {
            System.err.println("Error: " + e.getMessage());
        }
        return 1;
    }
    System.out.println("...done!");
    return 0;
}
Also used : ImportManager(com.walmartlabs.concord.imports.ImportManager) Runner(com.walmartlabs.concord.runtime.v2.runner.Runner) DependencyManager(com.walmartlabs.concord.dependencymanager.DependencyManager) ProcessDefinition(com.walmartlabs.concord.runtime.v2.model.ProcessDefinition) Profile(com.walmartlabs.concord.runtime.v2.model.Profile) ProjectSerializerV2(com.walmartlabs.concord.runtime.v2.ProjectSerializerV2) ImmutableProcessConfiguration(com.walmartlabs.concord.runtime.v2.sdk.ImmutableProcessConfiguration) ProcessConfiguration(com.walmartlabs.concord.runtime.v2.sdk.ProcessConfiguration) InjectorFactory(com.walmartlabs.concord.runtime.v2.runner.InjectorFactory) Injector(com.google.inject.Injector) RunnerConfiguration(com.walmartlabs.concord.runtime.common.cfg.RunnerConfiguration) ImportManagerFactory(com.walmartlabs.concord.imports.ImportManagerFactory) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Path(java.nio.file.Path) WorkingDirectory(com.walmartlabs.concord.runtime.v2.sdk.WorkingDirectory) ImportProcessingException(com.walmartlabs.concord.imports.ImportProcessingException) ProcessDependenciesModule(com.walmartlabs.concord.runtime.v2.runner.guice.ProcessDependenciesModule) IOException(java.io.IOException) ImportProcessingException(com.walmartlabs.concord.imports.ImportProcessingException) ProjectLoaderV2(com.walmartlabs.concord.runtime.v2.ProjectLoaderV2) TaskProviders(com.walmartlabs.concord.runtime.v2.runner.tasks.TaskProviders)

Aggregations

ProjectLoaderV2 (com.walmartlabs.concord.runtime.v2.ProjectLoaderV2)2 ProcessDefinition (com.walmartlabs.concord.runtime.v2.model.ProcessDefinition)2 Path (java.nio.file.Path)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Injector (com.google.inject.Injector)1 ApiClient (com.walmartlabs.concord.ApiClient)1 DependencyManager (com.walmartlabs.concord.dependencymanager.DependencyManager)1 ImportManager (com.walmartlabs.concord.imports.ImportManager)1 ImportManagerFactory (com.walmartlabs.concord.imports.ImportManagerFactory)1 ImportProcessingException (com.walmartlabs.concord.imports.ImportProcessingException)1 NoopImportManager (com.walmartlabs.concord.imports.NoopImportManager)1 AnsibleSecretService (com.walmartlabs.concord.plugins.ansible.secrets.AnsibleSecretService)1 RunnerConfiguration (com.walmartlabs.concord.runtime.common.cfg.RunnerConfiguration)1 NoopImportsNormalizer (com.walmartlabs.concord.runtime.v2.NoopImportsNormalizer)1 ProjectSerializerV2 (com.walmartlabs.concord.runtime.v2.ProjectSerializerV2)1 Profile (com.walmartlabs.concord.runtime.v2.model.Profile)1 InjectorFactory (com.walmartlabs.concord.runtime.v2.runner.InjectorFactory)1 Runner (com.walmartlabs.concord.runtime.v2.runner.Runner)1 ProcessDependenciesModule (com.walmartlabs.concord.runtime.v2.runner.guice.ProcessDependenciesModule)1 TaskProviders (com.walmartlabs.concord.runtime.v2.runner.tasks.TaskProviders)1