Skip to content

Commit f44ff72

Browse files
committed
Fix Sync of template.properties in Swift
1 parent 6be2cc7 commit f44ff72

2 files changed

Lines changed: 114 additions & 33 deletions

File tree

engine/storage/src/org/apache/cloudstack/storage/datastore/ObjectInDataStoreManagerImpl.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ public DataObject create(DataObject obj, DataStore dataStore) {
156156
// template.properties
157157
// there
158158
}
159+
159160
ts.setInstallPath(installPath);
160161
ts.setState(ObjectInDataStoreStateMachine.State.Allocated);
161162
ts = templateDataStoreDao.persist(ts);

services/secondary-storage/server/src/org/apache/cloudstack/storage/resource/NfsSecondaryStorageResource.java

Lines changed: 113 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -597,32 +597,14 @@ protected Answer createTemplateFromSnapshot(CopyCommand cmd) {
597597
return answer;
598598
}
599599
s_logger.debug("starting copy template to swift");
600-
DataTO newTemplate = answer.getNewData();
601-
File templateFile = getFile(newTemplate.getPath(), ((NfsTO)srcDataStore).getUrl());
602-
SwiftTO swift = (SwiftTO)destDataStore;
603-
String containterName = SwiftUtil.getContainerName(destData.getObjectType().toString(), destData.getId());
604-
String swiftPath = SwiftUtil.putObject(swift, templateFile, containterName, templateFile.getName());
605-
//upload template.properties
606-
File properties = new File(templateFile.getParent() + File.separator + _tmpltpp);
607-
if (properties.exists()) {
608-
SwiftUtil.putObject(swift, properties, containterName, _tmpltpp);
609-
}
600+
TemplateObjectTO newTemplate = (TemplateObjectTO)answer.getNewData();
601+
newTemplate.setDataStore(srcDataStore);
602+
CopyCommand newCpyCmd = new CopyCommand(newTemplate, destData, cmd.getWait(), cmd.executeInSequence());
603+
Answer result = copyFromNfsToSwift(newCpyCmd);
610604

611-
//clean up template data on staging area
612-
try {
613-
DeleteCommand deleteCommand = new DeleteCommand(newTemplate);
614-
execute(deleteCommand);
615-
} catch (Exception e) {
616-
s_logger.debug("Failed to clean up staging area:", e);
617-
}
605+
cleanupStagingNfs(newTemplate);
606+
return result;
618607

619-
TemplateObjectTO template = new TemplateObjectTO();
620-
template.setPath(swiftPath);
621-
template.setSize(templateFile.length());
622-
template.setPhysicalSize(template.getSize());
623-
SnapshotObjectTO snapshot = (SnapshotObjectTO)srcData;
624-
template.setFormat(snapshot.getVolume().getFormat());
625-
return new CopyCmdAnswer(template);
626608
} else if (destDataStore instanceof S3TO) {
627609
//create template on the same data store
628610
CopyCmdAnswer answer =
@@ -635,20 +617,29 @@ protected Answer createTemplateFromSnapshot(CopyCommand cmd) {
635617
newTemplate.setDataStore(srcDataStore);
636618
CopyCommand newCpyCmd = new CopyCommand(newTemplate, destData, cmd.getWait(), cmd.executeInSequence());
637619
Answer result = copyFromNfsToS3(newCpyCmd);
638-
//clean up template data on staging area
639-
try {
640-
DeleteCommand deleteCommand = new DeleteCommand(newTemplate);
641-
execute(deleteCommand);
642-
} catch (Exception e) {
643-
s_logger.debug("Failed to clean up staging area:", e);
644-
}
620+
621+
cleanupStagingNfs(newTemplate);
622+
645623
return result;
646624
}
647625
}
648626
s_logger.debug("Failed to create templat from snapshot");
649627
return new CopyCmdAnswer("Unsupported prototcol");
650628
}
651629

