Search in sources :

Example 1 with AgentNotFoundException

use of com.hubspot.singularity.data.SandboxManager.AgentNotFoundException in project Singularity by HubSpot.

the class SandboxResource method read.

@GET
@Path("/{taskId}/read")
@Operation(summary = "Retrieve part of the contents of a file in a specific task's sandbox", responses = { @ApiResponse(responseCode = "404", description = "An agent, task, or file with the specified id was not found") })
public MesosFileChunkObject read(@Parameter(hidden = true) @Auth SingularityUser user, @Parameter(required = true, description = "The task ID of the sandbox to read from") @PathParam("taskId") String taskId, @Parameter(required = true, description = "The path to the file to be read") @QueryParam("path") String path, @Parameter(description = "Optional string to grep for") @QueryParam("grep") Optional<String> grep, @Parameter(description = "Byte offset to start reading from") @QueryParam("offset") Optional<Long> offset, @Parameter(description = "Maximum number of bytes to read") @QueryParam("length") Optional<Long> length) {
    authorizationHelper.checkForAuthorizationByTaskId(taskId, user, SingularityAuthorizationScope.READ);
    final SingularityTaskHistory history = checkHistory(taskId, user);
    checkBadRequest(!Strings.isNullOrEmpty(path), "Must specify 'path'");
    final String hostname = history.getTask().getHostname();
    final String fullPath = new File(history.getDirectory().get(), path).toString();
    try {
        final Optional<MesosFileChunkObject> maybeChunk = sandboxManager.read(hostname, fullPath, offset, length);
        checkNotFound(maybeChunk.isPresent(), "File %s does not exist for task ID %s", fullPath, taskId);
        if (grep.isPresent() && !Strings.isNullOrEmpty(grep.get())) {
            final Pattern grepPattern = Pattern.compile(grep.get());
            final StringBuilder strBuilder = new StringBuilder(maybeChunk.get().getData().length());
            for (String line : Splitter.on("\n").split(maybeChunk.get().getData())) {
                if (grepPattern.matcher(line).find()) {
                    strBuilder.append(line);
                    strBuilder.append("\n");
                }
            }
            return new MesosFileChunkObject(strBuilder.toString(), maybeChunk.get().getOffset(), Optional.of(maybeChunk.get().getOffset() + maybeChunk.get().getData().length()));
        }
        return maybeChunk.get();
    } catch (AgentNotFoundException snfe) {
        throw notFound("Slave @ %s was not found, it is probably offline", hostname);
    }
}
Also used : Pattern(java.util.regex.Pattern) SingularityTaskHistory(com.hubspot.singularity.SingularityTaskHistory) MesosFileChunkObject(com.hubspot.mesos.json.MesosFileChunkObject) AgentNotFoundException(com.hubspot.singularity.data.SandboxManager.AgentNotFoundException) File(java.io.File) SingularitySandboxFile(com.hubspot.singularity.SingularitySandboxFile) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET) Operation(io.swagger.v3.oas.annotations.Operation)

Example 2 with AgentNotFoundException

use of com.hubspot.singularity.data.SandboxManager.AgentNotFoundException in project Singularity by HubSpot.

the class TaskResource method getFile.

private Response getFile(String agentHostname, String fileFullPath, boolean download) {
    String httpPrefix = configuration.getAgentHttpsPort().isPresent() ? "https" : "http";
    int httpPort = configuration.getAgentHttpsPort().isPresent() ? configuration.getAgentHttpsPort().get() : configuration.getAgentHttpPort();
    String url = String.format("%s://%s:%s/files/download", httpPrefix, agentHostname, httpPort);
    try {
        NingOutputToJaxRsStreamingOutputWrapper streamingOutputNingHandler = new NingOutputToJaxRsStreamingOutputWrapper(httpClient.prepareGet(url).addQueryParam("path", fileFullPath).setRequestTimeout(-1));
        // Strip file path down to just a file name if we can
        java.nio.file.Path filePath = Paths.get(fileFullPath).getFileName();
        String fileName = filePath != null ? filePath.toString() : fileFullPath;
        ResponseBuilder responseBuilder = Response.ok(streamingOutputNingHandler);
        if (download) {
            final String headerValue = String.format("attachment; filename=\"%s\"", fileName);
            responseBuilder.header("Content-Disposition", headerValue);
        } else {
            // Guess type based on extension since we don't have the file locally to check content
            final String maybeContentType = fileTypeMap.getContentType(fileFullPath);
            responseBuilder.header("Content-Type", maybeContentType);
        }
        return responseBuilder.build();
    } catch (Exception e) {
        if (e.getCause().getClass() == ConnectException.class) {
            throw new AgentNotFoundException(e);
        } else {
            throw new RuntimeException(e);
        }
    }
}
Also used : AgentNotFoundException(com.hubspot.singularity.data.SandboxManager.AgentNotFoundException) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) InvalidSingularityTaskIdException(com.hubspot.singularity.InvalidSingularityTaskIdException) WebApplicationException(javax.ws.rs.WebApplicationException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) AgentNotFoundException(com.hubspot.singularity.data.SandboxManager.AgentNotFoundException) ConnectException(java.net.ConnectException)

