Search in sources :

Example 21 with ProcessKey

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

the class ProcessResource method uploadAttachments.

/**
 * Upload process attachments.
 *
 * @param instanceId
 * @param data
 */
@POST
@javax.ws.rs.Path("{id}/attachment")
@Consumes(MediaType.APPLICATION_OCTET_STREAM)
public void uploadAttachments(@PathParam("id") UUID instanceId, InputStream data) {
    ProcessEntry entry = assertProcess(PartialProcessKey.from(instanceId));
    ProcessKey processKey = new ProcessKey(entry.instanceId(), entry.createdAt());
    Path tmpIn = null;
    Path tmpDir = null;
    try {
        tmpIn = IOUtils.createTempFile("attachments", ".zip");
        Files.copy(data, tmpIn, StandardCopyOption.REPLACE_EXISTING);
        tmpDir = IOUtils.createTempDir("attachments");
        IOUtils.unzip(tmpIn, tmpDir);
        assertAttachmentsPolicy(tmpDir, entry);
        Path finalTmpDir = tmpDir;
        stateManager.tx(tx -> {
            stateManager.deleteDirectory(tx, processKey, path(Constants.Files.JOB_ATTACHMENTS_DIR_NAME, Constants.Files.JOB_STATE_DIR_NAME));
            stateManager.importPath(tx, processKey, Constants.Files.JOB_ATTACHMENTS_DIR_NAME, finalTmpDir, (p, attrs) -> true);
        });
        Map<String, Object> out = OutVariablesUtils.read(tmpDir);
        if (out.isEmpty()) {
            queueDao.removeMeta(processKey, "out");
        } else {
            queueDao.updateMeta(processKey, Collections.singletonMap("out", out));
        }
    } catch (PolicyException e) {
        throw new ConcordApplicationException(e.getMessage(), Status.FORBIDDEN);
    } catch (IOException e) {
        log.error("uploadAttachments ['{}'] -> error", processKey, e);
        throw new ConcordApplicationException("upload error: " + e.getMessage());
    } finally {
        if (tmpDir != null) {
            try {
                IOUtils.deleteRecursively(tmpDir);
            } catch (IOException e) {
                log.warn("uploadAttachments -> cleanup error: {}", e.getMessage());
            }
        }
        if (tmpIn != null) {
            try {
                Files.delete(tmpIn);
            } catch (IOException e) {
                log.warn("uploadAttachments -> cleanup error: {}", e.getMessage());
            }
        }
    }
}
Also used : Path(java.nio.file.Path) PolicyException(com.walmartlabs.concord.server.policy.PolicyException) ConcordApplicationException(com.walmartlabs.concord.server.sdk.ConcordApplicationException) PartialProcessKey(com.walmartlabs.concord.server.sdk.PartialProcessKey) ProcessKey(com.walmartlabs.concord.server.sdk.ProcessKey)

Example 22 with ProcessKey

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

the class ProcessResource method updateStatus.

/**
 * Updates a process' status
 *
 * @param instanceId
 * @param status
 */
@POST
@ApiOperation("Update process status")
@javax.ws.rs.Path("{id}/status")
@Consumes(MediaType.TEXT_PLAIN)
@WithTimer
public void updateStatus(@ApiParam @PathParam("id") UUID instanceId, @ApiParam(required = true) @QueryParam("agentId") String agentId, @ApiParam(required = true) ProcessStatus status) {
    ProcessKey processKey = assertProcessKey(instanceId);
    processManager.updateStatus(processKey, agentId, status);
}
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 23 with ProcessKey

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

the class ProcessCheckpointResource method restore.

