use of com.cloud.legacymodel.to.NfsTO in project cosmic by MissionCriticalCloud.
the class KvmStorageProcessor method copyTemplateToPrimaryStorage.
@Override
public Answer copyTemplateToPrimaryStorage(final CopyCommand cmd) {
final DataTO srcData = cmd.getSrcTO();
final DataTO destData = cmd.getDestTO();
final TemplateObjectTO template = (TemplateObjectTO) srcData;
final DataStoreTO imageStore = template.getDataStore();
final PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO) destData.getDataStore();
if (!(imageStore instanceof NfsTO)) {
return new CopyCmdAnswer("unsupported protocol");
}
final NfsTO nfsImageStore = (NfsTO) imageStore;
final String tmplturl = nfsImageStore.getUrl() + File.separator + template.getPath();
final int index = tmplturl.lastIndexOf("/");
final String mountpoint = tmplturl.substring(0, index);
String tmpltname = null;
if (index < tmplturl.length() - 1) {
tmpltname = tmplturl.substring(index + 1);
}
KvmPhysicalDisk tmplVol = null;
KvmStoragePool secondaryPool = null;
try {
secondaryPool = this.storagePoolMgr.getStoragePoolByUri(mountpoint);
/* Get template vol */
if (tmpltname == null) {
secondaryPool.refresh();
final List<KvmPhysicalDisk> disks = secondaryPool.listPhysicalDisks();
if (disks == null || disks.isEmpty()) {
return new PrimaryStorageDownloadAnswer("Failed to get volumes from pool: " + secondaryPool.getUuid());
}
for (final KvmPhysicalDisk disk : disks) {
if (disk.getName().endsWith("qcow2")) {
tmplVol = disk;
break;
}
}
} else {
tmplVol = secondaryPool.getPhysicalDisk(tmpltname);
}
if (tmplVol == null) {
return new PrimaryStorageDownloadAnswer("Failed to get template from pool: " + secondaryPool.getUuid());
}
/* Copy volume to primary storage */
this.logger.debug("Copying template to primary storage, template format is " + tmplVol.getFormat());
final KvmStoragePool primaryPool = this.storagePoolMgr.getStoragePool(primaryStore.getPoolType(), primaryStore.getUuid());
final KvmPhysicalDisk primaryVol;
if (destData instanceof VolumeObjectTO) {
final VolumeObjectTO volume = (VolumeObjectTO) destData;
// rather than cloning on deploy
if (volume.getSize() != null && volume.getSize() > tmplVol.getVirtualSize()) {
this.logger.debug("Using configured size of " + volume.getSize());
tmplVol.setSize(volume.getSize());
tmplVol.setVirtualSize(volume.getSize());
} else {
this.logger.debug("Using template's size of " + tmplVol.getVirtualSize());
}
primaryVol = this.storagePoolMgr.copyPhysicalDisk(tmplVol, volume.getUuid(), primaryPool, cmd.getWaitInMillSeconds());
} else if (destData instanceof TemplateObjectTO) {
final TemplateObjectTO destTempl = (TemplateObjectTO) destData;
primaryVol = this.storagePoolMgr.copyPhysicalDisk(tmplVol, destTempl.getUuid(), primaryPool, cmd.getWaitInMillSeconds());
} else {
primaryVol = this.storagePoolMgr.copyPhysicalDisk(tmplVol, UUID.randomUUID().toString(), primaryPool, cmd.getWaitInMillSeconds());
}
DataTO data = null;
if (destData.getObjectType() == DataObjectType.TEMPLATE) {
final TemplateObjectTO newTemplate = new TemplateObjectTO();
newTemplate.setPath(primaryVol.getName());
newTemplate.setSize(primaryVol.getSize());
if (primaryPool.getType() == StoragePoolType.RBD) {
newTemplate.setFormat(ImageFormat.RAW);
} else {
newTemplate.setFormat(ImageFormat.QCOW2);
}
data = newTemplate;
} else if (destData.getObjectType() == DataObjectType.VOLUME) {
final VolumeObjectTO volumeObjectTo = new VolumeObjectTO();
volumeObjectTo.setPath(primaryVol.getName());
volumeObjectTo.setSize(primaryVol.getSize());
if (primaryVol.getFormat() == PhysicalDiskFormat.RAW) {
volumeObjectTo.setFormat(ImageFormat.RAW);
} else if (primaryVol.getFormat() == PhysicalDiskFormat.QCOW2) {
volumeObjectTo.setFormat(ImageFormat.QCOW2);
}
data = volumeObjectTo;
}
return new CopyCmdAnswer(data);
} catch (final CloudRuntimeException e) {
return new CopyCmdAnswer(e.toString());
} finally {
try {
if (secondaryPool != null) {
secondaryPool.delete();
}
} catch (final Exception e) {
this.logger.debug("Failed to clean up secondary storage", e);
}
}
}
use of com.cloud.legacymodel.to.NfsTO in project cosmic by MissionCriticalCloud.
the class RequestTest method testSerDeserTO.
@Test
public void testSerDeserTO() {
s_logger.info("Testing serializing and deserializing interface TO works as expected");
final NfsTO nfs = new NfsTO("nfs://192.168.56.10/opt/storage/secondary", DataStoreRole.Image);
// SecStorageSetupCommand cmd = new SecStorageSetupCommand(nfs, "nfs://192.168.56.10/opt/storage/secondary", null);
final ListTemplateCommand cmd = new ListTemplateCommand(nfs);
final Request sreq = new Request(2, 3, cmd, true);
sreq.setSequence(892403718);
final byte[] bytes = sreq.getBytes();
assert Request.getSequence(bytes) == 892403718;
assert Request.getManagementServerId(bytes) == 3;
assert Request.getAgentId(bytes) == 2;
assert Request.getViaAgentId(bytes) == 2;
Request creq = null;
try {
creq = Request.parse(bytes);
} catch (final ClassNotFoundException e) {
s_logger.error("Unable to parse bytes: ", e);
} catch (final UnsupportedVersionException e) {
s_logger.error("Unable to parse bytes: ", e);
}
assert creq != null : "Couldn't get the request back";
compareRequest(creq, sreq);
assertEquals("nfs://192.168.56.10/opt/storage/secondary", ((NfsTO) ((ListTemplateCommand) creq.getCommand()).getDataStore()).getUrl());
}
use of com.cloud.legacymodel.to.NfsTO in project cosmic by MissionCriticalCloud.
the class NfsSecondaryStorageResource method deleteTemplate.
protected Answer deleteTemplate(final DeleteCommand cmd) {
final DataTO obj = cmd.getData();
final DataStoreTO dstore = obj.getDataStore();
if (dstore instanceof NfsTO) {
final NfsTO nfs = (NfsTO) dstore;
String relativeTemplatePath = obj.getPath();
String parent = getRootDir(nfs.getUrl());
if (relativeTemplatePath.startsWith(File.separator)) {
relativeTemplatePath = relativeTemplatePath.substring(1);
}
if (!parent.endsWith(File.separator)) {
parent += File.separator;
}
final String absoluteTemplatePath = parent + relativeTemplatePath;
final File tmpltPath = new File(absoluteTemplatePath);
File tmpltParent = null;
if (tmpltPath.exists() && tmpltPath.isDirectory()) {
tmpltParent = tmpltPath;
} else {
tmpltParent = tmpltPath.getParentFile();
}
String details = null;
if (!tmpltParent.exists()) {
details = "template parent directory " + tmpltParent.getName() + " doesn't exist";
s_logger.debug(details);
return new Answer(cmd, true, details);
}
final File[] tmpltFiles = tmpltParent.listFiles();
if (tmpltFiles == null || tmpltFiles.length == 0) {
details = "No files under template parent directory " + tmpltParent.getName();
s_logger.debug(details);
} else {
boolean found = false;
for (final File f : tmpltFiles) {
if (!found && f.getName().equals("template.properties")) {
found = true;
}
// Don't let this stop us from cleaning up the template
if (f.isDirectory() && f.getName().equals("KVMHA")) {
s_logger.debug("Deleting KVMHA directory contents from template location");
final File[] haFiles = f.listFiles();
for (final File haFile : haFiles) {
haFile.delete();
}
}
if (!f.delete()) {
return new Answer(cmd, false, "Unable to delete file " + f.getName() + " under Template path " + relativeTemplatePath);
}
}
if (!found) {
details = "Can not find template.properties under " + tmpltParent.getName();
s_logger.debug(details);
}
}
if (!tmpltParent.delete()) {
details = "Unable to delete directory " + tmpltParent.getName() + " under Template path " + relativeTemplatePath;
s_logger.debug(details);
return new Answer(cmd, false, details);
}
return new Answer(cmd, true, null);
} else {
return new Answer(cmd, false, "Unsupported image data store: " + dstore);
}
}
use of com.cloud.legacymodel.to.NfsTO in project cosmic by MissionCriticalCloud.
the class NfsSecondaryStorageResource method execute.
private Answer execute(final SecStorageSetupCommand cmd) {
if (!this._inSystemVM) {
return new Answer(cmd, true, null);
}
Answer answer = null;
final DataStoreTO dStore = cmd.getDataStore();
if (dStore instanceof NfsTO) {
final String secUrl = cmd.getSecUrl();
try {
final URI uri = new URI(secUrl);
final String nfsHostIp = getUriHostIp(uri);
final String dir = mountUri(uri);
configCerts(cmd.getCerts());
this.nfsIps.add(nfsHostIp);
answer = new SecStorageSetupAnswer(dir);
} catch (final Exception e) {
final String msg = "GetRootDir for " + secUrl + " failed due to " + e.toString();
s_logger.error(msg);
answer = new Answer(cmd, false, msg);
}
} else {
answer = new Answer(cmd, true, null);
}
savePostUploadPSK(cmd.getPostUploadKey());
startPostUploadServer();
return answer;
}
use of com.cloud.legacymodel.to.NfsTO in project cosmic by MissionCriticalCloud.
the class KvmStorageProcessor method createVolumeFromSnapshot.
@Override
public Answer createVolumeFromSnapshot(final CopyCommand cmd) {
try {
final DataTO srcData = cmd.getSrcTO();
final SnapshotObjectTO snapshot = (SnapshotObjectTO) srcData;
final DataTO destData = cmd.getDestTO();
final PrimaryDataStoreTO pool = (PrimaryDataStoreTO) destData.getDataStore();
final DataStoreTO imageStore = srcData.getDataStore();
final VolumeObjectTO volume = snapshot.getVolume();
if (!(imageStore instanceof NfsTO)) {
return new CopyCmdAnswer("unsupported protocol");
}
final NfsTO nfsImageStore = (NfsTO) imageStore;
final String snapshotFullPath = snapshot.getPath();
final int index = snapshotFullPath.lastIndexOf("/");
final String snapshotPath = snapshotFullPath.substring(0, index);
final String snapshotName = snapshotFullPath.substring(index + 1);
final KvmStoragePool secondaryPool = this.storagePoolMgr.getStoragePoolByUri(nfsImageStore.getUrl() + File.separator + snapshotPath);
final KvmPhysicalDisk snapshotDisk = secondaryPool.getPhysicalDisk(snapshotName);
if (volume.getFormat() == ImageFormat.RAW) {
snapshotDisk.setFormat(PhysicalDiskFormat.RAW);
} else if (volume.getFormat() == ImageFormat.QCOW2) {
snapshotDisk.setFormat(PhysicalDiskFormat.QCOW2);
}
final String primaryUuid = pool.getUuid();
final KvmStoragePool primaryPool = this.storagePoolMgr.getStoragePool(pool.getPoolType(), primaryUuid);
final String volUuid = UUID.randomUUID().toString();
final KvmPhysicalDisk disk = this.storagePoolMgr.copyPhysicalDisk(snapshotDisk, volUuid, primaryPool, cmd.getWaitInMillSeconds());
final VolumeObjectTO newVol = new VolumeObjectTO();
newVol.setPath(disk.getName());
newVol.setSize(disk.getVirtualSize());
newVol.setFormat(ImageFormat.valueOf(disk.getFormat().toString().toUpperCase()));
return new CopyCmdAnswer(newVol);
} catch (final CloudRuntimeException e) {
this.logger.debug("Failed to createVolumeFromSnapshot: ", e);
return new CopyCmdAnswer(e.toString());
}
}
Aggregations