Search in sources :

Example 1 with Session

use of com.xensource.xenapi.Session in project cloudstack by apache.

the class XenServerConnectionPool method connect.

public Connection connect(String hostUuid, String poolUuid, String ipAddress, String username, Queue<String> password, int wait) {
    XenServerConnection mConn = null;
    if (hostUuid == null || poolUuid == null || ipAddress == null || username == null || password == null) {
        String msg = "Connect some parameter are null hostUuid:" + hostUuid + " ,poolUuid:" + poolUuid + " ,ipAddress:" + ipAddress;
        s_logger.debug(msg);
        throw new CloudRuntimeException(msg);
    }
    synchronized (poolUuid.intern()) {
        mConn = getConnect(poolUuid);
        if (mConn != null) {
            try {
                Host host = Host.getByUuid(mConn, hostUuid);
                if (!host.getEnabled(mConn)) {
                    String msg = "Cannot connect this host " + ipAddress + " due to the host is not enabled";
                    s_logger.debug(msg);
                    if (mConn.getIp().equalsIgnoreCase(ipAddress)) {
                        removeConnect(poolUuid);
                        mConn = null;
                    }
                    throw new CloudRuntimeException(msg);
                }
                return mConn;
            } catch (CloudRuntimeException e) {
                throw e;
            } catch (Exception e) {
                if (s_logger.isDebugEnabled()) {
                    s_logger.debug("connect through IP(" + mConn.getIp() + " for pool(" + poolUuid + ") is broken due to " + e.toString());
                }
                removeConnect(poolUuid);
                mConn = null;
            }
        }
        if (mConn == null) {
            try {
                Connection conn = new Connection(getURL(ipAddress), 5, _connWait);
                Session sess = loginWithPassword(conn, username, password, APIVersion.latest().toString());
                Host host = sess.getThisHost(conn);
                Boolean hostenabled = host.getEnabled(conn);
                if (sess != null) {
                    try {
                        Session.logout(conn);
                    } catch (Exception e) {
                        s_logger.debug("Caught exception during logout", e);
                    }
                    conn.dispose();
                }
                if (!hostenabled) {
                    String msg = "Unable to create master connection, due to master Host " + ipAddress + " is not enabled";
                    s_logger.debug(msg);
                    throw new CloudRuntimeException(msg);
                }
                mConn = new XenServerConnection(getURL(ipAddress), ipAddress, username, password, _retries, _interval, wait, _connWait);
                loginWithPassword(mConn, username, password, APIVersion.latest().toString());
            } catch (Types.HostIsSlave e) {
                String maddress = e.masterIPAddress;
                mConn = new XenServerConnection(getURL(maddress), maddress, username, password, _retries, _interval, wait, _connWait);
                try {
                    Session session = loginWithPassword(mConn, username, password, APIVersion.latest().toString());
                    Host host = session.getThisHost(mConn);
                    if (!host.getEnabled(mConn)) {
                        String msg = "Unable to create master connection, due to master Host " + maddress + " is not enabled";
                        s_logger.debug(msg);
                        throw new CloudRuntimeException(msg);
                    }
                } catch (Exception e1) {
                    String msg = "Unable to create master connection to host(" + maddress + ") , due to " + e1.toString();
                    s_logger.debug(msg);
                    throw new CloudRuntimeException(msg, e1);
                }
            } catch (CloudRuntimeException e) {
                throw e;
            } catch (Exception e) {
                String msg = "Unable to create master connection to host(" + ipAddress + ") , due to " + e.toString();
                s_logger.debug(msg);
                throw new CloudRuntimeException(msg, e);
            }
            addConnect(poolUuid, mConn);
        }
    }
    return mConn;
}
Also used : Types(com.xensource.xenapi.Types) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Connection(com.xensource.xenapi.Connection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) Host(com.xensource.xenapi.Host) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) XenAPIException(com.xensource.xenapi.Types.XenAPIException) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) FileNotFoundException(java.io.FileNotFoundException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) XmlRpcClientException(org.apache.xmlrpc.client.XmlRpcClientException) Session(com.xensource.xenapi.Session) SSLSession(javax.net.ssl.SSLSession)

Example 2 with Session

use of com.xensource.xenapi.Session in project cosmic by MissionCriticalCloud.

the class XenServerConnectionPool method slaveLocalLoginWithPassword.