@POST
@ApiOperation(value = "Restore process from checkpoint")
@javax.ws.rs.Path("{id}/checkpoint/restore")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@WithTimer
@Validate
public ResumeProcessResponse restore(@ApiParam @PathParam("id") UUID instanceId, @ApiParam @Valid RestoreCheckpointRequest request) {
    UUID checkpointId = request.getId();
    // TODO replace with ProcessKeyCache
    ProcessEntry entry = processManager.assertProcess(instanceId);
    ProcessKey processKey = new ProcessKey(entry.instanceId(), entry.createdAt());
    processManager.restoreFromCheckpoint(processKey, checkpointId);
    return new ResumeProcessResponse();
}
Also used : ProcessEntry(com.walmartlabs.concord.server.process.ProcessEntry) ProcessKey(com.walmartlabs.concord.server.sdk.ProcessKey) UUID(java.util.UUID) ResumeProcessResponse(com.walmartlabs.concord.server.process.ResumeProcessResponse) Validate(org.sonatype.siesta.Validate) WithTimer(com.walmartlabs.concord.server.sdk.metrics.WithTimer) ApiOperation(io.swagger.annotations.ApiOperation)

Example 24 with ProcessKey

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

the class ProcessCheckpointResource method uploadCheckpoint.

@POST
@javax.ws.rs.Path("{id}/checkpoint")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public void uploadCheckpoint(@PathParam("id") UUID instanceId, @ApiParam MultipartInput input) {
    // TODO replace with ProcessKeyCache
    ProcessEntry entry = processManager.assertProcess(instanceId);
    ProcessKey processKey = new ProcessKey(entry.instanceId(), entry.createdAt());
    UUID checkpointId = MultipartUtils.assertUuid(input, "id");
    UUID correlationId = MultipartUtils.assertUuid(input, "correlationId");
    String checkpointName = MultipartUtils.assertString(input, "name");
    try (InputStream data = MultipartUtils.assertStream(input, "data");
        TemporaryPath tmpIn = IOUtils.tempFile("checkpoint", ".zip")) {
        Files.copy(data, tmpIn.path(), StandardCopyOption.REPLACE_EXISTING);
        checkpointManager.importCheckpoint(processKey, checkpointId, correlationId, checkpointName, tmpIn.path());
    } catch (ValidationErrorsException e) {
        throw new ConcordApplicationException(e.getMessage(), Response.Status.BAD_REQUEST);
    } catch (IOException e) {
        log.error("uploadCheckpoint ['{}'] -> error", processKey, e);
        throw new ConcordApplicationException("upload error: " + e.getMessage());
    }
    log.info("uploadCheckpoint ['{}'] -> done", processKey);
}
Also used : InputStream(java.io.InputStream) ConcordApplicationException(com.walmartlabs.concord.server.sdk.ConcordApplicationException) ProcessEntry(com.walmartlabs.concord.server.process.ProcessEntry) ProcessKey(com.walmartlabs.concord.server.sdk.ProcessKey) IOException(java.io.IOException) UUID(java.util.UUID) TemporaryPath(com.walmartlabs.concord.common.TemporaryPath) ValidationErrorsException(org.sonatype.siesta.ValidationErrorsException)

Example 25 with ProcessKey

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

the class ProcessCheckpointResource method list.

@GET
@ApiOperation(value = "List the process checkpoints", responseContainer = "list", response = ProcessCheckpointEntry.class)
@javax.ws.rs.Path("{id}/checkpoint")
@Produces(MediaType.APPLICATION_JSON)
@WithTimer
public List<ProcessCheckpointEntry> list(@ApiParam @PathParam("id") UUID instanceId) {
    ProcessEntry entry = processManager.assertProcess(instanceId);
    ProcessKey processKey = new ProcessKey(entry.instanceId(), entry.createdAt());
    checkpointManager.assertProcessAccess(entry);
    return checkpointManager.list(processKey);
}
Also used : ProcessEntry(com.walmartlabs.concord.server.process.ProcessEntry) ProcessKey(com.walmartlabs.concord.server.sdk.ProcessKey) WithTimer(com.walmartlabs.concord.server.sdk.metrics.WithTimer) ApiOperation(io.swagger.annotations.ApiOperation)

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