use of com.cloud.dc.ClusterVO in project cosmic by MissionCriticalCloud.
the class ResourceManagerImpl method createHostVO.
protected HostVO createHostVO(final StartupCommand[] cmds, final ServerResource resource, final Map<String, String> details, List<String> hostTags, final ResourceStateAdapter.Event stateEvent) {
final StartupCommand startup = cmds[0];
HostVO host = findHostByGuid(startup.getGuid());
boolean isNew = false;
if (host == null) {
host = findHostByGuid(startup.getGuidWithoutResource());
}
if (host == null) {
host = new HostVO(startup.getGuid());
isNew = true;
}
String dataCenter = startup.getDataCenter();
String pod = startup.getPod();
final String cluster = startup.getCluster();
if (pod != null && dataCenter != null && pod.equalsIgnoreCase("default") && dataCenter.equalsIgnoreCase("default")) {
final List<HostPodVO> pods = _podDao.listAllIncludingRemoved();
for (final HostPodVO hpv : pods) {
if (checkCIDR(hpv, startup.getPrivateIpAddress(), startup.getPrivateNetmask())) {
pod = hpv.getName();
dataCenter = _dcDao.findById(hpv.getDataCenterId()).getName();
break;
}
}
}
long dcId;
DataCenterVO dc = _dcDao.findByName(dataCenter);
if (dc == null) {
try {
dcId = Long.parseLong(dataCenter);
dc = _dcDao.findById(dcId);
} catch (final NumberFormatException e) {
s_logger.debug("Cannot parse " + dataCenter + " into Long.");
}
}
if (dc == null) {
throw new IllegalArgumentException("Host " + startup.getPrivateIpAddress() + " sent incorrect data center: " + dataCenter);
}
dcId = dc.getId();
HostPodVO p = _podDao.findByName(pod, dcId);
if (p == null) {
try {
final long podId = Long.parseLong(pod);
p = _podDao.findById(podId);
} catch (final NumberFormatException e) {
s_logger.debug("Cannot parse " + pod + " into Long.");
}
}
/*
* ResourceStateAdapter is responsible for throwing Exception if Pod is
* null and non-null is required. for example, XcpServerDiscoever.
*/
final Long podId = p == null ? null : p.getId();
Long clusterId = null;
if (cluster != null) {
try {
clusterId = Long.valueOf(cluster);
} catch (final NumberFormatException e) {
if (podId != null) {
ClusterVO c = _clusterDao.findBy(cluster, podId.longValue());
if (c == null) {
c = new ClusterVO(dcId, podId.longValue(), cluster);
c = _clusterDao.persist(c);
}
clusterId = c.getId();
}
}
}
if (startup instanceof StartupRoutingCommand) {
final StartupRoutingCommand ssCmd = (StartupRoutingCommand) startup;
final List<String> implicitHostTags = ssCmd.getHostTags();
if (!implicitHostTags.isEmpty()) {
if (hostTags == null) {
hostTags = _hostTagsDao.gethostTags(host.getId());
}
if (hostTags != null) {
implicitHostTags.removeAll(hostTags);
hostTags.addAll(implicitHostTags);
} else {
hostTags = implicitHostTags;
}
}
}
host.setDataCenterId(dc.getId());
host.setPodId(podId);
host.setClusterId(clusterId);
host.setPrivateIpAddress(startup.getPrivateIpAddress());
host.setPrivateNetmask(startup.getPrivateNetmask());
host.setPrivateMacAddress(startup.getPrivateMacAddress());
host.setPublicIpAddress(startup.getPublicIpAddress());
host.setPublicMacAddress(startup.getPublicMacAddress());
host.setPublicNetmask(startup.getPublicNetmask());
host.setStorageIpAddress(startup.getStorageIpAddress());
host.setStorageMacAddress(startup.getStorageMacAddress());
host.setStorageNetmask(startup.getStorageNetmask());
host.setVersion(startup.getVersion());
host.setName(startup.getName());
host.setManagementServerId(_nodeId);
host.setStorageUrl(startup.getIqn());
host.setLastPinged(System.currentTimeMillis() >> 10);
host.setHostTags(hostTags);
host.setDetails(details);
if (startup.getStorageIpAddressDeux() != null) {
host.setStorageIpAddressDeux(startup.getStorageIpAddressDeux());
host.setStorageMacAddressDeux(startup.getStorageMacAddressDeux());
host.setStorageNetmaskDeux(startup.getStorageNetmaskDeux());
}
if (resource != null) {
/* null when agent is connected agent */
host.setResource(resource.getClass().getName());
}
host = (HostVO) dispatchToStateAdapters(stateEvent, true, host, cmds, resource, details, hostTags);
if (host == null) {
throw new CloudRuntimeException("No resource state adapter response");
}
if (isNew) {
host = _hostDao.persist(host);
} else {
_hostDao.update(host.getId(), host);
}
try {
resourceStateTransitTo(host, ResourceState.Event.InternalCreated, _nodeId);
/* Agent goes to Connecting status */
_agentMgr.agentStatusTransitTo(host, Status.Event.AgentConnected, _nodeId);
} catch (final Exception e) {
s_logger.debug("Cannot transmit host " + host.getId() + " to Creating state", e);
_agentMgr.agentStatusTransitTo(host, Status.Event.Error, _nodeId);
try {
resourceStateTransitTo(host, ResourceState.Event.Error, _nodeId);
} catch (final NoTransitionException e1) {
s_logger.debug("Cannot transmit host " + host.getId() + "to Error state", e);
}
}
return host;
}
use of com.cloud.dc.ClusterVO in project cosmic by MissionCriticalCloud.
the class ResourceManagerImpl method fillRoutingHostVO.
@Override
public HostVO fillRoutingHostVO(final HostVO host, final StartupRoutingCommand ssCmd, final HypervisorType hyType, Map<String, String> details, final List<String> hostTags) {
if (host.getPodId() == null) {
s_logger.error("Host " + ssCmd.getPrivateIpAddress() + " sent incorrect pod, pod id is null");
throw new IllegalArgumentException("Host " + ssCmd.getPrivateIpAddress() + " sent incorrect pod, pod id is null");
}
final ClusterVO clusterVO = _clusterDao.findById(host.getClusterId());
if (clusterVO.getHypervisorType() != hyType) {
throw new IllegalArgumentException("Can't add host whose hypervisor type is: " + hyType + " into cluster: " + clusterVO.getId() + " whose hypervisor type is: " + clusterVO.getHypervisorType());
}
final Map<String, String> hostDetails = ssCmd.getHostDetails();
if (hostDetails != null) {
if (details != null) {
details.putAll(hostDetails);
} else {
details = hostDetails;
}
}
final HostPodVO pod = _podDao.findById(host.getPodId());
final DataCenterVO dc = _dcDao.findById(host.getDataCenterId());
checkIPConflicts(pod, dc, ssCmd.getPrivateIpAddress(), ssCmd.getPublicIpAddress(), ssCmd.getPublicIpAddress(), ssCmd.getPublicNetmask());
host.setType(com.cloud.host.Host.Type.Routing);
host.setDetails(details);
host.setCaps(ssCmd.getCapabilities());
host.setCpuSockets(ssCmd.getCpuSockets());
host.setCpus(ssCmd.getCpus());
host.setTotalMemory(ssCmd.getMemory());
host.setHypervisorType(hyType);
host.setHypervisorVersion(ssCmd.getHypervisorVersion());
host.setGpuGroups(ssCmd.getGpuGroupDetails());
return host;
}
use of com.cloud.dc.ClusterVO in project cosmic by MissionCriticalCloud.
the class StorageManagerImpl method ListByDataCenterHypervisor.
@Override
public List<StoragePoolVO> ListByDataCenterHypervisor(final long datacenterId, final HypervisorType type) {
final List<StoragePoolVO> pools = _storagePoolDao.listByDataCenterId(datacenterId);
final List<StoragePoolVO> retPools = new ArrayList<>();
for (final StoragePoolVO pool : pools) {
if (pool.getStatus() != StoragePoolStatus.Up) {
continue;
}
if (pool.getScope() == ScopeType.ZONE) {
if (pool.getHypervisor() != null && pool.getHypervisor() == type) {
retPools.add(pool);
}
} else {
final ClusterVO cluster = _clusterDao.findById(pool.getClusterId());
if (type == cluster.getHypervisorType()) {
retPools.add(pool);
}
}
}
Collections.shuffle(retPools);
return retPools;
}
use of com.cloud.dc.ClusterVO in project cloudstack by apache.
the class CloudZonesStartupProcessor method updateComputeHost.
protected void updateComputeHost(final HostVO host, final StartupCommand startup, final Host.Type type) throws AgentAuthnException {
String zoneToken = startup.getDataCenter();
if (zoneToken == null) {
s_logger.warn("No Zone Token passed in, cannot not find zone for the agent");
throw new AgentAuthnException("No Zone Token passed in, cannot not find zone for agent");
}
DataCenterVO zone = _zoneDao.findByToken(zoneToken);
if (zone == null) {
zone = _zoneDao.findByName(zoneToken);
if (zone == null) {
try {
long zoneId = Long.parseLong(zoneToken);
zone = _zoneDao.findById(zoneId);
if (zone == null) {
throw new AgentAuthnException("Could not find zone for agent with token " + zoneToken);
}
} catch (NumberFormatException nfe) {
throw new AgentAuthnException("Could not find zone for agent with token " + zoneToken);
}
}
}
if (s_logger.isDebugEnabled()) {
s_logger.debug("Successfully loaded the DataCenter from the zone token passed in ");
}
long zoneId = zone.getId();
ResourceDetail maxHostsInZone = _zoneDetailsDao.findDetail(zoneId, ZoneConfig.MaxHosts.key());
if (maxHostsInZone != null) {
long maxHosts = Long.parseLong(maxHostsInZone.getValue());
long currentCountOfHosts = _hostDao.countRoutingHostsByDataCenter(zoneId);
if (s_logger.isDebugEnabled()) {
s_logger.debug("Number of hosts in Zone:" + currentCountOfHosts + ", max hosts limit: " + maxHosts);
}
if (currentCountOfHosts >= maxHosts) {
throw new AgentAuthnException("Number of running Routing hosts in the Zone:" + zone.getName() + " is already at the max limit:" + maxHosts + ", cannot start one more host");
}
}
HostPodVO pod = null;
if (startup.getPrivateIpAddress() == null) {
s_logger.warn("No private IP address passed in for the agent, cannot not find pod for agent");
throw new AgentAuthnException("No private IP address passed in for the agent, cannot not find pod for agent");
}
if (startup.getPrivateNetmask() == null) {
s_logger.warn("No netmask passed in for the agent, cannot not find pod for agent");
throw new AgentAuthnException("No netmask passed in for the agent, cannot not find pod for agent");
}
if (host.getPodId() != null) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Pod is already created for this agent, looks like agent is reconnecting...");
}
pod = _podDao.findById(host.getPodId());
if (!checkCIDR(type, pod, startup.getPrivateIpAddress(), startup.getPrivateNetmask())) {
pod = null;
if (s_logger.isDebugEnabled()) {
s_logger.debug("Subnet of Pod does not match the subnet of the agent, not using this Pod: " + host.getPodId());
}
} else {
updatePodNetmaskIfNeeded(pod, startup.getPrivateNetmask());
}
}
if (pod == null) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Trying to detect the Pod to use from the agent's ip address and netmask passed in ");
}
//deduce pod
boolean podFound = false;
List<HostPodVO> podsInZone = _podDao.listByDataCenterId(zoneId);
for (HostPodVO hostPod : podsInZone) {
if (checkCIDR(type, hostPod, startup.getPrivateIpAddress(), startup.getPrivateNetmask())) {
pod = hostPod;
//found the default POD having the same subnet.
updatePodNetmaskIfNeeded(pod, startup.getPrivateNetmask());
podFound = true;
break;
}
}
if (!podFound) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Creating a new Pod since no default Pod found that matches the agent's ip address and netmask passed in ");
}
if (startup.getGatewayIpAddress() == null) {
s_logger.warn("No Gateway IP address passed in for the agent, cannot create a new pod for the agent");
throw new AgentAuthnException("No Gateway IP address passed in for the agent, cannot create a new pod for the agent");
}
//auto-create a new pod, since pod matching the agent's ip is not found
String podName = "POD-" + (podsInZone.size() + 1);
try {
String gateway = startup.getGatewayIpAddress();
String cidr = NetUtils.getCidrFromGatewayAndNetmask(gateway, startup.getPrivateNetmask());
String[] cidrPair = cidr.split("\\/");
String cidrAddress = cidrPair[0];
long cidrSize = Long.parseLong(cidrPair[1]);
String startIp = NetUtils.getIpRangeStartIpFromCidr(cidrAddress, cidrSize);
String endIp = NetUtils.getIpRangeEndIpFromCidr(cidrAddress, cidrSize);
pod = _configurationManager.createPod(-1, podName, zoneId, gateway, cidr, startIp, endIp, null, true);
} catch (Exception e) {
// no longer tolerate exception during the cluster creation phase
throw new CloudRuntimeException("Unable to create new Pod " + podName + " in Zone: " + zoneId, e);
}
}
}
final StartupRoutingCommand scc = (StartupRoutingCommand) startup;
ClusterVO cluster = null;
if (host.getClusterId() != null) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Cluster is already created for this agent, looks like agent is reconnecting...");
}
cluster = _clusterDao.findById(host.getClusterId());
}
if (cluster == null) {
//auto-create cluster - assume one host per cluster
String clusterName = "Cluster-" + startup.getPrivateIpAddress();
ClusterVO existingCluster = _clusterDao.findBy(clusterName, pod.getId());
if (existingCluster != null) {
cluster = existingCluster;
} else {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Creating a new Cluster for this agent with name: " + clusterName + " in Pod: " + pod.getId() + ", in Zone:" + zoneId);
}
cluster = new ClusterVO(zoneId, pod.getId(), clusterName);
cluster.setHypervisorType(scc.getHypervisorType().toString());
try {
cluster = _clusterDao.persist(cluster);
} catch (Exception e) {
// no longer tolerate exception during the cluster creation phase
throw new CloudRuntimeException("Unable to create cluster " + clusterName + " in pod " + pod.getId() + " and data center " + zoneId, e);
}
}
}
if (s_logger.isDebugEnabled()) {
s_logger.debug("Detected Zone: " + zoneId + ", Pod: " + pod.getId() + ", Cluster:" + cluster.getId());
}
host.setDataCenterId(zone.getId());
host.setPodId(pod.getId());
host.setClusterId(cluster.getId());
host.setPrivateIpAddress(startup.getPrivateIpAddress());
host.setPrivateNetmask(startup.getPrivateNetmask());
host.setPrivateMacAddress(startup.getPrivateMacAddress());
host.setPublicIpAddress(startup.getPublicIpAddress());
host.setPublicMacAddress(startup.getPublicMacAddress());
host.setPublicNetmask(startup.getPublicNetmask());
host.setStorageIpAddress(startup.getStorageIpAddress());
host.setStorageMacAddress(startup.getStorageMacAddress());
host.setStorageNetmask(startup.getStorageNetmask());
host.setVersion(startup.getVersion());
host.setName(startup.getName());
host.setType(type);
host.setStorageUrl(startup.getIqn());
host.setLastPinged(System.currentTimeMillis() >> 10);
host.setCaps(scc.getCapabilities());
host.setCpus(scc.getCpus());
host.setTotalMemory(scc.getMemory());
host.setSpeed(scc.getSpeed());
HypervisorType hyType = scc.getHypervisorType();
host.setHypervisorType(hyType);
host.setHypervisorVersion(scc.getHypervisorVersion());
updateHostDetails(host, scc);
}
use of com.cloud.dc.ClusterVO in project cloudstack by apache.
the class SolidFireUtil method hostAddedToOrRemovedFromCluster.
public static void hostAddedToOrRemovedFromCluster(long hostId, long clusterId, boolean added, String storageProvider, ClusterDao clusterDao, ClusterDetailsDao clusterDetailsDao, PrimaryDataStoreDao storagePoolDao, StoragePoolDetailsDao storagePoolDetailsDao, HostDao hostDao) {
ClusterVO cluster = clusterDao.findById(clusterId);
GlobalLock lock = GlobalLock.getInternLock(cluster.getUuid());
if (!lock.lock(s_lockTimeInSeconds)) {
String errMsg = "Couldn't lock the DB on the following string: " + cluster.getUuid();
s_logger.debug(errMsg);
throw new CloudRuntimeException(errMsg);
}
try {
List<StoragePoolVO> storagePools = storagePoolDao.findPoolsByProvider(storageProvider);
if (storagePools != null && storagePools.size() > 0) {
List<SolidFireUtil.SolidFireConnection> sfConnections = new ArrayList<>();
for (StoragePoolVO storagePool : storagePools) {
ClusterDetailsVO clusterDetail = clusterDetailsDao.findDetail(clusterId, SolidFireUtil.getVagKey(storagePool.getId()));
String vagId = clusterDetail != null ? clusterDetail.getValue() : null;
if (vagId != null) {
SolidFireUtil.SolidFireConnection sfConnection = SolidFireUtil.getSolidFireConnection(storagePool.getId(), storagePoolDetailsDao);
if (!sfConnections.contains(sfConnection)) {
sfConnections.add(sfConnection);
SolidFireUtil.SolidFireVag sfVag = SolidFireUtil.getVag(sfConnection, Long.parseLong(vagId));
List<HostVO> hostsToAddOrRemove = new ArrayList<>();
HostVO hostToAddOrRemove = hostDao.findByIdIncludingRemoved(hostId);
hostsToAddOrRemove.add(hostToAddOrRemove);
String[] hostIqns = SolidFireUtil.getNewHostIqns(sfVag.getInitiators(), SolidFireUtil.getIqnsFromHosts(hostsToAddOrRemove), added);
SolidFireUtil.modifyVag(sfConnection, sfVag.getId(), hostIqns, sfVag.getVolumeIds());
}
}
}
}
} finally {
lock.unlock();
lock.releaseRef();
}
}
Aggregations