protected Session slaveLocalLoginWithPassword(final Connection conn, final String username, final Queue<String> password) throws BadServerResponse, XenAPIException, XmlRpcException {
    Session s = null;
    boolean logged_in = false;
    Exception ex = null;
    while (!logged_in) {
        try {
            s = Session.slaveLocalLoginWithPassword(conn, username, password.peek());
            logged_in = true;
        } catch (final BadServerResponse e) {
            logged_in = false;
            ex = e;
        } catch (final XenAPIException e) {
            logged_in = false;
            ex = e;
        } catch (final XmlRpcException e) {
            logged_in = false;
            ex = e;
        }
        if (logged_in && conn != null) {
            break;
        } else {
            if (password.size() > 1) {
                password.remove();
                continue;
            } else {
                // the last password did not work leave it and flag error
                if (ex instanceof BadServerResponse) {
                    throw (BadServerResponse) ex;
                } else if (ex instanceof XmlRpcException) {
                    throw (XmlRpcException) ex;
                } else if (ex instanceof Types.SessionAuthenticationFailed) {
                    throw (Types.SessionAuthenticationFailed) ex;
                } else if (ex instanceof XenAPIException) {
                    throw (XenAPIException) ex;
                }
                break;
            }
        }
    }
    return s;
}
Also used : BadServerResponse(com.xensource.xenapi.Types.BadServerResponse) XenAPIException(com.xensource.xenapi.Types.XenAPIException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) XenAPIException(com.xensource.xenapi.Types.XenAPIException) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) FileNotFoundException(java.io.FileNotFoundException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) XmlRpcClientException(org.apache.xmlrpc.client.XmlRpcClientException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) Session(com.xensource.xenapi.Session) SSLSession(javax.net.ssl.SSLSession)

Example 3 with Session

use of com.xensource.xenapi.Session in project cosmic by MissionCriticalCloud.

the class XenServerConnectionPool method connect.

public Connection connect(final String hostUuid, final String poolUuid, final String ipAddress, final String username, final Queue<String> password, final int wait) {
    XenServerConnection mConn = null;
    if (hostUuid == null || poolUuid == null || ipAddress == null || username == null || password == null) {
        final String msg = "Connect some parameter are null hostUuid:" + hostUuid + " ,poolUuid:" + poolUuid + " ,ipAddress:" + ipAddress;
        s_logger.debug(msg);
        throw new CloudRuntimeException(msg);
    }
    synchronized (poolUuid.intern()) {
        mConn = getConnect(poolUuid);
        if (mConn != null) {
            try {
                final Host host = Host.getByUuid(mConn, hostUuid);
                if (!host.getEnabled(mConn)) {
                    final String msg = "Cannot connect this host " + ipAddress + " due to the host is not enabled";
                    s_logger.debug(msg);
                    if (mConn.getIp().equalsIgnoreCase(ipAddress)) {
                        removeConnect(poolUuid);
                        mConn = null;
                    }
                    throw new CloudRuntimeException(msg);
                }
                return mConn;
            } catch (final CloudRuntimeException e) {
                throw e;
            } catch (final Exception e) {
                if (s_logger.isDebugEnabled()) {
                    s_logger.debug("connect through IP(" + mConn.getIp() + " for pool(" + poolUuid + ") is broken due to " + e.toString());
                }
                removeConnect(poolUuid);
                mConn = null;
            }
        }
        if (mConn == null) {
            try {
                final Connection conn = new Connection(getURL(ipAddress), 5, _connWait);
                final Session sess = loginWithPassword(conn, username, password, APIVersion.latest().toString());
                final Host host = sess.getThisHost(conn);
                final Boolean hostenabled = host.getEnabled(conn);
                if (sess != null) {
                    try {
                        Session.logout(conn);
                    } catch (final Exception e) {
                        s_logger.debug("Caught exception during logout", e);
                    }
                    conn.dispose();
                }
                if (!hostenabled) {
                    final String msg = "Unable to create master connection, due to master Host " + ipAddress + " is not enabled";
                    s_logger.debug(msg);
                    throw new CloudRuntimeException(msg);
                }
                mConn = new XenServerConnection(getURL(ipAddress), ipAddress, username, password, _retries, _interval, wait, _connWait);
                loginWithPassword(mConn, username, password, APIVersion.latest().toString());
            } catch (final Types.HostIsSlave e) {
                final String maddress = e.masterIPAddress;
                mConn = new XenServerConnection(getURL(maddress), maddress, username, password, _retries, _interval, wait, _connWait);
                try {
                    final Session session = loginWithPassword(mConn, username, password, APIVersion.latest().toString());
                    final Host host = session.getThisHost(mConn);
                    if (!host.getEnabled(mConn)) {
                        final String msg = "Unable to create master connection, due to master Host " + maddress + " is not enabled";
                        s_logger.debug(msg);
                        throw new CloudRuntimeException(msg);
                    }
                } catch (final Exception e1) {
                    final String msg = "Unable to create master connection to host(" + maddress + ") , due to " + e1.toString();
                    s_logger.debug(msg);
                    throw new CloudRuntimeException(msg, e1);
                }
            } catch (final CloudRuntimeException e) {
                throw e;
            } catch (final Exception e) {
                final String msg = "Unable to create master connection to host(" + ipAddress + ") , due to " + e.toString();
                s_logger.debug(msg);
                throw new CloudRuntimeException(msg, e);
            }
            addConnect(poolUuid, mConn);
        }
    }
    return mConn;
}
Also used : Types(com.xensource.xenapi.Types) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Connection(com.xensource.xenapi.Connection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) Host(com.xensource.xenapi.Host) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) XenAPIException(com.xensource.xenapi.Types.XenAPIException) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) FileNotFoundException(java.io.FileNotFoundException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) XmlRpcClientException(org.apache.xmlrpc.client.XmlRpcClientException) Session(com.xensource.xenapi.Session) SSLSession(javax.net.ssl.SSLSession)

