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