Search in sources :

Example 1 with WorkingDirectory

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

the class GitTaskV2Test method test.

@Test
public void test() throws Exception {
    Map<String, Object> input = new HashMap<>();
    input.put(GitTask.ACTION_KEY, GitTask.Action.CLONE.name());
    input.put(GitTask.GIT_URL, "https://github.com/walmartlabs/concord-plugins.git");
    GitTaskV2 task = new GitTaskV2(mock(SecretService.class), new WorkingDirectory(workDir));
    TaskResult.SimpleResult result = task.execute(new MapBackedVariables(input));
    assertTrue(result.ok());
}
Also used : WorkingDirectory(com.walmartlabs.concord.runtime.v2.sdk.WorkingDirectory) MapBackedVariables(com.walmartlabs.concord.runtime.v2.sdk.MapBackedVariables) HashMap(java.util.HashMap) TaskResult(com.walmartlabs.concord.runtime.v2.sdk.TaskResult) SecretService(com.walmartlabs.concord.runtime.v2.sdk.SecretService) Test(org.junit.Test)

Example 2 with WorkingDirectory

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

the class InjectorFactory method createDefault.

public static Injector createDefault(RunnerConfiguration runnerCfg) {
    Path src = Paths.get(System.getProperty("user.dir"));
    Provider<ProcessConfiguration> processCfgProvider = new DefaultProcessConfigurationProvider(src);
    WorkingDirectory workDir = new WorkingDirectory(src);
    return new InjectorFactory(workDir, runnerCfg, processCfgProvider, // bind default services
    new DefaultRunnerModule(), // grab process dependencies
    new ProcessDependenciesModule(workDir.getValue(), runnerCfg.dependencies(), runnerCfg.debug())).create();
}
Also used : Path(java.nio.file.Path) WorkingDirectory(com.walmartlabs.concord.runtime.v2.sdk.WorkingDirectory) ProcessConfiguration(com.walmartlabs.concord.runtime.v2.sdk.ProcessConfiguration) ProcessDependenciesModule(com.walmartlabs.concord.runtime.v2.runner.guice.ProcessDependenciesModule) DefaultRunnerModule(com.walmartlabs.concord.runtime.v2.runner.guice.DefaultRunnerModule)

Example 3 with WorkingDirectory

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

the class MainTest method run.

private byte[] run(RunnerConfiguration baseCfg) throws Exception {
    assertNotNull(processConfiguration, "save() the process configuration first");
    ImmutableRunnerConfiguration.Builder runnerCfg = RunnerConfiguration.builder().logging(LoggingConfiguration.builder().segmentedLogs(false).build());
    if (baseCfg != null) {
        runnerCfg.from(baseCfg);
    }
    runnerCfg.agentId(UUID.randomUUID().toString()).api(ApiConfiguration.builder().baseUrl(// TODO make optional?
    "http://localhost:8001").build());
    PrintStream oldOut = System.out;
    ByteArrayOutputStream logStream = new ByteArrayOutputStream();
    PrintStream out = new PrintStream(logStream);
    System.setOut(out);
    AbstractModule runtimeModule = new AbstractModule() {

        @Override
        protected void configure() {
            bind(DefaultTaskVariablesService.class).toProvider(new DefaultTaskVariablesProvider(processConfiguration));
            bind(RunnerLogger.class).toProvider(LoggerProvider.class);
            bind(LoggingClient.class).to(TestLoggingClient.class);
        }
    };
    byte[] log;
    try {
        Injector injector = new InjectorFactory(new WorkingDirectory(workDir), runnerCfg.build(), () -> processConfiguration, testServices, runtimeModule).create();
        injector.getInstance(Main.class).execute();
    } finally {
        out.flush();
        System.setOut(oldOut);
        log = logStream.toByteArray();
        System.out.write(log, 0, log.length);
        lastLog = log;
    }
    if (allLogs == null) {
        allLogs = log;
    } else {
        // append the current log to allLogs
        ByteArrayOutputStream baos = new ByteArrayOutputStream(allLogs.length + log.length);
        baos.write(allLogs);
        baos.write(log);
        allLogs = baos.toByteArray();
    }
    return log;
}
Also used : LoggingClient(com.walmartlabs.concord.runtime.v2.runner.logging.LoggingClient) AbstractModule(com.google.inject.AbstractModule) Injector(com.google.inject.Injector) ImmutableRunnerConfiguration(com.walmartlabs.concord.runtime.common.cfg.ImmutableRunnerConfiguration) RunnerLogger(com.walmartlabs.concord.runtime.v2.runner.logging.RunnerLogger)

Example 4 with WorkingDirectory

use of com.walmartlabs.concord.runtime.v2.sdk.WorkingDirectory 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

WorkingDirectory (com.walmartlabs.concord.runtime.v2.sdk.WorkingDirectory)3 Injector (com.google.inject.Injector)2 ProcessDependenciesModule (com.walmartlabs.concord.runtime.v2.runner.guice.ProcessDependenciesModule)2 ProcessConfiguration (com.walmartlabs.concord.runtime.v2.sdk.ProcessConfiguration)2 Path (java.nio.file.Path)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 AbstractModule (com.google.inject.AbstractModule)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 ImmutableRunnerConfiguration (com.walmartlabs.concord.runtime.common.cfg.ImmutableRunnerConfiguration)1 RunnerConfiguration (com.walmartlabs.concord.runtime.common.cfg.RunnerConfiguration)1 ProjectLoaderV2 (com.walmartlabs.concord.runtime.v2.ProjectLoaderV2)1 ProjectSerializerV2 (com.walmartlabs.concord.runtime.v2.ProjectSerializerV2)1 ProcessDefinition (com.walmartlabs.concord.runtime.v2.model.ProcessDefinition)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 DefaultRunnerModule (com.walmartlabs.concord.runtime.v2.runner.guice.DefaultRunnerModule)1