CLOUDSTACK-1963 New mapping model for CloudStack zone and Vmware datacenter Upgrade changes with legacy zone support
Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/f78c2a3c Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/f78c2a3c Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/f78c2a3c Branch: refs/heads/vmware-datamodel Commit: f78c2a3c4e6a6016a2f086a6595f08b84e07a9ae Parents: fba7713 Author: Sateesh Chodapuneedi <sate...@apache.org> Authored: Mon May 6 15:59:24 2013 +0530 Committer: Sateesh Chodapuneedi <sate...@apache.org> Committed: Sun May 19 08:52:03 2013 +0530 ---------------------------------------------------------------------- .../src/com/cloud/upgrade/dao/Upgrade410to420.java | 160 +++++++++++++++ .../cloud/hypervisor/vmware/VmwareDatacenter.java | 4 - 2 files changed, 160 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f78c2a3c/engine/schema/src/com/cloud/upgrade/dao/Upgrade410to420.java ---------------------------------------------------------------------- diff --git a/engine/schema/src/com/cloud/upgrade/dao/Upgrade410to420.java b/engine/schema/src/com/cloud/upgrade/dao/Upgrade410to420.java index c03d377..7baa1ab 100644 --- a/engine/schema/src/com/cloud/upgrade/dao/Upgrade410to420.java +++ b/engine/schema/src/com/cloud/upgrade/dao/Upgrade410to420.java @@ -18,6 +18,7 @@ package com.cloud.upgrade.dao; import com.cloud.deploy.DeploymentPlanner; +import com.cloud.hypervisor.Hypervisor.HypervisorType; import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.utils.script.Script; import org.apache.log4j.Logger; @@ -28,6 +29,8 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Types; +import java.util.ArrayList; +import java.util.List; import java.util.UUID; import com.cloud.network.vpc.NetworkACL; @@ -62,6 +65,7 @@ public class Upgrade410to420 implements DbUpgrade { @Override public void performDataMigration(Connection conn) { upgradeVmwareLabels(conn); + persistLegacyZones(conn); createPlaceHolderNics(conn); updateRemoteAccessVpn(conn); updateSystemVmTemplates(conn); @@ -245,6 +249,162 @@ public class Upgrade410to420 implements DbUpgrade { } } + private void persistLegacyZones(Connection conn) { + List<Long> listOfLegacyZones = new ArrayList<Long>(); + PreparedStatement pstmt = null; + PreparedStatement clustersQuery = null; + PreparedStatement clusterDetailsQuery = null; + ResultSet rs = null; + ResultSet clusters = null; + ResultSet clusterDetails = null; + ResultSet dcInfo = null; + Long vmwareDcId = 1L; + Long zoneId; + Long clusterId; + String clusterHypervisorType; + boolean legacyZone; + boolean ignoreZone; + Long count; + String dcOfPreviousCluster = null; + String dcOfCurrentCluster = null; + String[] tokens; + String url; + String user; + String password; + String vc = ""; + String dcName = ""; + String guid; + + try { + clustersQuery = conn.prepareStatement("select id, hypervisor_type from `cloud`.`cluster` where removed is NULL"); + pstmt = conn.prepareStatement("select id from `cloud`.`data_center` where removed is NULL"); + rs = pstmt.executeQuery(); + + while (rs.next()) { + zoneId = rs.getLong("id"); + legacyZone = false; + ignoreZone = true; + count = 0L; + // Legacy zone term is meant only for VMware + // Legacy zone is a zone with atleast 2 clusters & with multiple DCs or VCs + clusters = clustersQuery.executeQuery(); + if (!clusters.next()) { + continue; // Ignore the zone without any clusters + } else { + dcOfPreviousCluster = null; + dcOfCurrentCluster = null; + do { + clusterHypervisorType = clusters.getString("hypervisor_type"); + clusterId = clusters.getLong("id"); + if (clusterHypervisorType.equalsIgnoreCase("VMware")) { + ignoreZone = false; + if (count > 0) { + dcOfPreviousCluster = dcOfCurrentCluster; + clusterDetailsQuery = conn.prepareStatement("select value,username,password from `cloud`.`cluster_details` where name='url' and cluster_id=?"); + clusterDetailsQuery.setLong(1, clusterId); + clusterDetails = clusterDetailsQuery.executeQuery(); + url = clusterDetails.getString("value"); + tokens = url.split("/"); // url format - http://vcenter/dc/cluster + vc = tokens[2]; + dcName = tokens[3]; + dcOfCurrentCluster = dcName + "@" + vc; + if (!dcOfPreviousCluster.equals(dcOfCurrentCluster)) { + legacyZone = true; + s_logger.debug("Marking the zone " + zoneId + " as legacy zone."); + } + } + } else { + s_logger.debug("Ignoring zone " + zoneId + " with hypervisor type " + clusterHypervisorType); + break; + } + count++; + } while (clusters.next()); + if (ignoreZone) { + continue; // Ignore the zone with hypervisors other than VMware + } + } + if (legacyZone) { + listOfLegacyZones.add(zoneId); + } else { + user = clusterDetails.getString("username"); + password = clusterDetails.getString("password"); + guid = dcName + "@" + vc; + + pstmt = conn.prepareStatement("INSERT INTO `cloud`.`vmware_data_center` (uuid, name, guid, vcenter_host, username, password) values(?, ?, ?, ?, ?, ?)"); + pstmt.setString(1, UUID.randomUUID().toString()); + pstmt.setString(2, dcName); + pstmt.setString(3, guid); + pstmt.setString(4, vc); + pstmt.setString(5, user); + pstmt.setString(6, password); + pstmt.executeUpdate(); + + pstmt = conn.prepareStatement("SELECT id FROM `cloud`.`vmware_data_center` where guid=?"); + pstmt.setString(1, guid); + dcInfo = pstmt.executeQuery(); + if(dcInfo.next()) { + vmwareDcId = dcInfo.getLong("id"); + } + + pstmt = conn.prepareStatement("INSERT INTO `cloud`.`vmware_data_center_zone_map` (zone_id, vmware_data_center_id) values(?, ?)"); + pstmt.setLong(1, zoneId); + pstmt.setLong(2, vmwareDcId); + pstmt.executeUpdate(); + } + } + updateLegacyZones(conn, listOfLegacyZones); + } catch (SQLException e) { + throw new CloudRuntimeException("Unable to load legacy zones into cloud.legacy_zones table.", e); + } finally { + try { + if (rs != null) { + rs.close(); + } + if (pstmt != null) { + pstmt.close(); + } + if (dcInfo != null) { + dcInfo.close(); + } + if (clusters != null) { + clusters.close(); + } + if (clusterDetails != null) { + clusterDetails.close(); + } + if (clustersQuery != null) { + clustersQuery.close(); + } + if (clusterDetailsQuery != null) { + clusterDetailsQuery.close(); + } + } catch (SQLException e) { + } + } + } + + private void updateLegacyZones(Connection conn, List<Long> zones) { + PreparedStatement legacyZonesQuery = null; + //Insert legacy zones into table for legacy zones. + try { + legacyZonesQuery = conn.prepareStatement("INSERT INTO `cloud`.`legacy_zones` (zone_id) VALUES (?)"); + for(Long zoneId : zones) { + legacyZonesQuery.setLong(1, zoneId); + legacyZonesQuery.executeUpdate(); + s_logger.debug("Inserted zone " + zoneId + " into cloud.legacyzones table"); + } + } catch (SQLException e) { + throw new CloudRuntimeException("Unable add zones to cloud.legacyzones table.", e); + } finally { + try { + if (legacyZonesQuery != null) { + legacyZonesQuery.close(); + } + } catch (SQLException e) { + } + } + } + private void createPlaceHolderNics(Connection conn) { PreparedStatement pstmt = null; ResultSet rs = null; http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f78c2a3c/plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/VmwareDatacenter.java ---------------------------------------------------------------------- diff --git a/plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/VmwareDatacenter.java b/plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/VmwareDatacenter.java index 246b477..b978cd9 100644 --- a/plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/VmwareDatacenter.java +++ b/plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/VmwareDatacenter.java @@ -3,14 +3,10 @@ package com.cloud.hypervisor.vmware; import org.apache.cloudstack.api.Identity; import org.apache.cloudstack.api.InternalIdentity; -import com.vmware.vim25.ManagedObjectReference; - public interface VmwareDatacenter extends Identity, InternalIdentity { String getVmwareDatacenterName(); - ManagedObjectReference getVmwareDatacenterMor(); - String getGuid(); String getVcenterHost();