Search in sources :

Example 91 with Connection

use of org.ovirt.engine.sdk4.Connection in project ACS by ACS-Community.

the class Executor method remoteDownAllPortable.

/**
	 * Shuts down all ssh-sessions and -connections that may still be active.
	 */
private static void remoteDownAllPortable() {
    for (Session sess : sessions) {
        try {
            sess.close();
            log.fine("closed " + sess);
        } catch (Exception exc) {
            log.fine("could not close " + sess);
        }
    }
    for (Connection conn : connections) {
        try {
            conn.close();
            log.fine("closed " + conn);
        } catch (Exception exc) {
            log.fine("could not close " + conn);
        }
    }
}
Also used : Connection(com.trilead.ssh2.Connection) IOException(java.io.IOException) OrbInitException(alma.acs.commandcenter.meta.Firestarter.OrbInitException) AcsJException(alma.acs.exceptions.AcsJException) Session(com.trilead.ssh2.Session)

Example 92 with Connection

use of org.ovirt.engine.sdk4.Connection in project cloudstack by apache.

the class StressTestDirectAttach method sshWinTest.

private static String sshWinTest(String host) {
    if (host == null) {
        s_logger.info("Did not receive a host back from test, ignoring win ssh test");
        return null;
    }
    // We will retry 5 times before quitting
    int retry = 1;
    while (true) {
        try {
            if (retry > 0) {
                s_logger.info("Retry attempt : " + retry + " ...sleeping 300 seconds before next attempt. Account is " + s_account.get());
                Thread.sleep(300000);
            }
            s_logger.info("Attempting to SSH into windows host " + host + " with retry attempt: " + retry + " for account " + s_account.get());
            Connection conn = new Connection(host);
            conn.connect(null, 60000, 60000);
            s_logger.info("User " + s_account.get() + " ssHed successfully into windows host " + host);
            boolean success = false;
            boolean isAuthenticated = conn.authenticateWithPassword("Administrator", "password");
            if (isAuthenticated == false) {
                return "Authentication failed";
            } else {
                s_logger.info("Authentication is successfull");
            }
            try {
                SCPClient scp = new SCPClient(conn);
                scp.put("wget.exe", "wget.exe", "C:\\Users\\Administrator", "0777");
                s_logger.info("Successfully put wget.exe file");
            } catch (Exception ex) {
                s_logger.error("Unable to put wget.exe " + ex);
            }
            if (conn == null) {
                s_logger.error("Connection is null");
            }
            Session sess = conn.openSession();
            s_logger.info("User + " + s_account.get() + " executing : wget http://192.168.1.250/dump.bin");
            sess.execCommand("wget http://192.168.1.250/dump.bin && dir dump.bin");
            InputStream stdout = sess.getStdout();
            InputStream stderr = sess.getStderr();
            byte[] buffer = new byte[8192];
            while (true) {
                if ((stdout.available() == 0) && (stderr.available() == 0)) {
                    int conditions = sess.waitForCondition(ChannelCondition.STDOUT_DATA | ChannelCondition.STDERR_DATA | ChannelCondition.EOF, 120000);
                    if ((conditions & ChannelCondition.TIMEOUT) != 0) {
                        s_logger.info("Timeout while waiting for data from peer.");
                        return null;
                    }
                    if ((conditions & ChannelCondition.EOF) != 0) {
                        if ((conditions & (ChannelCondition.STDOUT_DATA | ChannelCondition.STDERR_DATA)) == 0) {
                            break;
                        }
                    }
                }
                while (stdout.available() > 0) {
                    success = true;
                    int len = stdout.read(buffer);
                    if (// this check is somewhat paranoid
                    len > 0)
                        s_logger.info(new String(buffer, 0, len));
                }
                while (stderr.available() > 0) {
                    /* int len = */
                    stderr.read(buffer);
                }
            }
            sess.close();
            conn.close();
            if (success) {
                Thread.sleep(120000);
                return null;
            } else {
                retry++;
                if (retry == MAX_RETRY_WIN) {
                    return "SSH Windows Network test fail for account " + s_account.get();
                }
            }
        } catch (Exception e) {
            s_logger.error(e);
            retry++;
            if (retry == MAX_RETRY_WIN) {
                return "SSH Windows Network test fail with error " + e.getMessage();
            }
        }
    }
}
Also used : SCPClient(com.trilead.ssh2.SCPClient) InputStream(java.io.InputStream) Connection(com.trilead.ssh2.Connection) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) HttpException(org.apache.commons.httpclient.HttpException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Session(com.trilead.ssh2.Session)

Example 93 with Connection

use of org.ovirt.engine.sdk4.Connection in project cloudstack by apache.

the class EventsApiTest 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;
        //!!!check if we need to execute mySql command
        NodeList commandName = fstElmnt.getElementsByTagName("name");
        Element commandElmnt = (Element) commandName.item(0);
        NodeList commandNm = commandElmnt.getChildNodes();
        if (commandNm.item(0).getNodeValue().equals("mysqlupdate")) {
            //establish connection to mysql server and execute an update command
            NodeList mysqlList = fstElmnt.getElementsByTagName("mysqlcommand");
            for (int j = 0; j < mysqlList.getLength(); j++) {
                Element itemVariableElement = (Element) mysqlList.item(j);
                s_logger.info("Executing mysql command " + itemVariableElement.getTextContent());
                try {
                    Statement st = this.getConn().createStatement();
                    st.executeUpdate(itemVariableElement.getTextContent());
                } catch (Exception ex) {
                    s_logger.error(ex);
                    return false;
                }
            }
        } else if (commandNm.item(0).getNodeValue().equals("agentcommand")) {
            //connect to all the agents and execute agent command
            NodeList commandList = fstElmnt.getElementsByTagName("commandname");
            Element commandElement = (Element) commandList.item(0);
            NodeList ipList = fstElmnt.getElementsByTagName("ip");
            for (int j = 0; j < ipList.getLength(); j++) {
                Element itemVariableElement = (Element) ipList.item(j);
                s_logger.info("Attempting to SSH into agent " + itemVariableElement.getTextContent());
                try {
                    Connection conn = new Connection(itemVariableElement.getTextContent());
                    conn.connect(null, 60000, 60000);
                    s_logger.info("SSHed successfully into agent " + itemVariableElement.getTextContent());
                    boolean isAuthenticated = conn.authenticateWithPassword("root", "password");
                    if (isAuthenticated == false) {
                        s_logger.info("Authentication failed for root with password");
                        return false;
                    }
                    Session sess = conn.openSession();
                    s_logger.info("Executing : " + commandElement.getTextContent());
                    sess.execCommand(commandElement.getTextContent());
                    Thread.sleep(60000);
                    sess.close();
                    conn.close();
                } catch (Exception ex) {
                    s_logger.error(ex);
                    return false;
                }
            }
        } else {
            //new command
            ApiCommand api = new ApiCommand(fstElmnt, this.getParam(), this.getCommands());
            //send a command
            api.sendCommand(this.getClient(), null);
            //verify the response of the command
            if ((api.getResponseType() == ResponseType.ERROR) && (api.getResponseCode() == 200)) {
                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)) {
                //verify if response is suppposed to be empty
                if (api.getResponseType() == ResponseType.EMPTY) {
                    if (api.isEmpty() == true) {
                        s_logger.info("Test case " + api.getTestCaseInfo() + " passed. Empty response was returned as expected. Command was sent with url " + api.getUrl());
                    } else {
                        s_logger.error("Test case " + api.getTestCaseInfo() + " failed. Empty response was expected. Command was sent with url " + api.getUrl());
                    }
                } else {
                    if (api.isEmpty() != false)
                        s_logger.error("Test case " + api.getTestCaseInfo() + " failed. Non-empty response was expected. Command was sent with url " + api.getUrl());
                    else {
                        //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 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());
                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. Command was sent with url " + api.getUrl());
            }
        }
    }
    //verify events with userid parameter - test case 97
    HashMap<String, Integer> expectedEvents = new HashMap<String, Integer>();
    expectedEvents.put("VM.START", 1);
    boolean eventResult = ApiCommand.verifyEvents(expectedEvents, "INFO", "http://" + this.getParam().get("hostip") + ":8096", "userid=" + this.getParam().get("userid1") + "&type=VM.START");
    s_logger.info("Test case 97 - listEvent command verification result is  " + eventResult);
    //verify error events
    eventResult = ApiCommand.verifyEvents("../metadata/error_events.properties", "ERROR", "http://" + this.getParam().get("hostip") + ":8096", this.getParam().get("erroruseraccount"));
    s_logger.info("listEvent command verification result is  " + eventResult);
    if (error != 0)
        return false;
    else
        return true;
}
Also used : HashMap(java.util.HashMap) Statement(java.sql.Statement) 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 94 with Connection

