Search in sources :

Example 46 with Session

use of com.google.spanner.v1.Session in project cloudstack by apache.

the class ConfigTest method executeTest.

@Override
public boolean executeTest() {
    int error = 0;
    Element rootElement = this.getInputFile().get(0).getDocumentElement();
    NodeList commandLst = rootElement.getElementsByTagName("command");
    // Analyze each command, send request and build the array list of api commands
    for (int i = 0; i < commandLst.getLength(); i++) {
        Node fstNode = commandLst.item(i);
        Element fstElmnt = (Element) fstNode;
        // new command
        ApiCommand api = new ApiCommand(fstElmnt, this.getParam(), this.getCommands());
        if (api.getName().equals("rebootManagementServer")) {
            s_logger.info("Attempting to SSH into management server " + this.getParam().get("hostip"));
            try {
                Connection conn = new Connection(this.getParam().get("hostip"));
                conn.connect(null, 60000, 60000);
                s_logger.info("SSHed successfully into management server " + this.getParam().get("hostip"));
                boolean isAuthenticated = conn.authenticateWithPassword("root", "password");
                if (isAuthenticated == false) {
                    s_logger.info("Authentication failed for root with password");
                    return false;
                }
                String restartCommand = "service cloud-management restart; service cloud-usage restart";
                Session sess = conn.openSession();
                s_logger.info("Executing : " + restartCommand);
                sess.execCommand(restartCommand);
                Thread.sleep(120000);
                sess.close();
                conn.close();
            } catch (Exception ex) {
                s_logger.error(ex);
                return false;
            }
        } else {
            // send a command
            api.sendCommand(this.getClient(), null);
            // verify the response of the command
            if ((api.getResponseType() == ResponseType.ERROR) && (api.getResponseCode() == 200) && (api.getTestCaseInfo() != null)) {
                s_logger.error("Test case " + api.getTestCaseInfo() + "failed. Command that was supposed to fail, passed. The command was sent with the following url " + api.getUrl());
                error++;
            } else if ((api.getResponseType() != ResponseType.ERROR) && (api.getResponseCode() == 200)) {
                // set parameters for the future use
                if (api.setParam(this.getParam()) == false) {
                    s_logger.error("Exiting the test...Command " + api.getName() + " didn't return parameters needed for the future use. The command was sent with url " + api.getUrl());
                    return false;
                } else {
                    // verify parameters
                    if (api.verifyParam() == false) {
                        s_logger.error("Command " + api.getName() + " failed. Verification for returned parameters failed. Command was sent with url " + api.getUrl());
                        error++;
                    } else if (api.getTestCaseInfo() != null) {
                        s_logger.info("Test case " + api.getTestCaseInfo() + " passed. Command was sent with the url " + api.getUrl());
                    }
                }
            } else if ((api.getResponseType() != ResponseType.ERROR) && (api.getResponseCode() != 200)) {
                s_logger.error("Command " + api.getName() + " failed with an error code " + api.getResponseCode() + " . Command was sent with url  " + api.getUrl() + " Required: " + api.getRequired());
                if (api.getRequired() == true) {
                    s_logger.info("The command is required for the future use, so exiging");
                    return false;
                }
                error++;
            } else if (api.getTestCaseInfo() != null) {
                s_logger.info("Test case " + api.getTestCaseInfo() + " passed. Command that was supposed to fail, failed - test passed. Command was sent with url " + api.getUrl());
            }
        }
    }
    if (error != 0)
        return false;
    else
        return true;
}
Also used : Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Connection(com.trilead.ssh2.Connection) Session(com.trilead.ssh2.Session)

Example 47 with Session

use of com.google.spanner.v1.Session in project cloudstack by apache.

the class SshHelperTest method canEndTheSshConnectionTest.

@Test
public void canEndTheSshConnectionTest() throws Exception {
    PowerMockito.spy(SshHelper.class);
    Session mockedSession = Mockito.mock(Session.class);
    PowerMockito.doReturn(true).when(SshHelper.class, "isChannelConditionEof", Mockito.anyInt());
    Mockito.when(mockedSession.waitForCondition(ChannelCondition.EXIT_STATUS, 1l)).thenReturn(0);
    PowerMockito.doNothing().when(SshHelper.class, "throwSshExceptionIfConditionsTimeout", Mockito.anyInt());
    SshHelper.canEndTheSshConnection(1, mockedSession, 0);
    PowerMockito.verifyStatic(SshHelper.class);
    SshHelper.isChannelConditionEof(Mockito.anyInt());
    SshHelper.throwSshExceptionIfConditionsTimeout(Mockito.anyInt());
    Mockito.verify(mockedSession).waitForCondition(ChannelCondition.EXIT_STATUS, 1l);
}
Also used : Session(com.trilead.ssh2.Session) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 48 with Session

use of com.google.spanner.v1.Session in project jstorm by alibaba.

the class MyScpClient method get.

/**
 * Download a set of files from the remote server to a local directory.
 *
 * @param remoteFiles          Paths and names of the remote files.
 * @param localTargetDirectory Local directory to put the downloaded files.
 * @throws IOException
 */
