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,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 );
0 commit comments