Skip to content

Commit 9e535e3

Browse files
authored
Support xz format for template registration (#11786)
1 parent dfcbd2e commit 9e535e3

File tree

16 files changed

+96
-87
lines changed

16 files changed

+96
-87
lines changed

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

Lines changed: 3 additions & 28 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,37 +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") || type.equalsIgnoreCase("zip");
182-
}
183-
184-
/**
185-
* Return extract command to execute given downloaded file
186-
* @param downloadedTemplateFile
187-
* @param templateUuid
188-
*/
189-
public static String getExtractCommandForDownloadedFile(String downloadedTemplateFile, String templateUuid) {
190-
if (downloadedTemplateFile.endsWith(".zip")) {
191-
return "unzip -p " + downloadedTemplateFile + " | cat > " + templateUuid;
192-
} else if (downloadedTemplateFile.endsWith(".bz2")) {
193-
return "bunzip2 -c " + downloadedTemplateFile + " > " + templateUuid;
194-
} else if (downloadedTemplateFile.endsWith(".gz")) {
195-
return "gunzip -c " + downloadedTemplateFile + " > " + templateUuid;
196-
} else {
197-
throw new CloudRuntimeException("Unable to extract template " + downloadedTemplateFile);
198-
}
199-
}
200-
201176
/**
202177
* Extract downloaded template into installPath, remove compressed file
203178
*/
204179
public static void extractDownloadedTemplate(String downloadedTemplateFile, KVMStoragePool destPool, String destinationFile) {
205-
String extractCommand = getExtractCommandForDownloadedFile(downloadedTemplateFile, destinationFile);
180+
String extractCommand = TemplateDownloaderUtil.getExtractCommandForDownloadedFile(downloadedTemplateFile, destinationFile);
206181
Script.runSimpleBashScript(extractCommand);
207182
Script.runSimpleBashScript("rm -f " + downloadedTemplateFile);
208183
}
@@ -221,7 +196,7 @@ public KVMPhysicalDisk createTemplateFromDirectDownloadFile(String templateFileP
221196

222197
if (destPool.getType() == StoragePoolType.NetworkFilesystem || destPool.getType() == StoragePoolType.Filesystem
223198
|| destPool.getType() == StoragePoolType.SharedMountPoint) {
224-
if (!Storage.ImageFormat.ISO.equals(format) && isTemplateExtractable(templateFilePath)) {
199+
if (!Storage.ImageFormat.ISO.equals(format) && TemplateDownloaderUtil.isTemplateExtractable(templateFilePath)) {
225200
extractDownloadedTemplate(templateFilePath, destPool, destinationFile);
226201
} else {
227202
Script.runSimpleBashScript("mv " + templateFilePath + " " + destinationFile);

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

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -423,24 +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") || type.equalsIgnoreCase("zip");
430-
}
431-
432-
String getExtractCommandForDownloadedFile(String downloadedTemplateFile, String templateFile) {
433-
if (downloadedTemplateFile.endsWith(".zip")) {
434-
return "unzip -p " + downloadedTemplateFile + " | cat > " + templateFile;
435-
} else if (downloadedTemplateFile.endsWith(".bz2")) {
436-
return "bunzip2 -c " + downloadedTemplateFile + " > " + templateFile;
437-
} else if (downloadedTemplateFile.endsWith(".gz")) {
438-
return "gunzip -c " + downloadedTemplateFile + " > " + templateFile;
439-
} else {
440-
throw new CloudRuntimeException("Unable to extract template " + downloadedTemplateFile);
441-
}
442-
}
443-
444426
boolean waitForDiskToBecomeAvailable(AddressInfo address, KVMStoragePool pool, long waitTimeInSec) {
445427
LOGGER.debug("Waiting for the volume with id: " + address.getPath() + " of the storage pool: " + pool.getUuid() + " to become available for " + waitTimeInSec + " secs");
446428

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

Lines changed: 4 additions & 19 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,23 +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") || type.equalsIgnoreCase("zip");
617-
}
618-
619-
private String getExtractCommandForDownloadedFile(String downloadedTemplateFile, String templateFile) {
620-
if (downloadedTemplateFile.endsWith(".zip")) {
621-
return "unzip -p " + downloadedTemplateFile + " | cat > " + templateFile;
622-
} else if (downloadedTemplateFile.endsWith(".bz2")) {
623-
return "bunzip2 -c " + downloadedTemplateFile + " > " + templateFile;
624-
} else if (downloadedTemplateFile.endsWith(".gz")) {
625-
return "gunzip -c " + downloadedTemplateFile + " > " + templateFile;
626-
} else {
627-
throw new CloudRuntimeException("Unable to extract template " + downloadedTemplateFile);
628-
}
629-
}
630-
631616
public void resizeQcow2ToVolume(String volumePath, QemuImageOptions options, List<QemuObject> objects, Integer timeout) throws QemuImgException, LibvirtException {
632617
long rawSizeBytes = getPhysicalDiskSize(volumePath);
633618
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
@@ -38,6 +38,7 @@
3838
import org.apache.logging.log4j.LogManager;
3939
import org.libvirt.LibvirtException;
4040

41+
import com.cloud.utils.storage.TemplateDownloaderUtil;
4142
import com.linbit.linstor.api.ApiClient;
4243
import com.linbit.linstor.api.ApiConsts;
4344
import com.linbit.linstor.api.ApiException;
@@ -694,7 +695,7 @@ private void fileExistsOrThrow(String templateFilePath) {
694695

695696
private String getFinalDirectDownloadPath(String templateFilePath, KVMStoragePool destPool) {
696697
String finalSourcePath = templateFilePath;
697-
if (LibvirtStorageAdaptor.isTemplateExtractable(templateFilePath)) {
698+
if (TemplateDownloaderUtil.isTemplateExtractable(templateFilePath)) {
698699
finalSourcePath = templateFilePath.substring(0, templateFilePath.lastIndexOf('.'));
699700
LibvirtStorageAdaptor.extractDownloadedTemplate(templateFilePath, destPool, finalSourcePath);
700701
}

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

Lines changed: 3 additions & 19 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,32 +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") || type.equalsIgnoreCase("zip");
543-
}
544-
545-
private String getExtractCommandForDownloadedFile(String downloadedTemplateFile, String templateFile) {
546-
if (downloadedTemplateFile.endsWith(".zip")) {
547-
return "unzip -p " + downloadedTemplateFile + " | cat > " + templateFile;
548-
} else if (downloadedTemplateFile.endsWith(".bz2")) {
549-
return "bunzip2 -c " + downloadedTemplateFile + " > " + templateFile;
550-
} else if (downloadedTemplateFile.endsWith(".gz")) {
551-
return "gunzip -c " + downloadedTemplateFile + " > " + templateFile;
552-
} else {
553-
throw new CloudRuntimeException("Unable to extract template " + downloadedTemplateFile);
554-
}
555-
}
556-
557541
private String getNameFromResponse(String resp, boolean tildeNeeded, boolean isSnapshot) {
558542
JsonParser jsonParser = new JsonParser();
559543
JsonObject respObj = (JsonObject) jsonParser.parse(resp);

scripts/installer/createtmplt.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ uncompress() {
8787
;;
8888
[zZ][iI][pP]) unzip -p $1 | cat > $tmpfile
8989
;;
90+
XZ) xz -d -c $1 > $tmpfile
91+
;;
9092
*) printf "$1"
9193
return 0
9294
;;

scripts/installer/createvolume.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ uncompress() {
8888
;;
8989
ZIP) unzip -p $1 | cat > $tmpfile
9090
;;
91+
XZ) xz -d -c $1 > $tmpfile
92+
;;
9193
*) printf "$1"
9294
return 0
9395
;;

scripts/storage/qcow2/createtmplt.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ uncompress() {
6565
;;
6666
[zZ][iI][pP]) unzip -p $1 | cat > $tmpfile
6767
;;
68+
XZ) xz -d -c $1 > $tmpfile
69+
;;
6870
*) printf "$1"
6971
return 0
7072
;;

scripts/storage/qcow2/createvolume.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ uncompress() {
6666
;;
6767
ZIP) unzip -p $1 | cat > $tmpfile
6868
;;
69+
XZ) xz -d -c $1 > $tmpfile
70+
;;
6971
*) printf "$1"
7072
return 0
7173
;;

scripts/storage/secondary/createtmplt.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ is_compressed() {
6363
;;
6464
[zZ][iI][pP]) ctype="zip"
6565
;;
66+
XZ) ctype="xz"
67+
;;
6668
*) echo "File $1 does not appear to be compressed" >&2
6769
return 1
6870
;;
@@ -82,6 +84,8 @@ uncompress() {
8284
;;
8385
[zZ][iI][pP]) unzip -q -p $1 | cat > $tmpfile
8486
;;
87+
XZ) xz -d -c $1 > $tmpfile
88+
;;
8589
*) printf "$1"
8690
return 0
8791
;;

0 commit comments

Comments
 (0)