public void get(String[] remoteFiles, String localTargetDirectory) throws IOException {
    Session sess = null;
    if ((remoteFiles == null) || (localTargetDirectory == null))
        throw new IllegalArgumentException("Null argument.");
    if (remoteFiles.length == 0)
        return;
    // String cmd = "scp -f -r";
    String cmd = "scp -f ";
    for (int i = 0; i < remoteFiles.length; i++) {
        if (remoteFiles[i] == null)
            throw new IllegalArgumentException("Cannot accept null filename.");
        cmd += (" " + remoteFiles[i]);
    }
    try {
        sess = conn.openSession();
        sess.execCommand(cmd);
        receiveFiles(sess, remoteFiles, localTargetDirectory);
        sess.close();
    } catch (IOException e) {
        if (sess != null)
            sess.close();
        throw (IOException) new IOException("Error during SCP transfer.").initCause(e);
    }
}
Also used : IOException(java.io.IOException) Session(ch.ethz.ssh2.Session)

Example 49 with Session

use of com.google.spanner.v1.Session in project jstorm by alibaba.

the class MyScpClient method put.

/**
 * Copy a set of local files to a remote directory, uses the specified mode
 * when creating the files on the remote side.
 *
 * @param localFiles            Paths and names of the local files.
 * @param remoteTargetDirectory Remote target directory.
 * @param mode                  a four digit string (e.g., 0644, see "man chmod", "man open")
 * @throws IOException
 */
public void put(String[] localFiles, String remoteTargetDirectory, String mode) throws IOException {
    Session sess = null;
    if ((localFiles == null) || (remoteTargetDirectory == null) || (mode == null))
        throw new IllegalArgumentException("Null argument.");
    if (mode.length() != 4)
        throw new IllegalArgumentException("Invalid mode.");
    for (int i = 0; i < mode.length(); i++) if (Character.isDigit(mode.charAt(i)) == false)
        throw new IllegalArgumentException("Invalid mode.");
    if (localFiles.length == 0)
        return;
    String cmd = "scp -t -d " + remoteTargetDirectory;
    for (int i = 0; i < localFiles.length; i++) {
        if (localFiles[i] == null)
            throw new IllegalArgumentException("Cannot accept null filename.");
    }
    try {
        sess = conn.openSession();
        sess.execCommand(cmd);
        sendFiles(sess, localFiles, mode);
        sess.close();
    } catch (IOException e) {
        if (sess != null)
            sess.close();
        throw (IOException) new IOException("Error during SCP transfer.").initCause(e);
    }
}
Also used : IOException(java.io.IOException) Session(ch.ethz.ssh2.Session)

Example 50 with Session

use of com.google.spanner.v1.Session in project jstorm by alibaba.

the class MyScpClient method put.

/**
 * Create a remote file and copy the contents of the passed byte array into it.
 * The method use the specified mode when creating the file on the remote side.
 *
 * @param data                  the data to be copied into the remote file.
 * @param remoteFileName        The name of the file which will be created in the remote target directory.
 * @param remoteTargetDirectory Remote target directory.
 * @param mode                  a four digit string (e.g., 0644, see "man chmod", "man open")
 * @throws IOException
 */
public void put(byte[] data, String remoteFileName, String remoteTargetDirectory, String mode) throws IOException {
    Session sess = null;
    if ((remoteFileName == null) || (remoteTargetDirectory == null) || (mode == null))
        throw new IllegalArgumentException("Null argument.");
    if (mode.length() != 4)
        throw new IllegalArgumentException("Invalid mode.");
    for (int i = 0; i < mode.length(); i++) if (Character.isDigit(mode.charAt(i)) == false)
        throw new IllegalArgumentException("Invalid mode.");
    String cmd = "scp -t -d " + remoteTargetDirectory;
    try {
        sess = conn.openSession();
        sess.execCommand(cmd);
        sendBytes(sess, data, remoteFileName, mode);
        sess.close();
    } catch (IOException e) {
        if (sess != null)
            sess.close();
        throw (IOException) new IOException("Error during SCP transfer.").initCause(e);
    }
}
Also used : IOException(java.io.IOException) Session(ch.ethz.ssh2.Session)

Aggregations

Session (com.trilead.ssh2.Session)43 Session (org.neo4j.driver.v1.Session)38 Connection (com.trilead.ssh2.Connection)32 IOException (java.io.IOException)30 Test (org.junit.Test)29 InputStream (java.io.InputStream)28 Driver (org.neo4j.driver.v1.Driver)27 StatementResult (org.neo4j.driver.v1.StatementResult)20 Record (org.neo4j.driver.v1.Record)12 Session (iaik.pkcs.pkcs11.Session)10 TokenException (iaik.pkcs.pkcs11.TokenException)10 P11TokenException (org.xipki.security.exception.P11TokenException)10 CoreClusterMember (org.neo4j.causalclustering.discovery.CoreClusterMember)9 RoutingNetworkSession (org.neo4j.driver.internal.RoutingNetworkSession)9 Session (ch.ethz.ssh2.Session)8 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)8 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)8 HttpException (org.apache.commons.httpclient.HttpException)8 Transaction (org.neo4j.driver.v1.Transaction)7 SCPClient (com.trilead.ssh2.SCPClient)6