Search in sources :

Example 56 with ProcessKey

use of com.walmartlabs.concord.server.sdk.ProcessKey in project concord by walmartlabs.

the class PayloadStoreProcessor method process.

@Override
@WithTimer
public Payload process(Chain chain, Payload payload) {
    ProcessKey processKey = payload.getProcessKey();
    // remove things that shouldn't be in the serialized payload
    Map<String, Object> headers = payload.getHeaders().entrySet().stream().filter(e -> !(e.getValue() instanceof Path)).filter(e -> !EXCLUDED_HEADERS.contains(e.getKey())).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
    String serializedHeaders = serialize(headers);
    stateManager.tx(tx -> {
        stateManager.insert(tx, processKey, "_initial/payload.json", serializedHeaders.getBytes());
        stateManager.importPath(tx, processKey, "_initial/attachments/", payload.getHeader(Payload.BASE_DIR), (path, basicFileAttributes) -> payload.getAttachments().containsValue(path));
    });
    return chain.process(payload);
}
Also used : Path(java.nio.file.Path) Payload(com.walmartlabs.concord.server.process.Payload) ProcessStateManager(com.walmartlabs.concord.server.process.state.ProcessStateManager) Arrays(java.util.Arrays) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Set(java.util.Set) ProcessKey(com.walmartlabs.concord.server.sdk.ProcessKey) Collectors(java.util.stream.Collectors) Jdk8Module(com.fasterxml.jackson.datatype.jdk8.Jdk8Module) Inject(javax.inject.Inject) HashSet(java.util.HashSet) GuavaModule(com.fasterxml.jackson.datatype.guava.GuavaModule) JavaTimeModule(com.fasterxml.jackson.datatype.jsr310.JavaTimeModule) Map(java.util.Map) WithTimer(com.walmartlabs.concord.server.sdk.metrics.WithTimer) Named(javax.inject.Named) Path(java.nio.file.Path) ProcessKey(com.walmartlabs.concord.server.sdk.ProcessKey) Map(java.util.Map) WithTimer(com.walmartlabs.concord.server.sdk.metrics.WithTimer)

Example 57 with ProcessKey

use of com.walmartlabs.concord.server.sdk.ProcessKey in project concord by walmartlabs.

the class PolicyProcessor method process.

@Override
public Payload process(Chain chain, Payload payload) {
    ProcessKey processKey = payload.getProcessKey();
    PolicyEngine policy = payload.getHeader(Payload.POLICY);
    if (policy == null) {
        return chain.process(payload);
    }
    logManager.info(processKey, "Applying policies...");
    try {
        // TODO merge check results
        for (PolicyApplier a : appliers) {
            a.apply(payload, policy);
        }
    } catch (ProcessException e) {
        throw e;
    } catch (Exception e) {
        logManager.error(processKey, "Error while applying policy '{}': {}", policy.policyNames(), e);
        throw new ProcessException(processKey, "Policy '" + policy.policyNames() + "' error", e);
    }
    return chain.process(payload);
}
Also used : PolicyApplier(com.walmartlabs.concord.server.process.pipelines.processors.policy.PolicyApplier) ProcessException(com.walmartlabs.concord.server.process.ProcessException) PolicyEngine(com.walmartlabs.concord.policyengine.PolicyEngine) ProcessKey(com.walmartlabs.concord.server.sdk.ProcessKey) ProcessException(com.walmartlabs.concord.server.process.ProcessException)

Example 58 with ProcessKey

use of com.walmartlabs.concord.server.sdk.ProcessKey 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)

Example 59 with ProcessKey

use of com.walmartlabs.concord.server.sdk.ProcessKey in project concord by walmartlabs.

the class RepositoryProcessor method process.

