From 6419e4997722f5945d396d774650fce034dea16a Mon Sep 17 00:00:00 2001 From: anthony Date: Thu, 16 Dec 2010 16:25:43 -0800 Subject: [PATCH] add guid in cluster table --- .../xen/resource/XenServerConnectionPool.java | 4 +- server/src/com/cloud/dc/ClusterVO.java | 14 +++++- server/src/com/cloud/dc/dao/ClusterDao.java | 1 + .../src/com/cloud/dc/dao/ClusterDaoImpl.java | 15 +++++- .../xen/discoverer/XcpServerDiscoverer.java | 22 +++++--- .../migration/Db21to22MigrationUtil.java | 50 ++++++++++++++++++- setup/db/create-schema.sql | 1 + setup/db/schema-21to22.sql | 4 +- 8 files changed, 96 insertions(+), 15 deletions(-) diff --git a/core/src/com/cloud/hypervisor/xen/resource/XenServerConnectionPool.java b/core/src/com/cloud/hypervisor/xen/resource/XenServerConnectionPool.java index 31b9f7cb643..182f5a44431 100644 --- a/core/src/com/cloud/hypervisor/xen/resource/XenServerConnectionPool.java +++ b/core/src/com/cloud/hypervisor/xen/resource/XenServerConnectionPool.java @@ -247,7 +247,7 @@ public class XenServerConnectionPool { return null; } - public Connection slaveConnect(String ip, String username, String password) { + static public Connection slaveConnect(String ip, String username, String password) { Connection conn = null; try{ conn = new Connection(getURL(ip), 100); @@ -579,7 +579,7 @@ public class XenServerConnectionPool { } } - static private Pool.Record getPoolRecord(Connection conn) + static public Pool.Record getPoolRecord(Connection conn) throws XmlRpcException, XenAPIException { Map pools = Pool.getAllRecords(conn); assert pools.size() == 1 : "Pool size is not one....hmmm....wth? " diff --git a/server/src/com/cloud/dc/ClusterVO.java b/server/src/com/cloud/dc/ClusterVO.java index f1fc7068122..d1544fcb0be 100644 --- a/server/src/com/cloud/dc/ClusterVO.java +++ b/server/src/com/cloud/dc/ClusterVO.java @@ -42,6 +42,9 @@ public class ClusterVO implements Cluster { @Column(name="name") String name; + @Column(name="guid") + String guid; + @Column(name="data_center_id") long dataCenterId; @@ -112,7 +115,16 @@ public class ClusterVO implements Cluster { return HypervisorType.getType(hypervisorType); } - public void setHypervisorType(String hy) { + public void setHypervisorType(String hy) { hypervisorType = hy; } + + public String getGuid() { + return guid; + } + + public void setGuid(String guid) { + this.guid = guid; + } + } diff --git a/server/src/com/cloud/dc/dao/ClusterDao.java b/server/src/com/cloud/dc/dao/ClusterDao.java index 759a566ac26..35a3f563836 100644 --- a/server/src/com/cloud/dc/dao/ClusterDao.java +++ b/server/src/com/cloud/dc/dao/ClusterDao.java @@ -25,4 +25,5 @@ import com.cloud.utils.db.GenericDao; public interface ClusterDao extends GenericDao { List listByPodId(long podId); ClusterVO findBy(String name, long podId); + List listByHyTypeWithoutGuid(String hyType); } diff --git a/server/src/com/cloud/dc/dao/ClusterDaoImpl.java b/server/src/com/cloud/dc/dao/ClusterDaoImpl.java index ffb4d2b6fc5..d400df284af 100644 --- a/server/src/com/cloud/dc/dao/ClusterDaoImpl.java +++ b/server/src/com/cloud/dc/dao/ClusterDaoImpl.java @@ -30,10 +30,15 @@ import com.cloud.utils.db.SearchCriteria; public class ClusterDaoImpl extends GenericDaoBase implements ClusterDao { protected final SearchBuilder PodSearch; - + protected final SearchBuilder HyTypeWithoutGuidSearch; protected ClusterDaoImpl() { super(); + HyTypeWithoutGuidSearch = createSearchBuilder(); + HyTypeWithoutGuidSearch.and("hypervisorType", HyTypeWithoutGuidSearch.entity().getHypervisorType(), SearchCriteria.Op.EQ); + HyTypeWithoutGuidSearch.and("guid", HyTypeWithoutGuidSearch.entity().getGuid(), SearchCriteria.Op.NULL); + HyTypeWithoutGuidSearch.done(); + PodSearch = createSearchBuilder(); PodSearch.and("pod", PodSearch.entity().getPodId(), SearchCriteria.Op.EQ); PodSearch.and("name", PodSearch.entity().getName(), SearchCriteria.Op.EQ); @@ -56,4 +61,12 @@ public class ClusterDaoImpl extends GenericDaoBase implements C return findOneBy(sc); } + + @Override + public List listByHyTypeWithoutGuid(String hyType) { + SearchCriteria sc = HyTypeWithoutGuidSearch.create(); + sc.setParameters("hypervisorType", hyType); + + return listBy(sc); + } } diff --git a/server/src/com/cloud/hypervisor/xen/discoverer/XcpServerDiscoverer.java b/server/src/com/cloud/hypervisor/xen/discoverer/XcpServerDiscoverer.java index 8daf0d2d450..9da372acb5e 100644 --- a/server/src/com/cloud/hypervisor/xen/discoverer/XcpServerDiscoverer.java +++ b/server/src/com/cloud/hypervisor/xen/discoverer/XcpServerDiscoverer.java @@ -156,6 +156,17 @@ public class XcpServerDiscoverer extends DiscovererBase implements Discoverer, L Pool.Record pr = pool.getRecord(conn); poolUuid = pr.uuid; Host master = pr.master; + + /*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 ) { + clu.setGuid(poolUuid); + } + _clusterDao.update(clusterId, clu); + LinkedHashMap hosts = new LinkedHashMap(20); hosts.put(master, master.getRecord(conn)); Map thosts = Host.getAllRecords(conn); @@ -164,7 +175,7 @@ public class XcpServerDiscoverer extends DiscovererBase implements Discoverer, L hosts.put(entry.getKey(), entry.getValue()); } } - + if (_checkHvm) { for (Map.Entry entry : hosts.entrySet()) { Host.Record record = entry.getValue(); @@ -269,12 +280,7 @@ public class XcpServerDiscoverer extends DiscovererBase implements Discoverer, L if (!addHostsToPool(url, conn, dcId, podId, clusterId, resources)) { return null; } - - /*set cluster hypervisor type to xenserver*/ - ClusterVO clu = _clusterDao.findById(clusterId); - clu.setHypervisorType(HypervisorType.XenServer.toString()); - _clusterDao.update(clusterId, clu); - + } catch (SessionAuthenticationFailed e) { s_logger.warn("Authentication error", e); return null; @@ -511,7 +517,7 @@ public class XcpServerDiscoverer extends DiscovererBase implements Discoverer, L public boolean processCommands(long agentId, long seq, Command[] commands) { return false; } - + private void createXsToolsISO() { String isoName = "xs-tools.iso"; VMTemplateVO tmplt = _tmpltDao.findByTemplateName(isoName); diff --git a/server/src/com/cloud/migration/Db21to22MigrationUtil.java b/server/src/com/cloud/migration/Db21to22MigrationUtil.java index d9114477639..78007068c3c 100644 --- a/server/src/com/cloud/migration/Db21to22MigrationUtil.java +++ b/server/src/com/cloud/migration/Db21to22MigrationUtil.java @@ -1,6 +1,7 @@ package com.cloud.migration; import java.io.File; +import java.net.InetAddress; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.List; @@ -12,11 +13,17 @@ import com.cloud.configuration.ConfigurationVO; import com.cloud.configuration.ResourceCountVO; import com.cloud.configuration.dao.ConfigurationDao; import com.cloud.configuration.dao.ResourceCountDao; +import com.cloud.dc.ClusterVO; import com.cloud.dc.DataCenterVO; import com.cloud.dc.DataCenter.NetworkType; +import com.cloud.dc.dao.ClusterDao; import com.cloud.dc.dao.DataCenterDao; import com.cloud.domain.DomainVO; import com.cloud.domain.dao.DomainDao; +import com.cloud.host.HostVO; +import com.cloud.host.dao.HostDao; +import com.cloud.hypervisor.Hypervisor.HypervisorType; +import com.cloud.hypervisor.xen.resource.XenServerConnectionPool; import com.cloud.user.Account; import com.cloud.user.dao.AccountDao; import com.cloud.utils.PropertiesUtil; @@ -29,8 +36,13 @@ import com.cloud.vm.InstanceGroupVMMapVO; import com.cloud.vm.InstanceGroupVO; import com.cloud.vm.dao.InstanceGroupDao; import com.cloud.vm.dao.InstanceGroupVMMapDao; +import com.xensource.xenapi.Connection; +import com.xensource.xenapi.Pool; +import com.xensource.xenapi.Session; public class Db21to22MigrationUtil { + private ClusterDao _clusterDao; + private HostDao _hostDao; private AccountDao _accountDao; private DomainDao _domainDao; private ResourceCountDao _resourceCountDao; @@ -48,9 +60,45 @@ public class Db21to22MigrationUtil { migrateZones(); + setupClusterGuid(); + System.out.println("Migration done"); } - + + /* add guid in cluster table */ + private void setupClusterGuid() { + List clusters = _clusterDao.listByHyTypeWithoutGuid(HypervisorType.XenServer.toString()); + for (ClusterVO cluster : clusters) { + List hosts = _hostDao.listByCluster(cluster.getId()); + for (HostVO host : hosts) { + String ip = host.getPrivateIpAddress(); + String username = host.getDetail("username"); + String password = host.getDetail("password"); + if (ip == null || ip.isEmpty() || username == null || username.isEmpty() || password == null + || password.isEmpty()) { + continue; + } + Connection conn = XenServerConnectionPool.slaveConnect(ip, username, password); + if (conn == null) + continue; + Pool.Record pr = null; + try { + pr = XenServerConnectionPool.getPoolRecord(conn); + } catch (Exception e) { + continue; + } finally { + try { + Session.localLogout(conn); + } catch (Exception e) { + } + } + cluster.setGuid(pr.uuid); + _clusterDao.update(cluster.getId(), cluster); + break; + } + } + } + /** * This method migrates the zones based on bug: 7204 * based on the param direct.attach.untagged.vlan.enabled, we update zone to basic or advanced for 2.2 diff --git a/setup/db/create-schema.sql b/setup/db/create-schema.sql index cb372ed8f7d..25c52b499d1 100755 --- a/setup/db/create-schema.sql +++ b/setup/db/create-schema.sql @@ -217,6 +217,7 @@ CREATE TABLE `cloud`.`network_offerings` ( CREATE TABLE `cloud`.`cluster` ( `id` bigint unsigned NOT NULL UNIQUE AUTO_INCREMENT COMMENT 'id', `name` varchar(255) NOT NULL COMMENT 'name for the cluster', + `guid` varchar(255) UNIQUE DEFAULT NULL COMMENT 'guid for the cluster', `pod_id` bigint unsigned NOT NULL COMMENT 'pod id', `data_center_id` bigint unsigned NOT NULL COMMENT 'data center id', `hypervisor_type` varchar(255), diff --git a/setup/db/schema-21to22.sql b/setup/db/schema-21to22.sql index 92296839550..c44f43e0a57 100644 --- a/setup/db/schema-21to22.sql +++ b/setup/db/schema-21to22.sql @@ -5,7 +5,8 @@ SET foreign_key_checks = 0; -- ALTER TABLE `cloud`.`template_host_ref` ADD COLUMN `physical_size` bigint unsigned NOT NULL DEFAULT 0 ALTER TABLE `cloud`.`snapshots` MODIFY COLUMN `id` bigint unsigned UNIQUE NOT NULL -ALTER TABLE `vm_instance` DROP COLUMN `group` +ALTER TABLE `cloud`.`vm_instance` DROP COLUMN `group` +ALTER TABLE `cloud`.`cluster` ADD COLUMN `guid` varchar(255) UNIQUE DEFAULT NULL -- NOTE for tables below @@ -16,4 +17,3 @@ ALTER TABLE `vm_instance` DROP COLUMN `group` -- network_group_vm_map table --> security_group_vm_map table DROP TABLE `cloud`.`security_group`; DROP TABLE `cloud`.`security_group_vm_map`; ---END NOTE