Search in sources :

Example 41 with ProcessKey

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

the class ProcessResource method fork.

/**
 * Starts a new child process by forking the start of the specified parent process.
 *
 * @param parentInstanceId
 * @param req
 * @param sync
 * @return
 */
@POST
@ApiOperation("Fork a process")
@javax.ws.rs.Path("/{id}/fork")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@WithTimer
public StartProcessResponse fork(@ApiParam @PathParam("id") UUID parentInstanceId, @ApiParam Map<String, Object> req, @ApiParam @Deprecated @DefaultValue("false") @QueryParam("sync") boolean sync, @ApiParam @QueryParam("out") String[] out) {
    if (sync) {
        throw syncIsForbidden();
    }
    ProcessEntry parent = processQueueManager.get(PartialProcessKey.from(parentInstanceId));
    if (parent == null) {
        throw new ValidationErrorsException("Unknown parent instance ID: " + parentInstanceId);
    }
    PartialProcessKey processKey = PartialProcessKey.from(UUID.randomUUID());
    ProcessKey parentProcessKey = new ProcessKey(parent.instanceId(), parent.createdAt());
    UUID projectId = parent.projectId();
    UserPrincipal userPrincipal = UserPrincipal.assertCurrent();
    Set<String> handlers = parent.handlers();
    Imports imports = queueDao.getImports(parentProcessKey);
    Payload payload;
    try {
        payload = payloadManager.createFork(processKey, parentProcessKey, ProcessKind.DEFAULT, userPrincipal.getId(), userPrincipal.getUsername(), projectId, req, out, handlers, imports);
    } catch (IOException e) {
        log.error("fork ['{}', '{}'] -> error creating a payload: {}", processKey, parentProcessKey, e);
        throw new ConcordApplicationException("Error creating a payload", e);
    }
    return toResponse(processManager.startFork(payload));
}
Also used : PartialProcessKey(com.walmartlabs.concord.server.sdk.PartialProcessKey) ConcordApplicationException(com.walmartlabs.concord.server.sdk.ConcordApplicationException) PartialProcessKey(com.walmartlabs.concord.server.sdk.PartialProcessKey) ProcessKey(com.walmartlabs.concord.server.sdk.ProcessKey) Imports(com.walmartlabs.concord.imports.Imports) ValidationErrorsException(org.sonatype.siesta.ValidationErrorsException) UserPrincipal(com.walmartlabs.concord.server.security.UserPrincipal) WithTimer(com.walmartlabs.concord.server.sdk.metrics.WithTimer) ApiOperation(io.swagger.annotations.ApiOperation)

Example 42 with ProcessKey

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

the class ProcessResource method downloadStateFile.

/**
 * Downloads a single file from the current state snapshot of a process.
 */
