use of com.cloud.legacymodel.to.StorageFilerTO in project cosmic by MissionCriticalCloud.
the class LibvirtManageSnapshotCommandWrapper method execute.
@Override
public Answer execute(final ManageSnapshotCommand command, final LibvirtComputingResource libvirtComputingResource) {
final String snapshotName = command.getSnapshotName();
final String snapshotPath = command.getSnapshotPath();
final String vmName = command.getVmName();
try {
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(vmName);
DomainState state = null;
Domain vm = null;
if (vmName != null) {
try {
vm = libvirtComputingResource.getDomain(conn, command.getVmName());
state = vm.getInfo().state;
} catch (final LibvirtException e) {
s_logger.trace("Ignoring libvirt error.", e);
}
}
final KvmStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
final StorageFilerTO pool = command.getPool();
final KvmStoragePool primaryPool = storagePoolMgr.getStoragePool(pool.getType(), pool.getUuid());
final KvmPhysicalDisk disk = primaryPool.getPhysicalDisk(command.getVolumePath());
if (state == DomainState.VIR_DOMAIN_RUNNING && !primaryPool.isExternalSnapshot()) {
final MessageFormat snapshotXml = new MessageFormat(" <domainsnapshot>" + " <name>{0}</name>" + " <domain>" + " <uuid>{1}</uuid>" + " </domain>" + " </domainsnapshot>");
final String vmUuid = vm.getUUIDString();
final Object[] args = new Object[] { snapshotName, vmUuid };
final String snapshot = snapshotXml.format(args);
s_logger.debug(snapshot);
if (command.getCommandSwitch().equalsIgnoreCase(ManageSnapshotCommand.CREATE_SNAPSHOT)) {
vm.snapshotCreateXML(snapshot);
} else {
final DomainSnapshot snap = vm.snapshotLookupByName(snapshotName);
snap.delete(0);
}
/*
* libvirt on RHEL6 doesn't handle resume event emitted from qemu
*/
vm = libvirtComputingResource.getDomain(conn, command.getVmName());
state = vm.getInfo().state;
if (state == DomainState.VIR_DOMAIN_PAUSED) {
vm.resume();
}
} else {
if (primaryPool.getType() == StoragePoolType.RBD) {
try {
final Rados r = new Rados(primaryPool.getAuthUserName());
r.confSet("mon_host", primaryPool.getSourceHost() + ":" + primaryPool.getSourcePort());
r.confSet("key", primaryPool.getAuthSecret());
r.confSet("client_mount_timeout", "30");
r.connect();
s_logger.debug("Succesfully connected to Ceph cluster at " + r.confGet("mon_host"));
final IoCTX io = r.ioCtxCreate(primaryPool.getSourceDir());
final Rbd rbd = new Rbd(io);
final RbdImage image = rbd.open(disk.getName());
if (command.getCommandSwitch().equalsIgnoreCase(ManageSnapshotCommand.CREATE_SNAPSHOT)) {
s_logger.debug("Attempting to create RBD snapshot " + disk.getName() + "@" + snapshotName);
image.snapCreate(snapshotName);
} else {
s_logger.debug("Attempting to remove RBD snapshot " + disk.getName() + "@" + snapshotName);
image.snapRemove(snapshotName);
}
rbd.close(image);
r.ioCtxDestroy(io);
} catch (final Exception e) {
s_logger.error("A RBD snapshot operation on " + disk.getName() + " failed. The error was: " + e.getMessage());
}
} else {
/* VM is not running, create a snapshot by ourself */
final int cmdsTimeout = libvirtComputingResource.getCmdsTimeout();
final String manageSnapshotPath = libvirtComputingResource.manageSnapshotPath();
final Script scriptCommand = new Script(manageSnapshotPath, cmdsTimeout, s_logger);
if (command.getCommandSwitch().equalsIgnoreCase(ManageSnapshotCommand.CREATE_SNAPSHOT)) {
scriptCommand.add("-c", disk.getPath());
} else {
scriptCommand.add("-d", snapshotPath);
}
scriptCommand.add("-n", snapshotName);
final String result = scriptCommand.execute();
if (result != null) {
s_logger.debug("Failed to manage snapshot: " + result);
return new ManageSnapshotAnswer(command, false, "Failed to manage snapshot: " + result);
}
}
}
return new ManageSnapshotAnswer(command, command.getSnapshotId(), disk.getPath() + File.separator + snapshotName, true, null);
} catch (final LibvirtException e) {
s_logger.debug("Failed to manage snapshot: " + e.toString());
return new ManageSnapshotAnswer(command, false, "Failed to manage snapshot: " + e.toString());
}
}
use of com.cloud.legacymodel.to.StorageFilerTO in project cosmic by MissionCriticalCloud.
the class LibvirtComputingResourceTest method testManageSnapshotCommandLibvirt.
@Test
public void testManageSnapshotCommandLibvirt() {
final StoragePool storagePool = Mockito.mock(StoragePool.class);
final String volumePath = "/123/vol";
final String vmName = "Test";
final long snapshotId = 1l;
final String preSnapshotPath = "/snapshot/path";
final String snapshotName = "snap";
final ManageSnapshotCommand command = new ManageSnapshotCommand(snapshotId, volumePath, storagePool, preSnapshotPath, snapshotName, vmName);
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
final Connect conn = Mockito.mock(Connect.class);
final KvmStoragePoolManager storagePoolMgr = Mockito.mock(KvmStoragePoolManager.class);
final KvmStoragePool primaryPool = Mockito.mock(KvmStoragePool.class);
final Domain vm = Mockito.mock(Domain.class);
final DomainInfo info = Mockito.mock(DomainInfo.class);
final DomainState state = DomainInfo.DomainState.VIR_DOMAIN_RUNNING;
info.state = state;
final KvmPhysicalDisk disk = Mockito.mock(KvmPhysicalDisk.class);
final StorageFilerTO pool = command.getPool();
when(this.libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
try {
when(libvirtUtilitiesHelper.getConnectionByVmName(vmName)).thenReturn(conn);
when(this.libvirtComputingResource.getDomain(conn, command.getVmName())).thenReturn(vm);
when(vm.getInfo()).thenReturn(info);
} catch (final LibvirtException e) {
fail(e.getMessage());
}
when(this.libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolMgr);
when(storagePoolMgr.getStoragePool(pool.getType(), pool.getUuid())).thenReturn(primaryPool);
when(primaryPool.getPhysicalDisk(command.getVolumePath())).thenReturn(disk);
when(primaryPool.isExternalSnapshot()).thenReturn(false);
try {
when(vm.getUUIDString()).thenReturn("cdb18980-546d-4153-b916-70ee9edf0908");
} catch (final LibvirtException e) {
fail(e.getMessage());
}
final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
assertNotNull(wrapper);
final Answer answer = wrapper.execute(command, this.libvirtComputingResource);
assertTrue(answer.getResult());
verify(this.libvirtComputingResource, times(1)).getStoragePoolMgr();
verify(this.libvirtComputingResource, times(1)).getLibvirtUtilitiesHelper();
try {
verify(libvirtUtilitiesHelper, times(1)).getConnectionByVmName(vmName);
} catch (final LibvirtException e) {
fail(e.getMessage());
}
}
use of com.cloud.legacymodel.to.StorageFilerTO in project cosmic by MissionCriticalCloud.
the class LibvirtComputingResourceTest method testCopyVolumeCommand.
@Test
public void testCopyVolumeCommand() {
final StoragePool storagePool = Mockito.mock(StoragePool.class);
final String secondaryStoragePoolURL = "nfs:/127.0.0.1/storage/secondary";
final Long volumeId = 1l;
final int wait = 0;
final String volumePath = "/vol/path";
final boolean toSecondaryStorage = true;
final boolean executeInSequence = false;
final CopyVolumeCommand command = new CopyVolumeCommand(volumeId, volumePath, storagePool, secondaryStoragePoolURL, toSecondaryStorage, wait, executeInSequence);
final String destVolumeName = "ce97bbc1-34fe-4259-9202-74bbce2562ab";
final String volumeDestPath = "/volumes/" + command.getVolumeId() + File.separator;
final KvmStoragePoolManager storagePoolMgr = Mockito.mock(KvmStoragePoolManager.class);
final KvmStoragePool secondary = Mockito.mock(KvmStoragePool.class);
final KvmStoragePool primary = Mockito.mock(KvmStoragePool.class);
final KvmPhysicalDisk disk = Mockito.mock(KvmPhysicalDisk.class);
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
final StorageFilerTO pool = command.getPool();
when(this.libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolMgr);
when(storagePoolMgr.getStoragePool(pool.getType(), pool.getUuid())).thenReturn(primary);
when(this.libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
when(libvirtUtilitiesHelper.generateUuidName()).thenReturn(destVolumeName);
when(primary.getPhysicalDisk(command.getVolumePath())).thenReturn(disk);
when(storagePoolMgr.getStoragePoolByUri(secondaryStoragePoolURL)).thenReturn(secondary);
when(secondary.getType()).thenReturn(StoragePoolType.ManagedNFS);
when(secondary.getUuid()).thenReturn("60d979d8-d132-4181-8eca-8dfde50d7df6");
when(secondary.createFolder(volumeDestPath)).thenReturn(true);
when(storagePoolMgr.deleteStoragePool(secondary.getType(), secondary.getUuid())).thenReturn(true);
when(storagePoolMgr.getStoragePoolByUri(secondaryStoragePoolURL + volumeDestPath)).thenReturn(secondary);
when(storagePoolMgr.copyPhysicalDisk(disk, destVolumeName, secondary, 0)).thenReturn(disk);
final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
assertNotNull(wrapper);
final Answer answer = wrapper.execute(command, this.libvirtComputingResource);
assertTrue(answer.getResult());
verify(this.libvirtComputingResource, times(1)).getStoragePoolMgr();
}
use of com.cloud.legacymodel.to.StorageFilerTO in project cosmic by MissionCriticalCloud.
the class LibvirtComputingResourceTest method testDeleteStoragePoolCommandException.
@Test
public void testDeleteStoragePoolCommandException() {
final StoragePool storagePool = Mockito.mock(StoragePool.class);
final KvmStoragePoolManager storagePoolMgr = Mockito.mock(KvmStoragePoolManager.class);
final DeleteStoragePoolCommand command = new DeleteStoragePoolCommand(storagePool);
final StorageFilerTO pool = command.getPool();
when(this.libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolMgr);
when(storagePoolMgr.deleteStoragePool(pool.getType(), pool.getUuid())).thenThrow(CloudRuntimeException.class);
final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
assertNotNull(wrapper);
final Answer answer = wrapper.execute(command, this.libvirtComputingResource);
assertFalse(answer.getResult());
verify(this.libvirtComputingResource, times(1)).getStoragePoolMgr();
verify(storagePoolMgr, times(1)).deleteStoragePool(pool.getType(), pool.getUuid());
}
use of com.cloud.legacymodel.to.StorageFilerTO in project cosmic by MissionCriticalCloud.
the class LibvirtComputingResourceTest method testResizeVolumeCommandSameSize.
@Test
public void testResizeVolumeCommandSameSize() {
final String path = "nfs:/127.0.0.1/storage/secondary";
final StorageFilerTO pool = Mockito.mock(StorageFilerTO.class);
final Long currentSize = 100l;
final Long newSize = 100l;
final boolean shrinkOk = false;
final String vmInstance = "Test";
final ResizeVolumeCommand command = new ResizeVolumeCommand(path, pool, currentSize, newSize, shrinkOk, vmInstance);
final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
assertNotNull(wrapper);
final Answer answer = wrapper.execute(command, this.libvirtComputingResource);
assertTrue(answer.getResult());
}
Aggregations