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);
}
}
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);
}
}
}
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);
}
}
Aggregations