use of org.ovirt.engine.sdk4.Connection in project cloudstack by apache.

the class GuestNetwork method run.

@Override
public void run() {
    NDC.push("Following thread has started" + Thread.currentThread().getName());
    int retry = 0;
    //Start copying files between machines in the network
    s_logger.info("The size of the array is " + this.virtualMachines.size());
    while (true) {
        try {
            if (retry > 0) {
                s_logger.info("Retry attempt : " + retry + " ...sleeping 120 seconds before next attempt");
                Thread.sleep(120000);
            }
            for (VirtualMachine vm : this.virtualMachines) {
                s_logger.info("Attempting to SSH into linux host " + this.publicIp + " with retry attempt: " + retry);
                Connection conn = new Connection(this.publicIp);
                conn.connect(null, 600000, 600000);
                s_logger.info("SSHed successfully into linux host " + this.publicIp);
                boolean isAuthenticated = conn.authenticateWithPassword("root", "password");
                if (isAuthenticated == false) {
                    s_logger.info("Authentication failed");
                }
                //execute copy command
                Session sess = conn.openSession();
                String fileName;
                Random ran = new Random();
                fileName = Math.abs(ran.nextInt()) + "-file";
                String copyCommand = new String("./scpScript " + vm.getPrivateIp() + " " + fileName);
                s_logger.info("Executing " + copyCommand);
                sess.execCommand(copyCommand);
                Thread.sleep(120000);
                sess.close();
                //execute wget command
                sess = conn.openSession();
                String downloadCommand = new String("wget http://172.16.0.220/scripts/checkDiskSpace.sh; chmod +x *sh; ./checkDiskSpace.sh; rm -rf checkDiskSpace.sh");
                s_logger.info("Executing " + downloadCommand);
                sess.execCommand(downloadCommand);
                Thread.sleep(120000);
                sess.close();
                //close the connection
                conn.close();
            }
        } catch (Exception ex) {
            s_logger.error(ex);
            retry++;
            if (retry == retryNum) {
                s_logger.info("Performance Guest Network test failed with error " + ex.getMessage());
            }
        }
    }
}
Also used : Random(java.util.Random) Connection(com.trilead.ssh2.Connection) Session(com.trilead.ssh2.Session)

