Search in sources :

Example 1 with MesosFileObject

use of com.hubspot.mesos.json.MesosFileObject 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

Function (com.google.common.base.Function)1 MesosFileObject (com.hubspot.mesos.json.MesosFileObject)1 SingularitySandbox (com.hubspot.singularity.SingularitySandbox)1 SingularitySandboxFile (com.hubspot.singularity.SingularitySandboxFile)1 SingularityTaskHistory (com.hubspot.singularity.SingularityTaskHistory)1 SlaveNotFoundException (com.hubspot.singularity.data.SandboxManager.SlaveNotFoundException)1 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)1 File (java.io.File)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1