Search in sources :

Example 1 with ImportProcessingException

use of com.walmartlabs.concord.imports.ImportProcessingException 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)

Example 2 with ImportProcessingException

use of com.walmartlabs.concord.imports.ImportProcessingException in project concord by walmartlabs.

the class ProcessDefinitionProcessor method process.

@Override
public Payload process(Chain chain, Payload payload) {
    ProcessKey processKey = payload.getProcessKey();
    Path workDir = payload.getHeader(Payload.WORKSPACE_DIR);
    if (workDir == null) {
        return chain.process(payload);
    }
    UUID projectId = payload.getHeader(Payload.PROJECT_ID);
    try {
        String runtime = getRuntimeType(payload);
        ProjectLoader.Result result = projectLoader.loadProject(workDir, runtime, importsNormalizer.forProject(projectId), new ProcessImportsListener(processKey));
        List<Snapshot> snapshots = result.snapshots();
        payload = PayloadUtils.addSnapshots(payload, snapshots);
        ProcessDefinition pd = result.projectDefinition();
        int depsCount = pd.configuration().dependencies().size();
        if (depsCount > MAX_DEPENDENCIES_COUNT) {
            String msg = String.format("Too many dependencies. Current: %d, maximum allowed: %d", depsCount, MAX_DEPENDENCIES_COUNT);
            throw new ConcordApplicationException(msg, Response.Status.BAD_REQUEST);
        }
        payload = payload.putHeader(Payload.PROJECT_DEFINITION, pd).putHeader(Payload.RUNTIME, pd.runtime()).putHeader(Payload.IMPORTS, pd.imports()).putHeader(Payload.DEPENDENCIES, pd.configuration().dependencies());
        // save the runtime type in the process configuration
        Map<String, Object> cfg = payload.getHeader(Payload.CONFIGURATION, Collections.emptyMap());
        // make mutable
        cfg = new HashMap<>(cfg);
        cfg.put(Constants.Request.RUNTIME_KEY, runtime);
        payload = payload.putHeader(Payload.CONFIGURATION, cfg);
    } catch (ImportProcessingException e) {
        throw new ProcessException(processKey, "Error while processing import " + e.getImport() + ". Error: " + e.getMessage(), e);
    } catch (Exception e) {
        log.warn("process -> ({}) project loading error: {}", workDir, e.getMessage());
        throw new ProcessException(processKey, "Error while loading the project, check the syntax. " + e.getMessage(), e);
    }
    return chain.process(payload);
}
Also used : Path(java.nio.file.Path) ImportProcessingException(com.walmartlabs.concord.imports.ImportProcessingException) ProcessDefinition(com.walmartlabs.concord.process.loader.model.ProcessDefinition) ProcessKey(com.walmartlabs.concord.server.sdk.ProcessKey) ProjectLoader(com.walmartlabs.concord.process.loader.ProjectLoader) ProcessException(com.walmartlabs.concord.server.process.ProcessException) IOException(java.io.IOException) ImportProcessingException(com.walmartlabs.concord.imports.ImportProcessingException) ConcordApplicationException(com.walmartlabs.concord.server.sdk.ConcordApplicationException) Snapshot(com.walmartlabs.concord.repository.Snapshot) ProcessException(com.walmartlabs.concord.server.process.ProcessException) ConcordApplicationException(com.walmartlabs.concord.server.sdk.ConcordApplicationException)

Aggregations

ImportProcessingException (com.walmartlabs.concord.imports.ImportProcessingException)2 IOException (java.io.IOException)2 Path (java.nio.file.Path)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Injector (com.google.inject.Injector)1 DependencyManager (com.walmartlabs.concord.dependencymanager.DependencyManager)1 ImportManager (com.walmartlabs.concord.imports.ImportManager)1 ImportManagerFactory (com.walmartlabs.concord.imports.ImportManagerFactory)1 ProjectLoader (com.walmartlabs.concord.process.loader.ProjectLoader)1 ProcessDefinition (com.walmartlabs.concord.process.loader.model.ProcessDefinition)1 Snapshot (com.walmartlabs.concord.repository.Snapshot)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 ProcessDependenciesModule (com.walmartlabs.concord.runtime.v2.runner.guice.ProcessDependenciesModule)1 TaskProviders (com.walmartlabs.concord.runtime.v2.runner.tasks.TaskProviders)1