@GET
@ApiOperation(value = "Download a single file from a process state snapshot", response = File.class)
@javax.ws.rs.Path("/{id}/state/snapshot/{name:.*}")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
@Validate
public Response downloadStateFile(@ApiParam @PathParam("id") UUID instanceId, @ApiParam @PathParam("name") @NotNull @Size(min = 1) String fileName) {
    ProcessEntry p = assertProcess(PartialProcessKey.from(instanceId));
    ProcessKey processKey = new ProcessKey(p.instanceId(), p.createdAt());
    assertProcessAccess(p, "state");
    StreamingOutput out = output -> {
        Path tmp = stateManager.get(processKey, fileName, ProcessResource::copyToTmp).orElseThrow(() -> new ConcordApplicationException("State file not found: " + fileName, Status.NOT_FOUND));
        try (InputStream in = Files.newInputStream(tmp)) {
            IOUtils.copy(in, output);
        } finally {
            Files.delete(tmp);
        }
    };
    return Response.ok(out).build();
}
Also used : ProcessWaitManager(com.walmartlabs.concord.server.process.waits.ProcessWaitManager) Resource(org.sonatype.siesta.Resource) Size(javax.validation.constraints.Size) ProcessLogAccessManager(com.walmartlabs.concord.server.process.logs.ProcessLogAccessManager) PolicyManager(com.walmartlabs.concord.server.policy.PolicyManager) LoggerFactory(org.slf4j.LoggerFactory) ProcessResult(com.walmartlabs.concord.server.process.ProcessManager.ProcessResult) ApiParam(io.swagger.annotations.ApiParam) MultipartInput(org.jboss.resteasy.plugins.providers.multipart.MultipartInput) ProcessKeyCache(com.walmartlabs.concord.server.process.queue.ProcessKeyCache) ApiOperation(io.swagger.annotations.ApiOperation) ProcessQueueDao(com.walmartlabs.concord.server.process.queue.ProcessQueueDao) WithTimer(com.walmartlabs.concord.server.sdk.metrics.WithTimer) ProcessWaitEntry(com.walmartlabs.concord.server.process.ProcessEntry.ProcessWaitEntry) Path(java.nio.file.Path) PartialProcessKey(com.walmartlabs.concord.server.sdk.PartialProcessKey) ProcessConfiguration(com.walmartlabs.concord.server.cfg.ProcessConfiguration) ProcessLogManager(com.walmartlabs.concord.server.process.logs.ProcessLogManager) IOUtils(com.walmartlabs.concord.common.IOUtils) EncryptedProjectValueManager(com.walmartlabs.concord.server.org.project.EncryptedProjectValueManager) ProcessStatus(com.walmartlabs.concord.server.sdk.ProcessStatus) NotNull(javax.validation.constraints.NotNull) Collectors(java.util.stream.Collectors) SecretStoreConfiguration(com.walmartlabs.concord.server.cfg.SecretStoreConfiguration) UnauthorizedException(org.apache.shiro.authz.UnauthorizedException) javax.ws.rs(javax.ws.rs) ValidationErrorsException(org.sonatype.siesta.ValidationErrorsException) CheckResult(com.walmartlabs.concord.policyengine.CheckResult) HttpUtils(com.walmartlabs.concord.server.HttpUtils) java.util(java.util) AbstractWaitCondition(com.walmartlabs.concord.server.process.waits.AbstractWaitCondition) ProcessQueueManager(com.walmartlabs.concord.server.process.queue.ProcessQueueManager) AttachmentsRule(com.walmartlabs.concord.policyengine.AttachmentsRule) ProcessKey(com.walmartlabs.concord.server.sdk.ProcessKey) Singleton(javax.inject.Singleton) ConfigurationUtils(com.walmartlabs.concord.common.ConfigurationUtils) StandardCopyOption(java.nio.file.StandardCopyOption) MessageFormat(java.text.MessageFormat) ProcessStateManager.zipTo(com.walmartlabs.concord.server.process.state.ProcessStateManager.zipTo) Inject(javax.inject.Inject) MultipartUtils(com.walmartlabs.concord.server.MultipartUtils) ExpressionUtils(com.walmartlabs.concord.server.events.ExpressionUtils) EntryPoint(com.walmartlabs.concord.server.process.PayloadManager.EntryPoint) HttpServletRequest(javax.servlet.http.HttpServletRequest) Constants(com.walmartlabs.concord.sdk.Constants) PolicyEngine(com.walmartlabs.concord.policyengine.PolicyEngine) Imports(com.walmartlabs.concord.imports.Imports) OffsetDateTimeParam(com.walmartlabs.concord.server.OffsetDateTimeParam) Api(io.swagger.annotations.Api) ZipArchiveOutputStream(org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream) Named(javax.inject.Named) Status(javax.ws.rs.core.Response.Status) ProcessStatusHistoryEntry(com.walmartlabs.concord.server.process.ProcessEntry.ProcessStatusHistoryEntry) ProcessLog(com.walmartlabs.concord.server.process.logs.ProcessLogsDao.ProcessLog) Roles(com.walmartlabs.concord.server.security.Roles) UserPrincipal(com.walmartlabs.concord.server.security.UserPrincipal) ProcessStateManager(com.walmartlabs.concord.server.process.state.ProcessStateManager) Logger(org.slf4j.Logger) Files(java.nio.file.Files) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Validate(org.sonatype.siesta.Validate) PolicyException(com.walmartlabs.concord.server.policy.PolicyException) ProjectAccessManager(com.walmartlabs.concord.server.org.project.ProjectAccessManager) javax.ws.rs.core(javax.ws.rs.core) ProcessFilter(com.walmartlabs.concord.server.process.queue.ProcessFilter) OrganizationManager(com.walmartlabs.concord.server.org.OrganizationManager) java.io(java.io) ProcessStateManager.path(com.walmartlabs.concord.server.process.state.ProcessStateManager.path) ConcordApplicationException(com.walmartlabs.concord.server.sdk.ConcordApplicationException) ResourceAccessLevel(com.walmartlabs.concord.server.org.ResourceAccessLevel) Authorization(io.swagger.annotations.Authorization) Path(java.nio.file.Path) ConcordApplicationException(com.walmartlabs.concord.server.sdk.ConcordApplicationException) PartialProcessKey(com.walmartlabs.concord.server.sdk.PartialProcessKey) ProcessKey(com.walmartlabs.concord.server.sdk.ProcessKey) Validate(org.sonatype.siesta.Validate) ApiOperation(io.swagger.annotations.ApiOperation)

