From 58a3f90a5146e93d2a4699cef439319f8e664f7d Mon Sep 17 00:00:00 2001 From: Wei Zhou Date: Sat, 6 Mar 2021 10:23:40 +0100 Subject: [PATCH] server: fix failed to remove template/iso if upload from local fails (#4626) * server: fix failed to remove template/iso if upload from local fails When upload template/iso/volume from local fails, the install_path will not be a full path of file so removing it will fail. ``` mysql> select install_path from template_store_ref; +--------------------------------------------------------------------+ | install_path | +--------------------------------------------------------------------+ | template/tmpl/1/3/805f4763-248e-40ec-b79a-b868cc480d0a.qcow2 | | template/tmpl/1/4/c7e32c9e-5e72-3726-85cf-aa5ccd84118d.qcow2 | | template/tmpl/2/201/bc4f4f08-138a-31b8-af1a-d4450eff7982.qcow2 | | template/tmpl/2/202 | | template/tmpl/2/203/203-2-d47f8cde-a2a8-31e7-a826-2628ad98a6c8.iso | | template/tmpl/2/204 | | template/tmpl/5/205 | | template/tmpl/2/206 | | template/tmpl/2/207 | | template/tmpl/2/208 | | template/tmpl/2/209 | | template/tmpl/2/210 | +--------------------------------------------------------------------+ 12 rows in set (0.00 sec) mysql> select install_path from volume_store_ref; +---------------------------------------------------------+ | install_path | +---------------------------------------------------------+ | volumes/2/22 | | volumes/2/19/f93face9-6521-4184-b89a-cb07f86bbae8.qcow2 | | volumes/2/23 | | volumes/2/24 | +---------------------------------------------------------+ 4 rows in set (0.00 sec) ``` * server: disallow removing template/iso in NotUpload and UploadInProgress state --- .../main/java/com/cloud/template/TemplateAdapterBase.java | 8 ++++++++ .../storage/resource/NfsSecondaryStorageResource.java | 6 ++++++ 2 files changed, 14 insertions(+) diff --git a/server/src/main/java/com/cloud/template/TemplateAdapterBase.java b/server/src/main/java/com/cloud/template/TemplateAdapterBase.java index 0e88c147f51..c42ca10bf30 100644 --- a/server/src/main/java/com/cloud/template/TemplateAdapterBase.java +++ b/server/src/main/java/com/cloud/template/TemplateAdapterBase.java @@ -456,6 +456,10 @@ public abstract class TemplateAdapterBase extends AdapterBase implements Templat throw new InvalidParameterValueException("Please specify a valid template."); } + if (template.getState() == VirtualMachineTemplate.State.NotUploaded || template.getState() == VirtualMachineTemplate.State.UploadInProgress) { + throw new InvalidParameterValueException("The template is either getting uploaded or it may be initiated shortly, please wait for it to be completed"); + } + return new TemplateProfile(userId, template, zoneId); } @@ -495,6 +499,10 @@ public abstract class TemplateAdapterBase extends AdapterBase implements Templat throw new InvalidParameterValueException("Please specify a valid iso."); } + if (template.getState() == VirtualMachineTemplate.State.NotUploaded || template.getState() == VirtualMachineTemplate.State.UploadInProgress) { + throw new InvalidParameterValueException("The iso is either getting uploaded or it may be initiated shortly, please wait for it to be completed"); + } + return new TemplateProfile(userId, template, zoneId); } diff --git a/services/secondary-storage/server/src/main/java/org/apache/cloudstack/storage/resource/NfsSecondaryStorageResource.java b/services/secondary-storage/server/src/main/java/org/apache/cloudstack/storage/resource/NfsSecondaryStorageResource.java index f4ae30e3e8f..274815d39c4 100644 --- a/services/secondary-storage/server/src/main/java/org/apache/cloudstack/storage/resource/NfsSecondaryStorageResource.java +++ b/services/secondary-storage/server/src/main/java/org/apache/cloudstack/storage/resource/NfsSecondaryStorageResource.java @@ -2253,6 +2253,9 @@ public class NfsSecondaryStorageResource extends ServerResourceBase implements S File tmpltParent = null; if (tmpltPath.exists() && tmpltPath.isDirectory()) { tmpltParent = tmpltPath; + } else if (absoluteTemplatePath.endsWith(File.separator + obj.getId())) { + // the path ends with /