use of com.xensource.xenapi.Connection in project cloudstack by apache.
the class XenServerStorageProcessor method cloneVolumeFromBaseTemplate.
@Override
public Answer cloneVolumeFromBaseTemplate(final CopyCommand cmd) {
final Connection conn = hypervisorResource.getConnection();
final DataTO srcData = cmd.getSrcTO();
final DataTO destData = cmd.getDestTO();
final VolumeObjectTO volume = (VolumeObjectTO) destData;
VDI vdi = null;
try {
VDI tmpltvdi = null;
tmpltvdi = getVDIbyUuid(conn, srcData.getPath());
vdi = tmpltvdi.createClone(conn, new HashMap<String, String>());
Long virtualSize = vdi.getVirtualSize(conn);
if (volume.getSize() > virtualSize) {
s_logger.debug("Overriding provided template's size with new size " + volume.getSize() + " for volume: " + volume.getName());
vdi.resize(conn, volume.getSize());
} else {
s_logger.debug("Using templates disk size of " + virtualSize + " for volume: " + volume.getName() + " since size passed was " + volume.getSize());
}
vdi.setNameLabel(conn, volume.getName());
VDI.Record vdir;
vdir = vdi.getRecord(conn);
s_logger.debug("Succesfully created VDI: Uuid = " + vdir.uuid);
final VolumeObjectTO newVol = new VolumeObjectTO();
newVol.setName(vdir.nameLabel);
newVol.setSize(vdir.virtualSize);
newVol.setPath(vdir.uuid);
return new CopyCmdAnswer(newVol);
} catch (final Exception e) {
s_logger.warn("Unable to create volume; Pool=" + destData + "; Disk: ", e);
return new CopyCmdAnswer(e.toString());
}
}
use of com.xensource.xenapi.Connection in project cloudstack by apache.
the class XenServerStorageProcessor method dettachIso.
@Override
public Answer dettachIso(final DettachCommand 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();
}
try {
final Connection conn = hypervisorResource.getConnection();
// Find the VM
final VM vm = hypervisorResource.getVM(conn, cmd.getVmName());
final String vmUUID = vm.getUuid(conn);
// Find the ISO VDI
final VDI isoVDI = hypervisorResource.getIsoVDIByURL(conn, cmd.getVmName(), 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 (!sr.getNameLabel(conn).startsWith("XenServer Tools")) {
hypervisorResource.removeSR(conn, sr);
}
return new DettachAnswer(disk);
} catch (final XenAPIException e) {
final String msg = "Failed to dettach volume" + " for uuid: " + data.getPath() + " due to " + e.toString();
s_logger.warn(msg, e);
return new DettachAnswer(msg);
} catch (final Exception e) {
final String msg = "Failed to dettach volume" + " for uuid: " + data.getPath() + " due to " + e.getMessage();
s_logger.warn(msg, e);
return new DettachAnswer(msg);
}
}
use of com.xensource.xenapi.Connection in project cloudstack by apache.
the class XenServerStorageProcessor method deleteSnapshot.
@Override
public Answer deleteSnapshot(final DeleteCommand cmd) {
final SnapshotObjectTO snapshot = (SnapshotObjectTO) cmd.getData();
final DataStoreTO store = snapshot.getDataStore();
if (store.getRole() == DataStoreRole.Primary) {
final Connection conn = hypervisorResource.getConnection();
final VDI snapshotVdi = getVDIbyUuid(conn, snapshot.getPath());
if (snapshotVdi == null) {
return new Answer(null);
}
String errMsg = null;
try {
deleteVDI(conn, snapshotVdi);
} catch (final BadServerResponse e) {
s_logger.debug("delete snapshot failed:" + e.toString());
errMsg = e.toString();
} catch (final XenAPIException e) {
s_logger.debug("delete snapshot failed:" + e.toString());
errMsg = e.toString();
} catch (final XmlRpcException e) {
s_logger.debug("delete snapshot failed:" + e.toString());
errMsg = e.toString();
}
return new Answer(cmd, false, errMsg);
}
return new Answer(cmd, false, "unsupported storage type");
}
use of com.xensource.xenapi.Connection in project cloudstack by apache.
the class XenServerStorageProcessor method execute.
protected Answer execute(final AttachPrimaryDataStoreCmd cmd) {
final String dataStoreUri = cmd.getDataStore();
final Connection conn = hypervisorResource.getConnection();
try {
final DecodedDataObject obj = Decoder.decode(dataStoreUri);
final DecodedDataStore store = obj.getStore();
final SR sr = hypervisorResource.getStorageRepository(conn, store.getUuid());
hypervisorResource.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: ";
s_logger.warn(msg);
return new Answer(cmd, false, msg);
}
final AttachPrimaryDataStoreAnswer answer = new AttachPrimaryDataStoreAnswer(cmd);
answer.setCapacity(capacity);
answer.setUuid(sr.getUuid(conn));
answer.setAvailable(available);
return answer;
} catch (final XenAPIException e) {
final String msg = "AttachPrimaryDataStoreCmd add XenAPIException:" + e.toString();
s_logger.warn(msg, e);
return new Answer(cmd, false, msg);
} catch (final Exception e) {
final String msg = "AttachPrimaryDataStoreCmd failed:" + e.getMessage();
s_logger.warn(msg, e);
return new Answer(cmd, false, msg);
}
}
use of com.xensource.xenapi.Connection in project cloudstack by apache.
the class XenServerStorageProcessor method createSnapshot.
@Override
public Answer createSnapshot(final CreateObjectCommand cmd) {
final Connection conn = hypervisorResource.getConnection();
final SnapshotObjectTO snapshotTO = (SnapshotObjectTO) cmd.getData();
final long snapshotId = snapshotTO.getId();
final String snapshotName = snapshotTO.getName();
String details = "create snapshot operation Failed for snapshotId: " + snapshotId;
String snapshotUUID = null;
try {
final String volumeUUID = snapshotTO.getVolume().getPath();
final VDI volume = VDI.getByUuid(conn, volumeUUID);
final VDI snapshot = volume.snapshot(conn, new HashMap<String, String>());
if (snapshotName != null) {
snapshot.setNameLabel(conn, snapshotName);
}
snapshotUUID = snapshot.getUuid(conn);
final String preSnapshotUUID = snapshotTO.getParentSnapshotPath();
//check if it is a empty snapshot
if (preSnapshotUUID != null) {
final SR sr = volume.getSR(conn);
final String srUUID = sr.getUuid(conn);
final String type = sr.getType(conn);
final Boolean isISCSI = IsISCSI(type);
final String snapshotParentUUID = getVhdParent(conn, srUUID, snapshotUUID, isISCSI);
try {
final String preSnapshotParentUUID = getVhdParent(conn, srUUID, preSnapshotUUID, isISCSI);
if (snapshotParentUUID != null && snapshotParentUUID.equals(preSnapshotParentUUID)) {
// this is empty snapshot, remove it
snapshot.destroy(conn);
snapshotUUID = preSnapshotUUID;
}
} catch (final Exception e) {
s_logger.debug("Failed to get parent snapshot", e);
}
}
final SnapshotObjectTO newSnapshot = new SnapshotObjectTO();
newSnapshot.setPath(snapshotUUID);
return new CreateObjectAnswer(newSnapshot);
} catch (final XenAPIException e) {
details += ", reason: " + e.toString();
s_logger.warn(details, e);
} catch (final Exception e) {
details += ", reason: " + e.toString();
s_logger.warn(details, e);
}
return new CreateObjectAnswer(details);
}
Aggregations