use of com.xensource.xenapi.SR in project cloudstack by apache.
the class CitrixAttachIsoCommandWrapper method execute.
@Override
public Answer execute(final AttachIsoCommand command, final CitrixResourceBase citrixResourceBase) {
final Connection conn = citrixResourceBase.getConnection();
final boolean attach = command.isAttach();
final String vmName = command.getVmName();
final String isoURL = command.getIsoPath();
String errorMsg;
if (attach) {
errorMsg = "Failed to attach ISO";
} else {
errorMsg = "Failed to detach ISO";
}
try {
if (attach) {
VBD isoVBD = null;
// Find the VM
final VM vm = citrixResourceBase.getVM(conn, vmName);
// Find the ISO VDI
final VDI isoVDI = citrixResourceBase.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) == false) {
isoVBD.eject(conn);
}
// Insert the new ISO
isoVBD.insert(conn, isoVDI);
}
return new Answer(command);
} else {
// Find the VM
final VM vm = citrixResourceBase.getVM(conn, vmName);
final String vmUUID = vm.getUuid(conn);
// Find the ISO VDI
final VDI isoVDI = citrixResourceBase.getIsoVDIByURL(conn, vmName, isoURL);
final SR sr = isoVDI.getSR(conn);
// Look up all VBDs for this VDI
final Set<VBD> vbds = isoVDI.getVBDs(conn);
// the ISO from it
for (final VBD vbd : vbds) {
final VM vbdVM = vbd.getVM(conn);
final String vbdVmUUID = vbdVM.getUuid(conn);
if (vbdVmUUID.equals(vmUUID)) {
// If an ISO is already inserted, eject it
if (!vbd.getEmpty(conn)) {
vbd.eject(conn);
}
break;
}
}
if (!XenServerUtilitiesHelper.isXenServerToolsSR(sr.getNameLabel(conn))) {
citrixResourceBase.removeSR(conn, sr);
}
return new Answer(command);
}
} catch (final XenAPIException e) {
s_logger.warn(errorMsg + ": " + e.toString(), e);
return new Answer(command, false, e.toString());
} catch (final Exception e) {
s_logger.warn(errorMsg + ": " + e.toString(), e);
return new Answer(command, false, e.getMessage());
}
}
use of com.xensource.xenapi.SR in project cloudstack by apache.
the class CitrixCreateCommandWrapper method execute.
@Override
public Answer execute(final CreateCommand command, final CitrixResourceBase citrixResourceBase) {
final Connection conn = citrixResourceBase.getConnection();
final StorageFilerTO pool = command.getPool();
final DiskProfile dskch = command.getDiskCharacteristics();
VDI vdi = null;
try {
final SR poolSr = citrixResourceBase.getStorageRepository(conn, CitrixHelper.getSRNameLabel(pool.getUuid(), pool.getType(), pool.getPath()));
if (command.getTemplateUrl() != null) {
VDI tmpltvdi = null;
tmpltvdi = citrixResourceBase.getVDIbyUuid(conn, command.getTemplateUrl());
vdi = tmpltvdi.createClone(conn, new HashMap<String, String>());
vdi.setNameLabel(conn, dskch.getName());
} else {
final VDI.Record vdir = new VDI.Record();
vdir.nameLabel = dskch.getName();
vdir.SR = poolSr;
vdir.type = Types.VdiType.USER;
vdir.virtualSize = dskch.getSize();
vdi = VDI.create(conn, vdir);
}
VDI.Record vdir;
vdir = vdi.getRecord(conn);
s_logger.debug("Succesfully created VDI for " + command + ". Uuid = " + vdir.uuid);
final VolumeTO vol = new VolumeTO(command.getVolumeId(), dskch.getType(), pool.getType(), pool.getUuid(), vdir.nameLabel, pool.getPath(), vdir.uuid, vdir.virtualSize, null);
return new CreateAnswer(command, vol);
} catch (final Exception e) {
s_logger.warn("Unable to create volume; Pool=" + pool + "; Disk: " + dskch, e);
return new CreateAnswer(command, e);
}
}
use of com.xensource.xenapi.SR in project cloudstack by apache.
the class CitrixDeleteStoragePoolCommandWrapper method execute.
@Override
public Answer execute(final DeleteStoragePoolCommand command, final CitrixResourceBase citrixResourceBase) {
final Connection conn = citrixResourceBase.getConnection();
final StorageFilerTO poolTO = command.getPool();
try {
final SR sr;
// getRemoveDatastore being true indicates we are using managed storage and need to pull the SR name out of a Map
// instead of pulling it out using getUuid of the StorageFilerTO instance.
String srNameLabel;
if (command.getRemoveDatastore()) {
Map<String, String> details = command.getDetails();
srNameLabel = details.get(DeleteStoragePoolCommand.DATASTORE_NAME);
} else {
srNameLabel = CitrixHelper.getSRNameLabel(poolTO.getUuid(), poolTO.getType(), poolTO.getPath());
}
sr = citrixResourceBase.getStorageRepository(conn, srNameLabel);
citrixResourceBase.removeSR(conn, sr);
final Answer answer = new Answer(command, true, "success");
return answer;
} catch (final Exception e) {
final String msg = "DeleteStoragePoolCommand XenAPIException:" + e.getMessage() + " host:" + citrixResourceBase.getHost().getUuid() + " pool: " + poolTO.getHost() + poolTO.getPath();
s_logger.error(msg, e);
return new Answer(command, false, msg);
}
}
use of com.xensource.xenapi.SR in project cloudstack by apache.
the class CitrixModifyStoragePoolCommandWrapper method execute.
@Override
public Answer execute(final ModifyStoragePoolCommand command, final CitrixResourceBase citrixResourceBase) {
final Connection conn = citrixResourceBase.getConnection();
final StorageFilerTO pool = command.getPool();
final boolean add = command.getAdd();
if (add) {
try {
String srName = command.getStoragePath();
if (srName == null) {
srName = CitrixHelper.getSRNameLabel(pool.getUuid(), pool.getType(), pool.getPath());
}
final SR sr = citrixResourceBase.getStorageRepository(conn, srName);
citrixResourceBase.setupHeartbeatSr(conn, sr, false);
final long capacity = sr.getPhysicalSize(conn);
final long available = capacity - sr.getPhysicalUtilisation(conn);
if (capacity == -1) {
final String msg = "Pool capacity is -1! pool: " + pool.getHost() + pool.getPath();
s_logger.warn(msg);
return new Answer(command, false, msg);
}
final Map<String, TemplateProp> tInfo = new HashMap<String, TemplateProp>();
final ModifyStoragePoolAnswer answer = new ModifyStoragePoolAnswer(command, capacity, available, tInfo);
return answer;
} catch (final XenAPIException e) {
final String msg = "ModifyStoragePoolCommand add XenAPIException:" + e.toString() + " host:" + citrixResourceBase.getHost().getUuid() + " pool: " + pool.getHost() + pool.getPath();
s_logger.warn(msg, e);
return new Answer(command, false, msg);
} catch (final Exception e) {
final String msg = "ModifyStoragePoolCommand add XenAPIException:" + e.getMessage() + " host:" + citrixResourceBase.getHost().getUuid() + " pool: " + pool.getHost() + pool.getPath();
s_logger.warn(msg, e);
return new Answer(command, false, msg);
}
} else {
try {
final SR sr = citrixResourceBase.getStorageRepository(conn, CitrixHelper.getSRNameLabel(pool.getUuid(), pool.getType(), pool.getPath()));
final String srUuid = sr.getUuid(conn);
final String result = citrixResourceBase.callHostPluginPremium(conn, "setup_heartbeat_file", "host", citrixResourceBase.getHost().getUuid(), "sr", srUuid, "add", "false");
if (result == null || !result.split("#")[1].equals("0")) {
throw new CloudRuntimeException("Unable to remove heartbeat file entry for SR " + srUuid + " due to " + result);
}
return new Answer(command, true, "success");
} catch (final XenAPIException e) {
final String msg = "ModifyStoragePoolCommand remove XenAPIException:" + e.toString() + " host:" + citrixResourceBase.getHost().getUuid() + " pool: " + pool.getHost() + pool.getPath();
s_logger.warn(msg, e);
return new Answer(command, false, msg);
} catch (final Exception e) {
final String msg = "ModifyStoragePoolCommand remove XenAPIException:" + e.getMessage() + " host:" + citrixResourceBase.getHost().getUuid() + " pool: " + pool.getHost() + pool.getPath();
s_logger.warn(msg, e);
return new Answer(command, false, msg);
}
}
}
use of com.xensource.xenapi.SR in project cloudstack by apache.
the class CitrixPrimaryStorageDownloadCommandWrapper method execute.
@Override
public Answer execute(final PrimaryStorageDownloadCommand command, final CitrixResourceBase citrixResourceBase) {
final String tmplturl = command.getUrl();
final String poolName = command.getPoolUuid();
final int wait = command.getWait();
try {
final URI uri = new URI(tmplturl);
final String tmplpath = uri.getHost() + ":" + uri.getPath();
final Connection conn = citrixResourceBase.getConnection();
SR poolsr = null;
final Set<SR> srs = SR.getByNameLabel(conn, poolName);
if (srs.size() != 1) {
final String msg = "There are " + srs.size() + " SRs with same name: " + poolName;
s_logger.warn(msg);
return new PrimaryStorageDownloadAnswer(msg);
} else {
poolsr = srs.iterator().next();
}
final String pUuid = poolsr.getUuid(conn);
final boolean isISCSI = citrixResourceBase.IsISCSI(poolsr.getType(conn));
final String uuid = citrixResourceBase.copyVhdFromSecondaryStorage(conn, tmplpath, pUuid, wait);
final VDI tmpl = citrixResourceBase.getVDIbyUuid(conn, uuid);
final VDI snapshotvdi = tmpl.snapshot(conn, new HashMap<String, String>());
final String snapshotUuid = snapshotvdi.getUuid(conn);
snapshotvdi.setNameLabel(conn, "Template " + command.getName());
final String parentuuid = citrixResourceBase.getVhdParent(conn, pUuid, snapshotUuid, isISCSI);
final VDI parent = citrixResourceBase.getVDIbyUuid(conn, parentuuid);
final Long phySize = parent.getPhysicalUtilisation(conn);
tmpl.destroy(conn);
poolsr.scan(conn);
try {
Thread.sleep(5000);
} catch (final Exception e) {
}
return new PrimaryStorageDownloadAnswer(snapshotvdi.getUuid(conn), phySize);
} catch (final Exception e) {
final String msg = "Catch Exception " + e.getClass().getName() + " on host:" + citrixResourceBase.getHost().getUuid() + " for template: " + tmplturl + " due to " + e.toString();
s_logger.warn(msg, e);
return new PrimaryStorageDownloadAnswer(msg);
}
}
Aggregations