use of org.platformlayer.ops.process.ProcessExecution in project platformlayer by platformlayer.
the class DiskImageController method waitForTarget.
private String waitForTarget(final OpsTarget target) throws OpsException {
// Maybe the config CD isn't yet mounted? Maybe SSH isn't yet started? Not sure yet..
try {
log.info("Sleeping in the hope of avoiding (not fully understood) SSH issue");
Thread.sleep(15000);
} catch (InterruptedException e) {
ExceptionUtils.handleInterrupted(e);
throw new OpsException("Interrupted", e);
}
try {
return TimeoutPoll.poll(TimeSpan.FIVE_MINUTES, TimeSpan.TEN_SECONDS, new PollFunction<String>() {
@Override
public String call() throws Exception {
try {
ProcessExecution execution = target.executeCommand("uname -srp");
return execution.getStdOut();
} catch (ProcessExecutionException e) {
log.info("Waiting for machine; got process execution error", e);
return null;
}
}
});
} catch (ExecutionException e) {
throw new OpsException("Error while waiting for machine", e);
} catch (TimeoutException e) {
throw new OpsException("Timeout while waiting for machine", e);
}
}
use of org.platformlayer.ops.process.ProcessExecution in project platformlayer by platformlayer.
the class OpsTargetBase method getFileHash.
@Override
public Md5Hash getFileHash(File filePath) throws OpsException {
Command command = Command.build("md5sum {0}", filePath);
ProcessExecution execution = executeCommandUnchecked(command);
if (execution.getExitCode() == 1) {
if (execution.getStdErr().contains("No such file or directory")) {
return null;
}
}
execution.checkExitCode();
String stdout = execution.getStdOut();
// Format is "hash filename"
String[] items = stdout.split(" ");
return new Md5Hash(items[0]);
}
use of org.platformlayer.ops.process.ProcessExecution in project platformlayer by platformlayer.
the class OpsTargetBase method doFind.
protected List<FilesystemInfo> doFind(File path, Integer maxDepth) throws OpsException {
Command command = maybeSudo("find");
String[] fields = new String[] { "T@", "s", "m", "u", "g", "n", "p", "l", "y", "d" };
StringBuilder format = new StringBuilder();
for (int i = 0; i < fields.length; i++) {
if (i != 0) {
format.append("\\t");
}
format.append("%");
format.append(fields[i]);
}
format.append("\\n");
command.addFile(path);
if (maxDepth != null) {
command.addLiteral("-maxdepth");
command.addQuoted(maxDepth.toString());
}
command.addLiteral("-printf");
command.addQuoted(format.toString());
ProcessExecution execution;
try {
execution = executeCommand(command);
} catch (ProcessExecutionException e) {
execution = e.getExecution();
if (execution != null && execution.getExitCode() == 1 && execution.getStdErr().contains("No such file or directory")) {
return null;
}
throw new OpsException("Error executing find command", e);
}
List<FilesystemInfo> filesystemInfos = Lists.newArrayList();
String stdout = execution.getStdOut();
for (String line : stdout.split("\n")) {
String[] fieldValues = line.split("\t");
if (fieldValues.length != fields.length) {
throw new OpsException("Cannot parse line: " + line);
}
FilesystemInfo filesystemInfo = new FilesystemInfo();
for (int i = 0; i < fieldValues.length; i++) {
String field = fields[i];
String fieldValue = fieldValues[i];
if (field.equals("u")) {
filesystemInfo.owner = fieldValue;
} else if (field.equals("g")) {
filesystemInfo.group = fieldValue;
} else if (field.equals("n")) {
filesystemInfo.links = fieldValue;
} else if (field.equals("m")) {
filesystemInfo.mode = fieldValue;
} else if (field.equals("p")) {
filesystemInfo.name = fieldValue;
} else if (field.equals("s")) {
filesystemInfo.size = Long.parseLong(fieldValue);
} else if (field.equals("y")) {
filesystemInfo.type = fieldValue;
} else if (field.equals("l")) {
if (!Strings.isNullOrEmpty(fieldValue)) {
filesystemInfo.symlinkTarget = fieldValue;
}
} else if (field.equals("T@")) {
filesystemInfo.date = fieldValue;
} else if (field.equals("d")) {
filesystemInfo.depth = Integer.parseInt(fieldValue);
} else {
throw new IllegalStateException();
}
}
filesystemInfos.add(filesystemInfo);
}
return filesystemInfos;
}
use of org.platformlayer.ops.process.ProcessExecution in project platformlayer by platformlayer.
the class OpsTargetBase method executeCommand.
@Override
public ProcessExecution executeCommand(Command command) throws OpsException {
log.info("Executing command: " + command.toString());
if (command.getEnvironment() != null) {
String s = Joiner.on(",").join(command.getEnvironment().keys());
log.info("Environment keys set: " + s);
}
ProcessExecution execution = executeCommandUnchecked(command);
if (execution.getExitCode() != 0) {
throw new ProcessExecutionException("Unexpected exit code from running command. Command=" + command.toString(), execution);
}
return execution;
}
use of org.platformlayer.ops.process.ProcessExecution in project platformlayer by platformlayer.
the class DirectOpenstackDownload method download.
public void download(OpsTarget target, File targetPath, OpenstackCredentials credentials, String containerName, String objectPath) throws OpsException {
RemoteCurlOpenstackSession session = new RemoteCurlOpenstackSession(target);
session.authenticate(credentials, false);
OpenstackStorageClient storageClient = session.getStorageClient();
RequestBuilder request = storageClient.root().containers().id(containerName).objects().id(objectPath).buildDownloadRequest();
CurlRequest curlRequest = session.toCurlRequest(request);
curlRequest.bareRequest = true;
Command curlCommand = curlRequest.toCommand();
curlCommand.addLiteral(">");
curlCommand.addFile(targetPath);
ProcessExecution execution = target.executeCommand(curlCommand);
// CurlResult curlResult = curlRequest.parseResponse(execution);
//
// int httpResult = curlResult.getHttpResult();
// switch (httpResult) {
// case 200:
// break;
// case 201:
// break;
// default:
// throw new OpsException("Unexpected result code while downloading file: " + httpResult + " Result=" +
// curlResult);
// }
}
Aggregations