Search in sources :

Example 31 with ProcessKey

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

the class ProcessResource method disable.

/**
 * Disable a process.
 *
 * @param instanceId
 * @param disabled
 */
@POST
@ApiOperation("Disable a process")
@javax.ws.rs.Path("/{id}/disable/{disabled}")
@WithTimer
public void disable(@ApiParam @PathParam("id") UUID instanceId, @ApiParam @PathParam("disabled") boolean disabled) {
    ProcessKey processKey = assertProcessKey(instanceId);
    processManager.disable(processKey, disabled);
}
Also used : PartialProcessKey(com.walmartlabs.concord.server.sdk.PartialProcessKey) ProcessKey(com.walmartlabs.concord.server.sdk.ProcessKey) WithTimer(com.walmartlabs.concord.server.sdk.metrics.WithTimer) ApiOperation(io.swagger.annotations.ApiOperation)

Example 32 with ProcessKey

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

the class ProcessResource method downloadAttachment.

/**
 * Returns a process' attachment file.
 */
@GET
@ApiOperation(value = "Download a process' attachment", response = File.class)
@javax.ws.rs.Path("/{id}/attachment/{name:.*}")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response downloadAttachment(@ApiParam @PathParam("id") UUID instanceId, @PathParam("name") @NotNull @Size(min = 1) String attachmentName) {
    ProcessEntry processEntry = processManager.assertProcess(instanceId);
    assertProcessAccess(processEntry, "attachment");
    PartialProcessKey processKey = new ProcessKey(processEntry.instanceId(), processEntry.createdAt());
    // TODO replace with javax.validation
    if (attachmentName.endsWith("/")) {
        throw new ConcordApplicationException("Invalid attachment name: " + attachmentName, Status.BAD_REQUEST);
    }
    String resource = path(Constants.Files.JOB_ATTACHMENTS_DIR_NAME, attachmentName);
    Optional<Path> o = stateManager.get(processKey, resource, src -> {
        try {
            Path tmp = IOUtils.createTempFile("attachment", ".bin");
            try (OutputStream dst = Files.newOutputStream(tmp)) {
                IOUtils.copy(src, dst);
            }
            return Optional.of(tmp);
        } catch (IOException e) {
            throw new ConcordApplicationException("Error while exporting an attachment: " + attachmentName, e);
        }
    });
    if (!o.isPresent()) {
        return Response.status(Status.NOT_FOUND).build();
    }
    Path tmp = o.get();
    return Response.ok((StreamingOutput) out -> {
        try (InputStream in = Files.newInputStream(tmp)) {
            IOUtils.copy(in, out);
        } finally {
            Files.delete(tmp);
        }
    }).build();
}
Also used : Path(java.nio.file.Path) PartialProcessKey(com.walmartlabs.concord.server.sdk.PartialProcessKey) ConcordApplicationException(com.walmartlabs.concord.server.sdk.ConcordApplicationException) ZipArchiveOutputStream(org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream) PartialProcessKey(com.walmartlabs.concord.server.sdk.PartialProcessKey) ProcessKey(com.walmartlabs.concord.server.sdk.ProcessKey) ApiOperation(io.swagger.annotations.ApiOperation)

Example 33 with ProcessKey

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

the class PayloadBuilder method with.

/**
 * Parses the provided {@code input} using the following logic:
 * <ul>
 *     <li>part names should not start with '/' or contain '..'</li>
 *     <li>any {@code text/plain} is converted into the process' configuration value.
 *     Dots '.' in the part's name are used as the property's path. E.g. {@code x.y.z} is
 *     converted into a nested Map object {@code {"x": {"y": {"z": ...}}}}</li>
 *     <li>parts of other types are saved as files in the payload's {@code ${workDir}}.
 *     The part's name will be used as the file's path.</li>
 * </ul>
 */
public PayloadBuilder with(MultipartInput input) throws IOException {
    Map<String, Object> cfg = payload.getHeader(Payload.CONFIGURATION);
    cfg = new HashMap<>(cfg != null ? cfg : Collections.emptyMap());
    ProcessKey pk = payload.getProcessKey();
    for (InputPart p : input.getParts()) {
        String name = MultipartUtils.extractName(p);
        if (name == null || name.startsWith("/") || name.contains("..")) {
            throw new ProcessException(pk, "Invalid attachment name: " + name, Response.Status.BAD_REQUEST);
        }
        if (p.getMediaType().isCompatible(MediaType.TEXT_PLAIN_TYPE)) {
            String v = p.getBodyAsString().trim();
            Map<String, Object> m = ConfigurationUtils.toNested(name, v);
            cfg = ConfigurationUtils.deepMerge(cfg, m);
        } else {
            try (InputStream in = p.getBody(InputStream.class, null)) {
                addAttachment(name, in);
            }
        }
    }
    payload = payload.putHeader(Payload.CONFIGURATION, cfg).putAttachments(attachments);
    return this;
}
Also used : InputPart(org.jboss.resteasy.plugins.providers.multipart.InputPart) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ProcessKey(com.walmartlabs.concord.server.sdk.ProcessKey) PartialProcessKey(com.walmartlabs.concord.server.sdk.PartialProcessKey)

