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 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 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 SimpleIptablesRules method listRules.
public static SimpleIptablesRules listRules(OpsTarget target, Transport transport, IptablesChain chain) throws OpsException {
Command command = buildCommand(transport, chain);
command.addLiteral("--list-rules");
ProcessExecution iptablesExecution = target.executeCommand(command);
SimpleIptablesRules ret = new SimpleIptablesRules();
for (String line : Splitter.on("\n").split(iptablesExecution.getStdOut())) {
SimpleIptablesRule rule = new SimpleIptablesRule(line);
ret.rules.add(rule);
}
return ret;
}
use of org.platformlayer.ops.process.ProcessExecution in project platformlayer by platformlayer.
the class IpTablesManager method getCurrentFirewallState.
public static List<FirewallRecord> getCurrentFirewallState(OpsTarget target, Transport transport) throws OpsException {
Command command = getIptablesCommand(transport);
command.addLiteral("--list-rules");
List<FirewallRecord> records = Lists.newArrayList();
ProcessExecution execution = target.executeCommand(command);
String stdout = execution.getStdOut();
for (String line : stdout.split("\n")) {
parseAndAdd(records, line);
}
return records;
}
Aggregations