use of com.xensource.xenapi.SR in project cloudstack by apache.
the class CitrixResourceBase method destroyPatchVbd.
public void destroyPatchVbd(final Connection conn, final Set<VM> vms) throws XmlRpcException, XenAPIException {
final SR sr = findPatchIsoSR(conn);
final VDI patchVDI = findPatchIsoVDI(conn, sr);
for (final VM vm : vms) {
final String vmName = vm.getNameLabel(conn);
if (!vmName.startsWith("r-") && !vmName.startsWith("s-") && !vmName.startsWith("v-")) {
continue;
}
final Set<VBD> vbds = vm.getVBDs(conn);
for (final VBD vbd : vbds) {
if (Types.VbdType.CD.equals(vbd.getType(conn))) {
try {
if (!vbd.getEmpty(conn)) {
vbd.eject(conn);
}
// Workaround for any file descriptor caching issue
if (patchVDI != null) {
vbd.insert(conn, patchVDI);
vbd.eject(conn);
}
} catch (Exception e) {
s_logger.debug("Cannot eject CD-ROM device for VM " + vmName + " due to " + e.toString(), e);
}
try {
vbd.destroy(conn);
} catch (Exception e) {
s_logger.debug("Cannot destroy CD-ROM device for VM " + vmName + " due to " + e.toString(), e);
}
break;
}
}
}
}
use of com.xensource.xenapi.SR in project cloudstack by apache.
the class CitrixResourceBase method createIsoSRbyURI.
protected SR createIsoSRbyURI(final Connection conn, final URI uri, final String vmName, final boolean shared) {
try {
final Map<String, String> deviceConfig = new HashMap<String, String>();
String path = uri.getPath();
path = path.replace("//", "/");
deviceConfig.put("location", uri.getHost() + ":" + path);
final Host host = Host.getByUuid(conn, _host.getUuid());
final SR sr = SR.create(conn, host, deviceConfig, new Long(0), uri.getHost() + path, "iso", "iso", "iso", shared, new HashMap<String, String>());
sr.setNameLabel(conn, vmName + "-ISO");
sr.setNameDescription(conn, deviceConfig.get("location"));
sr.scan(conn);
return sr;
} catch (final XenAPIException e) {
final String msg = "createIsoSRbyURI failed! mountpoint: " + uri.getHost() + uri.getPath() + " due to " + e.toString();
s_logger.warn(msg, e);
throw new CloudRuntimeException(msg, e);
} catch (final Exception e) {
final String msg = "createIsoSRbyURI failed! mountpoint: " + uri.getHost() + uri.getPath() + " due to " + e.getMessage();
s_logger.warn(msg, e);
throw new CloudRuntimeException(msg, e);
}
}
use of com.xensource.xenapi.SR in project cloudstack by apache.
the class CitrixResourceBase method createLocalIsoSR.
public SR createLocalIsoSR(final Connection conn, final String srName) throws XenAPIException, XmlRpcException {
// if config drive sr already exists then return
SR sr = getSRByNameLabelandHost(conn, srName);
if (sr != null) {
s_logger.debug("Config drive SR already exist, returing it");
return sr;
}
try {
final Map<String, String> deviceConfig = new HashMap<String, String>();
final com.trilead.ssh2.Connection sshConnection = new com.trilead.ssh2.Connection(_host.getIp(), 22);
try {
sshConnection.connect(null, 60000, 60000);
if (!sshConnection.authenticateWithPassword(_username, _password.peek())) {
throw new CloudRuntimeException("Unable to authenticate");
}
final String cmd = "mkdir -p " + _configDriveIsopath;
if (!SSHCmdHelper.sshExecuteCmd(sshConnection, cmd)) {
throw new CloudRuntimeException("Cannot create directory configdrive_iso on XenServer hosts");
}
} catch (final IOException e) {
throw new CloudRuntimeException("Unable to create iso folder", e);
} finally {
sshConnection.close();
}
s_logger.debug("Created the config drive SR " + srName + " folder path " + _configDriveIsopath);
deviceConfig.put("location", _configDriveIsopath);
deviceConfig.put("legacy_mode", "true");
final Host host = Host.getByUuid(conn, _host.getUuid());
final String type = SRType.ISO.toString();
sr = SR.create(conn, host, deviceConfig, new Long(0), _configDriveIsopath, "iso", type, "iso", false, new HashMap<String, String>());
sr.setNameLabel(conn, srName);
sr.setNameDescription(conn, deviceConfig.get("location"));
sr.scan(conn);
s_logger.debug("Config drive ISO SR at the path " + _configDriveIsopath + " got created in host " + _host);
return sr;
} catch (final XenAPIException e) {
final String msg = "createLocalIsoSR failed! mountpoint " + e.toString();
s_logger.warn(msg, e);
throw new CloudRuntimeException(msg, e);
} catch (final Exception e) {
final String msg = "createLocalIsoSR failed! mountpoint: due to " + e.getMessage();
s_logger.warn(msg, e);
throw new CloudRuntimeException(msg, e);
}
}
use of com.xensource.xenapi.SR in project cloudstack by apache.
the class CitrixResourceBaseTest method getAllLocalSrForTypeTest.
@Test
public void getAllLocalSrForTypeTest() throws Exception {
String mockHostUuid = "hostUuid";
citrixResourceBase._host.setUuid(mockHostUuid);
Connection connectionMock = Mockito.mock(Connection.class);
SR srExtShared = Mockito.mock(SR.class);
SR srExtNonShared = Mockito.mock(SR.class);
List<SR> expectedListOfSrs = new ArrayList<>();
expectedListOfSrs.add(srExtNonShared);
Set<PBD> pbds = new HashSet<>();
PBD pbdMock = Mockito.mock(PBD.class);
Host hostMock = Mockito.mock(Host.class);
Mockito.when(hostMock.getUuid(connectionMock)).thenReturn(mockHostUuid);
Mockito.when(hostMock.toWireString()).thenReturn(mockHostUuid);
Mockito.when(pbdMock.getHost(connectionMock)).thenReturn(hostMock);
pbds.add(pbdMock);
SR.Record srExtSharedRecord = Mockito.mock(SR.Record.class);
srExtSharedRecord.type = "EXT";
srExtSharedRecord.shared = true;
srExtSharedRecord.PBDs = pbds;
SR.Record srExtNonSharedRecord = Mockito.mock(SR.Record.class);
srExtNonSharedRecord.type = "EXT";
srExtNonSharedRecord.shared = false;
srExtNonSharedRecord.PBDs = pbds;
Map<SR, SR.Record> mapOfSrsRecords = new HashMap<>();
mapOfSrsRecords.put(srExtShared, srExtSharedRecord);
mapOfSrsRecords.put(srExtNonShared, srExtNonSharedRecord);
PowerMockito.mockStatic(SR.class);
BDDMockito.given(SR.getAllRecords(connectionMock)).willReturn(mapOfSrsRecords);
List<SR> allLocalSrForType = citrixResourceBase.getAllLocalSrForType(connectionMock, SRType.EXT);
Assert.assertEquals(expectedListOfSrs.size(), allLocalSrForType.size());
Assert.assertEquals(expectedListOfSrs.get(0), allLocalSrForType.get(0));
}
use of com.xensource.xenapi.SR in project cloudstack by apache.
the class CitrixResourceBaseTest method initializeLocalSrTest.
@Test
public void initializeLocalSrTest() throws XenAPIException, XmlRpcException {
Connection connectionMock = Mockito.mock(Connection.class);
List<SR> srsMocks = new ArrayList<>();
SR srMock1 = Mockito.mock(SR.class);
SR srMock2 = Mockito.mock(SR.class);
Mockito.when(srMock1.getPhysicalSize(connectionMock)).thenReturn(0l);
Mockito.when(srMock2.getPhysicalSize(connectionMock)).thenReturn(100l);
srsMocks.add(srMock1);
srsMocks.add(srMock2);
Mockito.doReturn(srsMocks).when(citrixResourceBase).getAllLocalSrs(connectionMock);
StartupStorageCommand startupStorageCommandMock = Mockito.mock(StartupStorageCommand.class);
Mockito.doReturn(startupStorageCommandMock).when(citrixResourceBase).createStartUpStorageCommand(Mockito.eq(connectionMock), Mockito.any(SR.class));
List<StartupStorageCommand> startUpCommandsForLocalStorage = citrixResourceBase.initializeLocalSrs(connectionMock);
Mockito.verify(citrixResourceBase, Mockito.times(0)).createStartUpStorageCommand(connectionMock, srMock1);
Mockito.verify(citrixResourceBase, Mockito.times(1)).createStartUpStorageCommand(connectionMock, srMock2);
Assert.assertEquals(1, startUpCommandsForLocalStorage.size());
}
Aggregations