use of com.cloud.legacymodel.communication.command.AttachCommand in project cosmic by MissionCriticalCloud.
the class NotAValidCommand method testStorageSubSystemCommand.
@Test
public void testStorageSubSystemCommand() {
final DiskTO disk = Mockito.mock(DiskTO.class);
final String vmName = "Test";
final AttachCommand command = new AttachCommand(disk, vmName);
final StorageSubsystemCommandHandler handler = Mockito.mock(StorageSubsystemCommandHandler.class);
when(this.citrixResourceBase.getStorageHandler()).thenReturn(handler);
when(handler.handleStorageCommands(command)).thenReturn(new AttachAnswer(disk));
final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
assertNotNull(wrapper);
final Answer answer = wrapper.execute(command, this.citrixResourceBase);
assertTrue(answer.getResult());
}
use of com.cloud.legacymodel.communication.command.AttachCommand in project cosmic by MissionCriticalCloud.
the class TemplateManagerImpl method attachISOToVM.
private boolean attachISOToVM(final long vmId, final long isoId, final boolean attach) {
final UserVmVO vm = this._userVmDao.findById(vmId);
if (vm == null) {
return false;
} else if (vm.getState() != State.Running) {
return true;
}
// prepare ISO ready to mount on hypervisor resource level
final TemplateInfo tmplt = prepareIso(isoId, vm.getDataCenterId());
final String vmName = vm.getInstanceName();
final HostVO host = this._hostDao.findById(vm.getHostId());
if (host == null) {
s_logger.warn("Host: " + vm.getHostId() + " does not exist");
return false;
}
final DataTO isoTO = tmplt.getTO();
final DiskTO disk = new DiskTO(isoTO, null, null, VolumeType.ISO);
final Command cmd;
if (attach) {
cmd = new AttachCommand(disk, vmName);
} else {
cmd = new DettachCommand(disk, vmName);
}
final Answer a = this._agentMgr.easySend(vm.getHostId(), cmd);
return a != null && a.getResult();
}
use of com.cloud.legacymodel.communication.command.AttachCommand in project cosmic by MissionCriticalCloud.
the class LibvirtComputingResourceTest method testStorageSubSystemCommand.
@Test
public void testStorageSubSystemCommand() {
final DiskTO disk = Mockito.mock(DiskTO.class);
final String vmName = "Test";
final AttachCommand command = new AttachCommand(disk, vmName);
final StorageSubsystemCommandHandler handler = Mockito.mock(StorageSubsystemCommandHandler.class);
when(this.libvirtComputingResource.getStorageHandler()).thenReturn(handler);
when(handler.handleStorageCommands(command)).thenReturn(new AttachAnswer(disk));
final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
assertNotNull(wrapper);
final Answer answer = wrapper.execute(command, this.libvirtComputingResource);
assertTrue(answer.getResult());
}
use of com.cloud.legacymodel.communication.command.AttachCommand in project cosmic by MissionCriticalCloud.
the class VolumeApiServiceImpl method sendAttachVolumeCommand.
private VolumeVO sendAttachVolumeCommand(final UserVmVO vm, VolumeVO volumeToAttach, Long deviceId, final DiskControllerType diskController) {
String errorMsg = "Failed to attach volume " + volumeToAttach.getName() + " to VM " + vm.getHostName();
boolean sendCommand = vm.getState() == State.Running;
AttachAnswer answer = null;
final Long hostId = vm.getHostId();
HostVO host = null;
final StoragePoolVO volumeToAttachStoragePool = this._storagePoolDao.findById(volumeToAttach.getPoolId());
if (hostId != null) {
host = this._hostDao.findById(hostId);
if (host != null && host.getHypervisorType() == HypervisorType.XenServer && volumeToAttachStoragePool != null && volumeToAttachStoragePool.isManaged()) {
sendCommand = true;
}
}
// volumeToAttachStoragePool should be null if the VM we are attaching the disk to has never been started before
final DataStore dataStore = volumeToAttachStoragePool != null ? this.dataStoreMgr.getDataStore(volumeToAttachStoragePool.getId(), DataStoreRole.Primary) : null;
// if we don't have a host, the VM we are attaching the disk to has never been started before
if (host != null) {
try {
this.volService.grantAccess(this.volFactory.getVolume(volumeToAttach.getId()), host, dataStore);
} catch (final Exception e) {
this.volService.revokeAccess(this.volFactory.getVolume(volumeToAttach.getId()), host, dataStore);
throw new CloudRuntimeException(e.getMessage());
}
}
if (sendCommand) {
if (host != null && host.getHypervisorType() == HypervisorType.KVM && volumeToAttachStoragePool.isManaged() && volumeToAttach.getPath() == null) {
volumeToAttach.setPath(volumeToAttach.get_iScsiName());
this._volsDao.update(volumeToAttach.getId(), volumeToAttach);
}
final DataTO volTO = this.volFactory.getVolume(volumeToAttach.getId()).getTO();
deviceId = getDeviceId(vm, deviceId);
if (diskController != null) {
volumeToAttach.setDiskController(diskController);
}
final DiskTO disk = new DiskTO(volTO, deviceId, volumeToAttach.getPath(), volumeToAttach.getVolumeType(), volumeToAttach.getDiskController(), volumeToAttach.getFormat());
final AttachCommand cmd = new AttachCommand(disk, vm.getInstanceName());
final ChapInfo chapInfo = this.volService.getChapInfo(this.volFactory.getVolume(volumeToAttach.getId()), dataStore);
final Map<String, String> details = new HashMap<>();
disk.setDetails(details);
details.put(DiskTO.MANAGED, String.valueOf(volumeToAttachStoragePool.isManaged()));
details.put(DiskTO.STORAGE_HOST, volumeToAttachStoragePool.getHostAddress());
details.put(DiskTO.STORAGE_PORT, String.valueOf(volumeToAttachStoragePool.getPort()));
details.put(DiskTO.VOLUME_SIZE, String.valueOf(volumeToAttach.getSize()));
details.put(DiskTO.IQN, volumeToAttach.get_iScsiName());
details.put(DiskTO.MOUNT_POINT, volumeToAttach.get_iScsiName());
details.put(DiskTO.PROTOCOL_TYPE, volumeToAttach.getPoolType() != null ? volumeToAttach.getPoolType().toString() : null);
if (chapInfo != null) {
details.put(DiskTO.CHAP_INITIATOR_USERNAME, chapInfo.getInitiatorUsername());
details.put(DiskTO.CHAP_INITIATOR_SECRET, chapInfo.getInitiatorSecret());
details.put(DiskTO.CHAP_TARGET_USERNAME, chapInfo.getTargetUsername());
details.put(DiskTO.CHAP_TARGET_SECRET, chapInfo.getTargetSecret());
}
this._userVmDao.loadDetails(vm);
try {
answer = (AttachAnswer) this._agentMgr.send(hostId, cmd);
} catch (final Exception e) {
if (host != null) {
this.volService.revokeAccess(this.volFactory.getVolume(volumeToAttach.getId()), host, dataStore);
}
throw new CloudRuntimeException(errorMsg + " due to: " + e.getMessage());
}
}
if (!sendCommand || answer != null && answer.getResult()) {
// Mark the volume as attached
if (sendCommand) {
final DiskTO disk = answer.getDisk();
this._volsDao.attachVolume(volumeToAttach.getId(), vm.getId(), disk.getDiskSeq(), diskController);
volumeToAttach = this._volsDao.findById(volumeToAttach.getId());
if (volumeToAttachStoragePool.isManaged() && volumeToAttach.getPath() == null) {
volumeToAttach.setPath(answer.getDisk().getPath());
if (diskController != null) {
volumeToAttach.setDiskController(diskController);
}
this._volsDao.update(volumeToAttach.getId(), volumeToAttach);
}
} else {
deviceId = getDeviceId(vm, deviceId);
this._volsDao.attachVolume(volumeToAttach.getId(), vm.getId(), deviceId, diskController);
}
// insert record for disk I/O statistics
VmDiskStatisticsVO diskstats = this._vmDiskStatsDao.findBy(vm.getAccountId(), vm.getDataCenterId(), vm.getId(), volumeToAttach.getId());
if (diskstats == null) {
diskstats = new VmDiskStatisticsVO(vm.getAccountId(), vm.getDataCenterId(), vm.getId(), volumeToAttach.getId());
this._vmDiskStatsDao.persist(diskstats);
}
// Change MaintenancePolicy when adding disk with HOST scope
if (volumeToAttachStoragePool != null && ScopeType.HOST.equals(volumeToAttachStoragePool.getScope())) {
vm.setMaintenancePolicy(MaintenancePolicy.ShutdownAndStart);
s_logger.debug("Setting MaintenancePolicy to '" + MaintenancePolicy.ShutdownAndStart.toString() + "' for VM " + vm.getInstanceName());
_vmInstanceDao.persist(vm);
}
return this._volsDao.findById(volumeToAttach.getId());
} else {
if (answer != null) {
final String details = answer.getDetails();
if (details != null && !details.isEmpty()) {
errorMsg += "; " + details;
}
}
if (host != null) {
this.volService.revokeAccess(this.volFactory.getVolume(volumeToAttach.getId()), host, dataStore);
}
throw new CloudRuntimeException(errorMsg);
}
}
Aggregations