use of org.apache.cloudstack.storage.command.AttachAnswer in project cloudstack by apache.
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(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, citrixResourceBase);
assertTrue(answer.getResult());
}
use of org.apache.cloudstack.storage.command.AttachAnswer in project cloudstack by apache.
the class Ovm3StorageProcessor method attachDetach.
/**
* Generic disk attach/detach.
* @param cmd
* @param vmName
* @param disk
* @param isAttach
* @return
*/
private AttachAnswer attachDetach(Command cmd, String vmName, DiskTO disk, boolean isAttach) {
Xen xen = new Xen(c);
String doThis = (isAttach) ? "Attach" : "Dettach";
LOGGER.debug(doThis + " volume type " + disk.getType() + " " + vmName);
String msg = "";
String path = "";
try {
Xen.Vm vm = xen.getVmConfig(vmName);
/* check running */
if (vm == null) {
msg = doThis + " can't find VM " + vmName;
LOGGER.debug(msg);
return new AttachAnswer(msg);
}
if (disk.getType() == Volume.Type.ISO) {
path = getIsoPath(disk);
} else if (disk.getType() == Volume.Type.DATADISK) {
path = getVirtualDiskPath(disk, vm.getPrimaryPoolUuid());
}
if ("".equals(path)) {
msg = doThis + " can't do anything with an empty path.";
LOGGER.debug(msg);
return new AttachAnswer(msg);
}
if (isAttach) {
if (disk.getType() == Volume.Type.ISO) {
vm.addIso(path);
} else {
vm.addDataDisk(path);
}
} else {
if (!vm.removeDisk(path)) {
msg = doThis + " failed for " + vmName + disk.getType() + " was not attached " + path;
LOGGER.debug(msg);
return new AttachAnswer(msg);
}
}
xen.configureVm(ovmObject.deDash(vm.getPrimaryPoolUuid()), vm.getVmUuid());
return new AttachAnswer(disk);
} catch (Ovm3ResourceException e) {
msg = doThis + " failed for " + vmName + " " + e.getMessage();
LOGGER.warn(msg, e);
return new AttachAnswer(msg);
}
}
use of org.apache.cloudstack.storage.command.AttachAnswer in project cloudstack by apache.
the class XenServerStorageProcessor method attachIso.
@Override
public AttachAnswer attachIso(final AttachCommand cmd) {
final DiskTO disk = cmd.getDisk();
final DataTO data = disk.getData();
final DataStoreTO store = data.getDataStore();
String isoURL = null;
if (store == null) {
final TemplateObjectTO iso = (TemplateObjectTO) disk.getData();
isoURL = iso.getName();
} else {
if (!(store instanceof NfsTO)) {
s_logger.debug("Can't attach a iso which is not created on nfs: ");
return new AttachAnswer("Can't attach a iso which is not created on nfs: ");
}
final NfsTO nfsStore = (NfsTO) store;
isoURL = nfsStore.getUrl() + nfsStore.getPathSeparator() + data.getPath();
}
final String vmName = cmd.getVmName();
try {
final Connection conn = hypervisorResource.getConnection();
VBD isoVBD = null;
// Find the VM
final VM vm = hypervisorResource.getVM(conn, vmName);
// Find the ISO VDI
final VDI isoVDI = hypervisorResource.getIsoVDIByURL(conn, vmName, isoURL);
// Find the VM's CD-ROM VBD
final Set<VBD> vbds = vm.getVBDs(conn);
for (final VBD vbd : vbds) {
final String userDevice = vbd.getUserdevice(conn);
final Types.VbdType type = vbd.getType(conn);
if (userDevice.equals("3") && type == Types.VbdType.CD) {
isoVBD = vbd;
break;
}
}
if (isoVBD == null) {
throw new CloudRuntimeException("Unable to find CD-ROM VBD for VM: " + vmName);
} else {
// If an ISO is already inserted, eject it
if (!isoVBD.getEmpty(conn)) {
isoVBD.eject(conn);
}
// Insert the new ISO
isoVBD.insert(conn, isoVDI);
}
return new AttachAnswer(disk);
} catch (final XenAPIException e) {
s_logger.warn("Failed to attach iso" + ": " + e.toString(), e);
return new AttachAnswer(e.toString());
} catch (final Exception e) {
s_logger.warn("Failed to attach iso" + ": " + e.toString(), e);
return new AttachAnswer(e.toString());
}
}
use of org.apache.cloudstack.storage.command.AttachAnswer in project cloudstack by apache.
the class VmwareStorageProcessor method attachIso.
private Answer attachIso(DiskTO disk, boolean isAttach, String vmName) {
try {
VmwareContext context = hostService.getServiceContext(null);
VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, null);
VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(vmName);
if (vmMo == null) {
String msg = "Unable to find VM in vSphere to execute AttachIsoCommand, vmName: " + vmName;
s_logger.error(msg);
throw new Exception(msg);
}
TemplateObjectTO iso = (TemplateObjectTO) disk.getData();
NfsTO nfsImageStore = (NfsTO) iso.getDataStore();
String storeUrl = null;
if (nfsImageStore != null) {
storeUrl = nfsImageStore.getUrl();
}
if (storeUrl == null) {
if (!iso.getName().equalsIgnoreCase("vmware-tools.iso")) {
String msg = "ISO store root url is not found in AttachIsoCommand";
s_logger.error(msg);
throw new Exception(msg);
} else {
if (isAttach) {
vmMo.mountToolsInstaller();
} else {
try {
if (!vmMo.unmountToolsInstaller()) {
return new AttachAnswer("Failed to unmount vmware-tools installer ISO as the corresponding CDROM device is locked by VM. Please unmount the CDROM device inside the VM and ret-try.");
}
} catch (Throwable e) {
vmMo.detachIso(null);
}
}
return new AttachAnswer(disk);
}
}
ManagedObjectReference morSecondaryDs = prepareSecondaryDatastoreOnHost(storeUrl);
String isoPath = nfsImageStore.getUrl() + File.separator + iso.getPath();
if (!isoPath.startsWith(storeUrl)) {
assert (false);
String msg = "ISO path does not start with the secondary storage root";
s_logger.error(msg);
throw new Exception(msg);
}
int isoNameStartPos = isoPath.lastIndexOf('/');
String isoFileName = isoPath.substring(isoNameStartPos + 1);
String isoStorePathFromRoot = isoPath.substring(storeUrl.length(), isoNameStartPos);
// TODO, check if iso is already attached, or if there is a previous
// attachment
DatastoreMO secondaryDsMo = new DatastoreMO(context, morSecondaryDs);
String storeName = secondaryDsMo.getName();
String isoDatastorePath = String.format("[%s] %s/%s", storeName, isoStorePathFromRoot, isoFileName);
if (isAttach) {
vmMo.attachIso(isoDatastorePath, morSecondaryDs, true, false);
} else {
vmMo.detachIso(isoDatastorePath);
}
return new AttachAnswer(disk);
} catch (Throwable e) {
if (e instanceof RemoteException) {
s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context");
hostService.invalidateServiceContext(null);
}
if (isAttach) {
String msg = "AttachIsoCommand(attach) failed due to " + VmwareHelper.getExceptionMessage(e);
msg = msg + " Also check if your guest os is a supported version";
s_logger.error(msg, e);
return new AttachAnswer(msg);
} else {
String msg = "AttachIsoCommand(detach) failed due to " + VmwareHelper.getExceptionMessage(e);
msg = msg + " Also check if your guest os is a supported version";
s_logger.warn(msg, e);
return new AttachAnswer(msg);
}
}
}
Aggregations