Example 43 with ProcessKey

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

the class ProcessResource method assertAttachmentsPolicy.

private void assertAttachmentsPolicy(Path tmpDir, ProcessEntry entry) throws IOException {
    PolicyEngine policy = policyManager.get(entry.orgId(), entry.projectId(), UserPrincipal.assertCurrent().getUser().getId());
    if (policy == null) {
        return;
    }
    CheckResult<AttachmentsRule, Long> checkResult = policy.getAttachmentsPolicy().check(tmpDir);
    if (!checkResult.getDeny().isEmpty()) {
        String errorMessage = buildErrorMessage(checkResult.getDeny());
        processLogManager.error(new ProcessKey(entry.instanceId(), entry.createdAt()), errorMessage);
        throw new PolicyException("Found forbidden policy: " + errorMessage);
    }
}
Also used : PolicyException(com.walmartlabs.concord.server.policy.PolicyException) PolicyEngine(com.walmartlabs.concord.policyengine.PolicyEngine) PartialProcessKey(com.walmartlabs.concord.server.sdk.PartialProcessKey) ProcessKey(com.walmartlabs.concord.server.sdk.ProcessKey) AttachmentsRule(com.walmartlabs.concord.policyengine.AttachmentsRule)

Example 44 with ProcessKey

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

the class ProcessResource method getLog.

/**
 * Retrieves a process' log.
 *
 * @param instanceId
 * @param rangeHeader
 * @return
 * @see ProcessLogResourceV2
 * @deprecated in favor of the /api/v2/process/{id}/log* endpoints
 */
@GET
@ApiOperation(value = "Retrieve the log")
@javax.ws.rs.Path("/{id}/log")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
@WithTimer
@Deprecated
public Response getLog(@ApiParam @PathParam("id") UUID instanceId, @HeaderParam("range") String rangeHeader) {
    // check the permissions, logs can contain sensitive data
    ProcessKey processKey = logAccessManager.assertLogAccess(instanceId);
    HttpUtils.Range range = HttpUtils.parseRangeHeaderValue(rangeHeader);
    ProcessLog l = logManager.get(processKey, range.start(), range.end());
    return ProcessLogResourceV2.toResponse(instanceId, 0, l, range);
}
Also used : HttpUtils(com.walmartlabs.concord.server.HttpUtils) PartialProcessKey(com.walmartlabs.concord.server.sdk.PartialProcessKey) ProcessKey(com.walmartlabs.concord.server.sdk.ProcessKey) ProcessLog(com.walmartlabs.concord.server.process.logs.ProcessLogsDao.ProcessLog) WithTimer(com.walmartlabs.concord.server.sdk.metrics.WithTimer) ApiOperation(io.swagger.annotations.ApiOperation)

Example 45 with ProcessKey

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

the class ProcessResource method kill.

/**
 * Forcefully stops a process.
 *
 * @param instanceId
 */
@DELETE
@ApiOperation("Forcefully stops a process")
@javax.ws.rs.Path("/{id}")
@WithTimer
public void kill(@ApiParam @PathParam("id") UUID instanceId) {
    ProcessKey processKey = assertProcessKey(instanceId);
    processManager.kill(processKey);
}
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)

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