7979import com .cloud .storage .StorageLayer ;
8080import com .cloud .utils .exception .CloudRuntimeException ;
8181import com .cloud .utils .script .Script ;
82+ import com .cloud .utils .storage .TemplateDownloaderUtil ;
8283
8384public 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 );
0 commit comments