use of com.xensource.xenapi.SR in project cosmic by MissionCriticalCloud.
the class CitrixResourceBase method cleanupTemplateSR.
public void cleanupTemplateSR(final Connection conn) {
Set<PBD> pbds = null;
try {
final Host host = Host.getByUuid(conn, _host.getUuid());
pbds = host.getPBDs(conn);
} catch (final XenAPIException e) {
s_logger.warn("Unable to get the SRs " + e.toString(), e);
throw new CloudRuntimeException("Unable to get SRs " + e.toString(), e);
} catch (final Exception e) {
throw new CloudRuntimeException("Unable to get SRs " + e.getMessage(), e);
}
for (final PBD pbd : pbds) {
SR sr = null;
SR.Record srRec = null;
try {
sr = pbd.getSR(conn);
srRec = sr.getRecord(conn);
} catch (final Exception e) {
s_logger.warn("pbd.getSR get Exception due to ", e);
continue;
}
final String type = srRec.type;
if (srRec.shared) {
continue;
}
if (SRType.NFS.equals(type) || SRType.ISO.equals(type) && srRec.nameDescription.contains("template")) {
try {
pbd.unplug(conn);
pbd.destroy(conn);
sr.forget(conn);
} catch (final Exception e) {
s_logger.warn("forget SR catch Exception due to ", e);
}
}
}
}
use of com.xensource.xenapi.SR in project cosmic by MissionCriticalCloud.
the class CitrixResourceBase method createPatchVbd.
public VBD createPatchVbd(final Connection conn, final String vmName, final VM vm) throws XmlRpcException, XenAPIException {
if (_host.getSystemvmisouuid() == null) {
final Set<SR> srs = SR.getByNameLabel(conn, "XenServer Tools");
if (srs.size() != 1) {
throw new CloudRuntimeException("There are " + srs.size() + " SRs with name XenServer Tools");
}
final SR sr = srs.iterator().next();
sr.scan(conn);
final SR.Record srr = sr.getRecord(conn);
if (_host.getSystemvmisouuid() == null) {
for (final VDI vdi : srr.VDIs) {
final VDI.Record vdir = vdi.getRecord(conn);
if (vdir.nameLabel.contains("systemvm.iso")) {
_host.setSystemvmisouuid(vdir.uuid);
break;
}
}
}
if (_host.getSystemvmisouuid() == null) {
throw new CloudRuntimeException("can not find systemvmiso");
}
}
final VBD.Record cdromVBDR = new VBD.Record();
cdromVBDR.VM = vm;
cdromVBDR.empty = true;
cdromVBDR.bootable = false;
cdromVBDR.userdevice = "3";
cdromVBDR.mode = Types.VbdMode.RO;
cdromVBDR.type = Types.VbdType.CD;
final VBD cdromVBD = VBD.create(conn, cdromVBDR);
cdromVBD.insert(conn, VDI.getByUuid(conn, _host.getSystemvmisouuid()));
return cdromVBD;
}
use of com.xensource.xenapi.SR in project cosmic by MissionCriticalCloud.
the class CitrixResourceBase method getSRByNameLabelandHost.
protected SR getSRByNameLabelandHost(final Connection conn, final String name) throws BadServerResponse, XenAPIException, XmlRpcException {
final Set<SR> srs = SR.getByNameLabel(conn, name);
SR ressr = null;
for (final SR sr : srs) {
final Set<PBD> pbds;
pbds = sr.getPBDs(conn);
for (final PBD pbd : pbds) {
final PBD.Record pbdr = pbd.getRecord(conn);
if (pbdr.host != null && pbdr.host.getUuid(conn).equals(_host.getUuid())) {
if (!pbdr.currentlyAttached) {
pbd.plug(conn);
}
ressr = sr;
break;
}
}
}
return ressr;
}
use of com.xensource.xenapi.SR in project cosmic by MissionCriticalCloud.
the class CitrixResourceBase method mount.
protected VDI mount(final Connection conn, final String vmName, final DiskTO volume) throws XmlRpcException, XenAPIException {
final DataTO data = volume.getData();
final Volume.Type type = volume.getType();
if (type == Volume.Type.ISO) {
final TemplateObjectTO iso = (TemplateObjectTO) data;
final DataStoreTO store = iso.getDataStore();
if (store == null) {
// It's a fake iso
return null;
}
// corer case, xenserver pv driver iso
final String templateName = iso.getName();
if (templateName.startsWith("xs-tools")) {
try {
final Set<VDI> vdis = VDI.getByNameLabel(conn, templateName);
if (vdis.isEmpty()) {
throw new CloudRuntimeException("Could not find ISO with URL: " + templateName);
}
return vdis.iterator().next();
} catch (final XenAPIException e) {
throw new CloudRuntimeException("Unable to get pv iso: " + templateName + " due to " + e.toString());
} catch (final Exception e) {
throw new CloudRuntimeException("Unable to get pv iso: " + templateName + " due to " + e.toString());
}
}
if (!(store instanceof NfsTO)) {
throw new CloudRuntimeException("only support mount iso on nfs");
}
final NfsTO nfsStore = (NfsTO) store;
final String isoPath = nfsStore.getUrl() + File.separator + iso.getPath();
final int index = isoPath.lastIndexOf("/");
final String mountpoint = isoPath.substring(0, index);
final URI uri;
try {
uri = new URI(mountpoint);
} catch (final URISyntaxException e) {
throw new CloudRuntimeException("Incorrect uri " + mountpoint, e);
}
final SR isoSr = createIsoSRbyURI(conn, uri, vmName, false);
final String isoname = isoPath.substring(index + 1);
final VDI isoVdi = getVDIbyLocationandSR(conn, isoname, isoSr);
if (isoVdi == null) {
throw new CloudRuntimeException("Unable to find ISO " + isoPath);
}
return isoVdi;
} else {
final VolumeObjectTO vol = (VolumeObjectTO) data;
return VDI.getByUuid(conn, vol.getPath());
}
}
use of com.xensource.xenapi.SR in project cosmic by MissionCriticalCloud.
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, _configDriveSRName + _host.getIp());
if (sr != null) {
s_logger.debug("Config drive SR already exist, returing it");
return sr;
}
try {
final Map<String, String> deviceConfig = new HashMap<>();
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<>());
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);
}
}
Aggregations