Example 4 with Session

use of com.xensource.xenapi.Session in project cosmic by MissionCriticalCloud.

the class XenServerConnectionPool method loginWithPassword.

protected Session loginWithPassword(final Connection conn, final String username, final Queue<String> password, final String version) throws BadServerResponse, XenAPIException, XmlRpcException {
    Session s = null;
    boolean logged_in = false;
    Exception ex = null;
    while (!logged_in) {
        try {
            s = Session.loginWithPassword(conn, username, password.peek(), APIVersion.latest().toString());
            logged_in = true;
        } catch (final BadServerResponse e) {
            logged_in = false;
            ex = e;
        } catch (final XenAPIException e) {
            logged_in = false;
            ex = e;
        } catch (final XmlRpcException e) {
            logged_in = false;
            ex = e;
        }
        if (logged_in && conn != null) {
            break;
        } else {
            if (password.size() > 1) {
                password.remove();
                continue;
            } else {
                // the last password did not work leave it and flag error
                if (ex instanceof BadServerResponse) {
                    throw (BadServerResponse) ex;
                } else if (ex instanceof XmlRpcException) {
                    throw (XmlRpcException) ex;
                } else if (ex instanceof Types.SessionAuthenticationFailed) {
                    throw (Types.SessionAuthenticationFailed) ex;
                } else if (ex instanceof XenAPIException) {
                    throw (XenAPIException) ex;
                }
            }
        }
    }
    return s;
}
Also used : BadServerResponse(com.xensource.xenapi.Types.BadServerResponse) XenAPIException(com.xensource.xenapi.Types.XenAPIException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) XenAPIException(com.xensource.xenapi.Types.XenAPIException) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) FileNotFoundException(java.io.FileNotFoundException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) XmlRpcClientException(org.apache.xmlrpc.client.XmlRpcClientException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) Session(com.xensource.xenapi.Session) SSLSession(javax.net.ssl.SSLSession)

Example 5 with Session

use of com.xensource.xenapi.Session in project cloudstack by apache.

the class XenServerConnectionPool method slaveLocalLoginWithPassword.

protected Session slaveLocalLoginWithPassword(Connection conn, String username, Queue<String> password) throws BadServerResponse, XenAPIException, XmlRpcException {
    Session s = null;
    boolean logged_in = false;
    Exception ex = null;
    while (!logged_in) {
        try {
            s = Session.slaveLocalLoginWithPassword(conn, username, password.peek());
            logged_in = true;
        } catch (BadServerResponse e) {
            logged_in = false;
            ex = e;
        } catch (XenAPIException e) {
            logged_in = false;
            ex = e;
        } catch (XmlRpcException e) {
            logged_in = false;
            ex = e;
        }
        if (logged_in && conn != null) {
            break;
        } else {
            if (password.size() > 1) {
                password.remove();
                continue;
            } else {
                // the last password did not work leave it and flag error
                if (ex instanceof BadServerResponse) {
                    throw (BadServerResponse) ex;
                } else if (ex instanceof XmlRpcException) {
                    throw (XmlRpcException) ex;
                } else if (ex instanceof Types.SessionAuthenticationFailed) {
                    throw (Types.SessionAuthenticationFailed) ex;
                } else if (ex instanceof XenAPIException) {
                    throw (XenAPIException) ex;
                }
                break;
            }
        }
    }
    return s;
}
Also used : BadServerResponse(com.xensource.xenapi.Types.BadServerResponse) XenAPIException(com.xensource.xenapi.Types.XenAPIException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) XenAPIException(com.xensource.xenapi.Types.XenAPIException) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) FileNotFoundException(java.io.FileNotFoundException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) XmlRpcClientException(org.apache.xmlrpc.client.XmlRpcClientException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) Session(com.xensource.xenapi.Session) SSLSession(javax.net.ssl.SSLSession)

Aggregations

CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)6 Session (com.xensource.xenapi.Session)6 XenAPIException (com.xensource.xenapi.Types.XenAPIException)6 FileNotFoundException (java.io.FileNotFoundException)6 IOException (java.io.IOException)6 KeyManagementException (java.security.KeyManagementException)6 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)6 SSLSession (javax.net.ssl.SSLSession)6 XmlRpcException (org.apache.xmlrpc.XmlRpcException)6 XmlRpcClientException (org.apache.xmlrpc.client.XmlRpcClientException)6 BadServerResponse (com.xensource.xenapi.Types.BadServerResponse)4 Connection (com.xensource.xenapi.Connection)2 Host (com.xensource.xenapi.Host)2 Types (com.xensource.xenapi.Types)2 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)2