|
55 | 55 | import org.apache.cloudstack.storage.command.DownloadCommand.ResourceType; |
56 | 56 | import org.apache.cloudstack.storage.to.SnapshotObjectTO; |
57 | 57 | import org.apache.cloudstack.storage.to.TemplateObjectTO; |
| 58 | +import org.apache.cloudstack.storage.to.VolumeObjectTO; |
58 | 59 | import org.apache.commons.lang.StringUtils; |
59 | 60 | import org.apache.log4j.Logger; |
60 | 61 |
|
@@ -237,7 +238,8 @@ protected Answer copyFromS3ToNfs(CopyCommand cmd, DataTO srcData, S3TO s3, DataT |
237 | 238 |
|
238 | 239 | try { |
239 | 240 |
|
240 | | - final File downloadDirectory = _storage.getFile(determineStorageTemplatePath(storagePath, destPath)); |
| 241 | + String downloadPath = determineStorageTemplatePath(storagePath, destPath); |
| 242 | + final File downloadDirectory = _storage.getFile(downloadPath); |
241 | 243 | downloadDirectory.mkdirs(); |
242 | 244 |
|
243 | 245 | if (!downloadDirectory.exists()) { |
@@ -267,12 +269,59 @@ public String determineFileName(final String key) { |
267 | 269 | return new CopyCmdAnswer("Can't find template"); |
268 | 270 | } |
269 | 271 |
|
| 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 | + |
270 | 319 | DataTO newDestTO = null; |
271 | 320 |
|
272 | 321 | if (destData.getObjectType() == DataObjectType.TEMPLATE) { |
273 | 322 | 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); |
276 | 325 | newDestTO = newTemplTO; |
277 | 326 | } else { |
278 | 327 | return new CopyCmdAnswer("not implemented yet"); |
|
0 commit comments