Skip to content

Commit 1b9ff62

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

2 files changed

Lines changed: 85 additions & 18 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: 84 additions & 18 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 =
@@ -771,9 +759,10 @@ protected Answer registerTemplateOnSwift(DownloadCommand cmd) {
771759
bufferWriter.write("filename=" + fileName);
772760
bufferWriter.write("\n");
773761
bufferWriter.write("size=" + file.length());
762+
bufferWriter.write("\n");
763+
bufferWriter.write("virtualsize=" + getVirtualSize(file, getTemplateFormat(file.getName())));
774764
bufferWriter.close();
775765
writer.close();
776-
777766
SwiftUtil.putObject(swiftTO, metaFile, container, "template.properties");
778767
metaFile.delete();
779768
uniqDir.delete();
@@ -942,6 +931,83 @@ protected Answer copyFromNfsToS3(CopyCommand cmd) {
942931
}
943932
}
944933

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

0 commit comments

Comments
 (0)