use of com.cloud.vm.VirtualMachine in project cosmic by MissionCriticalCloud.
the class DeploymentPlanningManagerImpl method checkForNonDedicatedResources.
private void checkForNonDedicatedResources(final VirtualMachineProfile vmProfile, final Zone zone, final ExcludeList avoids) {
boolean isExplicit = false;
final VirtualMachine vm = vmProfile.getVirtualMachine();
// check if zone is dedicated. if yes check if vm owner has access to it.
final DedicatedResourceVO dedicatedZone = _dedicatedDao.findByZoneId(zone.getId());
if (dedicatedZone != null && !_accountMgr.isRootAdmin(vmProfile.getOwner().getId())) {
final long accountDomainId = vmProfile.getOwner().getDomainId();
final long accountId = vmProfile.getOwner().getAccountId();
// account will be to use explicit dedication affinity group.
if (dedicatedZone.getAccountId() != null) {
if (dedicatedZone.getAccountId().equals(accountId)) {
return;
} else {
throw new CloudRuntimeException("Failed to deploy VM, Zone " + zone.getName() + " not available for the user account " + vmProfile.getOwner());
}
}
// domain level dedication group
if (!_affinityGroupService.isAffinityGroupAvailableInDomain(dedicatedZone.getAffinityGroupId(), accountDomainId)) {
throw new CloudRuntimeException("Failed to deploy VM, Zone " + zone.getName() + " not available for the user domain " + vmProfile.getOwner());
}
}
// check affinity group of type Explicit dedication exists. If No put
// dedicated pod/cluster/host in avoid list
final List<AffinityGroupVMMapVO> vmGroupMappings = _affinityGroupVMMapDao.findByVmIdType(vm.getId(), "ExplicitDedication");
if (vmGroupMappings != null && !vmGroupMappings.isEmpty()) {
isExplicit = true;
}
final List<Long> allPodsInDc = _podDao.listAllPods(zone.getId());
final List<Long> allDedicatedPods = _dedicatedDao.listAllPods();
allPodsInDc.retainAll(allDedicatedPods);
final List<Long> allClustersInDc = _clusterDao.listAllCusters(zone.getId());
final List<Long> allDedicatedClusters = _dedicatedDao.listAllClusters();
allClustersInDc.retainAll(allDedicatedClusters);
final List<Long> allHostsInDc = _hostDao.listAllHosts(zone.getId());
final List<Long> allDedicatedHosts = _dedicatedDao.listAllHosts();
allHostsInDc.retainAll(allDedicatedHosts);
// Only when the type is instance VM and not explicitly dedicated.
if (vm.getType() == VirtualMachine.Type.User && !isExplicit) {
// add explicitly dedicated resources in avoidList
avoids.addPodList(allPodsInDc);
avoids.addClusterList(allClustersInDc);
avoids.addHostList(allHostsInDc);
}
// No need to check the isExplicit. As both the cases are handled.
if (vm.getType() == VirtualMachine.Type.DomainRouter) {
final long vmAccountId = vm.getAccountId();
final long vmDomainId = vm.getDomainId();
// Lists all explicitly dedicated resources from vm account ID or domain ID.
final List<Long> allPodsFromDedicatedID = new ArrayList<>();
final List<Long> allClustersFromDedicatedID = new ArrayList<>();
final List<Long> allHostsFromDedicatedID = new ArrayList<>();
// Whether the dedicated resources belong to Domain or not. If not, it may belongs to Account or no dedication.
final List<AffinityGroupDomainMapVO> domainGroupMappings = _affinityGroupDomainMapDao.listByDomain(vmDomainId);
// For temporary storage and indexing.
List<DedicatedResourceVO> tempStorage;
if (domainGroupMappings == null || domainGroupMappings.isEmpty()) {
// The dedicated resource belongs to VM Account ID.
tempStorage = _dedicatedDao.searchDedicatedPods(null, vmDomainId, vmAccountId, null).first();
for (final DedicatedResourceVO vo : tempStorage) {
allPodsFromDedicatedID.add(vo.getPodId());
}
tempStorage.clear();
tempStorage = _dedicatedDao.searchDedicatedClusters(null, vmDomainId, vmAccountId, null).first();
for (final DedicatedResourceVO vo : tempStorage) {
allClustersFromDedicatedID.add(vo.getClusterId());
}
tempStorage.clear();
tempStorage = _dedicatedDao.searchDedicatedHosts(null, vmDomainId, vmAccountId, null).first();
for (final DedicatedResourceVO vo : tempStorage) {
allHostsFromDedicatedID.add(vo.getHostId());
}
// Remove the dedicated ones from main list
allPodsInDc.removeAll(allPodsFromDedicatedID);
allClustersInDc.removeAll(allClustersFromDedicatedID);
allHostsInDc.removeAll(allHostsFromDedicatedID);
} else {
// The dedicated resource belongs to VM Domain ID or No dedication.
tempStorage = _dedicatedDao.searchDedicatedPods(null, vmDomainId, null, null).first();
for (final DedicatedResourceVO vo : tempStorage) {
allPodsFromDedicatedID.add(vo.getPodId());
}
tempStorage.clear();
tempStorage = _dedicatedDao.searchDedicatedClusters(null, vmDomainId, null, null).first();
for (final DedicatedResourceVO vo : tempStorage) {
allClustersFromDedicatedID.add(vo.getClusterId());
}
tempStorage.clear();
tempStorage = _dedicatedDao.searchDedicatedHosts(null, vmDomainId, null, null).first();
for (final DedicatedResourceVO vo : tempStorage) {
allHostsFromDedicatedID.add(vo.getHostId());
}
// Remove the dedicated ones from main list
allPodsInDc.removeAll(allPodsFromDedicatedID);
allClustersInDc.removeAll(allClustersFromDedicatedID);
allHostsInDc.removeAll(allHostsFromDedicatedID);
}
// Add in avoid list or no addition if no dedication
avoids.addPodList(allPodsInDc);
avoids.addClusterList(allClustersInDc);
avoids.addHostList(allHostsInDc);
}
}
use of com.cloud.vm.VirtualMachine in project cosmic by MissionCriticalCloud.
the class AgentHookBase method onConsoleAccessAuthentication.
@Override
public AgentControlAnswer onConsoleAccessAuthentication(final ConsoleAccessAuthenticationCommand cmd) {
final Long vmId = null;
final String ticketInUrl = cmd.getTicket();
if (ticketInUrl == null) {
s_logger.error("Access ticket could not be found, you could be running an old version of console proxy. vmId: " + cmd.getVmId());
return new ConsoleAccessAuthenticationAnswer(cmd, false);
}
if (s_logger.isDebugEnabled()) {
s_logger.debug("Console authentication. Ticket in url for " + cmd.getHost() + ":" + cmd.getPort() + "-" + cmd.getVmId() + " is " + ticketInUrl);
}
if (!cmd.isReauthenticating()) {
final String ticket = ConsoleProxyServlet.genAccessTicket(cmd.getHost(), cmd.getPort(), cmd.getSid(), cmd.getVmId());
if (s_logger.isDebugEnabled()) {
s_logger.debug("Console authentication. Ticket in 1 minute boundary for " + cmd.getHost() + ":" + cmd.getPort() + "-" + cmd.getVmId() + " is " + ticket);
}
if (!ticket.equals(ticketInUrl)) {
final Date now = new Date();
// considering of minute round-up
final String minuteEarlyTicket = ConsoleProxyServlet.genAccessTicket(cmd.getHost(), cmd.getPort(), cmd.getSid(), cmd.getVmId(), new Date(now.getTime() - 60 * 1000));
if (s_logger.isDebugEnabled()) {
s_logger.debug("Console authentication. Ticket in 2-minute boundary for " + cmd.getHost() + ":" + cmd.getPort() + "-" + cmd.getVmId() + " is " + minuteEarlyTicket);
}
if (!minuteEarlyTicket.equals(ticketInUrl)) {
s_logger.error("Access ticket expired or has been modified. vmId: " + cmd.getVmId() + "ticket in URL: " + ticketInUrl + ", tickets to check against: " + ticket + "," + minuteEarlyTicket);
return new ConsoleAccessAuthenticationAnswer(cmd, false);
}
}
}
if (cmd.getVmId() != null && cmd.getVmId().isEmpty()) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Invalid vm id sent from proxy(happens when proxy session has terminated)");
}
return new ConsoleAccessAuthenticationAnswer(cmd, false);
}
VirtualMachine vm = _instanceDao.findByUuid(cmd.getVmId());
if (vm == null) {
vm = _instanceDao.findById(Long.parseLong(cmd.getVmId()));
}
if (vm == null) {
s_logger.error("Invalid vm id " + cmd.getVmId() + " sent from console access authentication");
return new ConsoleAccessAuthenticationAnswer(cmd, false);
}
if (vm.getHostId() == null) {
s_logger.warn("VM " + vmId + " lost host info, failed authentication request");
return new ConsoleAccessAuthenticationAnswer(cmd, false);
}
final HostVO host = _hostDao.findById(vm.getHostId());
if (host == null) {
s_logger.warn("VM " + vmId + "'s host does not exist, fail authentication request");
return new ConsoleAccessAuthenticationAnswer(cmd, false);
}
final String sid = cmd.getSid();
if (sid == null || !sid.equals(vm.getVncPassword())) {
s_logger.warn("sid " + sid + " in url does not match stored sid.");
return new ConsoleAccessAuthenticationAnswer(cmd, false);
}
if (cmd.isReauthenticating()) {
final ConsoleAccessAuthenticationAnswer authenticationAnswer = new ConsoleAccessAuthenticationAnswer(cmd, true);
authenticationAnswer.setReauthenticating(true);
s_logger.info("Re-authentication request, ask host " + vm.getHostId() + " for new console info");
final GetVncPortAnswer answer = (GetVncPortAnswer) _agentMgr.easySend(vm.getHostId(), new GetVncPortCommand(vm.getId(), vm.getInstanceName()));
if (answer != null && answer.getResult()) {
final Ternary<String, String, String> parsedHostInfo = ConsoleProxyServlet.parseHostInfo(answer.getAddress());
if (parsedHostInfo.second() != null && parsedHostInfo.third() != null) {
s_logger.info("Re-authentication result. vm: " + vm.getId() + ", tunnel url: " + parsedHostInfo.second() + ", tunnel session: " + parsedHostInfo.third());
authenticationAnswer.setTunnelUrl(parsedHostInfo.second());
authenticationAnswer.setTunnelSession(parsedHostInfo.third());
} else {
s_logger.info("Re-authentication result. vm: " + vm.getId() + ", host address: " + parsedHostInfo.first() + ", port: " + answer.getPort());
authenticationAnswer.setHost(parsedHostInfo.first());
authenticationAnswer.setPort(answer.getPort());
}
} else {
s_logger.warn("Re-authentication request failed");
authenticationAnswer.setSuccess(false);
}
return authenticationAnswer;
}
return new ConsoleAccessAuthenticationAnswer(cmd, true);
}
use of com.cloud.vm.VirtualMachine in project cosmic by MissionCriticalCloud.
the class RecreatableFencer method fenceOff.
@Override
public Boolean fenceOff(final VirtualMachine vm, final Host host) {
final VirtualMachine.Type type = vm.getType();
if (type != VirtualMachine.Type.ConsoleProxy && type != VirtualMachine.Type.DomainRouter && type != VirtualMachine.Type.SecondaryStorageVm) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Don't know how to fence off " + type);
}
return null;
}
final List<VolumeVO> vols = _volsDao.findByInstance(vm.getId());
for (final VolumeVO vol : vols) {
if (!vol.isRecreatable()) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Unable to fence off volumes that are not recreatable: " + vol);
}
return null;
}
if (vol.getPoolType().isShared()) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Unable to fence off volumes that are shared: " + vol);
}
return null;
}
}
return true;
}
use of com.cloud.vm.VirtualMachine in project cosmic by MissionCriticalCloud.
the class VMSnapshotManagerImpl method deleteAllVMSnapshots.
@Override
public boolean deleteAllVMSnapshots(final long vmId, final VMSnapshot.Type type) {
// serialize VM operation
final AsyncJobExecutionContext jobContext = AsyncJobExecutionContext.getCurrentExecutionContext();
if (jobContext.isJobDispatchedBy(VmWorkConstants.VM_WORK_JOB_DISPATCHER)) {
// avoid re-entrance
final VmWorkJobVO placeHolder;
placeHolder = createPlaceHolderWork(vmId);
try {
return orchestrateDeleteAllVMSnapshots(vmId, type);
} finally {
if (placeHolder != null) {
_workJobDao.expunge(placeHolder.getId());
}
}
} else {
final Outcome<VirtualMachine> outcome = deleteAllVMSnapshotsThroughJobQueue(vmId, type);
try {
outcome.get();
} catch (final InterruptedException e) {
throw new RuntimeException("Operation is interrupted", e);
} catch (final ExecutionException e) {
throw new RuntimeException("Execution excetion", e);
}
final Object jobResult = _jobMgr.unmarshallResultObject(outcome.getJob());
if (jobResult != null) {
if (jobResult instanceof ConcurrentOperationException) {
throw (ConcurrentOperationException) jobResult;
} else if (jobResult instanceof InvalidParameterValueException) {
throw (InvalidParameterValueException) jobResult;
} else if (jobResult instanceof Throwable) {
throw new RuntimeException("Unexpected exception", (Throwable) jobResult);
}
}
if (jobResult instanceof Boolean) {
return (Boolean) jobResult;
}
return false;
}
}
use of com.cloud.vm.VirtualMachine in project cosmic by MissionCriticalCloud.
the class KVMFencerTest method testWithSingleHostDown.
@Test
public void testWithSingleHostDown() {
final HostVO host = Mockito.mock(HostVO.class);
Mockito.when(host.getClusterId()).thenReturn(1l);
Mockito.when(host.getHypervisorType()).thenReturn(HypervisorType.KVM);
Mockito.when(host.getDataCenterId()).thenReturn(1l);
Mockito.when(host.getPodId()).thenReturn(1l);
Mockito.when(host.getStatus()).thenReturn(Status.Down);
Mockito.when(host.getId()).thenReturn(1l);
final VirtualMachine virtualMachine = Mockito.mock(VirtualMachine.class);
Mockito.when(resourceManager.listAllHostsInCluster(1l)).thenReturn(Collections.singletonList(host));
Assert.assertFalse(fencer.fenceOff(virtualMachine, host));
}
Aggregations