Skip to content

Commit 75a0a9d

Browse files
ustcweizhounlgordon
authored andcommitted
server: fix failed to remove template/iso if upload from local fails (apache#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
1 parent eb30420 commit 75a0a9d

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

server/src/main/java/com/cloud/template/TemplateAdapterBase.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,10 @@ public TemplateProfile prepareDelete(DeleteTemplateCmd cmd) {
456456
throw new InvalidParameterValueException("Please specify a valid template.");
457457
}
458458

459+
if (template.getState() == VirtualMachineTemplate.State.NotUploaded || template.getState() == VirtualMachineTemplate.State.UploadInProgress) {
460+
throw new InvalidParameterValueException("The template is either getting uploaded or it may be initiated shortly, please wait for it to be completed");
461+
}
462+
459463
return new TemplateProfile(userId, template, zoneId);
460464
}
461465

@@ -495,6 +499,10 @@ public TemplateProfile prepareDelete(DeleteIsoCmd cmd) {
495499
throw new InvalidParameterValueException("Please specify a valid iso.");
496500
}
497501

502+
if (template.getState() == VirtualMachineTemplate.State.NotUploaded || template.getState() == VirtualMachineTemplate.State.UploadInProgress) {
503+
throw new InvalidParameterValueException("The iso is either getting uploaded or it may be initiated shortly, please wait for it to be completed");
504+
}
505+
498506
return new TemplateProfile(userId, template, zoneId);
499507
}
500508

services/secondary-storage/server/src/main/java/org/apache/cloudstack/storage/resource/NfsSecondaryStorageResource.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2253,6 +2253,9 @@ protected Answer deleteTemplate(DeleteCommand cmd) {
22532253
File tmpltParent = null;
22542254
if (tmpltPath.exists() && tmpltPath.isDirectory()) {
22552255
tmpltParent = tmpltPath;
2256+
} else if (absoluteTemplatePath.endsWith(File.separator + obj.getId())) {
2257+
// the path ends with <account id>/<template id>, if upload fails
2258+
tmpltParent = tmpltPath;
22562259
} else {
22572260
tmpltParent = tmpltPath.getParentFile();
22582261
}
@@ -2357,6 +2360,9 @@ protected Answer deleteVolume(final DeleteCommand cmd) {
23572360
if (volPath.exists() && volPath.isDirectory()) {
23582361
// for vmware, absoluteVolumePath represents a directory where volume files are located.
23592362
tmpltParent = volPath;
2363+
} else if (absoluteVolumePath.endsWith(File.separator + obj.getId())) {
2364+
// the path ends with <account id>/<volume id>, if upload fails
2365+
tmpltParent = volPath;
23602366
} else {
23612367
// for other hypervisors, the volume .vhd or .qcow2 file path is passed
23622368
tmpltParent = new File(absoluteVolumePath).getParentFile();

0 commit comments

Comments
 (0)