630+
/**
631+
* clean up template data on staging area
632+
* @param newTemplate: The template on the secondary storage that needs to be cleaned up
633+
*/
634+
private void cleanupStagingNfs(TemplateObjectTO newTemplate) {
635+
try {
636+
DeleteCommand deleteCommand = new DeleteCommand(newTemplate);
637+
execute(deleteCommand);
638+
} catch (Exception e) {
639+
s_logger.debug("Failed to clean up staging area:", e);
640+
}
641+
}
642+
652643
protected Answer copyFromNfsToImage(CopyCommand cmd) {
653644
DataTO destData = cmd.getDestTO();
654645
DataStoreTO destDataStore = destData.getDataStore();
@@ -765,15 +756,17 @@ protected Answer registerTemplateOnSwift(DownloadCommand cmd) {
765756
_storage.create(uniqDir.getAbsolutePath(), "template.properties");
766757
File metaFile = new File(metaFileName);
767758
FileWriter writer = new FileWriter(metaFile);
759+
long virtualSize = getVirtualSize(file, getTemplateFormat(file.getName()));
768760
BufferedWriter bufferWriter = new BufferedWriter(writer);
769761
bufferWriter.write("uniquename=" + cmd.getName());
770762
bufferWriter.write("\n");
771763
bufferWriter.write("filename=" + fileName);
772764
bufferWriter.write("\n");
773765
bufferWriter.write("size=" + file.length());
766+
bufferWriter.write("\n");
767+
bufferWriter.write("virtualsize=" + virtualSize);
774768
bufferWriter.close();
775769
writer.close();
776-
777770
SwiftUtil.putObject(swiftTO, metaFile, container, "template.properties");
778771
metaFile.delete();
779772
uniqDir.delete();
@@ -785,7 +778,7 @@ protected Answer registerTemplateOnSwift(DownloadCommand cmd) {
785778
}
786779

787780
DownloadAnswer answer =
788-
new DownloadAnswer(null, 100, null, VMTemplateStorageResourceAssoc.Status.DOWNLOADED, swiftPath, swiftPath, file.length(), file.length(), md5sum);
781+
new DownloadAnswer(null, 100, null, VMTemplateStorageResourceAssoc.Status.DOWNLOADED, swiftPath, swiftPath, virtualSize, file.length(), md5sum);
789782
return answer;
790783
} catch (IOException e) {
791784
s_logger.debug("Failed to register template into swift", e);
@@ -942,6 +935,93 @@ protected Answer copyFromNfsToS3(CopyCommand cmd) {
942935
}
943936
}
944937

938+
/**
939+
* Creates a template.properties for Swift with its correct unique name
940+
*
941+
* @param swift The swift object
942+
* @param srcFile Source file on the staging NFS
943+
* @param containerName Destination container
944+
* @return true on successful write
945+
*/
946+
protected boolean swiftUploadMetadataFile(SwiftTO swift, File srcFile, String containerName) throws IOException {
947+
948+
File uniqDir = _storage.createUniqDir();
949+
String metaFileName = uniqDir.getAbsolutePath() + File.separator + "template.properties";
950+
_storage.create(uniqDir.getAbsolutePath(), "template.properties");
951+
952+
String uniqueName = FilenameUtils.getBaseName(srcFile.getName());
953+
File metaFile = new File(metaFileName);
954+
FileWriter writer = new FileWriter(metaFile);
955+
BufferedWriter bufferWriter = new BufferedWriter(writer);
956+
bufferWriter.write("uniquename=" + uniqueName);
957+
bufferWriter.write("\n");
958+
bufferWriter.write("filename=" + srcFile.getName());
959+
bufferWriter.write("\n");
960+
bufferWriter.write("size=" + srcFile.length());
961+
bufferWriter.write("\n");
962+
bufferWriter.write("virtualsize=" + getVirtualSize(srcFile, getTemplateFormat(srcFile.getName())));
963+
bufferWriter.close();
964+
writer.close();
965+
966+
SwiftUtil.putObject(swift, metaFile, containerName, _tmpltpp);
967+
metaFile.delete();
968+
uniqDir.delete();
969+
970+
return true;
971+
}
972+
973+
/**
974+
* Copies data from NFS and uploads it into a Swift container
975+
*
976+
* @param cmd CopyComand
977+
* @return CopyCmdAnswer
978+
*/
979+
protected Answer copyFromNfsToSwift(CopyCommand cmd) {
980+
981+
final DataTO srcData = cmd.getSrcTO();
982+
final DataTO destData = cmd.getDestTO();
983+
984+
DataStoreTO srcDataStore = srcData.getDataStore();
985+
NfsTO srcStore = (NfsTO)srcDataStore;
986+
DataStoreTO destDataStore = destData.getDataStore();
987+
File srcFile = getFile(srcData.getPath(), srcStore.getUrl());
988+
989+
SwiftTO swift = (SwiftTO)destDataStore;
990+
991+
try {
992+
993+
String containerName = SwiftUtil.getContainerName(destData.getObjectType().toString(), destData.getId());
994+
String swiftPath = SwiftUtil.putObject(swift, srcFile, containerName, srcFile.getName());
995+
996+
997+
DataTO retObj = null;
998+
if (destData.getObjectType() == DataObjectType.TEMPLATE) {
999+
swiftUploadMetadataFile(swift, srcFile, containerName);
1000+
TemplateObjectTO newTemplate = new TemplateObjectTO();
1001+
newTemplate.setPath(swiftPath);
1002+
newTemplate.setSize(getVirtualSize(srcFile, getTemplateFormat(srcFile.getName())));
1003+
newTemplate.setPhysicalSize(srcFile.length());
1004+
newTemplate.setFormat(getTemplateFormat(srcFile.getName()));
1005+
retObj = newTemplate;
1006+
} else if (destData.getObjectType() == DataObjectType.VOLUME) {
1007+
VolumeObjectTO newVol = new VolumeObjectTO();
1008+
newVol.setPath(containerName);
1009+
newVol.setSize(getVirtualSize(srcFile, getTemplateFormat(srcFile.getName())));
1010+
retObj = newVol;
1011+
} else if (destData.getObjectType() == DataObjectType.SNAPSHOT) {
1012+
SnapshotObjectTO newSnapshot = new SnapshotObjectTO();
1013+
newSnapshot.setPath(containerName);
1014+
retObj = newSnapshot;
1015+
}
1016+
1017+
return new CopyCmdAnswer(retObj);
1018+
1019+
} catch (Exception e) {
1020+
s_logger.error("failed to upload " + srcData.getPath(), e);
1021+
return new CopyCmdAnswer("failed to upload " + srcData.getPath() + e.toString());
1022+
}
1023+
}
1024+
9451025
String swiftDownload(SwiftTO swift, String container, String rfilename, String lFullPath) {
9461026
Script command = new Script("/bin/bash", s_logger);
9471027
command.add("-c");

0 commit comments

Comments
 (0)