Search in sources :

Example 11 with ProcessExecution

use of org.platformlayer.ops.process.ProcessExecution in project platformlayer by platformlayer.

the class MinaSshConnection method sshExecute0.

@Override
protected ProcessExecution sshExecute0(String command, TimeSpan timeout) throws SshException, IOException, InterruptedException {
    try {
        ByteArrayOutputStream stdoutBinary = new ByteArrayOutputStream();
        ByteArrayOutputStream stderrBinary = new ByteArrayOutputStream();
        int exitCode = sshExecute(command, stdoutBinary, stderrBinary, null, timeout);
        ProcessExecution processExecution = new ProcessExecution(command, exitCode, stdoutBinary.toByteArray(), stderrBinary.toByteArray());
        return processExecution;
    } catch (RuntimeSshException e) {
        throw new SshException("Unexpected SSH error", e);
    }
}
Also used : ProcessExecution(org.platformlayer.ops.process.ProcessExecution) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SshException(org.platformlayer.ops.ssh.SshException) RuntimeSshException(org.apache.sshd.common.RuntimeSshException) RuntimeSshException(org.apache.sshd.common.RuntimeSshException)

Example 12 with ProcessExecution

use of org.platformlayer.ops.process.ProcessExecution in project platformlayer by platformlayer.

the class NetworkTunDevice method handler.

@Handler
public void handler(OpsTarget target) throws OpsException {
    if (OpsContext.isConfigure()) {
        Command findCommand = Command.build("/sbin/ifconfig {0}", interfaceName);
        boolean found = false;
        try {
            target.executeCommand(findCommand);
            found = true;
        } catch (ProcessExecutionException e) {
            ProcessExecution execution = e.getExecution();
            if (execution.getExitCode() == 1 && execution.getStdErr().contains("Device not found")) {
                found = false;
            } else {
                throw new OpsException("Error checking for interface", e);
            }
        }
        if (!found) {
            // This is actually idempotent, but I think it's scary to rely on it being so
            Command command = Command.build("/usr/sbin/tunctl -t {0}", interfaceName);
            target.executeCommand(command);
        }
        {
            // TODO: Safe to re-run?
            Command command = Command.build("ifconfig {0} up", interfaceName);
            target.executeCommand(command);
        }
        if (bridgeName != null) {
            // TODO: Safe to re-run?
            Command command = Command.build("brctl addif {0} {1}", bridgeName.get(), interfaceName);
            try {
                target.executeCommand(command);
            } catch (ProcessExecutionException e) {
                ProcessExecution execution = e.getExecution();
                if (execution.getExitCode() == 1 && execution.getStdErr().contains("already a member of a bridge")) {
                // OK
                // TODO: Check that the bridge is bridgeName
                } else {
                    throw new OpsException("Error attaching interface to bridge", e);
                }
            }
        }
    }
    if (OpsContext.isDelete()) {
        // TODO: Implement
        log.warn("NetworkTunDevice delete not implemented");
    }
}
Also used : OpsException(org.platformlayer.ops.OpsException) Command(org.platformlayer.ops.Command) ProcessExecutionException(org.platformlayer.ops.process.ProcessExecutionException) ProcessExecution(org.platformlayer.ops.process.ProcessExecution) Handler(org.platformlayer.ops.Handler)

Example 13 with ProcessExecution

use of org.platformlayer.ops.process.ProcessExecution in project platformlayer by platformlayer.

the class AptPackageManager method findOutOfDatePackages.

