use of org.neo4j.driver.v1.Session in project cloudstack by apache.
the class SSHCmdHelper method sshExecuteCmdOneShotWithExitCode.
public static int sshExecuteCmdOneShotWithExitCode(com.trilead.ssh2.Connection sshConnection, String cmd) throws SshException {
s_logger.debug("Executing cmd: " + cmd);
Session sshSession = null;
try {
sshSession = sshConnection.openSession();
// There is a bug in Trilead library, wait a second before
// starting a shell and executing commands, from http://spci.st.ewi.tudelft.nl/chiron/xref/nl/tudelft/swerl/util/SSHConnection.html
Thread.sleep(1000);
if (sshSession == null) {
throw new SshException("Cannot open ssh session");
}
sshSession.execCommand(cmd);
InputStream stdout = sshSession.getStdout();
InputStream stderr = sshSession.getStderr();
byte[] buffer = new byte[8192];
StringBuffer sbResult = new StringBuffer();
int currentReadBytes = 0;
while (true) {
if (stdout == null || stderr == null) {
throw new SshException("stdout or stderr of ssh session is null");
}
if ((stdout.available() == 0) && (stderr.available() == 0)) {
int conditions = sshSession.waitForCondition(ChannelCondition.STDOUT_DATA | ChannelCondition.STDERR_DATA | ChannelCondition.EOF | ChannelCondition.EXIT_STATUS, 120000);
if ((conditions & ChannelCondition.TIMEOUT) != 0) {
String msg = "Timed out in waiting SSH execution result";
s_logger.error(msg);
throw new Exception(msg);
}
if ((conditions & ChannelCondition.EXIT_STATUS) != 0) {
if ((conditions & (ChannelCondition.STDOUT_DATA | ChannelCondition.STDERR_DATA)) == 0) {
break;
}
}
if ((conditions & ChannelCondition.EOF) != 0) {
if ((conditions & (ChannelCondition.STDOUT_DATA | ChannelCondition.STDERR_DATA)) == 0) {
break;
}
}
}
while (stdout.available() > 0) {
currentReadBytes = stdout.read(buffer);
sbResult.append(new String(buffer, 0, currentReadBytes));
}
while (stderr.available() > 0) {
currentReadBytes = stderr.read(buffer);
sbResult.append(new String(buffer, 0, currentReadBytes));
}
}
String result = sbResult.toString();
if (result != null && !result.isEmpty())
s_logger.debug(cmd + " output:" + result);
// exit status delivery might get delayed
for (int i = 0; i < 10; i++) {
Integer status = sshSession.getExitStatus();
if (status != null) {
return status;
}
Thread.sleep(100);
}
return -1;
} catch (Exception e) {
s_logger.debug("Ssh executed failed", e);
throw new SshException("Ssh executed failed " + e.getMessage());
} finally {
if (sshSession != null)
sshSession.close();
}
}
use of org.neo4j.driver.v1.Session in project open-kilda by telstra.
the class NeoDriver method getPath.
/**
* {@inheritDoc}
*/
@Override
public ImmutablePair<PathInfoData, PathInfoData> getPath(Flow flow, Strategy strategy) throws UnroutablePathException {
long latency = 0L;
List<PathNode> forwardNodes = new LinkedList<>();
List<PathNode> reverseNodes = new LinkedList<>();
if (!flow.isOneSwitchFlow()) {
Statement statement = getPathQuery(flow, strategy);
logger.debug("QUERY: {}", statement.toString());
try (Session session = driver.session()) {
StatementResult result = session.run(statement);
try {
Record record = result.next();
LinkedList<Relationship> isls = new LinkedList<>();
record.get(0).asPath().relationships().forEach(isls::add);
int seqId = 0;
for (Relationship isl : isls) {
latency += isl.get("latency").asLong();
forwardNodes.add(new PathNode(isl.get("src_switch").asString(), isl.get("src_port").asInt(), seqId, isl.get("latency").asLong()));
seqId++;
forwardNodes.add(new PathNode(isl.get("dst_switch").asString(), isl.get("dst_port").asInt(), seqId, 0L));
seqId++;
}
seqId = 0;
Collections.reverse(isls);
for (Relationship isl : isls) {
reverseNodes.add(new PathNode(isl.get("dst_switch").asString(), isl.get("dst_port").asInt(), seqId, isl.get("latency").asLong()));
seqId++;
reverseNodes.add(new PathNode(isl.get("src_switch").asString(), isl.get("src_port").asInt(), seqId, 0L));
seqId++;
}
} catch (NoSuchRecordException e) {
throw new UnroutablePathException(flow);
}
}
} else {
logger.info("No path computation for one-switch flow");
}
return new ImmutablePair<>(new PathInfoData(latency, forwardNodes), new PathInfoData(latency, reverseNodes));
}
use of org.neo4j.driver.v1.Session in project google-cloud-java by GoogleCloudPlatform.
the class SessionImplTest method setUp.
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
SpannerImpl spanner = new SpannerImpl(rpc, 1, null);
String dbName = "projects/p1/instances/i1/databases/d1";
String sessionName = dbName + "/sessions/s1";
DatabaseId db = DatabaseId.of(dbName);
Session sessionProto = Session.newBuilder().setName(sessionName).build();
Mockito.when(rpc.createSession(Mockito.eq(dbName), optionsCaptor.capture())).thenReturn(sessionProto);
session = spanner.createSession(db);
// We expect the same options, "options", on all calls on "session".
options = optionsCaptor.getValue();
Mockito.reset(rpc);
}
use of org.neo4j.driver.v1.Session in project Payara by payara.
the class SSHLauncher method runCommandAsIs.
private int runCommandAsIs(String command, OutputStream os, List<String> stdinLines, String[] env) throws IOException, InterruptedException {
if (logger.isLoggable(Level.FINER)) {
logger.finer("Running command " + command + " on host: " + this.host);
}
openConnection();
StringBuffer buff = new StringBuffer();
if (env != null) {
Session tempSession = connection.openSession();
OutputStream ous = new ByteArrayOutputStream();
exec(tempSession, "ps -p $$ | tail -1 | awk '{print $NF}'", ous, null);
String prefix;
if (ous.toString().contains("csh")) {
logger.fine("CSH shell");
prefix = "setenv";
} else {
logger.fine("BASH shell");
prefix = "export";
}
for (String st : env) {
String cmd = prefix + " " + st;
buff.append(cmd).append(";");
}
}
buff.append(command);
final Session sess = connection.openSession();
int status = exec(sess, buff.toString(), os, listInputStream(stdinLines));
// XXX: Should we close connection after each command or cache it
// and re-use it?
SSHUtil.unregister(connection);
connection = null;
return status;
}
use of org.neo4j.driver.v1.Session in project cypher-for-gremlin by opencypher.
the class GremlinNeo4jDriverTest method returnPath.
@Test
public void returnPath() {
Driver driver = GremlinDatabase.driver("//localhost:" + server.getPort());
try (Session session = driver.session()) {
StatementResult setup = session.run("CREATE (n1:Person {name: 'Anders'})-[r:knows]->(n2:Person)" + "RETURN n1,r,n2");
Record createdNodes = setup.single();
Node n1 = createdNodes.get("n1").asNode();
Node n2 = createdNodes.get("n2").asNode();
Relationship r = createdNodes.get("r").asRelationship();
StatementResult result = session.run("MATCH p =(b1 { name: 'Anders' })-->()" + "RETURN p");
Path path = result.single().get("p").asPath();
assertThat(path.contains(n1)).isTrue();
assertThat(path.contains(n2)).isTrue();
assertThat(path.contains(r)).isTrue();
assertThat(path.relationships()).hasSize(1);
assertThat(path.nodes()).hasSize(2);
}
}
Aggregations