use of com.xensource.xenapi.Types.XenAPIException in project cloudstack by apache.
the class XenServerStorageProcessor method getNfsSR.
protected SR getNfsSR(final Connection conn, final StorageFilerTO pool) {
final Map<String, String> deviceConfig = new HashMap<String, String>();
try {
final String server = pool.getHost();
String serverpath = pool.getPath();
serverpath = serverpath.replace("//", "/");
final Set<SR> srs = SR.getAll(conn);
for (final SR sr : srs) {
if (!SRType.NFS.equals(sr.getType(conn))) {
continue;
}
final Set<PBD> pbds = sr.getPBDs(conn);
if (pbds.isEmpty()) {
continue;
}
final PBD pbd = pbds.iterator().next();
final Map<String, String> dc = pbd.getDeviceConfig(conn);
if (dc == null) {
continue;
}
if (dc.get("server") == null) {
continue;
}
if (dc.get("serverpath") == null) {
continue;
}
if (server.equals(dc.get("server")) && serverpath.equals(dc.get("serverpath"))) {
throw new CloudRuntimeException("There is a SR using the same configuration server:" + dc.get("server") + ", serverpath:" + dc.get("serverpath") + " for pool " + pool.getUuid() + "on host:" + hypervisorResource.getHost().getUuid());
}
}
deviceConfig.put("server", server);
deviceConfig.put("serverpath", serverpath);
final Host host = Host.getByUuid(conn, hypervisorResource.getHost().getUuid());
final Map<String, String> smConfig = new HashMap<String, String>();
smConfig.put("nosubdir", "true");
final SR sr = SR.create(conn, host, deviceConfig, new Long(0), pool.getUuid(), Long.toString(pool.getId()), SRType.NFS.toString(), "user", true, smConfig);
sr.scan(conn);
return sr;
} catch (final XenAPIException e) {
throw new CloudRuntimeException("Unable to create NFS SR " + pool.toString(), e);
} catch (final XmlRpcException e) {
throw new CloudRuntimeException("Unable to create NFS SR " + pool.toString(), e);
}
}
use of com.xensource.xenapi.Types.XenAPIException in project cloudstack by apache.
the class XenServerStorageProcessor method directDownloadHttpTemplate.
protected Answer directDownloadHttpTemplate(final CopyCommand cmd, final DecodedDataObject srcObj, final DecodedDataObject destObj) {
final Connection conn = hypervisorResource.getConnection();
SR poolsr = null;
VDI vdi = null;
boolean result = false;
try {
if (destObj.getPath() == null) {
//need to create volume at first
}
vdi = VDI.getByUuid(conn, destObj.getPath());
if (vdi == null) {
throw new CloudRuntimeException("can't find volume: " + destObj.getPath());
}
final String destStoreUuid = destObj.getStore().getUuid();
final Set<SR> srs = SR.getByNameLabel(conn, destStoreUuid);
if (srs.size() != 1) {
throw new CloudRuntimeException("storage uuid: " + destStoreUuid + " is not unique");
}
poolsr = srs.iterator().next();
final VDI.Record vdir = vdi.getRecord(conn);
final String vdiLocation = vdir.location;
String pbdLocation = null;
if (destObj.getStore().getScheme().equalsIgnoreCase(DataStoreProtocol.NFS.toString())) {
pbdLocation = "/run/sr-mount/" + poolsr.getUuid(conn);
} else {
final Set<PBD> pbds = poolsr.getPBDs(conn);
if (pbds.size() != 1) {
throw new CloudRuntimeException("Don't how to handle multiple pbds:" + pbds.size() + " for sr: " + poolsr.getUuid(conn));
}
final PBD pbd = pbds.iterator().next();
final Map<String, String> deviceCfg = pbd.getDeviceConfig(conn);
pbdLocation = deviceCfg.get("location");
}
if (pbdLocation == null) {
throw new CloudRuntimeException("Can't get pbd location");
}
final String vdiPath = pbdLocation + "/" + vdiLocation + ".vhd";
//download a url into vdipath
//downloadHttpToLocalFile(vdiPath, template.getPath());
hypervisorResource.callHostPlugin(conn, "storagePlugin", "downloadTemplateFromUrl", "destPath", vdiPath, "srcUrl", srcObj.getPath());
result = true;
//return new CopyCmdAnswer(cmd, vdi.getUuid(conn));
} catch (final BadServerResponse e) {
s_logger.debug("Failed to download template", e);
} catch (final XenAPIException e) {
s_logger.debug("Failed to download template", e);
} catch (final XmlRpcException e) {
s_logger.debug("Failed to download template", e);
} catch (final Exception e) {
s_logger.debug("Failed to download template", e);
} finally {
if (!result && vdi != null) {
try {
vdi.destroy(conn);
} catch (final BadServerResponse e) {
s_logger.debug("Failed to cleanup newly created vdi");
} catch (final XenAPIException e) {
s_logger.debug("Failed to cleanup newly created vdi");
} catch (final XmlRpcException e) {
s_logger.debug("Failed to cleanup newly created vdi");
}
}
}
return new Answer(cmd, false, "Failed to download template");
}
use of com.xensource.xenapi.Types.XenAPIException in project cloudstack by apache.
the class CitrixResourceBase method getIsoVDIByURL.
public VDI getIsoVDIByURL(final Connection conn, final String vmName, final String isoURL) {
SR isoSR = null;
String mountpoint = null;
if (isoURL.startsWith("xs-tools")) {
try {
final Set<VDI> vdis = VDI.getByNameLabel(conn, isoURL);
if (vdis.isEmpty()) {
throw new CloudRuntimeException("Could not find ISO with URL: " + isoURL);
}
return vdis.iterator().next();
} catch (final XenAPIException e) {
throw new CloudRuntimeException("Unable to get pv iso: " + isoURL + " due to " + e.toString());
} catch (final Exception e) {
throw new CloudRuntimeException("Unable to get pv iso: " + isoURL + " due to " + e.toString());
}
}
final int index = isoURL.lastIndexOf("/");
mountpoint = isoURL.substring(0, index);
URI uri;
try {
uri = new URI(mountpoint);
} catch (final URISyntaxException e) {
throw new CloudRuntimeException("isoURL is wrong: " + isoURL);
}
isoSR = getISOSRbyVmName(conn, vmName);
if (isoSR == null) {
isoSR = createIsoSRbyURI(conn, uri, vmName, false);
}
final String isoName = isoURL.substring(index + 1);
final VDI isoVDI = getVDIbyLocationandSR(conn, isoName, isoSR);
if (isoVDI != null) {
return isoVDI;
} else {
throw new CloudRuntimeException("Could not find ISO with URL: " + isoURL);
}
}
use of com.xensource.xenapi.Types.XenAPIException in project cloudstack by apache.
the class CitrixResourceBase method getLocalLVMSR.
protected SR getLocalLVMSR(final Connection conn) {
try {
final Map<SR, SR.Record> map = SR.getAllRecords(conn);
if (map != null && !map.isEmpty()) {
for (final Map.Entry<SR, SR.Record> entry : map.entrySet()) {
final SR.Record srRec = entry.getValue();
if (SRType.LVM.equals(srRec.type)) {
final Set<PBD> pbds = srRec.PBDs;
if (pbds == null) {
continue;
}
for (final PBD pbd : pbds) {
final Host host = pbd.getHost(conn);
if (!isRefNull(host) && host.getUuid(conn).equals(_host.getUuid())) {
if (!pbd.getCurrentlyAttached(conn)) {
pbd.plug(conn);
}
final SR sr = entry.getKey();
sr.scan(conn);
return sr;
}
}
}
}
}
} catch (final XenAPIException e) {
final String msg = "Unable to get local LVMSR in host:" + _host.getUuid() + e.toString();
s_logger.warn(msg);
} catch (final XmlRpcException e) {
final String msg = "Unable to get local LVMSR in host:" + _host.getUuid() + e.getCause();
s_logger.warn(msg);
}
return null;
}
use of com.xensource.xenapi.Types.XenAPIException in project cloudstack by apache.
the class CitrixResourceBase method cleanupHaltedVms.
public boolean cleanupHaltedVms(final Connection conn) throws XenAPIException, XmlRpcException {
final Host host = Host.getByUuid(conn, _host.getUuid());
final Map<VM, VM.Record> vms = VM.getAllRecords(conn);
boolean success = true;
if (vms != null && !vms.isEmpty()) {
for (final Map.Entry<VM, VM.Record> entry : vms.entrySet()) {
final VM vm = entry.getKey();
final VM.Record vmRec = entry.getValue();
if (vmRec.isATemplate || vmRec.isControlDomain) {
continue;
}
if (VmPowerState.HALTED.equals(vmRec.powerState) && vmRec.affinity.equals(host) && !isAlienVm(vm, conn)) {
try {
vm.destroy(conn);
} catch (final Exception e) {
s_logger.warn("Catch Exception " + e.getClass().getName() + ": unable to destroy VM " + vmRec.nameLabel + " due to ", e);
success = false;
}
}
}
}
return success;
}
Aggregations