Example 3 with AgentNotFoundException

use of com.hubspot.singularity.data.SandboxManager.AgentNotFoundException in project Singularity by HubSpot.

the class SandboxResource method browse.

@GET
@Path("/{taskId}/browse")
@Operation(summary = "Retrieve information about a specific task's sandbox", responses = { @ApiResponse(responseCode = "404", description = "An agent or task with the specified id was not found") })
public SingularitySandbox browse(@Parameter(hidden = true) @Auth SingularityUser user, @Parameter(required = true, description = "The task ID to browse") @PathParam("taskId") String taskId, @Parameter(required = true, description = "The path to browse from") @QueryParam("path") String path) {
    authorizationHelper.checkForAuthorizationByTaskId(taskId, user, SingularityAuthorizationScope.READ);
    // Remove all trailing slashes from the path
    if (path != null) {
        path = path.replaceAll("\\/+$", "");
    }
    final String currentDirectory = getCurrentDirectory(taskId, path);
    final SingularityTaskHistory history = checkHistory(taskId, user);
    final String hostname = history.getTask().getHostname();
    final String pathToRoot = history.getDirectory().get();
    final String fullPath = new File(pathToRoot, currentDirectory).toString();
    final int substringTruncationLength = currentDirectory.length() == 0 ? pathToRoot.length() + 1 : pathToRoot.length() + currentDirectory.length() + 2;
    try {
        Collection<MesosFileObject> mesosFiles = sandboxManager.browse(hostname, fullPath);
        List<SingularitySandboxFile> sandboxFiles = Lists.newArrayList(Iterables.transform(mesosFiles, new Function<MesosFileObject, SingularitySandboxFile>() {

            @Override
            public SingularitySandboxFile apply(MesosFileObject input) {
                return new SingularitySandboxFile(input.getPath().substring(substringTruncationLength), input.getMtime(), input.getSize(), input.getMode());
            }
        }));
        return new SingularitySandbox(sandboxFiles, pathToRoot, currentDirectory, hostname);
    } catch (AgentNotFoundException snfe) {
        throw notFound("Slave @ %s was not found, it is probably offline", hostname);
    }
}
Also used : SingularitySandbox(com.hubspot.singularity.SingularitySandbox) SingularitySandboxFile(com.hubspot.singularity.SingularitySandboxFile) Function(com.google.common.base.Function) MesosFileObject(com.hubspot.mesos.json.MesosFileObject) SingularityTaskHistory(com.hubspot.singularity.SingularityTaskHistory) AgentNotFoundException(com.hubspot.singularity.data.SandboxManager.AgentNotFoundException) File(java.io.File) SingularitySandboxFile(com.hubspot.singularity.SingularitySandboxFile) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET) Operation(io.swagger.v3.oas.annotations.Operation)

Aggregations

AgentNotFoundException (com.hubspot.singularity.data.SandboxManager.AgentNotFoundException)3 SingularitySandboxFile (com.hubspot.singularity.SingularitySandboxFile)2 SingularityTaskHistory (com.hubspot.singularity.SingularityTaskHistory)2 Operation (io.swagger.v3.oas.annotations.Operation)2 File (java.io.File)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 Function (com.google.common.base.Function)1 MesosFileChunkObject (com.hubspot.mesos.json.MesosFileChunkObject)1 MesosFileObject (com.hubspot.mesos.json.MesosFileObject)1 InvalidSingularityTaskIdException (com.hubspot.singularity.InvalidSingularityTaskIdException)1 SingularitySandbox (com.hubspot.singularity.SingularitySandbox)1 IOException (java.io.IOException)1 ConnectException (java.net.ConnectException)1 ExecutionException (java.util.concurrent.ExecutionException)1 Pattern (java.util.regex.Pattern)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)1