pass pooluuid to xen.configure

This commit is contained in:
anthony 2010-12-16 18:51:21 -08:00
parent 8ffe2ecf62
commit 7faf41e04e
4 changed files with 117 additions and 241 deletions

View File

@ -25,7 +25,6 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
@ -174,7 +173,6 @@ import com.cloud.vm.State;
import com.cloud.vm.VirtualMachine;
import com.cloud.vm.VirtualMachineName;
import com.trilead.ssh2.SCPClient;
import com.xensource.xenapi.APIVersion;
import com.xensource.xenapi.Bond;
import com.xensource.xenapi.Connection;
import com.xensource.xenapi.Console;
@ -272,10 +270,6 @@ public abstract class CitrixResourceBase implements ServerResource {
@Override
public void disconnected() {
s_logger.debug("Logging out of " + _host.uuid);
if (_host.pool != null) {
_connPool.disconnect(_host.uuid, _host.pool);
}
}
protected VDI cloudVDIcopy(Connection conn, VDI vdi, SR sr) throws BadServerResponse, XenAPIException, XmlRpcException{
@ -2387,109 +2381,7 @@ public abstract class CitrixResourceBase implements ServerResource {
return answer;
}
public boolean joinPool(String masterIp, String username, String password) {
Connection hostConn = null;
Connection poolConn = null;
Session hostSession = null;
URL hostUrl = null;
try {
// Connect and find out about the new connection to the new pool.
poolConn = _connPool.masterConnect(masterIp, username, password);
Set<Pool> pools = Pool.getAll(poolConn);
Pool pool = pools.iterator().next();
String poolUUID = pool.getUuid(poolConn);
//check if this host is already in pool
Set<Host> hosts = Host.getAll(poolConn);
for( Host host : hosts ) {
if(host.getAddress(poolConn).equals(_host.ip)) {
_host.pool = poolUUID;
return true;
}
}
hostUrl = new URL("http://" + _host.ip);
hostConn = new Connection(hostUrl, 100);
hostSession = Session.loginWithPassword(hostConn, _username, _password, APIVersion.latest().toString());
// Now join it.
Pool.join(hostConn, masterIp, username, password);
if (s_logger.isDebugEnabled()) {
s_logger.debug("Joined the pool at " + masterIp);
}
try {
// slave will restart xapi in 10 sec
Thread.sleep(10000);
} catch (InterruptedException e) {
}
// check if the master of this host is set correctly.
Connection c = new Connection(hostUrl, 100);
int i;
for (i = 0 ; i < 15; i++) {
try {
Session.loginWithPassword(c, _username, _password, APIVersion.latest().toString());
s_logger.debug(_host.ip + " is still master, waiting for the conversion to the slave");
Session.logout(c);
c.dispose();
} catch (Types.HostIsSlave e) {
try {
Session.logout(c);
c.dispose();
} catch (XmlRpcException e1) {
s_logger.debug("Unable to logout of test connection due to " + e1.getMessage());
} catch (XenAPIException e1) {
s_logger.debug("Unable to logout of test connection due to " + e1.getMessage());
}
break;
} catch (XmlRpcException e) {
s_logger.debug("XmlRpcException: Still waiting for the conversion to the master");
} catch (Exception e) {
s_logger.debug("Exception: Still waiting for the conversion to the master");
}
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
}
}
if( i >= 15 ) {
throw new CloudRuntimeException(_host.ip + " didn't change to slave after waiting 30 secondary");
}
_host.pool = poolUUID;
return true;
} catch (MalformedURLException e) {
throw new CloudRuntimeException("Problem with url " + _host.ip);
} catch (XenAPIException e) {
String msg = "Unable to allow host " + _host.uuid
+ " to join pool " + masterIp + " due to " + e.toString();
s_logger.warn(msg, e);
throw new RuntimeException(msg);
} catch (XmlRpcException e) {
String msg = "Unable to allow host " + _host.uuid
+ " to join pool " + masterIp + " due to " + e.getMessage();
s_logger.warn(msg, e);
throw new RuntimeException(msg);
} finally {
if (poolConn != null) {
try {
Session.logout(poolConn);
} catch (Exception e) {
}
poolConn.dispose();
}
if(hostSession != null) {
try {
Session.logout(hostConn);
} catch (Exception e) {
}
}
}
}
protected void startvmfailhandle(Connection conn, VM vm, List<Ternary<SR, VDI, VolumeVO>> mounts) {
if (vm != null) {
@ -3505,19 +3397,6 @@ public abstract class CitrixResourceBase implements ServerResource {
return new StartupCommand[] { cmd };
}
protected String getPoolUuid(Connection conn) {
try {
Map<Pool, Pool.Record> pools = Pool.getAllRecords(conn);
assert (pools.size() == 1) : "Tell me how pool size can be " + pools.size();
Pool.Record rec = pools.values().iterator().next();
return rec.uuid;
} catch (XenAPIException e) {
throw new CloudRuntimeException("Unable to get pool ", e);
} catch (XmlRpcException e) {
throw new CloudRuntimeException("Unable to get pool ", e);
}
}
protected void setupServer(Connection conn) {
String version = CitrixResourceBase.class.getPackage().getImplementationVersion();
@ -3913,7 +3792,7 @@ public abstract class CitrixResourceBase implements ServerResource {
throw new ConfigurationException("Unable to get the zone " + params.get("zone"));
}
_name = _host.uuid;
_host.ip = (String) params.get("url");
_host.ip = (String) params.get("ipaddress");
_host.pool = (String) params.get("pool");
_username = (String) params.get("username");
_password = (String) params.get("password");

View File

@ -17,6 +17,7 @@
*/
package com.cloud.hypervisor.xen.resource;
import java.net.MalformedURLException;
import java.net.SocketException;
import java.net.URL;
import java.util.HashMap;
@ -96,6 +97,64 @@ public class XenServerConnectionPool {
}
}
static public boolean joinPool(Connection conn, String hostIp, String masterIp, String username, String password) {
try {
Pool.join(conn, masterIp, username, password);
if (s_logger.isDebugEnabled()) {
s_logger.debug("Host(" + hostIp + ") Join the pool at " + masterIp);
}
try {
// slave will restart xapi in 10 sec
Thread.sleep(10000);
} catch (InterruptedException e) {
}
for (int i = 0 ; i < 15; i++) {
Connection slaveConn = null;
Session slaveSession = null;
try {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Logging on as the slave to " + hostIp);
}
slaveConn = new Connection(getURL(hostIp), 100);
slaveSession = Session.slaveLocalLoginWithPassword(slaveConn, username, password);
if (s_logger.isDebugEnabled()) {
s_logger.debug("Slave logon successful. session= " + slaveSession);
}
Pool.Record pr = getPoolRecord(slaveConn);
Host master = pr.master;
String ma = master.getAddress(slaveConn);
if (ma.trim().equals(masterIp.trim())) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Host(" + hostIp + ") Joined the pool at " + masterIp);
}
return true;
}
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
}
} catch (Exception e) {
} finally {
if (slaveSession != null) {
try {
Session.logout(slaveConn);
} catch (Exception e) {
}
slaveConn.dispose();
}
}
}
} catch (Exception e) {
String msg = "Catch " + e.getClass().getName() + " Unable to allow host " + hostIp + " to join pool " + masterIp + " due to " + e.toString();
s_logger.warn(msg, e);
}
if (s_logger.isDebugEnabled()) {
s_logger.debug("Host(" + hostIp + ") unable to Join the pool at " + masterIp);
}
return false;
}
public void switchMaster(String slaveIp, String poolUuid,
Connection conn, Host host, String username, String password,
int wait) throws XmlRpcException, XenAPIException {
@ -182,11 +241,6 @@ public class XenServerConnectionPool {
}
}
public void disconnect(String uuid, String poolUuid) {
}
public static void logout(Connection conn) {
try {
s_logger.debug("Logging out of the session "

View File

@ -1366,6 +1366,18 @@ public class AgentManagerImpl implements AgentManager, HandlerFactory, ResourceS
if (host.getClusterId() != null) {
params.put("cluster", Long.toString(host.getClusterId()));
}
String guid = null;
ClusterVO cluster = _clusterDao.findById(host.getClusterId());
if ( cluster.getGuid() == null ) {
guid = host.getDetail("pool");
} else {
guid = cluster.getGuid();
}
if( guid == null || guid.isEmpty() ) {
throw new CloudRuntimeException("Can not find guid for cluster " + cluster.getId() + " name " + cluster.getName());
}
params.put("pool", guid);
params.put("ipaddress", host.getPrivateIpAddress());
params.put("secondary.storage.vm", "false");
params.put("max.template.iso.size", _configDao.getValue("max.template.iso.size"));

View File

@ -20,8 +20,6 @@ package com.cloud.hypervisor.xen.discoverer;
import java.net.InetAddress;
import java.net.URI;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
@ -41,7 +39,6 @@ import com.cloud.agent.api.AgentControlCommand;
import com.cloud.agent.api.Answer;
import com.cloud.agent.api.Command;
import com.cloud.agent.api.StartupCommand;
import com.cloud.agent.api.StartupStorageCommand;
import com.cloud.alert.AlertManager;
import com.cloud.configuration.Config;
import com.cloud.dc.ClusterVO;
@ -63,12 +60,9 @@ import com.cloud.resource.DiscovererBase;
import com.cloud.resource.ServerResource;
import com.cloud.storage.Storage.ImageFormat;
import com.cloud.storage.Storage.TemplateType;
import com.cloud.storage.VMTemplateHostVO;
import com.cloud.storage.VMTemplateStorageResourceAssoc;
import com.cloud.storage.VMTemplateVO;
import com.cloud.storage.dao.VMTemplateDao;
import com.cloud.storage.dao.VMTemplateHostDao;
import com.cloud.storage.template.TemplateInfo;
import com.cloud.user.Account;
import com.cloud.utils.NumbersUtil;
import com.cloud.utils.component.Inject;
@ -112,64 +106,70 @@ public class XcpServerDiscoverer extends DiscovererBase implements Discoverer, L
s_logger.debug(msg);
return null;
}
String cluster = null;
if (clusterId == null) {
String msg = "must specify cluster Id when add host";
s_logger.debug(msg);
throw new RuntimeException(msg);
} else {
cluster = Long.toString(clusterId);
}
}
String pod;
if (podId == null) {
String msg = "must specify pod Id when add host";
s_logger.debug(msg);
throw new RuntimeException(msg);
} else {
pod = Long.toString(podId);
}
try {
String poolUuid = null;
List<HostVO> eHosts = _hostDao.listByCluster(clusterId);
if( eHosts.size() > 0 ) {
HostVO eHost = eHosts.get(0);
_hostDao.loadDetails(eHost);
poolUuid = eHost.getDetail("pool");
}
String hostname = url.getHost();
InetAddress ia = InetAddress.getByName(hostname);
String addr = ia.getHostAddress();
String hostIp = ia.getHostAddress();
conn = _connPool.masterConnect(addr, username, password);
conn = _connPool.masterConnect(hostIp, username, password);
if (conn == null) {
String msg = "Unable to get a connection to " + url;
s_logger.debug(msg);
return null;
throw new DiscoveryException(msg);
}
Set<Pool> pools = Pool.getAll(conn);
Pool pool = pools.iterator().next();
Pool.Record pr = pool.getRecord(conn);
poolUuid = pr.uuid;
Pool.Record pr = pool.getRecord(conn);
String poolUuid = pr.uuid;
Host master = pr.master;
Map<Host, Host.Record> thosts = Host.getAllRecords(conn);
/*set cluster hypervisor type to xenserver*/
ClusterVO clu = _clusterDao.findById(clusterId);
if ( HypervisorType.None == clu.getHypervisorType() ) {
clu.setHypervisorType(HypervisorType.XenServer.toString());
}
if ( clu.getClusterType() == null ) {
if ( clu.getGuid()== null ) {
clu.setGuid(poolUuid);
} else {
if( !clu.getGuid().equals(poolUuid)) {
if (thosts.size() == 1 ) {
if( !addHostsToPool(conn, hostIp, clusterId)){
String msg = "Unable to add host(" + hostIp + ") to cluster " + clusterId;
s_logger.warn(msg);
throw new DiscoveryException(msg);
}
} else {
String msg = "Host (" + hostIp + ") is already in pool(" + poolUuid +"), can to join pool(" + clu.getGuid() + ")";
s_logger.warn(msg);
throw new DiscoveryException(msg);
}
}
}
poolUuid = clu.getGuid();
if ( HypervisorType.None == clu.getHypervisorType() ) {
clu.setHypervisorType(HypervisorType.XenServer.toString());
}
_clusterDao.update(clusterId, clu);
LinkedHashMap<Host, Host.Record> hosts = new LinkedHashMap<Host, Host.Record>(20);
hosts.put(master, master.getRecord(conn));
Map<Host, Host.Record> thosts = Host.getAllRecords(conn);
for (Map.Entry<Host, Host.Record> entry : thosts.entrySet()) {
if( !master.equals(entry.getKey()) ) {
hosts.put(entry.getKey(), entry.getValue());
@ -217,18 +217,17 @@ public class XcpServerDiscoverer extends DiscovererBase implements Discoverer, L
Map<String, String> details = new HashMap<String, String>();
Map<String, Object> params = new HashMap<String, Object>();
details.put("url", hostAddr);
params.put("url", hostAddr);
details.put("pool", poolUuid);
params.put("pool", poolUuid);
details.put("username", username);
params.put("username", username);
details.put("password", password);
params.put("password", password);
params.put("zone", Long.toString(dcId));
params.put("guid", record.uuid);
params.put("pod", pod);
params.put("cluster", cluster);
params.put("pod", podId.toString());
params.put("cluster", clusterId.toString());
params.put("pool", poolUuid);
params.put("ipaddress", record.address);
if (_increase != null) {
params.put(Config.XenPreallocatedLunSizeRange.name(), _increase);
}
@ -260,9 +259,7 @@ public class XcpServerDiscoverer extends DiscovererBase implements Discoverer, L
if (!params.containsKey("storage.network.device2") && _storageNic2 != null) {
params.put("storage.network.device2", _storageNic2);
details.put("storage.network.device2", _storageNic2);
}
}
params.put(Config.Wait.toString().toLowerCase(), Integer.toString(_wait));
details.put(Config.Wait.toString().toLowerCase(), Integer.toString(_wait));
try {
@ -272,15 +269,9 @@ public class XcpServerDiscoverer extends DiscovererBase implements Discoverer, L
s_logger.warn("Unable to instantiate " + record.address, e);
continue;
}
resource.start();
resources.put(resource, details);
}
if (!addHostsToPool(url, conn, dcId, podId, clusterId, resources)) {
return null;
}
}
} catch (SessionAuthenticationFailed e) {
s_logger.warn("Authentication error", e);
return null;
@ -327,76 +318,32 @@ public class XcpServerDiscoverer extends DiscovererBase implements Discoverer, L
}
}
protected boolean addHostsToPool(URI url, Connection conn, long dcId, Long podId, Long clusterId, Map<? extends CitrixResourceBase, Map<String, String>> resources) throws XenAPIException, XmlRpcException, DiscoveryException {
if ( resources.size() == 0 ) {
return false;
}
if (clusterId == null ) {
if (resources.size() > 1) {
s_logger.warn("There's no cluster specified but we found a pool of xenservers " + resources.size());
throw new DiscoveryException("There's no cluster specified but we found a pool of xenservers " + resources.size());
} else if (resources.size() == 1) {
s_logger.debug("No cluster specified and we found only one host so no pool");
return true;
}
}
protected boolean addHostsToPool(Connection conn, String hostIp, Long clusterId) throws XenAPIException, XmlRpcException, DiscoveryException {
List<HostVO> hosts;
String poolLabel;
String poolDescription;
if (clusterId != null) {
hosts = _hostDao.listByCluster(clusterId);
ClusterVO cluster = _clusterDao.findById(clusterId);
poolLabel = "cluster-" + clusterId;
poolDescription = cluster.getName();
} else if (podId != null) {
hosts = _hostDao.listByHostPod(podId);
poolLabel = "pod-" + podId;
poolDescription = "Auto-Created Pool from Pod";
} else {
hosts= new ArrayList<HostVO>();
poolLabel = "cluster-self-created";
poolDescription = "Auto-Created Pool";
}
if (hosts.size() == 0) {
Set<Pool> pools = Pool.getAll(conn);
Pool pool = pools.iterator().next();
pool.setNameLabel(conn, poolLabel);
pool.setNameDescription(conn, poolDescription);
return true;
}
/*
if (hosts.size() + resources.size() > 16) {
s_logger.debug("A pool can only have 16 hosts");
throw new DiscoveryException("A XenServer cluster can only have 16 hosts maximum");
}
*/
String poolUuid1 = null;
String poolMaster = null;
hosts = _hostDao.listByCluster(clusterId);
String masterIp = null;
String username = null;
String password = null;
String address = null;
for (HostVO host : hosts) {
_hostDao.loadDetails(host);
username = host.getDetail("username");
password = host.getDetail("password");
address = host.getDetail("url");
Connection hostConn = _connPool.slaveConnect(address, username, password);
String address = host.getPrivateIpAddress();
Connection hostConn = XenServerConnectionPool.slaveConnect(address, username, password);
if (hostConn == null) {
continue;
}
try {
Set<Pool> pools = Pool.getAll(hostConn);
Pool pool = pools.iterator().next();
poolUuid1 = pool.getUuid(hostConn);
poolMaster = pool.getMaster(hostConn).getAddress(hostConn);
masterIp = pool.getMaster(hostConn).getAddress(hostConn);
break;
} catch (Exception e ) {
s_logger.warn("Can not get master ip address from host " + address);
}
finally {
} finally {
try{
Session.localLogout(hostConn);
} catch (Exception e ) {
@ -406,31 +353,15 @@ public class XcpServerDiscoverer extends DiscovererBase implements Discoverer, L
}
}
if (poolMaster == null) {
if (masterIp == null) {
s_logger.warn("Unable to reach the pool master of the existing cluster");
throw new CloudRuntimeException("Unable to reach the pool master of the existing cluster");
}
Set<Pool> pools = Pool.getAll(conn);
Pool pool = pools.iterator().next();
String poolUuid2 = pool.getUuid(conn);
if (resources.size() > 1 && !poolUuid1.equals(poolUuid2)) {
s_logger.debug("Can't add a pool of servers into an existing pool");
throw new DiscoveryException("Can't add a pool of servers into an existing pool");
}
if (poolUuid1.equals(poolUuid2)) {
s_logger.debug("The hosts that are discovered are already in the same pool as existing hosts");
return true;
}
CitrixResourceBase resource = resources.keySet().iterator().next();
if (!resource.joinPool(poolMaster, username, password)) {
if( !XenServerConnectionPool.joinPool(conn, hostIp, masterIp, username, password) ){
s_logger.warn("Unable to join the pool");
throw new DiscoveryException("Unable to join the pool");
}
}
return true;
}