Skip to content

Commit 652a34f

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

File tree

2 files changed

+97
-19
lines changed

2 files changed

+97
-19
lines changed

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: 96 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -597,16 +597,10 @@ 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

611605
//clean up template data on staging area
612606
try {
@@ -615,14 +609,8 @@ protected Answer createTemplateFromSnapshot(CopyCommand cmd) {
615609
} catch (Exception e) {
616610
s_logger.debug("Failed to clean up staging area:", e);
617611
}
612+
return result;
618613

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);
626614
} else if (destDataStore instanceof S3TO) {
627615
//create template on the same data store
628616
CopyCmdAnswer answer =
@@ -765,15 +753,17 @@ protected Answer registerTemplateOnSwift(DownloadCommand cmd) {
765753
_storage.create(uniqDir.getAbsolutePath(), "template.properties");
766754
File metaFile = new File(metaFileName);
767755
FileWriter writer = new FileWriter(metaFile);
756+
long virtualSize = getVirtualSize(file, getTemplateFormat(file.getName()));
768757
BufferedWriter bufferWriter = new BufferedWriter(writer);
769758
bufferWriter.write("uniquename=" + cmd.getName());
770759
bufferWriter.write("\n");
771760
bufferWriter.write("filename=" + fileName);
772761
bufferWriter.write("\n");
773762
bufferWriter.write("size=" + file.length());
763+
bufferWriter.write("\n");
764+
bufferWriter.write("virtualsize=" + virtualSize);
774765
bufferWriter.close();
775766
writer.close();
776-
777767
SwiftUtil.putObject(swiftTO, metaFile, container, "template.properties");
778768
metaFile.delete();
779769
uniqDir.delete();
@@ -785,7 +775,7 @@ protected Answer registerTemplateOnSwift(DownloadCommand cmd) {
785775
}
786776

787777
DownloadAnswer answer =
788-
new DownloadAnswer(null, 100, null, VMTemplateStorageResourceAssoc.Status.DOWNLOADED, swiftPath, swiftPath, file.length(), file.length(), md5sum);
778+
new DownloadAnswer(null, 100, null, VMTemplateStorageResourceAssoc.Status.DOWNLOADED, swiftPath, swiftPath, virtualSize, file.length(), md5sum);
789779
return answer;
790780
} catch (IOException e) {
791781
s_logger.debug("Failed to register template into swift", e);
@@ -942,6 +932,93 @@ protected Answer copyFromNfsToS3(CopyCommand cmd) {
942932
}
943933
}
944934

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

0 commit comments

Comments
 (0)