@Override
@WithTimer
public Payload process(Chain chain, final Payload payload) {
    ProcessKey processKey = payload.getProcessKey();
    UUID projectId = payload.getHeader(Payload.PROJECT_ID);
    RepositoryEntry repo = getRepositoryEntry(payload);
    if (projectId == null || repo == null) {
        return chain.process(payload);
    }
    logManager.info(processKey, "Copying the repository's data: {} @ {}:{}, path: {}", repo.getUrl(), repo.getBranch() != null ? repo.getBranch() : "*", repo.getCommitId() != null ? repo.getCommitId() : "head", repo.getPath() != null ? repo.getPath() : "/");
    Path dst = payload.getHeader(Payload.WORKSPACE_DIR);
    Payload newPayload = repositoryManager.withLock(repo.getUrl(), () -> {
        try {
            Repository repository = payload.getHeader(Payload.REPOSITORY);
            if (repository == null) {
                repository = repositoryManager.fetch(projectId, repo, true);
            }
            Snapshot snapshot = repository.export(dst);
            CommitInfo ci = null;
            if (repository.fetchResult() != null) {
                FetchResult r = Objects.requireNonNull(repository.fetchResult());
                ci = new CommitInfo(r.head(), r.branchOrTag(), r.author(), r.message());
            }
            RepositoryInfo i = new RepositoryInfo(repo.getId(), repo.getName(), repo.getUrl(), repo.getPath(), repo.getBranch(), repo.getCommitId(), ci);
            return payload.putHeader(REPOSITORY_INFO_KEY, i).putHeader(Payload.REPOSITORY, repository).putHeader(Payload.REPOSITORY_SNAPSHOT, Collections.singletonList(snapshot));
        } catch (Exception e) {
            log.error("process -> repository error", e);
            logManager.error(processKey, "Error while processing a repository: " + repo.getUrl(), e);
            throw new ProcessException(processKey, "Error while processing a repository: " + repo.getUrl(), e);
        }
    });
    return chain.process(newPayload);
}
Also used : Path(java.nio.file.Path) Snapshot(com.walmartlabs.concord.repository.Snapshot) Repository(com.walmartlabs.concord.repository.Repository) ProcessException(com.walmartlabs.concord.server.process.ProcessException) FetchResult(com.walmartlabs.concord.repository.FetchResult) Payload(com.walmartlabs.concord.server.process.Payload) ProcessKey(com.walmartlabs.concord.server.sdk.ProcessKey) RepositoryEntry(com.walmartlabs.concord.server.org.project.RepositoryEntry) UUID(java.util.UUID) ProcessException(com.walmartlabs.concord.server.process.ProcessException) WithTimer(com.walmartlabs.concord.server.sdk.metrics.WithTimer)

Example 60 with ProcessKey

use of com.walmartlabs.concord.server.sdk.ProcessKey in project concord by walmartlabs.

the class WorkspaceArchiveProcessor method process.

@Override
public Payload process(Chain chain, Payload payload) {
    ProcessKey processKey = payload.getProcessKey();
    Path archive = payload.getAttachment(Payload.WORKSPACE_ARCHIVE);
    if (archive == null) {
        return chain.process(payload);
    }
    if (!Files.exists(archive)) {
        logManager.error(processKey, "No input archive found: " + archive);
        throw new ProcessException(processKey, "No input archive found: " + archive, Status.BAD_REQUEST);
    }
    Path workspace = payload.getHeader(Payload.WORKSPACE_DIR);
    try {
        IOUtils.unzip(archive, workspace, StandardCopyOption.REPLACE_EXISTING);
    } catch (IOException e) {
        logManager.error(processKey, "Error while unpacking an archive: " + archive, e);
        throw new ProcessException(processKey, "Error while unpacking an archive: " + archive, e);
    }
    payload = payload.removeAttachment(Payload.WORKSPACE_ARCHIVE);
    return chain.process(payload);
}
Also used : Path(java.nio.file.Path) ProcessException(com.walmartlabs.concord.server.process.ProcessException) ProcessKey(com.walmartlabs.concord.server.sdk.ProcessKey) IOException(java.io.IOException)

Aggregations

ProcessKey (com.walmartlabs.concord.server.sdk.ProcessKey)69 WithTimer (com.walmartlabs.concord.server.sdk.metrics.WithTimer)28 Path (java.nio.file.Path)27 PartialProcessKey (com.walmartlabs.concord.server.sdk.PartialProcessKey)25 ApiOperation (io.swagger.annotations.ApiOperation)22 ProcessException (com.walmartlabs.concord.server.process.ProcessException)20 ConcordApplicationException (com.walmartlabs.concord.server.sdk.ConcordApplicationException)16 IOException (java.io.IOException)16 UUID (java.util.UUID)9 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)8 PolicyEngine (com.walmartlabs.concord.policyengine.PolicyEngine)8 Inject (javax.inject.Inject)8 Named (javax.inject.Named)8 ProcessKeyCache (com.walmartlabs.concord.server.process.queue.ProcessKeyCache)7 ProcessQueueDao (com.walmartlabs.concord.server.process.queue.ProcessQueueDao)7 ProcessStateManager (com.walmartlabs.concord.server.process.state.ProcessStateManager)7 UserPrincipal (com.walmartlabs.concord.server.security.UserPrincipal)7 ProcessLogManager (com.walmartlabs.concord.server.process.logs.ProcessLogManager)6 HttpUtils (com.walmartlabs.concord.server.HttpUtils)5 ResourceAccessLevel (com.walmartlabs.concord.server.org.ResourceAccessLevel)5