// // some packages might need longer if the network is slow
// private static Map<String, TimeSpan> packageTimeMap = new HashMap<String, TimeSpan>() {
// {
// put("sun-java6-jdk", new TimeSpan("30m"));
// }
// };
//
// public static void update(OpsServer server) throws OpsException {
// SimpleBashCommand command = new SimpleBashCommand("/usr/bin/apt-get");
// command.addLiteralArg("-q");
// command.addLiteralArg("-y");
// command.addLiteralArg("update");
//
// server.simpleRun(command, new TimeSpan("5m"));
// }
//
// public static void installUpdates(OpsServer server) throws OpsException {
// SimpleBashCommand command = new SimpleBashCommand("/usr/bin/apt-get");
// command.addLiteralArg("-q");
// command.addLiteralArg("-y");
// command.addLiteralArg("upgrade");
//
// server.simpleRun(command, new TimeSpan("15m"));
// }
public List<String> findOutOfDatePackages(OpsTarget target) throws OpsException {
    Command command = Command.build("apt-get -q -q --simulate dist-upgrade");
    ProcessExecution execution = target.executeCommand(command);
    final List<String> packages = Lists.newArrayList();
    for (String line : Splitter.on("\n").split(execution.getStdOut())) {
        line = line.trim();
        if (line.isEmpty()) {
            continue;
        }
        List<String> tokens = Lists.newArrayList(Splitter.on(CharMatcher.WHITESPACE).omitEmptyStrings().split(line));
        if (tokens.size() < 2) {
            continue;
        }
        String action = tokens.get(0);
        if (action.equals("Inst")) {
            // e.g. Inst coreutils [8.13-3.1] (8.13-3.2 Debian:testing [amd64])
            packages.add(tokens.get(1));
        // New version is in tokens[2]
        // Current version is in tokens[3], but that's a trick
        }
    }
    return packages;
}
Also used : Command(org.platformlayer.ops.Command) ProcessExecution(org.platformlayer.ops.process.ProcessExecution)

Example 14 with ProcessExecution

use of org.platformlayer.ops.process.ProcessExecution in project platformlayer by platformlayer.

the class AptPackageManager method getInstalledPackageInfo.

public List<String> getInstalledPackageInfo(OpsTarget target) throws OpsException {
    AptInfoCache cached = getCache(target);
    if (cached.installedPackages == null) {
        Command command = Command.build("/usr/bin/dpkg --get-selections");
        ProcessExecution execution = target.executeCommand(command);
        final List<String> packages = Lists.newArrayList();
        for (String line : Splitter.on("\n").split(execution.getStdOut())) {
            line = line.trim();
            if (line.isEmpty()) {
                continue;
            }
            List<String> tokens = Lists.newArrayList(Splitter.on(CharMatcher.WHITESPACE).omitEmptyStrings().split(line));
            if (tokens.size() != 2) {
                throw new OpsException("Error parsing line; expected 2 items: " + line);
            }
            String state = tokens.get(1);
            if (state.equals("install")) {
                String packageName = tokens.get(0);
                int colonIndex = packageName.indexOf(':');
                if (colonIndex != -1) {
                    // Architecture sometimes follows package name
                    packageName = packageName.substring(0, colonIndex);
                }
                packages.add(packageName);
            } else if (state.equals("deinstall")) {
            // Not installed (?)
            } else {
                throw new OpsException("Unknown package state in line: " + line);
            }
        }
        cached.installedPackages = packages;
    } else {
        log.debug("Re-using cached package info");
    }
    return cached.installedPackages;
}
Also used : OpsException(org.platformlayer.ops.OpsException) Command(org.platformlayer.ops.Command) ProcessExecution(org.platformlayer.ops.process.ProcessExecution)

Example 15 with ProcessExecution

use of org.platformlayer.ops.process.ProcessExecution in project platformlayer by platformlayer.

the class OpenLdapManager method doLdapQuery.

public static List<LdifRecord> doLdapQuery(OpsTarget target, LdapDN bindDN, String ldapPassword, LdapDN searchBaseDN, String filter, SearchScope searchScope) throws OpsException {
    Command command = Command.build(CMD_LDAP_SEARCH);
    // Pure LDIF, no extra junk
    command.addLiteral("-LLL");
    // Simple auth
    command.addLiteral("-x");
    // Bind DN
    command.addLiteral("-D");
    command.addQuoted(bindDN.toLdifEncoded());
    // Simple auth password
    command.addLiteral("-w");
    command.addQuoted(ldapPassword);
    // Search base
    command.addLiteral("-b");
    command.addQuoted(searchBaseDN.toLdifEncoded());
    // Scope
    command.addLiteral("-s");
    command.addLiteral(searchScope.toString().toLowerCase());
    if (!Strings.isNullOrEmpty(filter)) {
        command.addQuoted(filter);
    }
    ProcessExecution processExecution = target.executeCommand(command);
    return LdifRecord.parse(processExecution.getStdOut());
}
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