Example 95 with Connection

use of org.ovirt.engine.sdk4.Connection in project cloudstack by apache.

the class TestClientWithAPI method sshWinTest.

private static String sshWinTest(String host) {
    if (host == null) {
        s_logger.info("Did not receive a host back from test, ignoring win ssh test");
        return null;
    }
    // We will retry 5 times before quitting
    int retry = 1;
    while (true) {
        try {
            if (retry > 0) {
                s_logger.info("Retry attempt : " + retry + " ...sleeping 300 seconds before next attempt. Account is " + s_account.get());
                Thread.sleep(300000);
            }
            s_logger.info("Attempting to SSH into windows host " + host + " with retry attempt: " + retry + " for account " + s_account.get());
            Connection conn = new Connection(host);
            conn.connect(null, 60000, 60000);
            s_logger.info("User " + s_account.get() + " ssHed successfully into windows host " + host);
            boolean success = false;
            boolean isAuthenticated = conn.authenticateWithPassword("Administrator", "password");
            if (isAuthenticated == false) {
                return "Authentication failed";
            } else {
                s_logger.info("Authentication is successfull");
            }
            try {
                SCPClient scp = new SCPClient(conn);
                scp.put("wget.exe", "wget.exe", "C:\\Users\\Administrator", "0777");
                s_logger.info("Successfully put wget.exe file");
            } catch (Exception ex) {
                s_logger.error("Unable to put wget.exe " + ex);
            }
            if (conn == null) {
                s_logger.error("Connection is null");
            }
            Session sess = conn.openSession();
            s_logger.info("User + " + s_account.get() + " executing : wget http://" + downloadUrl);
            String downloadCommand = "wget http://" + downloadUrl + " && dir dump.bin";
            sess.execCommand(downloadCommand);
            InputStream stdout = sess.getStdout();
            InputStream stderr = sess.getStderr();
            byte[] buffer = new byte[8192];
            while (true) {
                if ((stdout.available() == 0) && (stderr.available() == 0)) {
                    int conditions = sess.waitForCondition(ChannelCondition.STDOUT_DATA | ChannelCondition.STDERR_DATA | ChannelCondition.EOF, 120000);
                    if ((conditions & ChannelCondition.TIMEOUT) != 0) {
                        s_logger.info("Timeout while waiting for data from peer.");
                        return null;
                    }
                    if ((conditions & ChannelCondition.EOF) != 0) {
                        if ((conditions & (ChannelCondition.STDOUT_DATA | ChannelCondition.STDERR_DATA)) == 0) {
                            break;
                        }
                    }
                }
                while (stdout.available() > 0) {
                    success = true;
                    int len = stdout.read(buffer);
                    if (// this check is somewhat paranoid
                    len > 0)
                        s_logger.info(new String(buffer, 0, len));
                }
                while (stderr.available() > 0) {
                    /* int len = */
                    stderr.read(buffer);
                }
            }
            sess.close();
            conn.close();
            if (success) {
                return null;
            } else {
                retry++;
                if (retry == MAX_RETRY_WIN) {
                    return "SSH Windows Network test fail for account " + s_account.get();
                }
            }
        } catch (Exception e) {
            s_logger.error(e);
            retry++;
            if (retry == MAX_RETRY_WIN) {
                return "SSH Windows Network test fail with error " + e.getMessage();
            }
        }
    }
}
Also used : SCPClient(com.trilead.ssh2.SCPClient) InputStream(java.io.InputStream) Connection(com.trilead.ssh2.Connection) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) HttpException(org.apache.commons.httpclient.HttpException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Session(com.trilead.ssh2.Session)

Aggregations

Connection (org.ovirt.engine.sdk4.Connection)63 Connection (com.trilead.ssh2.Connection)52 IOException (java.io.IOException)41 VmsService (org.ovirt.engine.sdk4.services.VmsService)33 Session (com.trilead.ssh2.Session)32 Vm (org.ovirt.engine.sdk4.types.Vm)30 InputStream (java.io.InputStream)25 VmService (org.ovirt.engine.sdk4.services.VmService)18 SystemService (org.ovirt.engine.sdk4.services.SystemService)14 StorageDomainsService (org.ovirt.engine.sdk4.services.StorageDomainsService)12 StorageDomain (org.ovirt.engine.sdk4.types.StorageDomain)12 Connection (okhttp3.Connection)11 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)10 Request (okhttp3.Request)10 File (java.io.File)9 Response (okhttp3.Response)9 Connection (ch.ethz.ssh2.Connection)8 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)8 MediaType (okhttp3.MediaType)8 RequestBody (okhttp3.RequestBody)8