Skip to content

Commit f8e51f7

Browse files
committed
Invoke reatetmplt.sh script after copying template from S3 to cache
storage.
1 parent 3d91a4e commit f8e51f7

3 files changed

Lines changed: 56 additions & 10 deletions

File tree

core/src/com/cloud/storage/resource/LocalNfsSecondaryStorageResource.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public LocalNfsSecondaryStorageResource(){
4646
((DownloadManagerImpl)_dlMgr).setThreadPool(Executors.newFixedThreadPool(10));
4747
_storage = new JavaStorageLayer();
4848
this._inSystemVM = false;
49+
System.setProperty("paths.script", "/Users/minc/dev/cloud-asf"); //This is just for my testing, not for QA build
4950
}
5051

5152
@Override

core/src/com/cloud/storage/resource/NfsSecondaryStorageResource.java

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import org.apache.cloudstack.storage.command.DownloadCommand.ResourceType;
5656
import org.apache.cloudstack.storage.to.SnapshotObjectTO;
5757
import org.apache.cloudstack.storage.to.TemplateObjectTO;
58+
import org.apache.cloudstack.storage.to.VolumeObjectTO;
5859
import org.apache.commons.lang.StringUtils;
5960
import org.apache.log4j.Logger;
6061

@@ -237,7 +238,8 @@ protected Answer copyFromS3ToNfs(CopyCommand cmd, DataTO srcData, S3TO s3, DataT
237238

238239
try {
239240

240-
final File downloadDirectory = _storage.getFile(determineStorageTemplatePath(storagePath, destPath));
241+
String downloadPath = determineStorageTemplatePath(storagePath, destPath);
242+
final File downloadDirectory = _storage.getFile(downloadPath);
241243
downloadDirectory.mkdirs();
242244

243245
if (!downloadDirectory.exists()) {
@@ -267,12 +269,59 @@ public String determineFileName(final String key) {
267269
return new CopyCmdAnswer("Can't find template");
268270
}
269271

272+
// do post processing to unzip the file if it is compressed
273+
String scriptsDir = "scripts/storage/secondary";
274+
String createTmpltScr = Script.findScript(scriptsDir, "createtmplt.sh");
275+
if (createTmpltScr == null) {
276+
throw new ConfigurationException("Unable to find createtmplt.sh");
277+
}
278+
s_logger.info("createtmplt.sh found in " + createTmpltScr);
279+
String createVolScr = Script.findScript(scriptsDir, "createvolume.sh");
280+
if (createVolScr == null) {
281+
throw new ConfigurationException("Unable to find createvolume.sh");
282+
}
283+
s_logger.info("createvolume.sh found in " + createVolScr);
284+
String script = srcData.getObjectType() == DataObjectType.TEMPLATE ? createTmpltScr : createVolScr;
285+
286+
int installTimeoutPerGig = 180 * 60 * 1000;
287+
int imgSizeGigs = (int) Math.ceil(destFile.length() * 1.0d / (1024 * 1024 * 1024));
288+
imgSizeGigs++; // add one just in case
289+
long timeout = imgSizeGigs * installTimeoutPerGig;
290+
291+
String origPath = destFile.getAbsolutePath();
292+
String extension = null;
293+
if ( srcData.getObjectType() == DataObjectType.TEMPLATE){
294+
extension = ((TemplateObjectTO)srcData).getFormat().getFileExtension();
295+
} else{
296+
extension = ((VolumeObjectTO)srcData).getDiskType().toString().toLowerCase();
297+
}
298+
299+
String templateName = UUID.randomUUID().toString();
300+
String templateFilename = templateName + "." + extension;
301+
Script scr = new Script(script, timeout, s_logger);
302+
scr.add("-s", Integer.toString(imgSizeGigs)); // not used for now
303+
scr.add("-n", templateFilename);
304+
305+
scr.add("-t", downloadPath);
306+
scr.add("-f", origPath); // this is the temporary
307+
// template file downloaded
308+
String result;
309+
result = scr.execute();
310+
311+
if (result != null) {
312+
// script execution failure
313+
throw new CloudRuntimeException("Failed to run script " + script);
314+
}
315+
316+
String finalFileName = templateFilename;
317+
String finalDownloadPath = destPath + File.separator + templateFilename;
318+
270319
DataTO newDestTO = null;
271320

272321
if (destData.getObjectType() == DataObjectType.TEMPLATE) {
273322
TemplateObjectTO newTemplTO = new TemplateObjectTO();
274-
newTemplTO.setPath(destPath + File.separator + destFile.getName());
275-
newTemplTO.setName(destFile.getName());
323+
newTemplTO.setPath(finalDownloadPath);
324+
newTemplTO.setName(finalFileName);
276325
newDestTO = newTemplTO;
277326
} else {
278327
return new CopyCmdAnswer("not implemented yet");

engine/storage/cache/src/org/apache/cloudstack/storage/cache/manager/StorageCacheManagerImpl.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.apache.cloudstack.storage.cache.allocator.StorageCacheAllocator;
4040
import org.apache.cloudstack.storage.command.CommandResult;
4141
import org.apache.cloudstack.storage.command.CopyCmdAnswer;
42+
import org.apache.cloudstack.storage.datastore.ObjectInDataStoreManager;
4243
import org.apache.log4j.Logger;
4344

4445
import com.cloud.utils.component.Manager;
@@ -50,6 +51,7 @@ public class StorageCacheManagerImpl implements StorageCacheManager, Manager {
5051
List<StorageCacheAllocator> storageCacheAllocator;
5152
@Inject
5253
DataMotionService dataMotionSvr;
54+
5355
@Override
5456
public DataStore getCacheStorage(Scope scope) {
5557
for (StorageCacheAllocator allocator : storageCacheAllocator) {
@@ -131,16 +133,10 @@ public CreateCacheObjectContext(AsyncCompletionCallback<T> callback, AsyncCallFu
131133
@Override
132134
public DataObject createCacheObject(DataObject data, Scope scope) {
133135
DataStore cacheStore = this.getCacheStorage(scope);
136+
//TODO: consider multiple thread to create
134137
DataObject objOnCacheStore = cacheStore.create(data);
135138

136139
AsyncCallFuture<CopyCommandResult> future = new AsyncCallFuture<CopyCommandResult>();
137-
/*
138-
CreateCacheObjectContext<CopyCommandResult> context = new CreateCacheObjectContext<CopyCommandResult>(null, future);
139-
AsyncCallbackDispatcher<StorageCacheManagerImpl, CopyCommandResult> caller = AsyncCallbackDispatcher.create(this);
140-
caller.setContext(context);
141-
caller.setCallback(future);
142-
*/
143-
144140
CopyCommandResult result = null;
145141
try {
146142
objOnCacheStore.processEvent(Event.CreateOnlyRequested);

0 commit comments

Comments
 (0)