|
22 | 22 | import com.cloud.agent.api.Answer;
|
23 | 23 | import com.cloud.agent.api.DeleteStoragePoolCommand;
|
24 | 24 | import com.cloud.agent.api.to.StorageFilerTO;
|
| 25 | +import com.cloud.agent.dao.impl.PropertiesStorage; |
| 26 | +import com.cloud.agent.properties.AgentProperties; |
| 27 | +import com.cloud.agent.properties.AgentPropertiesFileHandler; |
25 | 28 | import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource;
|
26 | 29 | import com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager;
|
27 | 30 | import com.cloud.resource.CommandWrapper;
|
28 | 31 | import com.cloud.resource.ResourceWrapper;
|
| 32 | +import com.cloud.storage.Storage; |
29 | 33 | import com.cloud.utils.exception.CloudRuntimeException;
|
30 | 34 |
|
| 35 | +import java.util.Arrays; |
| 36 | +import java.util.HashMap; |
| 37 | +import java.util.stream.Collectors; |
| 38 | + |
31 | 39 | @ResourceWrapper(handles = DeleteStoragePoolCommand.class)
|
32 | 40 | public final class LibvirtDeleteStoragePoolCommandWrapper extends CommandWrapper<DeleteStoragePoolCommand, Answer, LibvirtComputingResource> {
|
33 | 41 | @Override
|
34 | 42 | public Answer execute(final DeleteStoragePoolCommand command, final LibvirtComputingResource libvirtComputingResource) {
|
35 | 43 | try {
|
36 | 44 | // if getRemoveDatastore() is true, then we are dealing with managed storage and can skip the delete logic here
|
37 | 45 | if (!command.getRemoveDatastore()) {
|
38 |
| - final StorageFilerTO pool = command.getPool(); |
39 |
| - final KVMStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr(); |
40 |
| - |
41 |
| - storagePoolMgr.deleteStoragePool(pool.getType(), pool.getUuid()); |
| 46 | + handleStoragePoolDeletion(command, libvirtComputingResource); |
42 | 47 | }
|
43 |
| - |
44 | 48 | return new Answer(command);
|
45 | 49 | } catch (final CloudRuntimeException e) {
|
46 | 50 | return new Answer(command, false, e.toString());
|
47 | 51 | }
|
48 | 52 | }
|
| 53 | + |
| 54 | + private void handleStoragePoolDeletion(final DeleteStoragePoolCommand command, final LibvirtComputingResource libvirtComputingResource) { |
| 55 | + final StorageFilerTO pool = command.getPool(); |
| 56 | + final KVMStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr(); |
| 57 | + storagePoolMgr.deleteStoragePool(pool.getType(), pool.getUuid()); |
| 58 | + |
| 59 | + if (isLocalStorageAndNotHavingDefaultPath(pool, libvirtComputingResource)) { |
| 60 | + updateLocalStorageProperties(pool); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + private boolean isLocalStorageAndNotHavingDefaultPath(final StorageFilerTO pool, final LibvirtComputingResource libvirtComputingResource) { |
| 65 | + return Storage.StoragePoolType.Filesystem.equals(pool.getType()) |
| 66 | + && !libvirtComputingResource.DEFAULT_LOCAL_STORAGE_PATH.equals(pool.getPath()); |
| 67 | + } |
| 68 | + |
| 69 | + private void updateLocalStorageProperties(final StorageFilerTO pool) { |
| 70 | + String localStoragePath = AgentPropertiesFileHandler.getPropertyValue(AgentProperties.LOCAL_STORAGE_PATH); |
| 71 | + String localStorageUuid = AgentPropertiesFileHandler.getPropertyValue(AgentProperties.LOCAL_STORAGE_UUID); |
| 72 | + |
| 73 | + String uuidToRemove = pool.getUuid(); |
| 74 | + String pathToRemove = pool.getPath(); |
| 75 | + |
| 76 | + if (localStorageUuid != null && uuidToRemove != null) { |
| 77 | + localStorageUuid = Arrays.stream(localStorageUuid.split(",")) |
| 78 | + .filter(uuid -> !uuid.equals(uuidToRemove)) |
| 79 | + .collect(Collectors.joining(",")); |
| 80 | + } |
| 81 | + |
| 82 | + if (localStoragePath != null && pathToRemove != null) { |
| 83 | + localStoragePath = Arrays.stream(localStoragePath.split(",")) |
| 84 | + .filter(path -> !path.equals(pathToRemove)) |
| 85 | + .collect(Collectors.joining(",")); |
| 86 | + } |
| 87 | + |
| 88 | + PropertiesStorage agentProperties = new PropertiesStorage(); |
| 89 | + agentProperties.configure("AgentProperties", new HashMap<String, Object>()); |
| 90 | + |
| 91 | + if (localStorageUuid != null) { |
| 92 | + agentProperties.persist(AgentProperties.LOCAL_STORAGE_UUID.getName(), localStorageUuid); |
| 93 | + } |
| 94 | + |
| 95 | + if (localStoragePath != null) { |
| 96 | + agentProperties.persist(AgentProperties.LOCAL_STORAGE_PATH.getName(), localStoragePath); |
| 97 | + } |
| 98 | + } |
49 | 99 | }
|
0 commit comments