Skip to content

Commit 47db519

Browse files
committed
use utility to get extract command
1 parent 1e5a903 commit 47db519

File tree

6 files changed

+73
-97
lines changed

6 files changed

+73
-97
lines changed

plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/storage/LibvirtStorageAdaptor.java

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
import com.cloud.storage.StorageLayer;
8080
import com.cloud.utils.exception.CloudRuntimeException;
8181
import com.cloud.utils.script.Script;
82+
import com.cloud.utils.storage.TemplateDownloaderUtil;
8283

8384
public class LibvirtStorageAdaptor implements StorageAdaptor {
8485
protected Logger logger = LogManager.getLogger(getClass());
@@ -172,40 +173,11 @@ public KVMPhysicalDisk createDiskFromTemplateBacking(KVMPhysicalDisk template, S
172173
return null;
173174
}
174175

175-
/**
176-
* Checks if downloaded template is extractable
177-
* @return true if it should be extracted, false if not
178-
*/
179-
public static boolean isTemplateExtractable(String templatePath) {
180-
String type = Script.runSimpleBashScript("file " + templatePath + " | awk -F' ' '{print $2}'");
181-
return type.equalsIgnoreCase("bzip2") || type.equalsIgnoreCase("gzip")
182-
|| type.equalsIgnoreCase("zip") || type.equalsIgnoreCase("xz");
183-
}
184-
185-
/**
186-
* Return extract command to execute given downloaded file
187-
* @param downloadedTemplateFile
188-
* @param templateUuid
189-
*/
190-
public static String getExtractCommandForDownloadedFile(String downloadedTemplateFile, String templateUuid) {
191-
if (downloadedTemplateFile.endsWith(".zip")) {
192-
return "unzip -p " + downloadedTemplateFile + " | cat > " + templateUuid;
193-
} else if (downloadedTemplateFile.endsWith(".bz2")) {
194-
return "bunzip2 -c " + downloadedTemplateFile + " > " + templateUuid;
195-
} else if (downloadedTemplateFile.endsWith(".gz")) {
196-
return "gunzip -c " + downloadedTemplateFile + " > " + templateUuid;
197-
} else if (downloadedTemplateFile.endsWith(".xz")) {
198-
return "xz -d -c " + downloadedTemplateFile + " > " + templateUuid;
199-
} else {
200-
throw new CloudRuntimeException("Unable to extract template " + downloadedTemplateFile);
201-
}
202-
}
203-
204176
/**
205177
* Extract downloaded template into installPath, remove compressed file
206178
*/
207179
public static void extractDownloadedTemplate(String downloadedTemplateFile, KVMStoragePool destPool, String destinationFile) {
208-
String extractCommand = getExtractCommandForDownloadedFile(downloadedTemplateFile, destinationFile);
180+
String extractCommand = TemplateDownloaderUtil.getExtractCommandForDownloadedFile(downloadedTemplateFile, destinationFile);
209181
Script.runSimpleBashScript(extractCommand);
210182
Script.runSimpleBashScript("rm -f " + downloadedTemplateFile);
211183
}
@@ -224,7 +196,7 @@ public KVMPhysicalDisk createTemplateFromDirectDownloadFile(String templateFileP
224196

225197
if (destPool.getType() == StoragePoolType.NetworkFilesystem || destPool.getType() == StoragePoolType.Filesystem
226198
|| destPool.getType() == StoragePoolType.SharedMountPoint) {
227-
if (!Storage.ImageFormat.ISO.equals(format) && isTemplateExtractable(templateFilePath)) {
199+
if (!Storage.ImageFormat.ISO.equals(format) && TemplateDownloaderUtil.isTemplateExtractable(templateFilePath)) {
228200
extractDownloadedTemplate(templateFilePath, destPool, destinationFile);
229201
} else {
230202
Script.runSimpleBashScript("mv " + templateFilePath + " " + destinationFile);

plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/storage/MultipathSCSIAdapterBase.java

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -423,27 +423,6 @@ public KVMPhysicalDisk createPhysicalDisk(String name, KVMStoragePool pool,
423423
throw new UnsupportedOperationException("Unimplemented method 'createPhysicalDisk'");
424424
}
425425

426-
boolean isTemplateExtractable(String templatePath) {
427-
ScriptResult result = runScript("file", 5000L, templatePath, "| awk -F' ' '{print $2}'");
428-
String type = result.getResult();
429-
return type.equalsIgnoreCase("bzip2") || type.equalsIgnoreCase("gzip")
430-
|| type.equalsIgnoreCase("zip") || type.equalsIgnoreCase("xz");
431-
}
432-
433-
String getExtractCommandForDownloadedFile(String downloadedTemplateFile, String templateFile) {
434-
if (downloadedTemplateFile.endsWith(".zip")) {
435-
return "unzip -p " + downloadedTemplateFile + " | cat > " + templateFile;
436-
} else if (downloadedTemplateFile.endsWith(".bz2")) {
437-
return "bunzip2 -c " + downloadedTemplateFile + " > " + templateFile;
438-
} else if (downloadedTemplateFile.endsWith(".gz")) {
439-
return "gunzip -c " + downloadedTemplateFile + " > " + templateFile;
440-
} else if (downloadedTemplateFile.endsWith(".xz")) {
441-
return "xz -d -c " + downloadedTemplateFile + " > " + templateFile;
442-
} else {
443-
throw new CloudRuntimeException("Unable to extract template " + downloadedTemplateFile);
444-
}
445-
}
446-
447426
boolean waitForDiskToBecomeAvailable(AddressInfo address, KVMStoragePool pool, long waitTimeInSec) {
448427
LOGGER.debug("Waiting for the volume with id: " + address.getPath() + " of the storage pool: " + pool.getUuid() + " to become available for " + waitTimeInSec + " secs");
449428

plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/storage/ScaleIOStorageAdaptor.java

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@
5353
import com.cloud.utils.exception.CloudRuntimeException;
5454
import com.cloud.utils.script.OutputInterpreter;
5555
import com.cloud.utils.script.Script;
56+
import com.cloud.utils.storage.TemplateDownloaderUtil;
57+
5658
import org.apache.commons.lang3.StringUtils;
5759

5860
public class ScaleIOStorageAdaptor implements StorageAdaptor {
@@ -572,10 +574,10 @@ public KVMPhysicalDisk createTemplateFromDirectDownloadFile(String templateFileP
572574
throw new CloudRuntimeException("Failed to find the disk: " + destTemplatePath + " of the storage pool: " + destPool.getUuid());
573575
}
574576

575-
if (isTemplateExtractable(templateFilePath)) {
577+
if (TemplateDownloaderUtil.isTemplateExtractable(templateFilePath)) {
576578
srcTemplateFilePath = sourceFile.getParent() + "/" + UUID.randomUUID().toString();
577579
logger.debug("Extract the downloaded template " + templateFilePath + " to " + srcTemplateFilePath);
578-
String extractCommand = getExtractCommandForDownloadedFile(templateFilePath, srcTemplateFilePath);
580+
String extractCommand = TemplateDownloaderUtil.getExtractCommandForDownloadedFile(templateFilePath, srcTemplateFilePath);
579581
Script.runSimpleBashScript(extractCommand);
580582
Script.runSimpleBashScript("rm -f " + templateFilePath);
581583
}
@@ -611,26 +613,6 @@ public KVMPhysicalDisk createTemplateFromDirectDownloadFile(String templateFileP
611613
return destDisk;
612614
}
613615

614-
private boolean isTemplateExtractable(String templatePath) {
615-
String type = Script.runSimpleBashScript("file " + templatePath + " | awk -F' ' '{print $2}'");
616-
return type.equalsIgnoreCase("bzip2") || type.equalsIgnoreCase("gzip")
617-
|| type.equalsIgnoreCase("zip") || type.equalsIgnoreCase("xz");
618-
}
619-
620-
private String getExtractCommandForDownloadedFile(String downloadedTemplateFile, String templateFile) {
621-
if (downloadedTemplateFile.endsWith(".zip")) {
622-
return "unzip -p " + downloadedTemplateFile + " | cat > " + templateFile;
623-
} else if (downloadedTemplateFile.endsWith(".bz2")) {
624-
return "bunzip2 -c " + downloadedTemplateFile + " > " + templateFile;
625-
} else if (downloadedTemplateFile.endsWith(".gz")) {
626-
return "gunzip -c " + downloadedTemplateFile + " > " + templateFile;
627-
} else if (downloadedTemplateFile.endsWith(".xz")) {
628-
return "xz -d -c " + downloadedTemplateFile + " > " + templateFile;
629-
} else {
630-
throw new CloudRuntimeException("Unable to extract template " + downloadedTemplateFile);
631-
}
632-
}
633-
634616
public void resizeQcow2ToVolume(String volumePath, QemuImageOptions options, List<QemuObject> objects, Integer timeout) throws QemuImgException, LibvirtException {
635617
long rawSizeBytes = getPhysicalDiskSize(volumePath);
636618
long usableSizeBytes = getUsableBytesFromRawBytes(rawSizeBytes);

plugins/storage/volume/linstor/src/main/java/com/cloud/hypervisor/kvm/storage/LinstorStorageAdaptor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.apache.logging.log4j.LogManager;
4040
import org.libvirt.LibvirtException;
4141

42+
import com.cloud.utils.storage.TemplateDownloaderUtil;
4243
import com.linbit.linstor.api.ApiClient;
4344
import com.linbit.linstor.api.ApiConsts;
4445
import com.linbit.linstor.api.ApiException;
@@ -730,7 +731,7 @@ private void fileExistsOrThrow(String templateFilePath) {
730731

731732
private String getFinalDirectDownloadPath(String templateFilePath, KVMStoragePool destPool) {
732733
String finalSourcePath = templateFilePath;
733-
if (LibvirtStorageAdaptor.isTemplateExtractable(templateFilePath)) {
734+
if (TemplateDownloaderUtil.isTemplateExtractable(templateFilePath)) {
734735
finalSourcePath = templateFilePath.substring(0, templateFilePath.lastIndexOf('.'));
735736
LibvirtStorageAdaptor.extractDownloadedTemplate(templateFilePath, destPool, finalSourcePath);
736737
}

plugins/storage/volume/storpool/src/main/java/com/cloud/hypervisor/kvm/storage/StorPoolStorageAdaptor.java

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.cloud.utils.script.OutputInterpreter;
2727
import com.cloud.utils.script.Script;
2828

29+
import com.cloud.utils.storage.TemplateDownloaderUtil;
2930
import com.google.gson.Gson;
3031
import com.google.gson.JsonObject;
3132
import com.google.gson.JsonParser;
@@ -528,35 +529,15 @@ private static Script createStorPoolRequest(String js, String apiCall, String pa
528529

529530
private String extractTemplate(String templateFilePath, File sourceFile, String srcTemplateFilePath,
530531
String templateName) {
531-
if (isTemplateExtractable(templateFilePath)) {
532+
if (TemplateDownloaderUtil.isTemplateExtractable(templateFilePath)) {
532533
srcTemplateFilePath = sourceFile.getParent() + "/" + templateName;
533-
String extractCommand = getExtractCommandForDownloadedFile(templateFilePath, srcTemplateFilePath);
534+
String extractCommand = TemplateDownloaderUtil.getExtractCommandForDownloadedFile(templateFilePath, srcTemplateFilePath);
534535
Script.runSimpleBashScript(extractCommand);
535536
Script.runSimpleBashScript("rm -f " + templateFilePath);
536537
}
537538
return srcTemplateFilePath;
538539
}
539540

540-
private boolean isTemplateExtractable(String templatePath) {
541-
String type = Script.runSimpleBashScript("file " + templatePath + " | awk -F' ' '{print $2}'");
542-
return type.equalsIgnoreCase("bzip2") || type.equalsIgnoreCase("gzip")
543-
|| type.equalsIgnoreCase("zip") || type.equalsIgnoreCase("xz");
544-
}
545-
546-
private String getExtractCommandForDownloadedFile(String downloadedTemplateFile, String templateFile) {
547-
if (downloadedTemplateFile.endsWith(".zip")) {
548-
return "unzip -p " + downloadedTemplateFile + " | cat > " + templateFile;
549-
} else if (downloadedTemplateFile.endsWith(".bz2")) {
550-
return "bunzip2 -c " + downloadedTemplateFile + " > " + templateFile;
551-
} else if (downloadedTemplateFile.endsWith(".gz")) {
552-
return "gunzip -c " + downloadedTemplateFile + " > " + templateFile;
553-
} else if (downloadedTemplateFile.endsWith(".xz")) {
554-
return "xz -d -c " + downloadedTemplateFile + " > " + templateFile;
555-
} else {
556-
throw new CloudRuntimeException("Unable to extract template " + downloadedTemplateFile);
557-
}
558-
}
559-
560541
private String getNameFromResponse(String resp, boolean tildeNeeded, boolean isSnapshot) {
561542
JsonParser jsonParser = new JsonParser();
562543
JsonObject respObj = (JsonObject) jsonParser.parse(resp);
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//
2+
// Licensed to the Apache Software Foundation (ASF) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The ASF licenses this file
6+
// to you under the Apache License, Version 2.0 (the
7+
// "License"); you may not use this file except in compliance
8+
// with the License. You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing,
13+
// software distributed under the License is distributed on an
14+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
// KIND, either express or implied. See the License for the
16+
// specific language governing permissions and limitations
17+
// under the License.
18+
//
19+
20+
package com.cloud.utils.storage;
21+
22+
import org.apache.commons.io.FilenameUtils;
23+
24+
import com.cloud.utils.exception.CloudRuntimeException;
25+
import com.cloud.utils.script.Script;
26+
27+
public class TemplateDownloaderUtil {
28+
29+
private TemplateDownloaderUtil() {}
30+
31+
/**
32+
* Checks if downloaded template is extractable
33+
* @return true if it should be extracted, false if not
34+
*/
35+
public static boolean isTemplateExtractable(String templatePath) {
36+
String type = Script.runSimpleBashScript("file " + templatePath + " | awk -F' ' '{print $2}'");
37+
return type.equalsIgnoreCase("bzip2") || type.equalsIgnoreCase("gzip")
38+
|| type.equalsIgnoreCase("zip") || type.equalsIgnoreCase("xz");
39+
}
40+
41+
/**
42+
* Return extract command to execute given downloaded file
43+
* @param downloadedTemplateFile
44+
* @param templateFile
45+
*/
46+
public static String getExtractCommandForDownloadedFile(String downloadedTemplateFile, String templateFile) {
47+
String extension = FilenameUtils.getExtension(downloadedTemplateFile).toLowerCase();
48+
switch (extension) {
49+
case "zip":
50+
return String.format("unzip -p '%s' | cat > '%s'", downloadedTemplateFile, templateFile);
51+
case "bz2":
52+
return String.format("bunzip2 -c '%s' > '%s'", downloadedTemplateFile, templateFile);
53+
case "gz":
54+
return String.format("gunzip -c '%s' > '%s'", downloadedTemplateFile, templateFile);
55+
case "xz":
56+
return String.format("xz -d -c '%s' > '%s'", downloadedTemplateFile, templateFile);
57+
default:
58+
throw new CloudRuntimeException("Unable to extract template: " + downloadedTemplateFile + " (unsupported format: ." + extension + ")");
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)