Example 34 with ProcessKey

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

the class ProcessQueueManager method insert.

/**
 * Creates the initial queue record for the specified process payload.
 */
public void insert(Payload payload, ProcessStatus status) {
    ProcessKey processKey = payload.getProcessKey();
    ProcessKind kind = payload.getHeader(Payload.PROCESS_KIND, ProcessKind.DEFAULT);
    UUID projectId = payload.getHeader(Payload.PROJECT_ID);
    UUID repoId = payload.getHeader(Payload.REPOSITORY_ID);
    UUID parentInstanceId = payload.getHeader(Payload.PARENT_INSTANCE_ID);
    UUID initiatorId = payload.getHeader(Payload.INITIATOR_ID);
    Map<String, Object> cfg = getCfg(payload);
    Map<String, Object> meta = getMeta(cfg);
    TriggeredByEntry triggeredBy = payload.getHeader(Payload.TRIGGERED_BY);
    String branchOrTag = MapUtils.getString(cfg, Constants.Request.REPO_BRANCH_OR_TAG);
    String commitId = MapUtils.getString(cfg, Constants.Request.REPO_COMMIT_ID);
    queueDao.tx(tx -> {
        queueDao.insert(tx, processKey, status, kind, parentInstanceId, projectId, repoId, branchOrTag, commitId, initiatorId, meta, triggeredBy);
        notifyStatusChange(tx, processKey, status);
        processLogManager.createSystemSegment(tx, payload.getProcessKey());
    });
}
Also used : ProcessKey(com.walmartlabs.concord.server.sdk.ProcessKey) PartialProcessKey(com.walmartlabs.concord.server.sdk.PartialProcessKey)

Example 35 with ProcessKey

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

the class ProcessQueueManager method enqueue.

/**
 * Updates an existing record, moving the process into the ENQUEUED status.
 */
public boolean enqueue(Payload payload) {
    ProcessKey processKey = payload.getProcessKey();
    ProcessStatus s = queueDao.getStatus(processKey);
    if (s == null) {
        throw new ProcessException(processKey, "Process not found: " + processKey);
    }
    if (s == ProcessStatus.CANCELLED) {
        // (e.g. it was a process in an "exclusive" group)
        return false;
    }
    if (!TO_ENQUEUED_STATUSES.contains(s)) {
        // something's wrong (e.g. someone tried to change the process' status directly in the DB and was unlucky)
        throw new ProcessException(processKey, "Invalid process status: " + s);
    }
    Set<String> tags = payload.getHeader(Payload.PROCESS_TAGS);
    OffsetDateTime startAt = PayloadUtils.getStartAt(payload);
    Map<String, Object> requirements = PayloadUtils.getRequirements(payload);
    Long processTimeout = getProcessTimeout(payload);
    Long suspendTimeout = getSuspendTimeout(payload);
    Set<String> handlers = payload.getHeader(Payload.PROCESS_HANDLERS);
    Map<String, Object> meta = getMeta(getCfg(payload));
    Imports imports = payload.getHeader(Payload.IMPORTS);
    ExclusiveMode exclusive = PayloadUtils.getExclusive(payload);
    String runtime = payload.getHeader(Payload.RUNTIME);
    List<String> dependencies = payload.getHeader(Payload.DEPENDENCIES);
    return queueDao.txResult(tx -> {
        boolean updated = queueDao.enqueue(tx, processKey, tags, startAt, requirements, processTimeout, handlers, meta, imports, exclusive, runtime, dependencies, suspendTimeout, TO_ENQUEUED_STATUSES);
        if (updated) {
            notifyStatusChange(tx, processKey, ProcessStatus.ENQUEUED);
        }
        return updated;
    });
}
Also used : ProcessStatus(com.walmartlabs.concord.server.sdk.ProcessStatus) ProcessKey(com.walmartlabs.concord.server.sdk.ProcessKey) PartialProcessKey(com.walmartlabs.concord.server.sdk.PartialProcessKey) Imports(com.walmartlabs.concord.imports.Imports) ExclusiveMode(com.walmartlabs.concord.runtime.v2.model.ExclusiveMode) OffsetDateTime(java.time.OffsetDateTime)

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