Search in sources :

Example 1 with SlaveNotFoundException

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

the class SandboxResource method read.

@GET
@Path("/{taskId}/read")
@ApiOperation("Retrieve part of the contents of a file in a specific task's sandbox.")
public MesosFileChunkObject read(@Auth SingularityUser user, @ApiParam("The task ID of the sandbox to read from") @PathParam("taskId") String taskId, @ApiParam("The path to the file to be read") @QueryParam("path") String path, @ApiParam("Optional string to grep for") @QueryParam("grep") Optional<String> grep, @ApiParam("Byte offset to start reading from") @QueryParam("offset") Optional<Long> offset, @ApiParam("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 slaveHostname = history.getTask().getHostname();
    final String fullPath = new File(history.getDirectory().get(), path).toString();
    try {
        final Optional<MesosFileChunkObject> maybeChunk = sandboxManager.read(slaveHostname, 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 (SlaveNotFoundException snfe) {
        throw notFound("Slave @ %s was not found, it is probably offline", slaveHostname);
    }
}
Also used : Pattern(java.util.regex.Pattern) SingularityTaskHistory(com.hubspot.singularity.SingularityTaskHistory) MesosFileChunkObject(com.hubspot.mesos.json.MesosFileChunkObject) File(java.io.File) SingularitySandboxFile(com.hubspot.singularity.SingularitySandboxFile) SlaveNotFoundException(com.hubspot.singularity.data.SandboxManager.SlaveNotFoundException) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET) ApiOperation(com.wordnik.swagger.annotations.ApiOperation)

Example 2 with SlaveNotFoundException

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

the class SandboxResource method browse.

@GET
@Path("/{taskId}/browse")
@ApiOperation("Retrieve information about a specific task's sandbox.")
public SingularitySandbox browse(@Auth SingularityUser user, @ApiParam("The task ID to browse") @PathParam("taskId") String taskId, @ApiParam("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 slaveHostname = 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(slaveHostname, 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, slaveHostname);
    } catch (SlaveNotFoundException snfe) {
        throw notFound("Slave @ %s was not found, it is probably offline", slaveHostname);
    }
}
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) File(java.io.File) SingularitySandboxFile(com.hubspot.singularity.SingularitySandboxFile) SlaveNotFoundException(com.hubspot.singularity.data.SandboxManager.SlaveNotFoundException) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET) ApiOperation(com.wordnik.swagger.annotations.ApiOperation)

Aggregations

SingularitySandboxFile (com.hubspot.singularity.SingularitySandboxFile)2 SingularityTaskHistory (com.hubspot.singularity.SingularityTaskHistory)2 SlaveNotFoundException (com.hubspot.singularity.data.SandboxManager.SlaveNotFoundException)2 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)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 SingularitySandbox (com.hubspot.singularity.SingularitySandbox)1 Pattern (java.util.regex.Pattern)1