Search in sources :

Example 1 with ProcessExecution

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;
}
Also used : FilesystemInfo(org.platformlayer.ops.filesystem.FilesystemInfo) ProcessExecution(org.platformlayer.ops.process.ProcessExecution) ProcessExecutionException(org.platformlayer.ops.process.ProcessExecutionException)

Example 2 with ProcessExecution

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]);
}
Also used : ProcessExecution(org.platformlayer.ops.process.ProcessExecution) Md5Hash(com.fathomdb.hash.Md5Hash)

Example 3 with ProcessExecution

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;
}
Also used : ProcessExecution(org.platformlayer.ops.process.ProcessExecution) ProcessExecutionException(org.platformlayer.ops.process.ProcessExecutionException)

Example 4 with ProcessExecution

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;
}
Also used : Command(org.platformlayer.ops.Command) ProcessExecution(org.platformlayer.ops.process.ProcessExecution)

Example 5 with ProcessExecution

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;
}
Also used : Command(org.platformlayer.ops.Command) ProcessExecution(org.platformlayer.ops.process.ProcessExecution)

Aggregations

ProcessExecution (org.platformlayer.ops.process.ProcessExecution)25 Command (org.platformlayer.ops.Command)16 OpsException (org.platformlayer.ops.OpsException)8 ProcessExecutionException (org.platformlayer.ops.process.ProcessExecutionException)8 File (java.io.File)5 OpsTarget (org.platformlayer.ops.OpsTarget)3 Md5Hash (com.fathomdb.hash.Md5Hash)2 UnknownHostException (java.net.UnknownHostException)2 ExecutionException (java.util.concurrent.ExecutionException)2 TimeoutException (java.util.concurrent.TimeoutException)2 RequestBuilder (org.openstack.client.common.RequestBuilder)2 Handler (org.platformlayer.ops.Handler)2 FilesystemInfo (org.platformlayer.ops.filesystem.FilesystemInfo)2 CurlRequest (org.platformlayer.ops.helpers.CurlRequest)2 SshKey (org.platformlayer.ops.helpers.SshKey)2 ImageFormat (org.platformlayer.ops.images.ImageFormat)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 Inet6Address (java.net.Inet6Address)1 List (java.util.List)1