From 0588ba6fb5458d6822207c02f52f942d299d099f Mon Sep 17 00:00:00 2001 From: Wido den Hollander Date: Tue, 22 Sep 2026 14:35:26 +0000 Subject: [PATCH 1/4] kvm: remove command wrappers for commands the management server no longer sends The KVM agent still registered wrappers for a set of commands that date back to the pre-4.2 storage model. None of these commands is created or subclassed anywhere in the management server any more, so the wrappers could never be reached: AttachIsoCommand, BackupSnapshotCommand, CheckConsoleProxyLoadCommand, WatchConsoleProxyLoadCommand, CheckStorageAvailabilityCommand, CreateCommand, CreatePrivateTemplateFromSnapshotCommand, CreatePrivateTemplateFromVolumeCommand, CreateVolumeFromSnapshotCommand, ManageSnapshotCommand, PrimaryStorageDownloadCommand, RebootRouterCommand, UpgradeSnapshotCommand Along with the wrappers and their shared console proxy base class, this removes what only they used: - the createtmplt.sh and createvm.sh lookups in LibvirtComputingResource. The storage processor has its own createtmplt.sh lookup; createvm.sh was looked up at startup but never executed. - the buildTemplateLocation, buildQCOW2Processor and retrieveBashScriptPath helpers in LibvirtUtilitiesHelper and the BASH_SCRIPT_PATH constant. - fields that were declared but never read: heartBeatPath, hostOsVersion, javaTempDir, and an unused local in getMacAddressToNicNumPair. - the 27 unit tests that exercised the removed wrappers. The Command classes themselves stay in core because the XenServer, VMware and simulator resources still reference them. --- .../resource/LibvirtComputingResource.java | 21 - .../LibvirtAttachIsoCommandWrapper.java | 50 - .../LibvirtBackupSnapshotCommandWrapper.java | 210 ---- ...rtCheckConsoleProxyLoadCommandWrapper.java | 43 - ...ibvirtCheckStorageAvailabilityWrapper.java | 59 - ...LibvirtConsoleProxyLoadCommandWrapper.java | 76 -- .../wrapper/LibvirtCreateCommandWrapper.java | 85 -- ...ateTemplateFromSnapshotCommandWrapper.java | 113 -- ...ivateTemplateFromVolumeCommandWrapper.java | 175 --- ...reateVolumeFromSnapshotCommandWrapper.java | 70 -- .../LibvirtManageSnapshotCommandWrapper.java | 167 --- ...tPrimaryStorageDownloadCommandWrapper.java | 90 -- .../LibvirtRebootRouterCommandWrapper.java | 49 - .../LibvirtUpgradeSnapshotCommandWrapper.java | 35 - .../wrapper/LibvirtUtilitiesHelper.java | 26 - ...rtWatchConsoleProxyLoadCommandWrapper.java | 43 - .../LibvirtComputingResourceTest.java | 1112 +---------------- 17 files changed, 4 insertions(+), 2420 deletions(-) delete mode 100644 plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtAttachIsoCommandWrapper.java delete mode 100644 plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtBackupSnapshotCommandWrapper.java delete mode 100644 plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtCheckConsoleProxyLoadCommandWrapper.java delete mode 100644 plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtCheckStorageAvailabilityWrapper.java delete mode 100644 plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtConsoleProxyLoadCommandWrapper.java delete mode 100644 plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtCreateCommandWrapper.java delete mode 100644 plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtCreatePrivateTemplateFromSnapshotCommandWrapper.java delete mode 100644 plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtCreatePrivateTemplateFromVolumeCommandWrapper.java delete mode 100644 plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtCreateVolumeFromSnapshotCommandWrapper.java delete mode 100644 plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtManageSnapshotCommandWrapper.java delete mode 100644 plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtPrimaryStorageDownloadCommandWrapper.java delete mode 100644 plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtRebootRouterCommandWrapper.java delete mode 100644 plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtUpgradeSnapshotCommandWrapper.java delete mode 100644 plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtWatchConsoleProxyLoadCommandWrapper.java diff --git a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java index 9009ec629ca3..0502b5f0e47e 100644 --- a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java +++ b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java @@ -421,11 +421,8 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv private String versionStringPath; private String gpuDiscoveryPath; private String patchScriptPath; - private String createVmPath; private String manageSnapshotPath; private String resizeVolumePath; - private String createTmplPath; - private String heartBeatPath; private String vmActivityCheckPath; private String nasBackupPath; private String imageServerPath; @@ -467,7 +464,6 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv public final static String CONFIG_DIR = "config"; private boolean enableIoUring; - public static final String BASH_SCRIPT_PATH = "/bin/bash"; private StorageLayer storageLayer; private KVMStoragePoolManager storagePoolManager; @@ -512,7 +508,6 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv protected boolean noKvmClock; protected String videoHw; protected int videoRam; - protected Pair hostOsVersion; protected int migrateSpeed; protected int migrateDowntime; protected int migratePauseAfter; @@ -823,10 +818,6 @@ public StorageLayer getStorage() { return storageLayer; } - public String createTmplPath() { - return createTmplPath; - } - public int getCmdsTimeout() { return cmdsTimeout; } @@ -964,7 +955,6 @@ public String toString() { private String vddkThumbprint = null; private String vddkVersion = null; private String detectedPasswordFileOption = null; - protected String javaTempDir = System.getProperty("java.io.tmpdir"); private String getEndIpFromStartIp(final String startIp, final int numIps) { final String[] tokens = startIp.split("[.]"); @@ -1163,11 +1153,6 @@ public boolean configure(final String name, final Map params) th throw new ConfigurationException("Unable to find patch.sh"); } - createVmPath = Script.findScript(storageScriptsDir, "createvm.sh"); - if (createVmPath == null) { - throw new ConfigurationException("Unable to find the createvm.sh"); - } - manageSnapshotPath = Script.findScript(storageScriptsDir, "managesnapshot.sh"); if (manageSnapshotPath == null) { throw new ConfigurationException("Unable to find the managesnapshot.sh"); @@ -1194,11 +1179,6 @@ public boolean configure(final String name, final Map params) th } imageServerPath = new File(imageServerMain).getParent(); - createTmplPath = Script.findScript(storageScriptsDir, "createtmplt.sh"); - if (createTmplPath == null) { - throw new ConfigurationException("Unable to find the createtmplt.sh"); - } - securityGroupPath = Script.findScript(networkScriptsDir, "security_group.py"); if (securityGroupPath == null) { throw new ConfigurationException("Unable to find the security_group.py"); @@ -2859,7 +2839,6 @@ private Pair, Integer> getMacAddressToNicNumPair(Connect co final List pluggedNics = getInterfaces(conn, routerName); final Map macAddressToNicNum = new HashMap<>(pluggedNics.size()); for (final InterfaceDef pluggedNic : pluggedNics) { - final String pluggedVlan = pluggedNic.getBrName(); macAddressToNicNum.put(pluggedNic.getMacAddress(), devNum); devNum++; } diff --git a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtAttachIsoCommandWrapper.java b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtAttachIsoCommandWrapper.java deleted file mode 100644 index e15706b3d81b..000000000000 --- a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtAttachIsoCommandWrapper.java +++ /dev/null @@ -1,50 +0,0 @@ -// -// Licensed to the Apache Software Foundation (ASF) under one -// or more contributor license agreements. See the NOTICE file -// distributed with this work for additional information -// regarding copyright ownership. The ASF licenses this file -// to you under the Apache License, Version 2.0 (the -// "License"); you may not use this file except in compliance -// with the License. You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, -// software distributed under the License is distributed on an -// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. See the License for the -// specific language governing permissions and limitations -// under the License. -// - -package com.cloud.hypervisor.kvm.resource.wrapper; - -import java.net.URISyntaxException; - -import org.libvirt.Connect; -import org.libvirt.LibvirtException; - -import com.cloud.agent.api.AttachIsoAnswer; -import com.cloud.agent.api.AttachIsoCommand; -import com.cloud.exception.InternalErrorException; -import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource; -import com.cloud.resource.CommandWrapper; -import com.cloud.resource.ResourceWrapper; - -@ResourceWrapper(handles = AttachIsoCommand.class) -public final class LibvirtAttachIsoCommandWrapper extends CommandWrapper { - - @Override - public AttachIsoAnswer execute(final AttachIsoCommand command, final LibvirtComputingResource libvirtComputingResource) { - try { - final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper(); - - final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(command.getVmName()); - libvirtComputingResource.attachOrDetachISO(conn, command.getVmName(), command.getIsoPath(), command.isAttach(), command.getDeviceKey()); - } catch (final LibvirtException|URISyntaxException|InternalErrorException e) { - return new AttachIsoAnswer(command, e); - } - - return new AttachIsoAnswer(command, command.getDeviceKey()); - } -} diff --git a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtBackupSnapshotCommandWrapper.java b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtBackupSnapshotCommandWrapper.java deleted file mode 100644 index 964e6591878c..000000000000 --- a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtBackupSnapshotCommandWrapper.java +++ /dev/null @@ -1,210 +0,0 @@ -// -// Licensed to the Apache Software Foundation (ASF) under one -// or more contributor license agreements. See the NOTICE file -// distributed with this work for additional information -// regarding copyright ownership. The ASF licenses this file -// to you under the Apache License, Version 2.0 (the -// "License"); you may not use this file except in compliance -// with the License. You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, -// software distributed under the License is distributed on an -// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. See the License for the -// specific language governing permissions and limitations -// under the License. -// - -package com.cloud.hypervisor.kvm.resource.wrapper; - -import java.io.BufferedOutputStream; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.text.MessageFormat; - -import org.libvirt.Connect; -import org.libvirt.Domain; -import org.libvirt.DomainInfo.DomainState; -import org.libvirt.DomainSnapshot; -import org.libvirt.LibvirtException; - -import com.ceph.rados.IoCTX; -import com.ceph.rados.Rados; -import com.ceph.rados.exceptions.RadosException; -import com.ceph.rbd.Rbd; -import com.ceph.rbd.RbdException; -import com.ceph.rbd.RbdImage; -import com.cloud.agent.api.Answer; -import com.cloud.agent.api.BackupSnapshotAnswer; -import com.cloud.agent.api.BackupSnapshotCommand; -import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource; -import com.cloud.hypervisor.kvm.storage.KVMPhysicalDisk; -import com.cloud.hypervisor.kvm.storage.KVMStoragePool; -import com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager; -import com.cloud.resource.CommandWrapper; -import com.cloud.resource.ResourceWrapper; -import com.cloud.storage.Storage.StoragePoolType; -import com.cloud.utils.exception.CloudRuntimeException; -import com.cloud.utils.script.Script; - -import static com.cloud.utils.NumbersUtil.toHumanReadableSize; - -@ResourceWrapper(handles = BackupSnapshotCommand.class) -public final class LibvirtBackupSnapshotCommandWrapper extends CommandWrapper { - - - @Override - public Answer execute(final BackupSnapshotCommand command, final LibvirtComputingResource libvirtComputingResource) { - final Long dcId = command.getDataCenterId(); - final Long accountId = command.getAccountId(); - final Long volumeId = command.getVolumeId(); - final String secondaryStoragePoolUrl = command.getSecondaryStorageUrl(); - final String snapshotName = command.getSnapshotName(); - String snapshotDestPath = null; - String snapshotRelPath = null; - final String vmName = command.getVmName(); - KVMStoragePool secondaryStoragePool = null; - final KVMStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr(); - - try { - final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper(); - final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(vmName); - - secondaryStoragePool = storagePoolMgr.getStoragePoolByURI(secondaryStoragePoolUrl); - - final String ssPmountPath = secondaryStoragePool.getLocalPath(); - snapshotRelPath = File.separator + "snapshots" + File.separator + dcId + File.separator + accountId + File.separator + volumeId; - - snapshotDestPath = ssPmountPath + File.separator + "snapshots" + File.separator + dcId + File.separator + accountId + File.separator + volumeId; - final KVMStoragePool primaryPool = storagePoolMgr.getStoragePool(command.getPool().getType(), command.getPrimaryStoragePoolNameLabel()); - final KVMPhysicalDisk snapshotDisk = primaryPool.getPhysicalDisk(command.getVolumePath()); - - final String manageSnapshotPath = libvirtComputingResource.manageSnapshotPath(); - final int cmdsTimeout = libvirtComputingResource.getCmdsTimeout(); - - /** - * RBD snapshots can't be copied using qemu-img, so we have to use - * the Java bindings for librbd here. - * - * These bindings will read the snapshot and write the contents to - * the secondary storage directly - * - * It will stop doing so if the amount of time spend is longer then - * cmds.timeout - */ - if (primaryPool.getType() == StoragePoolType.RBD) { - try { - final Rados r = new Rados(primaryPool.getAuthUserName()); - r.confSet("mon_host", primaryPool.getSourceHost() + ":" + primaryPool.getSourcePort()); - r.confSet("key", primaryPool.getAuthSecret()); - r.confSet("client_mount_timeout", "30"); - r.connect(); - logger.debug("Successfully connected to Ceph cluster at " + r.confGet("mon_host")); - - final IoCTX io = r.ioCtxCreate(primaryPool.getSourceDir()); - final Rbd rbd = new Rbd(io); - final RbdImage image = rbd.open(snapshotDisk.getName(), snapshotName); - final File fh = new File(snapshotDestPath); - try(BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(fh));) { - final int chunkSize = 4194304; - long offset = 0; - logger.debug("Backuping up RBD snapshot " + snapshotName + " to " + snapshotDestPath); - while (true) { - final byte[] buf = new byte[chunkSize]; - final int bytes = image.read(offset, buf, chunkSize); - if (bytes <= 0) { - break; - } - bos.write(buf, 0, bytes); - offset += bytes; - } - logger.debug("Completed backing up RBD snapshot " + snapshotName + " to " + snapshotDestPath + ". Bytes written: " + toHumanReadableSize(offset)); - }catch(final IOException ex) - { - logger.error("BackupSnapshotAnswer:Exception:"+ ex.getMessage()); - } - r.ioCtxDestroy(io); - } catch (final RadosException e) { - logger.error("A RADOS operation failed. The error was: " + e.getMessage()); - return new BackupSnapshotAnswer(command, false, e.toString(), null, true); - } catch (final RbdException e) { - logger.error("A RBD operation on " + snapshotDisk.getName() + " failed. The error was: " + e.getMessage()); - return new BackupSnapshotAnswer(command, false, e.toString(), null, true); - } - } else { - final Script scriptCommand = new Script(manageSnapshotPath, cmdsTimeout, logger); - scriptCommand.add("-b", snapshotDisk.getPath()); - scriptCommand.add("-n", snapshotName); - scriptCommand.add("-p", snapshotDestPath); - scriptCommand.add("-t", snapshotName); - final String result = scriptCommand.execute(); - - if (result != null) { - logger.debug("Failed to backup snaptshot: " + result); - return new BackupSnapshotAnswer(command, false, result, null, true); - } - } - /* Delete the snapshot on primary */ - - DomainState state = null; - Domain vm = null; - if (vmName != null) { - try { - vm = libvirtComputingResource.getDomain(conn, command.getVmName()); - state = vm.getInfo().state; - } catch (final LibvirtException e) { - logger.trace("Ignoring libvirt error.", e); - } - } - - final KVMStoragePool primaryStorage = storagePoolMgr.getStoragePool(command.getPool().getType(), command.getPool().getUuid()); - - if (state == DomainState.VIR_DOMAIN_RUNNING && !primaryStorage.isExternalSnapshot()) { - final MessageFormat snapshotXML = new MessageFormat(" " + " {0}" + " " - + " {1}" + " " + " "); - - final String vmUuid = vm.getUUIDString(); - final Object[] args = new Object[] {snapshotName, vmUuid}; - final String snapshot = snapshotXML.format(args); - logger.debug(snapshot); - final DomainSnapshot snap = vm.snapshotLookupByName(snapshotName); - if (snap != null) { - snap.delete(0); - } else { - throw new CloudRuntimeException("Unable to find Instance Snapshot with name -" + snapshotName); - } - - /* - * libvirt on RHEL6 doesn't handle resume event emitted from - * qemu - */ - vm = libvirtComputingResource.getDomain(conn, command.getVmName()); - state = vm.getInfo().state; - if (state == DomainState.VIR_DOMAIN_PAUSED) { - vm.resume(); - } - } else { - final Script scriptCommand = new Script(manageSnapshotPath, cmdsTimeout, logger); - scriptCommand.add("-d", snapshotDisk.getPath()); - scriptCommand.add("-n", snapshotName); - final String result = scriptCommand.execute(); - if (result != null) { - logger.debug("Failed to backup snapshot: " + result); - return new BackupSnapshotAnswer(command, false, "Failed to backup snapshot: " + result, null, true); - } - } - } catch (final LibvirtException e) { - return new BackupSnapshotAnswer(command, false, e.toString(), null, true); - } catch (final CloudRuntimeException e) { - return new BackupSnapshotAnswer(command, false, e.toString(), null, true); - } finally { - if (secondaryStoragePool != null) { - storagePoolMgr.deleteStoragePool(secondaryStoragePool.getType(), secondaryStoragePool.getUuid()); - } - } - return new BackupSnapshotAnswer(command, true, null, snapshotRelPath + File.separator + snapshotName, true); - } -} diff --git a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtCheckConsoleProxyLoadCommandWrapper.java b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtCheckConsoleProxyLoadCommandWrapper.java deleted file mode 100644 index b692c742df85..000000000000 --- a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtCheckConsoleProxyLoadCommandWrapper.java +++ /dev/null @@ -1,43 +0,0 @@ -// -// Licensed to the Apache Software Foundation (ASF) under one -// or more contributor license agreements. See the NOTICE file -// distributed with this work for additional information -// regarding copyright ownership. The ASF licenses this file -// to you under the Apache License, Version 2.0 (the -// "License"); you may not use this file except in compliance -// with the License. You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, -// software distributed under the License is distributed on an -// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. See the License for the -// specific language governing permissions and limitations -// under the License. -// - -package com.cloud.hypervisor.kvm.resource.wrapper; - -import com.cloud.agent.api.Answer; -import com.cloud.agent.api.Command; -import com.cloud.agent.api.proxy.CheckConsoleProxyLoadCommand; -import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource; -import com.cloud.resource.ResourceWrapper; -import com.cloud.resource.ServerResource; - -@ResourceWrapper(handles = CheckConsoleProxyLoadCommand.class) -public class LibvirtCheckConsoleProxyLoadCommandWrapper extends LibvirtConsoleProxyLoadCommandWrapper { - - @Override - public Answer execute(final Command command, final ServerResource serverResource) { - final CheckConsoleProxyLoadCommand cmd = (CheckConsoleProxyLoadCommand) command; - - final long proxyVmId = cmd.getProxyVmId(); - final String proxyVmName = cmd.getProxyVmName(); - final String proxyManagementIp = cmd.getProxyManagementIp(); - final int proxyCmdPort = cmd.getProxyCmdPort(); - - return executeProxyLoadScan(cmd, proxyVmId, proxyVmName, proxyManagementIp, proxyCmdPort); - } -} diff --git a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtCheckStorageAvailabilityWrapper.java b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtCheckStorageAvailabilityWrapper.java deleted file mode 100644 index b1d57f41fb78..000000000000 --- a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtCheckStorageAvailabilityWrapper.java +++ /dev/null @@ -1,59 +0,0 @@ -// -// Licensed to the Apache Software Foundation (ASF) under one -// or more contributor license agreements. See the NOTICE file -// distributed with this work for additional information -// regarding copyright ownership. The ASF licenses this file -// to you under the Apache License, Version 2.0 (the -// "License"); you may not use this file except in compliance -// with the License. You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, -// software distributed under the License is distributed on an -// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. See the License for the -// specific language governing permissions and limitations -// under the License. -// - -package com.cloud.hypervisor.kvm.resource.wrapper; - -import com.cloud.agent.api.Answer; -import com.cloud.agent.api.storage.CheckStorageAvailabilityCommand; -import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource; -import com.cloud.hypervisor.kvm.storage.KVMStoragePool; -import com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager; -import com.cloud.resource.CommandWrapper; -import com.cloud.resource.ResourceWrapper; -import com.cloud.storage.Storage; -import com.cloud.utils.exception.CloudRuntimeException; - -import java.util.Map; - -@ResourceWrapper(handles = CheckStorageAvailabilityCommand.class) -public class LibvirtCheckStorageAvailabilityWrapper extends CommandWrapper { - - - @Override - public Answer execute(CheckStorageAvailabilityCommand command, LibvirtComputingResource resource) { - KVMStoragePoolManager storagePoolMgr = resource.getStoragePoolMgr(); - Map poolsMap = command.getPoolsMap(); - - for (String poolUuid : poolsMap.keySet()) { - Storage.StoragePoolType type = poolsMap.get(poolUuid); - logger.debug("Checking if storage pool " + poolUuid + " (" + type + ") is mounted on this host"); - try { - KVMStoragePool storagePool = storagePoolMgr.getStoragePool(type, poolUuid); - if (storagePool == null) { - logger.info("Storage pool " + poolUuid + " is not available"); - return new Answer(command, false, "Storage pool " + poolUuid + " not available"); - } - } catch (CloudRuntimeException e) { - logger.info("Storage pool " + poolUuid + " is not available"); - return new Answer(command, e); - } - } - return new Answer(command); - } -} diff --git a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtConsoleProxyLoadCommandWrapper.java b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtConsoleProxyLoadCommandWrapper.java deleted file mode 100644 index ecfa062ed887..000000000000 --- a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtConsoleProxyLoadCommandWrapper.java +++ /dev/null @@ -1,76 +0,0 @@ -// -// Licensed to the Apache Software Foundation (ASF) under one -// or more contributor license agreements. See the NOTICE file -// distributed with this work for additional information -// regarding copyright ownership. The ASF licenses this file -// to you under the Apache License, Version 2.0 (the -// "License"); you may not use this file except in compliance -// with the License. You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, -// software distributed under the License is distributed on an -// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. See the License for the -// specific language governing permissions and limitations -// under the License. -// - -package com.cloud.hypervisor.kvm.resource.wrapper; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.net.URL; -import java.net.URLConnection; - - -import com.cloud.agent.api.Answer; -import com.cloud.agent.api.Command; -import com.cloud.agent.api.proxy.ConsoleProxyLoadAnswer; -import com.cloud.resource.CommandWrapper; -import com.cloud.resource.ServerResource; - -public abstract class LibvirtConsoleProxyLoadCommandWrapper extends CommandWrapper { - - - public Answer executeProxyLoadScan(final Command cmd, final long proxyVmId, final String proxyVmName, final String proxyManagementIp, final int cmdPort) { - String result = null; - - final StringBuffer sb = new StringBuffer(); - sb.append("http://").append(proxyManagementIp).append(":" + cmdPort).append("/cmd/getstatus"); - - boolean success = true; - try { - final URL url = new URL(sb.toString()); - final URLConnection conn = url.openConnection(); - - final InputStream is = conn.getInputStream(); - final BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8")); - final StringBuilder sb2 = new StringBuilder(); - String line = null; - try { - while ((line = reader.readLine()) != null) { - sb2.append(line + "\n"); - } - result = sb2.toString(); - } catch (final IOException e) { - success = false; - } finally { - try { - is.close(); - } catch (final IOException e) { - logger.warn("Exception when closing , console proxy address : " + proxyManagementIp); - success = false; - } - } - } catch (final IOException e) { - logger.warn("Unable to open console proxy command port url, console proxy address : " + proxyManagementIp); - success = false; - } - - return new ConsoleProxyLoadAnswer(cmd, proxyVmId, proxyVmName, success, result); - } -} diff --git a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtCreateCommandWrapper.java b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtCreateCommandWrapper.java deleted file mode 100644 index 5ec00889df96..000000000000 --- a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtCreateCommandWrapper.java +++ /dev/null @@ -1,85 +0,0 @@ -// -// Licensed to the Apache Software Foundation (ASF) under one -// or more contributor license agreements. See the NOTICE file -// distributed with this work for additional information -// regarding copyright ownership. The ASF licenses this file -// to you under the Apache License, Version 2.0 (the -// "License"); you may not use this file except in compliance -// with the License. You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, -// software distributed under the License is distributed on an -// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. See the License for the -// specific language governing permissions and limitations -// under the License. -// - -package com.cloud.hypervisor.kvm.resource.wrapper; - - -import com.cloud.agent.api.Answer; -import com.cloud.agent.api.storage.CreateAnswer; -import com.cloud.agent.api.storage.CreateCommand; -import com.cloud.agent.api.to.StorageFilerTO; -import com.cloud.agent.api.to.VolumeTO; -import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource; -import com.cloud.hypervisor.kvm.storage.KVMPhysicalDisk; -import com.cloud.hypervisor.kvm.storage.KVMStoragePool; -import com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager; -import com.cloud.resource.CommandWrapper; -import com.cloud.resource.ResourceWrapper; -import com.cloud.storage.Storage.StoragePoolType; -import com.cloud.utils.exception.CloudRuntimeException; -import com.cloud.vm.DiskProfile; - -@ResourceWrapper(handles = CreateCommand.class) -public final class LibvirtCreateCommandWrapper extends CommandWrapper { - - - @Override - public Answer execute(final CreateCommand command, final LibvirtComputingResource libvirtComputingResource) { - final StorageFilerTO pool = command.getPool(); - final DiskProfile dskch = command.getDiskCharacteristics(); - KVMPhysicalDisk baseVol = null; - KVMStoragePool primaryPool = null; - KVMPhysicalDisk vol = null; - long disksize; - try { - final KVMStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr(); - primaryPool = storagePoolMgr.getStoragePool(pool.getType(), pool.getUuid()); - disksize = dskch.getSize(); - - if (command.getTemplateUrl() != null) { - if (primaryPool.getType() == StoragePoolType.CLVM) { - vol = libvirtComputingResource.templateToPrimaryDownload(command.getTemplateUrl(), primaryPool, dskch.getPath()); - } else { - baseVol = primaryPool.getPhysicalDisk(command.getTemplateUrl()); - vol = storagePoolMgr.createDiskFromTemplate(baseVol, dskch.getPath(), dskch.getProvisioningType(), primaryPool, baseVol.getSize(), 0, null); - } - if (vol == null) { - return new Answer(command, false, " Can't create storage volume on storage pool"); - } - } else { - vol = primaryPool.createPhysicalDisk(dskch.getPath(), dskch.getProvisioningType(), dskch.getSize(), null); - if (vol == null) { - return new Answer(command, false, " Can't create Physical Disk"); - } - } - final VolumeTO volume = new VolumeTO(command.getVolumeId(), dskch.getType(), pool.getType(), pool.getUuid(), pool.getPath(), vol.getName(), vol.getName(), disksize, - null); - - volume.setBytesReadRate(dskch.getBytesReadRate()); - volume.setBytesWriteRate(dskch.getBytesWriteRate()); - volume.setIopsReadRate(dskch.getIopsReadRate()); - volume.setIopsWriteRate(dskch.getIopsWriteRate()); - volume.setCacheMode(dskch.getCacheMode()); - return new CreateAnswer(command, volume); - } catch (final CloudRuntimeException e) { - logger.debug("Failed to create volume: " + e.toString()); - return new CreateAnswer(command, e); - } - } -} diff --git a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtCreatePrivateTemplateFromSnapshotCommandWrapper.java b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtCreatePrivateTemplateFromSnapshotCommandWrapper.java deleted file mode 100644 index b05d6f06d742..000000000000 --- a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtCreatePrivateTemplateFromSnapshotCommandWrapper.java +++ /dev/null @@ -1,113 +0,0 @@ -// -// Licensed to the Apache Software Foundation (ASF) under one -// or more contributor license agreements. See the NOTICE file -// distributed with this work for additional information -// regarding copyright ownership. The ASF licenses this file -// to you under the Apache License, Version 2.0 (the -// "License"); you may not use this file except in compliance -// with the License. You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, -// software distributed under the License is distributed on an -// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. See the License for the -// specific language governing permissions and limitations -// under the License. -// - -package com.cloud.hypervisor.kvm.resource.wrapper; - -import java.io.File; -import java.io.IOException; - -import javax.naming.ConfigurationException; - - -import com.cloud.agent.api.Answer; -import com.cloud.agent.api.CreatePrivateTemplateFromSnapshotCommand; -import com.cloud.agent.api.storage.CreatePrivateTemplateAnswer; -import com.cloud.exception.InternalErrorException; -import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource; -import com.cloud.hypervisor.kvm.storage.KVMPhysicalDisk; -import com.cloud.hypervisor.kvm.storage.KVMStoragePool; -import com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager; -import com.cloud.resource.CommandWrapper; -import com.cloud.resource.ResourceWrapper; -import com.cloud.storage.StorageLayer; -import com.cloud.storage.template.Processor; -import com.cloud.storage.template.Processor.FormatInfo; -import com.cloud.storage.template.TemplateLocation; -import com.cloud.utils.exception.CloudRuntimeException; -import com.cloud.utils.script.Script; - -@ResourceWrapper(handles = CreatePrivateTemplateFromSnapshotCommand.class) -public final class LibvirtCreatePrivateTemplateFromSnapshotCommandWrapper extends CommandWrapper { - - - @Override - public Answer execute(final CreatePrivateTemplateFromSnapshotCommand command, final LibvirtComputingResource libvirtComputingResource) { - final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper(); - - final String templateFolder = command.getAccountId() + File.separator + command.getNewTemplateId(); - final String templateInstallFolder = "template/tmpl/" + templateFolder; - final String tmplName = libvirtUtilitiesHelper.generateUUIDName(); - final String tmplFileName = tmplName + ".qcow2"; - - KVMStoragePool secondaryPool = null; - KVMStoragePool snapshotPool = null; - final KVMStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr(); - - try { - String snapshotPath = command.getSnapshotUuid(); - final int index = snapshotPath.lastIndexOf("/"); - snapshotPath = snapshotPath.substring(0, index); - - snapshotPool = storagePoolMgr.getStoragePoolByURI(command.getSecondaryStorageUrl() + snapshotPath); - secondaryPool = storagePoolMgr.getStoragePoolByURI(command.getSecondaryStorageUrl()); - - final KVMPhysicalDisk snapshot = snapshotPool.getPhysicalDisk(command.getSnapshotName()); - - final String templatePath = secondaryPool.getLocalPath() + File.separator + templateInstallFolder; - - final StorageLayer storage = libvirtComputingResource.getStorage(); - storage.mkdirs(templatePath); - - final String tmplPath = templateInstallFolder + File.separator + tmplFileName; - final String createTmplPath = libvirtComputingResource.createTmplPath(); - final int cmdsTimeout = libvirtComputingResource.getCmdsTimeout(); - - final Script scriptCommand = new Script(createTmplPath, cmdsTimeout, logger); - scriptCommand.add("-t", templatePath); - scriptCommand.add("-n", tmplFileName); - scriptCommand.add("-f", snapshot.getPath()); - scriptCommand.execute(); - - final Processor qcow2Processor = libvirtUtilitiesHelper.buildQCOW2Processor(storage); - final FormatInfo info = qcow2Processor.process(templatePath, null, tmplName); - final TemplateLocation loc = libvirtUtilitiesHelper.buildTemplateLocation(storage, templatePath); - - loc.create(1, true, tmplName); - loc.addFormat(info); - loc.save(); - - return new CreatePrivateTemplateAnswer(command, true, "", tmplPath, info.virtualSize, info.size, tmplName, info.format); - } catch (final ConfigurationException e) { - return new CreatePrivateTemplateAnswer(command, false, e.getMessage()); - } catch (final InternalErrorException e) { - return new CreatePrivateTemplateAnswer(command, false, e.getMessage()); - } catch (final IOException e) { - return new CreatePrivateTemplateAnswer(command, false, e.getMessage()); - } catch (final CloudRuntimeException e) { - return new CreatePrivateTemplateAnswer(command, false, e.getMessage()); - } finally { - if (secondaryPool != null) { - storagePoolMgr.deleteStoragePool(secondaryPool.getType(), secondaryPool.getUuid()); - } - if (snapshotPool != null) { - storagePoolMgr.deleteStoragePool(snapshotPool.getType(), snapshotPool.getUuid()); - } - } - } -} diff --git a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtCreatePrivateTemplateFromVolumeCommandWrapper.java b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtCreatePrivateTemplateFromVolumeCommandWrapper.java deleted file mode 100644 index 9fb282866cbd..000000000000 --- a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtCreatePrivateTemplateFromVolumeCommandWrapper.java +++ /dev/null @@ -1,175 +0,0 @@ -// -// Licensed to the Apache Software Foundation (ASF) under one -// or more contributor license agreements. See the NOTICE file -// distributed with this work for additional information -// regarding copyright ownership. The ASF licenses this file -// to you under the Apache License, Version 2.0 (the -// "License"); you may not use this file except in compliance -// with the License. You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, -// software distributed under the License is distributed on an -// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. See the License for the -// specific language governing permissions and limitations -// under the License. -// - -package com.cloud.hypervisor.kvm.resource.wrapper; - -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.text.DateFormat; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.HashMap; -import java.util.Map; - -import javax.naming.ConfigurationException; - -import org.apache.cloudstack.utils.qemu.QemuImg; -import org.apache.cloudstack.utils.qemu.QemuImg.PhysicalDiskFormat; -import org.apache.cloudstack.utils.qemu.QemuImgException; -import org.apache.cloudstack.utils.qemu.QemuImgFile; - -import com.cloud.agent.api.Answer; -import com.cloud.agent.api.CreatePrivateTemplateFromVolumeCommand; -import com.cloud.agent.api.storage.CreatePrivateTemplateAnswer; -import com.cloud.exception.InternalErrorException; -import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource; -import com.cloud.hypervisor.kvm.storage.KVMPhysicalDisk; -import com.cloud.hypervisor.kvm.storage.KVMStoragePool; -import com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager; -import com.cloud.resource.CommandWrapper; -import com.cloud.resource.ResourceWrapper; -import com.cloud.storage.Storage.ImageFormat; -import com.cloud.storage.Storage.StoragePoolType; -import com.cloud.storage.StorageLayer; -import com.cloud.storage.template.Processor; -import com.cloud.storage.template.Processor.FormatInfo; -import com.cloud.storage.template.QCOW2Processor; -import com.cloud.storage.template.TemplateLocation; -import com.cloud.utils.exception.CloudRuntimeException; -import com.cloud.utils.script.Script; -import org.libvirt.LibvirtException; - -@ResourceWrapper(handles = CreatePrivateTemplateFromVolumeCommand.class) -public final class LibvirtCreatePrivateTemplateFromVolumeCommandWrapper extends CommandWrapper { - - - @Override - public Answer execute(final CreatePrivateTemplateFromVolumeCommand command, final LibvirtComputingResource libvirtComputingResource) { - final String secondaryStorageURL = command.getSecondaryStorageUrl(); - - KVMStoragePool secondaryStorage = null; - KVMStoragePool primary = null; - final KVMStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr(); - try { - final String templateFolder = command.getAccountId() + File.separator + command.getTemplateId() + File.separator; - final String templateInstallFolder = "/template/tmpl/" + templateFolder; - - secondaryStorage = storagePoolMgr.getStoragePoolByURI(secondaryStorageURL); - - try { - primary = storagePoolMgr.getStoragePool(command.getPool().getType(), command.getPrimaryStoragePoolNameLabel()); - } catch (final CloudRuntimeException e) { - if (e.getMessage().contains("not found")) { - primary = - storagePoolMgr.createStoragePool(command.getPool().getUuid(), command.getPool().getHost(), command.getPool().getPort(), command.getPool().getPath(), - command.getPool().getUserInfo(), command.getPool().getType()); - } else { - return new CreatePrivateTemplateAnswer(command, false, e.getMessage()); - } - } - - final KVMPhysicalDisk disk = primary.getPhysicalDisk(command.getVolumePath()); - final String tmpltPath = secondaryStorage.getLocalPath() + File.separator + templateInstallFolder; - final StorageLayer storage = libvirtComputingResource.getStorage(); - storage.mkdirs(tmpltPath); - - if (primary.getType() != StoragePoolType.RBD) { - final String createTmplPath = libvirtComputingResource.createTmplPath(); - final int cmdsTimeout = libvirtComputingResource.getCmdsTimeout(); - - final Script scriptCommand = new Script(createTmplPath, cmdsTimeout, logger); - scriptCommand.add("-f", disk.getPath()); - scriptCommand.add("-t", tmpltPath); - scriptCommand.add("-n", command.getUniqueName() + ".qcow2"); - - final String result = scriptCommand.execute(); - - if (result != null) { - logger.debug("failed to create template: " + result); - return new CreatePrivateTemplateAnswer(command, false, result); - } - } else { - logger.debug("Converting RBD disk " + disk.getPath() + " into template " + command.getUniqueName()); - - final QemuImgFile srcFile = new QemuImgFile(KVMPhysicalDisk.RBDStringBuilder(primary, disk.getPath())); - srcFile.setFormat(PhysicalDiskFormat.RAW); - - final QemuImgFile destFile = new QemuImgFile(tmpltPath + "/" + command.getUniqueName() + ".qcow2"); - destFile.setFormat(PhysicalDiskFormat.QCOW2); - - try { - final QemuImg q = new QemuImg(0); - q.convert(srcFile, destFile); - } catch (final QemuImgException | LibvirtException e) { - logger.error("Failed to create new template while converting " + srcFile.getFileName() + " to " + destFile.getFileName() + " the error was: " + - e.getMessage()); - } - - final File templateProp = new File(tmpltPath + "/template.properties"); - if (!templateProp.exists()) { - templateProp.createNewFile(); - } - - String templateContent = "filename=" + command.getUniqueName() + ".qcow2" + System.getProperty("line.separator"); - - final DateFormat dateFormat = new SimpleDateFormat("MM_dd_yyyy"); - final Date date = new Date(); - templateContent += "snapshot.name=" + dateFormat.format(date) + System.getProperty("line.separator"); - - try(FileOutputStream templFo = new FileOutputStream(templateProp);) { - templFo.write(templateContent.getBytes("UTF-8")); - templFo.flush(); - }catch(final IOException ex) - { - logger.error("CreatePrivateTemplateAnswer:Exception:"+ex.getMessage()); - } - - } - - final Map params = new HashMap(); - params.put(StorageLayer.InstanceConfigKey, storage); - final Processor qcow2Processor = new QCOW2Processor(); - - qcow2Processor.configure("QCOW2 Processor", params); - - final FormatInfo info = qcow2Processor.process(tmpltPath, null, command.getUniqueName()); - - final TemplateLocation loc = new TemplateLocation(storage, tmpltPath); - loc.create(1, true, command.getUniqueName()); - loc.addFormat(info); - loc.save(); - - return new CreatePrivateTemplateAnswer(command, true, null, templateInstallFolder + command.getUniqueName() + ".qcow2", info.virtualSize, info.size, - command.getUniqueName(), ImageFormat.QCOW2); - } catch (final InternalErrorException e) { - return new CreatePrivateTemplateAnswer(command, false, e.toString()); - } catch (final IOException e) { - return new CreatePrivateTemplateAnswer(command, false, e.toString()); - } catch (final ConfigurationException e) { - return new CreatePrivateTemplateAnswer(command, false, e.toString()); - } catch (final CloudRuntimeException e) { - return new CreatePrivateTemplateAnswer(command, false, e.toString()); - } finally { - if (secondaryStorage != null) { - storagePoolMgr.deleteStoragePool(secondaryStorage.getType(), secondaryStorage.getUuid()); - } - } - } -} diff --git a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtCreateVolumeFromSnapshotCommandWrapper.java b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtCreateVolumeFromSnapshotCommandWrapper.java deleted file mode 100644 index ba47b754a09b..000000000000 --- a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtCreateVolumeFromSnapshotCommandWrapper.java +++ /dev/null @@ -1,70 +0,0 @@ -// -// Licensed to the Apache Software Foundation (ASF) under one -// or more contributor license agreements. See the NOTICE file -// distributed with this work for additional information -// regarding copyright ownership. The ASF licenses this file -// to you under the Apache License, Version 2.0 (the -// "License"); you may not use this file except in compliance -// with the License. You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, -// software distributed under the License is distributed on an -// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. See the License for the -// specific language governing permissions and limitations -// under the License. -// - -package com.cloud.hypervisor.kvm.resource.wrapper; - -import java.util.UUID; - -import com.cloud.agent.api.Answer; -import com.cloud.agent.api.CreateVolumeFromSnapshotAnswer; -import com.cloud.agent.api.CreateVolumeFromSnapshotCommand; -import com.cloud.agent.api.to.StorageFilerTO; -import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource; -import com.cloud.hypervisor.kvm.storage.KVMPhysicalDisk; -import com.cloud.hypervisor.kvm.storage.KVMStoragePool; -import com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager; -import com.cloud.resource.CommandWrapper; -import com.cloud.resource.ResourceWrapper; -import com.cloud.utils.exception.CloudRuntimeException; - -@ResourceWrapper(handles = CreateVolumeFromSnapshotCommand.class) -public final class LibvirtCreateVolumeFromSnapshotCommandWrapper extends CommandWrapper { - - @Override - public Answer execute(final CreateVolumeFromSnapshotCommand command, final LibvirtComputingResource libvirtComputingResource) { - try { - - String snapshotPath = command.getSnapshotUuid(); - final int index = snapshotPath.lastIndexOf("/"); - snapshotPath = snapshotPath.substring(0, index); - - final KVMStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr(); - final KVMStoragePool secondaryPool = storagePoolMgr.getStoragePoolByURI(command.getSecondaryStorageUrl() + snapshotPath); - final KVMPhysicalDisk snapshot = secondaryPool.getPhysicalDisk(command.getSnapshotName()); - - final String primaryUuid = command.getPrimaryStoragePoolNameLabel(); - - final StorageFilerTO pool = command.getPool(); - final KVMStoragePool primaryPool = storagePoolMgr.getStoragePool(pool.getType(), primaryUuid); - - final String volUuid = UUID.randomUUID().toString(); - final KVMPhysicalDisk disk = storagePoolMgr.copyPhysicalDisk(snapshot, volUuid, primaryPool, 0); - - if (disk == null) { - throw new NullPointerException("Disk was not successfully copied to the new storage."); - } - - return new CreateVolumeFromSnapshotAnswer(command, true, "", disk.getName()); - } catch (final CloudRuntimeException e) { - return new CreateVolumeFromSnapshotAnswer(command, false, e.toString(), null); - } catch (final Exception e) { - return new CreateVolumeFromSnapshotAnswer(command, false, e.toString(), null); - } - } -} diff --git a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtManageSnapshotCommandWrapper.java b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtManageSnapshotCommandWrapper.java deleted file mode 100644 index ec900e9981e0..000000000000 --- a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtManageSnapshotCommandWrapper.java +++ /dev/null @@ -1,167 +0,0 @@ -// -// Licensed to the Apache Software Foundation (ASF) under one -// or more contributor license agreements. See the NOTICE file -// distributed with this work for additional information -// regarding copyright ownership. The ASF licenses this file -// to you under the Apache License, Version 2.0 (the -// "License"); you may not use this file except in compliance -// with the License. You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, -// software distributed under the License is distributed on an -// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. See the License for the -// specific language governing permissions and limitations -// under the License. -// - -package com.cloud.hypervisor.kvm.resource.wrapper; - -import java.io.File; -import java.text.MessageFormat; - -import org.libvirt.Connect; -import org.libvirt.Domain; -import org.libvirt.DomainInfo.DomainState; -import org.libvirt.DomainSnapshot; -import org.libvirt.LibvirtException; - -import com.ceph.rados.IoCTX; -import com.ceph.rados.Rados; -import com.ceph.rbd.Rbd; -import com.ceph.rbd.RbdImage; -import com.cloud.agent.api.Answer; -import com.cloud.agent.api.ManageSnapshotAnswer; -import com.cloud.agent.api.ManageSnapshotCommand; -import com.cloud.agent.api.to.StorageFilerTO; -import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource; -import com.cloud.hypervisor.kvm.storage.KVMPhysicalDisk; -import com.cloud.hypervisor.kvm.storage.KVMStoragePool; -import com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager; -import com.cloud.resource.CommandWrapper; -import com.cloud.resource.ResourceWrapper; -import com.cloud.storage.Storage.StoragePoolType; -import com.cloud.utils.script.Script; - -@ResourceWrapper(handles = ManageSnapshotCommand.class) -public final class LibvirtManageSnapshotCommandWrapper extends CommandWrapper { - - - @Override - public Answer execute(final ManageSnapshotCommand command, final LibvirtComputingResource libvirtComputingResource) { - final String snapshotName = command.getSnapshotName(); - final String snapshotPath = command.getSnapshotPath(); - final String vmName = command.getVmName(); - try { - final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper(); - final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(vmName); - DomainState state = null; - Domain vm = null; - if (vmName != null) { - try { - vm = libvirtComputingResource.getDomain(conn, command.getVmName()); - state = vm.getInfo().state; - } catch (final LibvirtException e) { - logger.trace("Ignoring libvirt error.", e); - } - } - - final KVMStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr(); - final StorageFilerTO pool = command.getPool(); - final KVMStoragePool primaryPool = storagePoolMgr.getStoragePool(pool.getType(), pool.getUuid()); - - final KVMPhysicalDisk disk = primaryPool.getPhysicalDisk(command.getVolumePath()); - if (state == DomainState.VIR_DOMAIN_RUNNING && !primaryPool.isExternalSnapshot()) { - - final MessageFormat snapshotXML = new MessageFormat(" " + " {0}" + " " - + " {1}" + " " + " "); - - final String vmUuid = vm.getUUIDString(); - final Object[] args = new Object[] {snapshotName, vmUuid}; - final String snapshot = snapshotXML.format(args); - logger.debug(snapshot); - if (command.getCommandSwitch().equalsIgnoreCase(ManageSnapshotCommand.CREATE_SNAPSHOT)) { - vm.snapshotCreateXML(snapshot); - } else { - final DomainSnapshot snap = vm.snapshotLookupByName(snapshotName); - snap.delete(0); - } - - /* - * libvirt on RHEL6 doesn't handle resume event emitted from - * qemu - */ - vm = libvirtComputingResource.getDomain(conn, command.getVmName()); - state = vm.getInfo().state; - if (state == DomainState.VIR_DOMAIN_PAUSED) { - vm.resume(); - } - } else { - /** - * For RBD we can't use libvirt to do our snapshotting or any Bash scripts. - * libvirt also wants to store the memory contents of the Virtual Machine, - * but that's not possible with RBD since there is no way to store the memory - * contents in RBD. - * - * So we rely on the Java bindings for RBD to create our snapshot - * - * This snapshot might not be 100% consistent due to writes still being in the - * memory of the Virtual Machine, but if the VM runs a kernel which supports - * barriers properly (>2.6.32) this won't be any different then pulling the power - * cord out of a running machine. - */ - if (primaryPool.getType() == StoragePoolType.RBD) { - try { - final Rados r = new Rados(primaryPool.getAuthUserName()); - r.confSet("mon_host", primaryPool.getSourceHost() + ":" + primaryPool.getSourcePort()); - r.confSet("key", primaryPool.getAuthSecret()); - r.confSet("client_mount_timeout", "30"); - r.connect(); - logger.debug("Successfully connected to Ceph cluster at " + r.confGet("mon_host")); - - final IoCTX io = r.ioCtxCreate(primaryPool.getSourceDir()); - final Rbd rbd = new Rbd(io); - final RbdImage image = rbd.open(disk.getName()); - - if (command.getCommandSwitch().equalsIgnoreCase(ManageSnapshotCommand.CREATE_SNAPSHOT)) { - logger.debug("Attempting to create RBD snapshot " + disk.getName() + "@" + snapshotName); - image.snapCreate(snapshotName); - } else { - logger.debug("Attempting to remove RBD snapshot " + disk.getName() + "@" + snapshotName); - image.snapRemove(snapshotName); - } - - rbd.close(image); - r.ioCtxDestroy(io); - } catch (final Exception e) { - logger.error("A RBD snapshot operation on " + disk.getName() + " failed. The error was: " + e.getMessage()); - } - } else { - /* VM is not running, create a snapshot by ourself */ - final int cmdsTimeout = libvirtComputingResource.getCmdsTimeout(); - final String manageSnapshotPath = libvirtComputingResource.manageSnapshotPath(); - - final Script scriptCommand = new Script(manageSnapshotPath, cmdsTimeout, logger); - if (command.getCommandSwitch().equalsIgnoreCase(ManageSnapshotCommand.CREATE_SNAPSHOT)) { - scriptCommand.add("-c", disk.getPath()); - } else { - scriptCommand.add("-d", snapshotPath); - } - - scriptCommand.add("-n", snapshotName); - final String result = scriptCommand.execute(); - if (result != null) { - logger.debug("Failed to manage snapshot: " + result); - return new ManageSnapshotAnswer(command, false, "Failed to manage snapshot: " + result); - } - } - } - return new ManageSnapshotAnswer(command, command.getSnapshotId(), disk.getPath() + File.separator + snapshotName, true, null); - } catch (final LibvirtException e) { - logger.debug("Failed to manage snapshot: " + e.toString()); - return new ManageSnapshotAnswer(command, false, "Failed to manage snapshot: " + e.toString()); - } - } -} diff --git a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtPrimaryStorageDownloadCommandWrapper.java b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtPrimaryStorageDownloadCommandWrapper.java deleted file mode 100644 index 7966062d9f0c..000000000000 --- a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtPrimaryStorageDownloadCommandWrapper.java +++ /dev/null @@ -1,90 +0,0 @@ -// -// Licensed to the Apache Software Foundation (ASF) under one -// or more contributor license agreements. See the NOTICE file -// distributed with this work for additional information -// regarding copyright ownership. The ASF licenses this file -// to you under the Apache License, Version 2.0 (the -// "License"); you may not use this file except in compliance -// with the License. You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, -// software distributed under the License is distributed on an -// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. See the License for the -// specific language governing permissions and limitations -// under the License. -// - -package com.cloud.hypervisor.kvm.resource.wrapper; - -import java.util.List; -import java.util.UUID; - -import com.cloud.agent.api.Answer; -import com.cloud.agent.api.storage.PrimaryStorageDownloadAnswer; -import com.cloud.agent.api.storage.PrimaryStorageDownloadCommand; -import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource; -import com.cloud.hypervisor.kvm.storage.KVMPhysicalDisk; -import com.cloud.hypervisor.kvm.storage.KVMStoragePool; -import com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager; -import com.cloud.resource.CommandWrapper; -import com.cloud.resource.ResourceWrapper; -import com.cloud.utils.exception.CloudRuntimeException; - -@ResourceWrapper(handles = PrimaryStorageDownloadCommand.class) -public final class LibvirtPrimaryStorageDownloadCommandWrapper extends CommandWrapper { - - @Override - public Answer execute(final PrimaryStorageDownloadCommand command, final LibvirtComputingResource libvirtComputingResource) { - final String tmplturl = command.getUrl(); - final int index = tmplturl.lastIndexOf("/"); - final String mountpoint = tmplturl.substring(0, index); - String tmpltname = null; - - if (index < tmplturl.length() - 1) { - tmpltname = tmplturl.substring(index + 1); - } - - KVMPhysicalDisk tmplVol = null; - KVMStoragePool secondaryPool = null; - final KVMStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr(); - try { - secondaryPool = storagePoolMgr.getStoragePoolByURI(mountpoint); - - /* Get template vol */ - if (tmpltname == null) { - secondaryPool.refresh(); - final List disks = secondaryPool.listPhysicalDisks(); - if (disks == null || disks.isEmpty()) { - return new PrimaryStorageDownloadAnswer("Failed to get volumes from pool: " + secondaryPool.getUuid()); - } - for (final KVMPhysicalDisk disk : disks) { - if (disk.getName().endsWith("qcow2")) { - tmplVol = disk; - break; - } - } - if (tmplVol == null) { - return new PrimaryStorageDownloadAnswer("Failed to get template from pool: " + secondaryPool.getUuid()); - } - } else { - tmplVol = secondaryPool.getPhysicalDisk(tmpltname); - } - - /* Copy volume to primary storage */ - final KVMStoragePool primaryPool = storagePoolMgr.getStoragePool(command.getPool().getType(), command.getPoolUuid()); - - final KVMPhysicalDisk primaryVol = storagePoolMgr.copyPhysicalDisk(tmplVol, UUID.randomUUID().toString(), primaryPool, 0); - - return new PrimaryStorageDownloadAnswer(primaryVol.getName(), primaryVol.getSize()); - } catch (final CloudRuntimeException e) { - return new PrimaryStorageDownloadAnswer(e.toString()); - } finally { - if (secondaryPool != null) { - storagePoolMgr.deleteStoragePool(secondaryPool.getType(), secondaryPool.getUuid()); - } - } - } -} diff --git a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtRebootRouterCommandWrapper.java b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtRebootRouterCommandWrapper.java deleted file mode 100644 index 67c902e0b49d..000000000000 --- a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtRebootRouterCommandWrapper.java +++ /dev/null @@ -1,49 +0,0 @@ -// -// Licensed to the Apache Software Foundation (ASF) under one -// or more contributor license agreements. See the NOTICE file -// distributed with this work for additional information -// regarding copyright ownership. The ASF licenses this file -// to you under the Apache License, Version 2.0 (the -// "License"); you may not use this file except in compliance -// with the License. You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, -// software distributed under the License is distributed on an -// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. See the License for the -// specific language governing permissions and limitations -// under the License. -// - -package com.cloud.hypervisor.kvm.resource.wrapper; - -import com.cloud.agent.api.Answer; -import com.cloud.agent.api.RebootCommand; -import com.cloud.agent.api.RebootRouterCommand; -import com.cloud.agent.resource.virtualnetwork.VirtualRoutingResource; -import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource; -import com.cloud.resource.CommandWrapper; -import com.cloud.resource.ResourceWrapper; - -@ResourceWrapper(handles = RebootRouterCommand.class) -public final class LibvirtRebootRouterCommandWrapper extends CommandWrapper { - - @Override - public Answer execute(final RebootRouterCommand command, final LibvirtComputingResource libvirtComputingResource) { - final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance(); - - final RebootCommand rebootCommand = new RebootCommand(command.getVmName(), true); - final Answer answer = wrapper.execute(rebootCommand, libvirtComputingResource); - - final VirtualRoutingResource virtualRouterResource = libvirtComputingResource.getVirtRouterResource(); - if (virtualRouterResource.connect(command.getPrivateIpAddress())) { - libvirtComputingResource.networkUsage(command.getPrivateIpAddress(), "create", null); - - return answer; - } else { - return new Answer(command, false, "Failed to connect to virtual router " + command.getVmName()); - } - } -} diff --git a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtUpgradeSnapshotCommandWrapper.java b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtUpgradeSnapshotCommandWrapper.java deleted file mode 100644 index fb617e1d110f..000000000000 --- a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtUpgradeSnapshotCommandWrapper.java +++ /dev/null @@ -1,35 +0,0 @@ -// -// Licensed to the Apache Software Foundation (ASF) under one -// or more contributor license agreements. See the NOTICE file -// distributed with this work for additional information -// regarding copyright ownership. The ASF licenses this file -// to you under the Apache License, Version 2.0 (the -// "License"); you may not use this file except in compliance -// with the License. You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, -// software distributed under the License is distributed on an -// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. See the License for the -// specific language governing permissions and limitations -// under the License. -// - -package com.cloud.hypervisor.kvm.resource.wrapper; - -import com.cloud.agent.api.Answer; -import com.cloud.agent.api.UpgradeSnapshotCommand; -import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource; -import com.cloud.resource.CommandWrapper; -import com.cloud.resource.ResourceWrapper; - -@ResourceWrapper(handles = UpgradeSnapshotCommand.class) -public final class LibvirtUpgradeSnapshotCommandWrapper extends CommandWrapper { - - @Override - public Answer execute(final UpgradeSnapshotCommand command, final LibvirtComputingResource libvirtComputingResource) { - return new Answer(command, true, "success"); - } -} diff --git a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtUtilitiesHelper.java b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtUtilitiesHelper.java index 70e7f074c879..ca12ab280ead 100644 --- a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtUtilitiesHelper.java +++ b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtUtilitiesHelper.java @@ -16,11 +16,8 @@ // under the License. package com.cloud.hypervisor.kvm.resource.wrapper; -import java.util.HashMap; -import java.util.Map; import java.util.UUID; -import javax.naming.ConfigurationException; import com.cloud.agent.api.to.DataTO; import com.cloud.hypervisor.kvm.storage.KVMStoragePool; @@ -34,10 +31,6 @@ import com.cloud.agent.api.VMSnapshotTO; import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource; import com.cloud.hypervisor.kvm.resource.LibvirtConnection; -import com.cloud.storage.StorageLayer; -import com.cloud.storage.template.Processor; -import com.cloud.storage.template.QCOW2Processor; -import com.cloud.storage.template.TemplateLocation; import com.cloud.utils.Pair; import com.cloud.utils.script.Script; @@ -63,21 +56,6 @@ public Connect getConnection() throws LibvirtException { return LibvirtConnection.getConnection(); } - public TemplateLocation buildTemplateLocation(final StorageLayer storage, final String templatePath) { - final TemplateLocation location = new TemplateLocation(storage, templatePath); - return location; - } - - public Processor buildQCOW2Processor(final StorageLayer storage) throws ConfigurationException { - final Map params = new HashMap(); - params.put(StorageLayer.InstanceConfigKey, storage); - - final Processor qcow2Processor = new QCOW2Processor(); - qcow2Processor.configure("QCOW2 Processor", params); - - return qcow2Processor; - } - public String generateUUIDName() { return UUID.randomUUID().toString(); } @@ -98,10 +76,6 @@ public String retrieveSshPrvKeyPath() { return LibvirtComputingResource.SSHPRVKEYPATH; } - public String retrieveBashScriptPath() { - return LibvirtComputingResource.BASH_SCRIPT_PATH; - } - public Connect retrieveQemuConnection(final String qemuURI) throws LibvirtException { return new Connect(qemuURI); } diff --git a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtWatchConsoleProxyLoadCommandWrapper.java b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtWatchConsoleProxyLoadCommandWrapper.java deleted file mode 100644 index fd2b0205d45a..000000000000 --- a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtWatchConsoleProxyLoadCommandWrapper.java +++ /dev/null @@ -1,43 +0,0 @@ -// -// Licensed to the Apache Software Foundation (ASF) under one -// or more contributor license agreements. See the NOTICE file -// distributed with this work for additional information -// regarding copyright ownership. The ASF licenses this file -// to you under the Apache License, Version 2.0 (the -// "License"); you may not use this file except in compliance -// with the License. You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, -// software distributed under the License is distributed on an -// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. See the License for the -// specific language governing permissions and limitations -// under the License. -// - -package com.cloud.hypervisor.kvm.resource.wrapper; - -import com.cloud.agent.api.Answer; -import com.cloud.agent.api.Command; -import com.cloud.agent.api.proxy.WatchConsoleProxyLoadCommand; -import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource; -import com.cloud.resource.ResourceWrapper; -import com.cloud.resource.ServerResource; - -@ResourceWrapper(handles = WatchConsoleProxyLoadCommand.class) -public class LibvirtWatchConsoleProxyLoadCommandWrapper extends LibvirtConsoleProxyLoadCommandWrapper { - - @Override - public Answer execute(final Command command, final ServerResource serverResource) { - final WatchConsoleProxyLoadCommand cmd = (WatchConsoleProxyLoadCommand) command; - - final long proxyVmId = cmd.getProxyVmId(); - final String proxyVmName = cmd.getProxyVmName(); - final String proxyManagementIp = cmd.getProxyManagementIp(); - final int proxyCmdPort = cmd.getProxyCmdPort(); - - return executeProxyLoadScan(cmd, proxyVmId, proxyVmName, proxyManagementIp, proxyCmdPort); - } -} diff --git a/plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/LibvirtComputingResourceTest.java b/plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/LibvirtComputingResourceTest.java index 008d9444fe5f..1e23bfc6b24c 100644 --- a/plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/LibvirtComputingResourceTest.java +++ b/plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/LibvirtComputingResourceTest.java @@ -110,8 +110,6 @@ import org.xml.sax.SAXException; import com.cloud.agent.api.Answer; -import com.cloud.agent.api.AttachIsoCommand; -import com.cloud.agent.api.BackupSnapshotCommand; import com.cloud.agent.api.CheckHealthCommand; import com.cloud.agent.api.CheckNetworkCommand; import com.cloud.agent.api.CheckOnHostCommand; @@ -120,10 +118,7 @@ import com.cloud.agent.api.CheckVirtualMachineCommand; import com.cloud.agent.api.CleanupNetworkRulesCmd; import com.cloud.agent.api.Command; -import com.cloud.agent.api.CreatePrivateTemplateFromSnapshotCommand; -import com.cloud.agent.api.CreatePrivateTemplateFromVolumeCommand; import com.cloud.agent.api.CreateStoragePoolCommand; -import com.cloud.agent.api.CreateVolumeFromSnapshotCommand; import com.cloud.agent.api.DeleteStoragePoolCommand; import com.cloud.agent.api.FenceCommand; import com.cloud.agent.api.GetHostStatsCommand; @@ -132,7 +127,6 @@ import com.cloud.agent.api.GetVmStatsCommand; import com.cloud.agent.api.GetVncPortCommand; import com.cloud.agent.api.MaintainCommand; -import com.cloud.agent.api.ManageSnapshotCommand; import com.cloud.agent.api.MigrateCommand; import com.cloud.agent.api.ModifySshKeysCommand; import com.cloud.agent.api.ModifyStoragePoolCommand; @@ -156,7 +150,6 @@ import com.cloud.agent.api.PvlanSetupCommand; import com.cloud.agent.api.ReadyCommand; import com.cloud.agent.api.RebootCommand; -import com.cloud.agent.api.RebootRouterCommand; import com.cloud.agent.api.SecurityGroupRulesCmd; import com.cloud.agent.api.SecurityGroupRulesCmd.IpPortAndProto; import com.cloud.agent.api.StartCommand; @@ -164,16 +157,11 @@ import com.cloud.agent.api.UnPlugNicCommand; import com.cloud.agent.api.UnsupportedAnswer; import com.cloud.agent.api.UpdateHostPasswordCommand; -import com.cloud.agent.api.UpgradeSnapshotCommand; import com.cloud.agent.api.VgpuTypesInfo; import com.cloud.agent.api.VmStatsEntry; import com.cloud.agent.api.check.CheckSshCommand; -import com.cloud.agent.api.proxy.CheckConsoleProxyLoadCommand; -import com.cloud.agent.api.proxy.WatchConsoleProxyLoadCommand; import com.cloud.agent.api.storage.CopyVolumeCommand; -import com.cloud.agent.api.storage.CreateCommand; import com.cloud.agent.api.storage.DestroyCommand; -import com.cloud.agent.api.storage.PrimaryStorageDownloadCommand; import com.cloud.agent.api.storage.ResizeVolumeCommand; import com.cloud.agent.api.to.DataStoreTO; import com.cloud.agent.api.to.DiskTO; @@ -215,15 +203,10 @@ import com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager; import com.cloud.network.Networks.TrafficType; import com.cloud.network.PhysicalNetworkSetupInfo; -import com.cloud.storage.Storage.ImageFormat; import com.cloud.storage.Storage.StoragePoolType; -import com.cloud.storage.StorageLayer; import com.cloud.storage.StoragePool; import com.cloud.storage.Volume; import com.cloud.storage.resource.StorageSubsystemCommandHandler; -import com.cloud.storage.template.Processor; -import com.cloud.storage.template.Processor.FormatInfo; -import com.cloud.storage.template.TemplateLocation; import com.cloud.template.VirtualMachineTemplate.BootloaderType; import com.cloud.utils.Pair; import com.cloud.utils.exception.CloudRuntimeException; @@ -231,7 +214,6 @@ import com.cloud.utils.script.OutputInterpreter.OneLineParser; import com.cloud.utils.script.Script; import com.cloud.utils.ssh.SshHelper; -import com.cloud.vm.DiskProfile; import com.cloud.vm.VirtualMachine; import com.cloud.vm.VirtualMachine.PowerState; import com.cloud.vm.VirtualMachine.Type; @@ -1394,72 +1376,6 @@ public void testRebootCommandException2() { } } - @Test - public void testRebootRouterCommand() { - final VirtualRoutingResource routingResource = Mockito.mock(VirtualRoutingResource.class); - final Connect conn = Mockito.mock(Connect.class); - final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class); - - final String vmName = "Test"; - final RebootRouterCommand command = new RebootRouterCommand(vmName, "127.0.0.1"); - - when(libvirtComputingResourceMock.getVirtRouterResource()).thenReturn(routingResource); - when(libvirtComputingResourceMock.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper); - try { - when(libvirtUtilitiesHelper.getConnectionByVmName(vmName)).thenReturn(conn); - } catch (final LibvirtException e) { - fail(e.getMessage()); - } - - final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance(); - assertNotNull(wrapper); - - final Answer answer = wrapper.execute(command, libvirtComputingResourceMock); - assertFalse(answer.getResult()); - - verify(libvirtComputingResourceMock, times(1)).getVirtRouterResource(); - - verify(libvirtComputingResourceMock, times(1)).getLibvirtUtilitiesHelper(); - try { - verify(libvirtUtilitiesHelper, times(1)).getConnectionByVmName(vmName); - } catch (final LibvirtException e) { - fail(e.getMessage()); - } - } - - @Test - public void testRebootRouterCommandConnect() { - final VirtualRoutingResource routingResource = Mockito.mock(VirtualRoutingResource.class); - final Connect conn = Mockito.mock(Connect.class); - final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class); - - final String vmName = "Test"; - final RebootRouterCommand command = new RebootRouterCommand(vmName, "127.0.0.1"); - - when(libvirtComputingResourceMock.getVirtRouterResource()).thenReturn(routingResource); - when(libvirtComputingResourceMock.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper); - when(routingResource.connect(command.getPrivateIpAddress())).thenReturn(true); - try { - when(libvirtUtilitiesHelper.getConnectionByVmName(vmName)).thenReturn(conn); - } catch (final LibvirtException e) { - fail(e.getMessage()); - } - - final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance(); - assertNotNull(wrapper); - - final Answer answer = wrapper.execute(command, libvirtComputingResourceMock); - assertTrue(answer.getResult()); - - verify(libvirtComputingResourceMock, times(1)).getVirtRouterResource(); - verify(libvirtComputingResourceMock, times(1)).getLibvirtUtilitiesHelper(); - try { - verify(libvirtUtilitiesHelper, times(1)).getConnectionByVmName(vmName); - } catch (final LibvirtException e) { - fail(e.getMessage()); - } - } - @Test public void testGetHostStatsCommand() { // A bit difficult to test due to the logger being passed and the parser itself relying on the connection. @@ -1930,155 +1846,6 @@ public void testReadyCommand() { assertTrue(answer.getResult()); } - @Test - public void testAttachIsoCommand() { - final Connect conn = Mockito.mock(Connect.class); - final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class); - - final String vmName = "Test"; - final AttachIsoCommand command = new AttachIsoCommand(vmName, "/path", true); - - when(libvirtComputingResourceMock.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper); - try { - when(libvirtUtilitiesHelper.getConnectionByVmName(vmName)).thenReturn(conn); - } catch (final LibvirtException e) { - fail(e.getMessage()); - } - - final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance(); - assertNotNull(wrapper); - - final Answer answer = wrapper.execute(command, libvirtComputingResourceMock); - assertTrue(answer.getResult()); - - verify(libvirtComputingResourceMock, times(1)).getLibvirtUtilitiesHelper(); - try { - verify(libvirtUtilitiesHelper, times(1)).getConnectionByVmName(vmName); - } catch (final LibvirtException e) { - fail(e.getMessage()); - } - } - - @SuppressWarnings("unchecked") - @Test - public void testAttachIsoCommandLibvirtException() { - final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class); - - final String vmName = "Test"; - final AttachIsoCommand command = new AttachIsoCommand(vmName, "/path", true); - - when(libvirtComputingResourceMock.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper); - try { - when(libvirtUtilitiesHelper.getConnectionByVmName(vmName)).thenThrow(LibvirtException.class); - } catch (final LibvirtException e) { - fail(e.getMessage()); - } - - final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance(); - assertNotNull(wrapper); - - final Answer answer = wrapper.execute(command, libvirtComputingResourceMock); - assertFalse(answer.getResult()); - - verify(libvirtComputingResourceMock, times(1)).getLibvirtUtilitiesHelper(); - try { - verify(libvirtUtilitiesHelper, times(1)).getConnectionByVmName(vmName); - } catch (final LibvirtException e) { - fail(e.getMessage()); - } - } - - @SuppressWarnings("unchecked") - @Test - public void testAttachIsoCommandURISyntaxException() { - final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class); - - final String vmName = "Test"; - final AttachIsoCommand command = new AttachIsoCommand(vmName, "/path", true); - - when(libvirtComputingResourceMock.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper); - try { - BDDMockito.given(libvirtUtilitiesHelper.getConnectionByVmName(vmName)).willAnswer(invocationOnMock -> {throw new URISyntaxException("Exception trying to get connection by VM name", vmName);}); - } catch (final LibvirtException e) { - fail(e.getMessage()); - } - - final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance(); - assertNotNull(wrapper); - - final Answer answer = wrapper.execute(command, libvirtComputingResourceMock); - assertFalse(answer.getResult()); - - verify(libvirtComputingResourceMock, times(1)).getLibvirtUtilitiesHelper(); - try { - verify(libvirtUtilitiesHelper, times(1)).getConnectionByVmName(vmName); - } catch (final LibvirtException e) { - fail(e.getMessage()); - } - } - - @SuppressWarnings("unchecked") - @Test - public void testAttachIsoCommandInternalErrorException() { - final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class); - - final String vmName = "Test"; - final AttachIsoCommand command = new AttachIsoCommand(vmName, "/path", true); - - when(libvirtComputingResourceMock.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper); - try { - BDDMockito.given(libvirtUtilitiesHelper.getConnectionByVmName(vmName)).willAnswer(invocationOnMock -> {throw new InternalErrorException("Exception Occurred");}); - } catch (final LibvirtException e) { - fail(e.getMessage()); - } - - final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance(); - assertNotNull(wrapper); - - final Answer answer = wrapper.execute(command, libvirtComputingResourceMock); - assertFalse(answer.getResult()); - - verify(libvirtComputingResourceMock, times(1)).getLibvirtUtilitiesHelper(); - try { - verify(libvirtUtilitiesHelper, times(1)).getConnectionByVmName(vmName); - } catch (final LibvirtException e) { - fail(e.getMessage()); - } - } - - @Test - public void testWatchConsoleProxyLoadCommand() { - final int interval = 0; - final long proxyVmId = 0l; - final String proxyVmName = "host"; - final String proxyManagementIp = "127.0.0.1"; - final int proxyCmdPort = 0; - - final WatchConsoleProxyLoadCommand command = new WatchConsoleProxyLoadCommand(interval, proxyVmId, proxyVmName, proxyManagementIp, proxyCmdPort); - - final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance(); - assertNotNull(wrapper); - - final Answer answer = wrapper.execute(command, libvirtComputingResourceMock); - assertFalse(answer.getResult()); - } - - @Test - public void testCheckConsoleProxyLoadCommand() { - final long proxyVmId = 0l; - final String proxyVmName = "host"; - final String proxyManagementIp = "127.0.0.1"; - final int proxyCmdPort = 0; - - final CheckConsoleProxyLoadCommand command = new CheckConsoleProxyLoadCommand(proxyVmId, proxyVmName, proxyManagementIp, proxyCmdPort); - - final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance(); - assertNotNull(wrapper); - - final Answer answer = wrapper.execute(command, libvirtComputingResourceMock); - assertFalse(answer.getResult()); - } - @Test public void testGetVncPortCommand() { final Connect conn = Mockito.mock(Connect.class); @@ -2168,92 +1935,6 @@ public void testMaintainCommand() { assertTrue(answer.getResult()); } - @Test - public void testCreateCommandNoTemplate() { - final DiskProfile diskCharacteristics = Mockito.mock(DiskProfile.class); - final StorageFilerTO pool = Mockito.mock(StorageFilerTO.class); - final boolean executeInSequence = false; - - final CreateCommand command = new CreateCommand(diskCharacteristics, pool, executeInSequence ); - - final KVMStoragePoolManager poolManager = Mockito.mock(KVMStoragePoolManager.class); - final KVMStoragePool primary = Mockito.mock(KVMStoragePool.class); - final KVMPhysicalDisk vol = Mockito.mock(KVMPhysicalDisk.class); - - when(libvirtComputingResourceMock.getStoragePoolMgr()).thenReturn(poolManager); - when(poolManager.getStoragePool(pool.getType(), pool.getUuid())).thenReturn(primary); - - when(primary.createPhysicalDisk(diskCharacteristics.getPath(), diskCharacteristics.getProvisioningType(), diskCharacteristics.getSize(), null)).thenReturn(vol); - - final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance(); - assertNotNull(wrapper); - - final Answer answer = wrapper.execute(command, libvirtComputingResourceMock); - assertTrue(answer.getResult()); - - verify(libvirtComputingResourceMock, times(1)).getStoragePoolMgr(); - verify(poolManager, times(1)).getStoragePool(pool.getType(), pool.getUuid()); - } - - @Test - public void testCreateCommand() { - final DiskProfile diskCharacteristics = Mockito.mock(DiskProfile.class); - final StorageFilerTO pool = Mockito.mock(StorageFilerTO.class); - final String templateUrl = "http://template"; - final boolean executeInSequence = false; - - final CreateCommand command = new CreateCommand(diskCharacteristics, templateUrl, pool, executeInSequence ); - - final KVMStoragePoolManager poolManager = Mockito.mock(KVMStoragePoolManager.class); - final KVMStoragePool primary = Mockito.mock(KVMStoragePool.class); - final KVMPhysicalDisk vol = Mockito.mock(KVMPhysicalDisk.class); - - when(libvirtComputingResourceMock.getStoragePoolMgr()).thenReturn(poolManager); - when(poolManager.getStoragePool(pool.getType(), pool.getUuid())).thenReturn(primary); - - when(primary.getType()).thenReturn(StoragePoolType.CLVM); - when(libvirtComputingResourceMock.templateToPrimaryDownload(command.getTemplateUrl(), primary, diskCharacteristics.getPath())).thenReturn(vol); - - final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance(); - assertNotNull(wrapper); - - final Answer answer = wrapper.execute(command, libvirtComputingResourceMock); - assertTrue(answer.getResult()); - - verify(libvirtComputingResourceMock, times(1)).getStoragePoolMgr(); - verify(poolManager, times(1)).getStoragePool(pool.getType(), pool.getUuid()); - } - - @Test - public void testCreateCommandCLVM() { - final DiskProfile diskCharacteristics = Mockito.mock(DiskProfile.class); - final StorageFilerTO pool = Mockito.mock(StorageFilerTO.class); - final String templateUrl = "http://template"; - final boolean executeInSequence = false; - - final CreateCommand command = new CreateCommand(diskCharacteristics, templateUrl, pool, executeInSequence ); - - final KVMStoragePoolManager poolManager = Mockito.mock(KVMStoragePoolManager.class); - final KVMStoragePool primary = Mockito.mock(KVMStoragePool.class); - final KVMPhysicalDisk vol = Mockito.mock(KVMPhysicalDisk.class); - final KVMPhysicalDisk baseVol = Mockito.mock(KVMPhysicalDisk.class); - - when(libvirtComputingResourceMock.getStoragePoolMgr()).thenReturn(poolManager); - when(poolManager.getStoragePool(pool.getType(), pool.getUuid())).thenReturn(primary); - - when(primary.getPhysicalDisk(command.getTemplateUrl())).thenReturn(baseVol); - when(poolManager.createDiskFromTemplate(baseVol, diskCharacteristics.getPath(), diskCharacteristics.getProvisioningType(), primary, baseVol.getSize(), 0,null)).thenReturn(vol); - - final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance(); - assertNotNull(wrapper); - - final Answer answer = wrapper.execute(command, libvirtComputingResourceMock); - assertTrue(answer.getResult()); - - verify(libvirtComputingResourceMock, times(1)).getStoragePoolMgr(); - verify(poolManager, times(1)).getStoragePool(pool.getType(), pool.getUuid()); - } - @Test public void testDestroyCommand() { final StoragePool pool = Mockito.mock(StoragePool.class); @@ -2309,156 +1990,10 @@ public void testDestroyCommandError() { verify(poolManager, times(1)).getStoragePool(vol.getPoolType(), vol.getPoolUuid()); } - @Test(expected = NullPointerException.class) - public void testPrimaryStorageDownloadCommandNOTemplateDisk() { - final StoragePool pool = Mockito.mock(StoragePool.class); - - final List disks = new ArrayList(); - - final String name = "Test"; - final String url = "http://template/"; - final ImageFormat format = ImageFormat.QCOW2; - final long accountId = 1l; - final int wait = 0; - final PrimaryStorageDownloadCommand command = new PrimaryStorageDownloadCommand(name, url, format, accountId, pool, wait); - - final KVMStoragePoolManager storagePoolMgr = Mockito.mock(KVMStoragePoolManager.class); - final KVMStoragePool primaryPool = Mockito.mock(KVMStoragePool.class); - final KVMStoragePool secondaryPool = Mockito.mock(KVMStoragePool.class); - final KVMPhysicalDisk tmplVol = Mockito.mock(KVMPhysicalDisk.class); - final KVMPhysicalDisk primaryVol = Mockito.mock(KVMPhysicalDisk.class); - - final KVMPhysicalDisk disk = new KVMPhysicalDisk("/path", "disk.qcow2", primaryPool); - disks.add(disk); - - final int index = url.lastIndexOf("/"); - final String mountpoint = url.substring(0, index); - - when(libvirtComputingResourceMock.getStoragePoolMgr()).thenReturn(storagePoolMgr); - when(storagePoolMgr.getStoragePoolByURI(mountpoint)).thenReturn(secondaryPool); - when(secondaryPool.listPhysicalDisks()).thenReturn(disks); - when(storagePoolMgr.getStoragePool(command.getPool().getType(), command.getPoolUuid())).thenReturn(primaryPool); - - final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance(); - assertNotNull(wrapper); - - final Answer answer = wrapper.execute(command, libvirtComputingResourceMock); - assertFalse(answer.getResult()); - - verify(libvirtComputingResourceMock, times(1)).getStoragePoolMgr(); - } - - @Test - public void testPrimaryStorageDownloadCommandNOTemplateNODisk() { - final StoragePool pool = Mockito.mock(StoragePool.class); - - final List disks = new ArrayList(); - - final String name = "Test"; - final String url = "http://template/"; - final ImageFormat format = ImageFormat.QCOW2; - final long accountId = 1l; - final int wait = 0; - final PrimaryStorageDownloadCommand command = new PrimaryStorageDownloadCommand(name, url, format, accountId, pool, wait); - - final KVMStoragePoolManager storagePoolMgr = Mockito.mock(KVMStoragePoolManager.class); - final KVMStoragePool primaryPool = Mockito.mock(KVMStoragePool.class); - final KVMStoragePool secondaryPool = Mockito.mock(KVMStoragePool.class); - final KVMPhysicalDisk tmplVol = Mockito.mock(KVMPhysicalDisk.class); - final KVMPhysicalDisk primaryVol = Mockito.mock(KVMPhysicalDisk.class); - - final int index = url.lastIndexOf("/"); - final String mountpoint = url.substring(0, index); - - when(libvirtComputingResourceMock.getStoragePoolMgr()).thenReturn(storagePoolMgr); - when(storagePoolMgr.getStoragePoolByURI(mountpoint)).thenReturn(secondaryPool); - when(secondaryPool.listPhysicalDisks()).thenReturn(disks); - - final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance(); - assertNotNull(wrapper); - - final Answer answer = wrapper.execute(command, libvirtComputingResourceMock); - assertFalse(answer.getResult()); - - verify(libvirtComputingResourceMock, times(1)).getStoragePoolMgr(); - } - - @Test - public void testPrimaryStorageDownloadCommandNOTemplateNOQcow2() { - final StoragePool pool = Mockito.mock(StoragePool.class); - - final List disks = new ArrayList(); - final List spiedDisks = Mockito.spy(disks); - - final String name = "Test"; - final String url = "http://template/"; - final ImageFormat format = ImageFormat.QCOW2; - final long accountId = 1l; - final int wait = 0; - final PrimaryStorageDownloadCommand command = new PrimaryStorageDownloadCommand(name, url, format, accountId, pool, wait); - - final KVMStoragePoolManager storagePoolMgr = Mockito.mock(KVMStoragePoolManager.class); - final KVMStoragePool primaryPool = Mockito.mock(KVMStoragePool.class); - final KVMStoragePool secondaryPool = Mockito.mock(KVMStoragePool.class); - final KVMPhysicalDisk tmplVol = Mockito.mock(KVMPhysicalDisk.class); - final KVMPhysicalDisk primaryVol = Mockito.mock(KVMPhysicalDisk.class); - - final int index = url.lastIndexOf("/"); - final String mountpoint = url.substring(0, index); - - when(libvirtComputingResourceMock.getStoragePoolMgr()).thenReturn(storagePoolMgr); - when(storagePoolMgr.getStoragePoolByURI(mountpoint)).thenReturn(secondaryPool); - when(secondaryPool.listPhysicalDisks()).thenReturn(spiedDisks); - when(spiedDisks.isEmpty()).thenReturn(false); - - final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance(); - assertNotNull(wrapper); - - final Answer answer = wrapper.execute(command, libvirtComputingResourceMock); - assertFalse(answer.getResult()); - - verify(libvirtComputingResourceMock, times(1)).getStoragePoolMgr(); - } - - @Test(expected = NullPointerException.class) - public void testPrimaryStorageDownloadCommandTemplateNoDisk() { - final StoragePool pool = Mockito.mock(StoragePool.class); - - final String name = "Test"; - final String url = "http://template/template.qcow2"; - final ImageFormat format = ImageFormat.VHD; - final long accountId = 1l; - final int wait = 0; - final PrimaryStorageDownloadCommand command = new PrimaryStorageDownloadCommand(name, url, format, accountId, pool, wait); - - final KVMStoragePoolManager storagePoolMgr = Mockito.mock(KVMStoragePoolManager.class); - final KVMStoragePool primaryPool = Mockito.mock(KVMStoragePool.class); - final KVMStoragePool secondaryPool = Mockito.mock(KVMStoragePool.class); - final KVMPhysicalDisk tmplVol = Mockito.mock(KVMPhysicalDisk.class); - final KVMPhysicalDisk primaryVol = Mockito.mock(KVMPhysicalDisk.class); - - final int index = url.lastIndexOf("/"); - final String mountpoint = url.substring(0, index); - - when(libvirtComputingResourceMock.getStoragePoolMgr()).thenReturn(storagePoolMgr); - when(storagePoolMgr.getStoragePoolByURI(mountpoint)).thenReturn(secondaryPool); - when(secondaryPool.getPhysicalDisk("template.qcow2")).thenReturn(tmplVol); - when(storagePoolMgr.getStoragePool(command.getPool().getType(), command.getPoolUuid())).thenReturn(primaryPool); - - final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance(); - assertNotNull(wrapper); - - final Answer answer = wrapper.execute(command, libvirtComputingResourceMock); - assertTrue(answer.getResult()); - - verify(libvirtComputingResourceMock, times(1)).getStoragePoolMgr(); - verify(storagePoolMgr, times(1)).getStoragePool(command.getPool().getType(), command.getPoolUuid()); - } - - @Test - public void testGetStorageStatsCommand() { - final DataStoreTO store = Mockito.mock(DataStoreTO.class); - final GetStorageStatsCommand command = new GetStorageStatsCommand(store ); + @Test + public void testGetStorageStatsCommand() { + final DataStoreTO store = Mockito.mock(DataStoreTO.class); + final GetStorageStatsCommand command = new GetStorageStatsCommand(store ); final KVMStoragePoolManager storagePoolMgr = Mockito.mock(KVMStoragePoolManager.class); final KVMStoragePool secondaryPool = Mockito.mock(KVMStoragePool.class); @@ -2493,29 +2028,6 @@ public void testGetStorageStatsCommandException() { verify(libvirtComputingResourceMock, times(1)).getStoragePoolMgr(); } - @Test - public void testUpgradeSnapshotCommand() { - final StoragePool pool = Mockito.mock(StoragePool.class);; - final String secondaryStoragePoolURL = "url"; - final Long dcId = 1l; - final Long accountId = 1l; - final Long volumeId = 1l; - final Long templateId = 1l; - final Long tmpltAccountId = 1l; - final String volumePath = "/opt/path"; - final String snapshotUuid = "uuid:/8edb1156-a851-4914-afc6-468ee52ac861/"; - final String snapshotName = "uuid:/8edb1156-a851-4914-afc6-468ee52ac861/"; - final String version = "1"; - - final UpgradeSnapshotCommand command = new UpgradeSnapshotCommand(pool, secondaryStoragePoolURL, dcId, accountId, volumeId, templateId, tmpltAccountId, volumePath, snapshotUuid, snapshotName, version); - - final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance(); - assertNotNull(wrapper); - - final Answer answer = wrapper.execute(command, libvirtComputingResourceMock); - assertTrue(answer.getResult()); - } - @Test public void testDeleteStoragePoolCommand() { final StoragePool storagePool = Mockito.mock(StoragePool.class); @@ -3293,93 +2805,6 @@ public void testOvsCreateTunnelCommandFailure2() { command.getNetworkName()); } - @Test - public void testCreateVolumeFromSnapshotCommand() { - // This tests asserts to False because there will be a NPE due to UUID static method calls. - - final StoragePool pool = Mockito.mock(StoragePool.class); - final String secondaryStoragePoolURL = "/opt/storage/"; - final Long dcId = 1l; - final Long accountId = 1l; - final Long volumeId = 1l; - final String backedUpSnapshotUuid = "uuid:/8edb1156-a851-4914-afc6-468ee52ac861/"; - final String backedUpSnapshotName = "uuid:/8edb1156-a851-4914-afc6-468ee52ac862/"; - final int wait = 0; - - final CreateVolumeFromSnapshotCommand command = new CreateVolumeFromSnapshotCommand(pool, secondaryStoragePoolURL, dcId, accountId, volumeId, backedUpSnapshotUuid, backedUpSnapshotName, wait); - - final KVMStoragePoolManager storagePoolMgr = Mockito.mock(KVMStoragePoolManager.class); - final KVMStoragePool secondaryPool = Mockito.mock(KVMStoragePool.class); - final KVMPhysicalDisk snapshot = Mockito.mock(KVMPhysicalDisk.class); - final KVMStoragePool primaryPool = Mockito.mock(KVMStoragePool.class); - - String snapshotPath = command.getSnapshotUuid(); - final int index = snapshotPath.lastIndexOf("/"); - snapshotPath = snapshotPath.substring(0, index); - - final String primaryUuid = command.getPrimaryStoragePoolNameLabel(); - - when(libvirtComputingResourceMock.getStoragePoolMgr()).thenReturn(storagePoolMgr); - when(storagePoolMgr.getStoragePoolByURI(command.getSecondaryStorageUrl() + snapshotPath)).thenReturn(secondaryPool); - when(secondaryPool.getPhysicalDisk(command.getSnapshotName())).thenReturn(snapshot); - when(storagePoolMgr.getStoragePool(command.getPool().getType(), primaryUuid)).thenReturn(primaryPool); - - //when(storagePoolMgr.copyPhysicalDisk(snapshot, volUuid, primaryPool, 0)).thenReturn(disk); - - final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance(); - assertNotNull(wrapper); - - final Answer answer = wrapper.execute(command, libvirtComputingResourceMock); - assertFalse(answer.getResult()); - - verify(libvirtComputingResourceMock, times(1)).getStoragePoolMgr(); - verify(storagePoolMgr, times(1)).getStoragePoolByURI(command.getSecondaryStorageUrl() + snapshotPath); - verify(secondaryPool, times(1)).getPhysicalDisk(command.getSnapshotName()); - verify(storagePoolMgr, times(1)).getStoragePool(command.getPool().getType(), primaryUuid); - //verify(storagePoolMgr, times(1)).copyPhysicalDisk(snapshot, volUuid, primaryPool, 0); - } - - @SuppressWarnings("unchecked") - @Test - public void testCreateVolumeFromSnapshotCommandCloudException() { - final StoragePool pool = Mockito.mock(StoragePool.class); - final String secondaryStoragePoolURL = "/opt/storage/"; - final Long dcId = 1l; - final Long accountId = 1l; - final Long volumeId = 1l; - final String backedUpSnapshotUuid = "uuid:/8edb1156-a851-4914-afc6-468ee52ac861/"; - final String backedUpSnapshotName = "uuid:/8edb1156-a851-4914-afc6-468ee52ac862/"; - final int wait = 0; - - final CreateVolumeFromSnapshotCommand command = new CreateVolumeFromSnapshotCommand(pool, secondaryStoragePoolURL, dcId, accountId, volumeId, backedUpSnapshotUuid, backedUpSnapshotName, wait); - - final KVMStoragePoolManager storagePoolMgr = Mockito.mock(KVMStoragePoolManager.class); - final KVMStoragePool secondaryPool = Mockito.mock(KVMStoragePool.class); - final KVMPhysicalDisk snapshot = Mockito.mock(KVMPhysicalDisk.class); - - String snapshotPath = command.getSnapshotUuid(); - final int index = snapshotPath.lastIndexOf("/"); - snapshotPath = snapshotPath.substring(0, index); - - final String primaryUuid = command.getPrimaryStoragePoolNameLabel(); - - when(libvirtComputingResourceMock.getStoragePoolMgr()).thenReturn(storagePoolMgr); - when(storagePoolMgr.getStoragePoolByURI(command.getSecondaryStorageUrl() + snapshotPath)).thenReturn(secondaryPool); - when(secondaryPool.getPhysicalDisk(command.getSnapshotName())).thenReturn(snapshot); - when(storagePoolMgr.getStoragePool(command.getPool().getType(), primaryUuid)).thenThrow(CloudRuntimeException.class); - - final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance(); - assertNotNull(wrapper); - - final Answer answer = wrapper.execute(command, libvirtComputingResourceMock); - assertFalse(answer.getResult()); - - verify(libvirtComputingResourceMock, times(1)).getStoragePoolMgr(); - verify(storagePoolMgr, times(1)).getStoragePoolByURI(command.getSecondaryStorageUrl() + snapshotPath); - verify(secondaryPool, times(1)).getPhysicalDisk(command.getSnapshotName()); - verify(storagePoolMgr, times(1)).getStoragePool(command.getPool().getType(), primaryUuid); - } - @Test public void testFenceCommand() { final VirtualMachine vm = Mockito.mock(VirtualMachine.class);; @@ -4022,535 +3447,6 @@ public void testNetworkUsageCommandVpcNoOption() { verify(libvirtComputingResourceMock, times(1)).configureVPCNetworkUsage(command.getPrivateIP(), command.getGatewayIP(), command.getOption(), command.getVpcCIDR()); } - @Test - public void testCreatePrivateTemplateFromVolumeCommand() { - //Simple test used to make sure the flow (LibvirtComputingResource => Request => CommandWrapper) is working. - //The code is way to big and complex. Will finish the refactor and come back to this to add more cases. - - final StoragePool pool = Mockito.mock(StoragePool.class);; - final String secondaryStorageUrl = "nfs:/127.0.0.1/storage/secondary"; - final long templateId = 1l; - final long accountId = 1l; - final String userSpecifiedName = "User"; - final String uniqueName = "Unique"; - final String volumePath = "/123/vol"; - final String vmName = "Test"; - final int wait = 0; - - final CreatePrivateTemplateFromVolumeCommand command = new CreatePrivateTemplateFromVolumeCommand(pool, secondaryStorageUrl, templateId, accountId, userSpecifiedName, uniqueName, volumePath, vmName, wait); - - final KVMStoragePoolManager storagePoolMgr = Mockito.mock(KVMStoragePoolManager.class); - final KVMStoragePool secondaryStorage = Mockito.mock(KVMStoragePool.class); - //final KVMStoragePool primary = Mockito.mock(KVMStoragePool.class); - - when(libvirtComputingResourceMock.getStoragePoolMgr()).thenReturn(storagePoolMgr); - when(storagePoolMgr.getStoragePoolByURI(secondaryStorageUrl)).thenReturn(secondaryStorage); - when(storagePoolMgr.getStoragePool(command.getPool().getType(), command.getPrimaryStoragePoolNameLabel())).thenThrow(new CloudRuntimeException("error")); - - final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance(); - assertNotNull(wrapper); - - final Answer answer = wrapper.execute(command, libvirtComputingResourceMock); - assertFalse(answer.getResult()); - - verify(libvirtComputingResourceMock, times(1)).getStoragePoolMgr(); - verify(storagePoolMgr, times(1)).getStoragePoolByURI(secondaryStorageUrl); - verify(storagePoolMgr, times(1)).getStoragePool(command.getPool().getType(), command.getPrimaryStoragePoolNameLabel()); - } - - @SuppressWarnings("unchecked") - @Test - public void testManageSnapshotCommandLibvirtException() { - //Simple test used to make sure the flow (LibvirtComputingResource => Request => CommandWrapper) is working. - //The code is way to big and complex. Will finish the refactor and come back to this to add more cases. - - final StoragePool pool = Mockito.mock(StoragePool.class);; - final String volumePath = "/123/vol"; - final String vmName = "Test"; - - final long snapshotId = 1l; - final String preSnapshotPath = "/snapshot/path"; - final String snapshotName = "snap"; - - final ManageSnapshotCommand command = new ManageSnapshotCommand(snapshotId, volumePath, pool, preSnapshotPath, snapshotName, vmName); - - final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class); - //final Connect conn = Mockito.mock(Connect.class); - - when(libvirtComputingResourceMock.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper); - - try { - when(libvirtUtilitiesHelper.getConnectionByVmName(command.getVmName())).thenThrow(LibvirtException.class); - } catch (final LibvirtException e) { - fail(e.getMessage()); - } - - final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance(); - assertNotNull(wrapper); - - final Answer answer = wrapper.execute(command, libvirtComputingResourceMock); - assertFalse(answer.getResult()); - - verify(libvirtComputingResourceMock, times(1)).getLibvirtUtilitiesHelper(); - try { - verify(libvirtUtilitiesHelper, times(1)).getConnectionByVmName(command.getVmName()); - } catch (final LibvirtException e) { - fail(e.getMessage()); - } - } - - @Test - public void testManageSnapshotCommandLibvirt() { - final StoragePool storagePool = Mockito.mock(StoragePool.class);; - final String volumePath = "/123/vol"; - final String vmName = "Test"; - final long snapshotId = 1l; - final String preSnapshotPath = "/snapshot/path"; - final String snapshotName = "snap"; - - final ManageSnapshotCommand command = new ManageSnapshotCommand(snapshotId, volumePath, storagePool, preSnapshotPath, snapshotName, vmName); - - final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class); - final Connect conn = Mockito.mock(Connect.class); - final KVMStoragePoolManager storagePoolMgr = Mockito.mock(KVMStoragePoolManager.class); - final KVMStoragePool primaryPool = Mockito.mock(KVMStoragePool.class); - final Domain vm = Mockito.mock(Domain.class); - final DomainInfo info = Mockito.mock(DomainInfo.class); - final DomainState state = DomainInfo.DomainState.VIR_DOMAIN_RUNNING; - info.state = state; - - final KVMPhysicalDisk disk = Mockito.mock(KVMPhysicalDisk.class); - - final StorageFilerTO pool = command.getPool(); - - when(libvirtComputingResourceMock.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper); - try { - when(libvirtUtilitiesHelper.getConnectionByVmName(vmName)).thenReturn(conn); - when(libvirtComputingResourceMock.getDomain(conn, command.getVmName())).thenReturn(vm); - when(vm.getInfo()).thenReturn(info); - } catch (final LibvirtException e) { - fail(e.getMessage()); - } - - when(libvirtComputingResourceMock.getStoragePoolMgr()).thenReturn(storagePoolMgr); - when(storagePoolMgr.getStoragePool(pool.getType(), pool.getUuid())).thenReturn(primaryPool); - when(primaryPool.getPhysicalDisk(command.getVolumePath())).thenReturn(disk); - when(primaryPool.isExternalSnapshot()).thenReturn(false); - - try { - when(vm.getUUIDString()).thenReturn("cdb18980-546d-4153-b916-70ee9edf0908"); - } catch (final LibvirtException e) { - fail(e.getMessage()); - } - - final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance(); - assertNotNull(wrapper); - - final Answer answer = wrapper.execute(command, libvirtComputingResourceMock); - assertTrue(answer.getResult()); - - verify(libvirtComputingResourceMock, times(1)).getStoragePoolMgr(); - verify(libvirtComputingResourceMock, times(1)).getLibvirtUtilitiesHelper(); - try { - verify(libvirtUtilitiesHelper, times(1)).getConnectionByVmName(vmName); - } catch (final LibvirtException e) { - fail(e.getMessage()); - } - } - - @SuppressWarnings("unchecked") - @Test - public void testBackupSnapshotCommandLibvirtException() { - //Simple test used to make sure the flow (LibvirtComputingResource => Request => CommandWrapper) is working. - //The code is way to big and complex. Will finish the refactor and come back to this to add more cases. - - final StoragePool pool = Mockito.mock(StoragePool.class);; - final String secondaryStorageUrl = "nfs:/127.0.0.1/storage/secondary"; - final long accountId = 1l; - final String volumePath = "/123/vol"; - final String vmName = "Test"; - final int wait = 0; - - final long snapshotId = 1l; - final String snapshotName = "snap"; - - final Long dcId = 1l; - final Long volumeId = 1l; - final Long secHostId = 1l; - final String snapshotUuid = "9a0afe7c-26a7-4585-bf87-abf82ae106d9"; - final String prevBackupUuid = "003a0cc2-2e04-417a-bee0-534ef1724561"; - final boolean isVolumeInactive = false; - final String prevSnapshotUuid = "1791efae-f22d-474b-87c6-92547d6c5877"; - - final BackupSnapshotCommand command = new BackupSnapshotCommand(secondaryStorageUrl, dcId, accountId, volumeId, snapshotId, secHostId, volumePath, pool, snapshotUuid, snapshotName, prevSnapshotUuid, prevBackupUuid, isVolumeInactive, vmName, wait); - - final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class); - //final Connect conn = Mockito.mock(Connect.class); - - when(libvirtComputingResourceMock.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper); - - try { - when(libvirtUtilitiesHelper.getConnectionByVmName(command.getVmName())).thenThrow(LibvirtException.class); - } catch (final LibvirtException e) { - fail(e.getMessage()); - } - - final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance(); - assertNotNull(wrapper); - - final Answer answer = wrapper.execute(command, libvirtComputingResourceMock); - assertFalse(answer.getResult()); - - verify(libvirtComputingResourceMock, times(1)).getLibvirtUtilitiesHelper(); - try { - verify(libvirtUtilitiesHelper, times(1)).getConnectionByVmName(command.getVmName()); - } catch (final LibvirtException e) { - fail(e.getMessage()); - } - } - - @Test - public void testCreatePrivateTemplateFromSnapshotCommand() { - final StoragePool pool = Mockito.mock(StoragePool.class); - final String secondaryStoragePoolURL = "nfs:/127.0.0.1/storage/secondary"; - final Long dcId = 1l; - final Long accountId = 1l; - final Long volumeId = 1l; - final String backedUpSnapshotUuid = "/run/9a0afe7c-26a7-4585-bf87-abf82ae106d9/"; - final String backedUpSnapshotName = "snap"; - final String origTemplateInstallPath = "/install/path/"; - final Long newTemplateId = 2l; - final String templateName = "templ"; - final int wait = 0; - - final CreatePrivateTemplateFromSnapshotCommand command = new CreatePrivateTemplateFromSnapshotCommand(pool, secondaryStoragePoolURL, dcId, accountId, volumeId, backedUpSnapshotUuid, backedUpSnapshotName, origTemplateInstallPath, newTemplateId, templateName, wait); - - final String templatePath = "/template/path"; - final String localPath = "/mnt/local"; - final String tmplName = "ce97bbc1-34fe-4259-9202-74bbce2562ab"; - - final KVMStoragePoolManager storagePoolMgr = Mockito.mock(KVMStoragePoolManager.class); - final KVMStoragePool secondaryPool = Mockito.mock(KVMStoragePool.class); - final KVMStoragePool snapshotPool = Mockito.mock(KVMStoragePool.class); - final KVMPhysicalDisk snapshot = Mockito.mock(KVMPhysicalDisk.class); - final StorageLayer storage = Mockito.mock(StorageLayer.class); - final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class); - final TemplateLocation location = Mockito.mock(TemplateLocation.class); - final Processor qcow2Processor = Mockito.mock(Processor.class); - final FormatInfo info = Mockito.mock(FormatInfo.class); - - when(libvirtComputingResourceMock.getStoragePoolMgr()).thenReturn(storagePoolMgr); - - String snapshotPath = command.getSnapshotUuid(); - final int index = snapshotPath.lastIndexOf("/"); - snapshotPath = snapshotPath.substring(0, index); - - when(storagePoolMgr.getStoragePoolByURI(command.getSecondaryStorageUrl() + snapshotPath)).thenReturn(snapshotPool); - when(storagePoolMgr.getStoragePoolByURI(command.getSecondaryStorageUrl())).thenReturn(secondaryPool); - when(snapshotPool.getPhysicalDisk(command.getSnapshotName())).thenReturn(snapshot); - when(secondaryPool.getLocalPath()).thenReturn(localPath); - when(libvirtComputingResourceMock.getStorage()).thenReturn(storage); - - when(libvirtComputingResourceMock.createTmplPath()).thenReturn(templatePath); - when(libvirtComputingResourceMock.getCmdsTimeout()).thenReturn(1); - - final String templateFolder = command.getAccountId() + File.separator + command.getNewTemplateId(); - final String templateInstallFolder = "template/tmpl/" + templateFolder; - final String tmplPath = secondaryPool.getLocalPath() + File.separator + templateInstallFolder; - - when(libvirtComputingResourceMock.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper); - when(libvirtUtilitiesHelper.buildTemplateLocation(storage, tmplPath)).thenReturn(location); - when(libvirtUtilitiesHelper.generateUUIDName()).thenReturn(tmplName); - - try { - when(libvirtUtilitiesHelper.buildQCOW2Processor(storage)).thenReturn(qcow2Processor); - when(qcow2Processor.process(tmplPath, null, tmplName)).thenReturn(info); - } catch (final ConfigurationException e) { - fail(e.getMessage()); - } catch (final InternalErrorException e) { - fail(e.getMessage()); - } - - final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance(); - assertNotNull(wrapper); - - final Answer answer = wrapper.execute(command, libvirtComputingResourceMock); - assertTrue(answer.getResult()); - - verify(libvirtComputingResourceMock, times(1)).getStoragePoolMgr(); - verify(storagePoolMgr, times(1)).getStoragePoolByURI(command.getSecondaryStorageUrl() + snapshotPath); - verify(storagePoolMgr, times(1)).getStoragePoolByURI(command.getSecondaryStorageUrl()); - } - - @SuppressWarnings("unchecked") - @Test - public void testCreatePrivateTemplateFromSnapshotCommandConfigurationException() { - final StoragePool pool = Mockito.mock(StoragePool.class); - final String secondaryStoragePoolURL = "nfs:/127.0.0.1/storage/secondary"; - final Long dcId = 1l; - final Long accountId = 1l; - final Long volumeId = 1l; - final String backedUpSnapshotUuid = "/run/9a0afe7c-26a7-4585-bf87-abf82ae106d9/"; - final String backedUpSnapshotName = "snap"; - final String origTemplateInstallPath = "/install/path/"; - final Long newTemplateId = 2l; - final String templateName = "templ"; - final int wait = 0; - - final CreatePrivateTemplateFromSnapshotCommand command = new CreatePrivateTemplateFromSnapshotCommand(pool, secondaryStoragePoolURL, dcId, accountId, volumeId, backedUpSnapshotUuid, backedUpSnapshotName, origTemplateInstallPath, newTemplateId, templateName, wait); - - final String templatePath = "/template/path"; - final String localPath = "/mnt/local"; - final String tmplName = "ce97bbc1-34fe-4259-9202-74bbce2562ab"; - - final KVMStoragePoolManager storagePoolMgr = Mockito.mock(KVMStoragePoolManager.class); - final KVMStoragePool secondaryPool = Mockito.mock(KVMStoragePool.class); - final KVMStoragePool snapshotPool = Mockito.mock(KVMStoragePool.class); - final KVMPhysicalDisk snapshot = Mockito.mock(KVMPhysicalDisk.class); - final StorageLayer storage = Mockito.mock(StorageLayer.class); - final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class); - final TemplateLocation location = Mockito.mock(TemplateLocation.class); - final Processor qcow2Processor = Mockito.mock(Processor.class); - final FormatInfo info = Mockito.mock(FormatInfo.class); - - when(libvirtComputingResourceMock.getStoragePoolMgr()).thenReturn(storagePoolMgr); - - String snapshotPath = command.getSnapshotUuid(); - final int index = snapshotPath.lastIndexOf("/"); - snapshotPath = snapshotPath.substring(0, index); - - when(storagePoolMgr.getStoragePoolByURI(command.getSecondaryStorageUrl() + snapshotPath)).thenReturn(snapshotPool); - when(storagePoolMgr.getStoragePoolByURI(command.getSecondaryStorageUrl())).thenReturn(secondaryPool); - when(snapshotPool.getPhysicalDisk(command.getSnapshotName())).thenReturn(snapshot); - when(secondaryPool.getLocalPath()).thenReturn(localPath); - when(libvirtComputingResourceMock.getStorage()).thenReturn(storage); - - when(libvirtComputingResourceMock.createTmplPath()).thenReturn(templatePath); - when(libvirtComputingResourceMock.getCmdsTimeout()).thenReturn(1); - - final String templateFolder = command.getAccountId() + File.separator + command.getNewTemplateId(); - final String templateInstallFolder = "template/tmpl/" + templateFolder; - final String tmplPath = secondaryPool.getLocalPath() + File.separator + templateInstallFolder; - - when(libvirtComputingResourceMock.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper); - when(libvirtUtilitiesHelper.generateUUIDName()).thenReturn(tmplName); - - try { - when(libvirtUtilitiesHelper.buildQCOW2Processor(storage)).thenThrow(ConfigurationException.class); - } catch (final ConfigurationException e) { - fail(e.getMessage()); - } - - final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance(); - assertNotNull(wrapper); - - final Answer answer = wrapper.execute(command, libvirtComputingResourceMock); - assertFalse(answer.getResult()); - - verify(libvirtComputingResourceMock, times(1)).getStoragePoolMgr(); - verify(storagePoolMgr, times(1)).getStoragePoolByURI(command.getSecondaryStorageUrl() + snapshotPath); - verify(storagePoolMgr, times(1)).getStoragePoolByURI(command.getSecondaryStorageUrl()); - } - - @SuppressWarnings("unchecked") - @Test - public void testCreatePrivateTemplateFromSnapshotCommandInternalErrorException() { - final StoragePool pool = Mockito.mock(StoragePool.class); - final String secondaryStoragePoolURL = "nfs:/127.0.0.1/storage/secondary"; - final Long dcId = 1l; - final Long accountId = 1l; - final Long volumeId = 1l; - final String backedUpSnapshotUuid = "/run/9a0afe7c-26a7-4585-bf87-abf82ae106d9/"; - final String backedUpSnapshotName = "snap"; - final String origTemplateInstallPath = "/install/path/"; - final Long newTemplateId = 2l; - final String templateName = "templ"; - final int wait = 0; - - final CreatePrivateTemplateFromSnapshotCommand command = new CreatePrivateTemplateFromSnapshotCommand(pool, secondaryStoragePoolURL, dcId, accountId, volumeId, backedUpSnapshotUuid, backedUpSnapshotName, origTemplateInstallPath, newTemplateId, templateName, wait); - - final String templatePath = "/template/path"; - final String localPath = "/mnt/local"; - final String tmplName = "ce97bbc1-34fe-4259-9202-74bbce2562ab"; - - final KVMStoragePoolManager storagePoolMgr = Mockito.mock(KVMStoragePoolManager.class); - final KVMStoragePool secondaryPool = Mockito.mock(KVMStoragePool.class); - final KVMStoragePool snapshotPool = Mockito.mock(KVMStoragePool.class); - final KVMPhysicalDisk snapshot = Mockito.mock(KVMPhysicalDisk.class); - final StorageLayer storage = Mockito.mock(StorageLayer.class); - final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class); - final TemplateLocation location = Mockito.mock(TemplateLocation.class); - final Processor qcow2Processor = Mockito.mock(Processor.class); - - when(libvirtComputingResourceMock.getStoragePoolMgr()).thenReturn(storagePoolMgr); - - String snapshotPath = command.getSnapshotUuid(); - final int index = snapshotPath.lastIndexOf("/"); - snapshotPath = snapshotPath.substring(0, index); - - when(storagePoolMgr.getStoragePoolByURI(command.getSecondaryStorageUrl() + snapshotPath)).thenReturn(snapshotPool); - when(storagePoolMgr.getStoragePoolByURI(command.getSecondaryStorageUrl())).thenReturn(secondaryPool); - when(snapshotPool.getPhysicalDisk(command.getSnapshotName())).thenReturn(snapshot); - when(secondaryPool.getLocalPath()).thenReturn(localPath); - when(libvirtComputingResourceMock.getStorage()).thenReturn(storage); - - when(libvirtComputingResourceMock.createTmplPath()).thenReturn(templatePath); - when(libvirtComputingResourceMock.getCmdsTimeout()).thenReturn(1); - - final String templateFolder = command.getAccountId() + File.separator + command.getNewTemplateId(); - final String templateInstallFolder = "template/tmpl/" + templateFolder; - final String tmplPath = secondaryPool.getLocalPath() + File.separator + templateInstallFolder; - - when(libvirtComputingResourceMock.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper); - when(libvirtUtilitiesHelper.generateUUIDName()).thenReturn(tmplName); - - try { - when(libvirtUtilitiesHelper.buildQCOW2Processor(storage)).thenReturn(qcow2Processor); - when(qcow2Processor.process(tmplPath, null, tmplName)).thenThrow(InternalErrorException.class); - } catch (final ConfigurationException e) { - fail(e.getMessage()); - } catch (final InternalErrorException e) { - fail(e.getMessage()); - } - - final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance(); - assertNotNull(wrapper); - - final Answer answer = wrapper.execute(command, libvirtComputingResourceMock); - assertFalse(answer.getResult()); - - verify(libvirtComputingResourceMock, times(1)).getStoragePoolMgr(); - verify(storagePoolMgr, times(1)).getStoragePoolByURI(command.getSecondaryStorageUrl() + snapshotPath); - verify(storagePoolMgr, times(1)).getStoragePoolByURI(command.getSecondaryStorageUrl()); - } - - @SuppressWarnings("unchecked") - @Test - public void testCreatePrivateTemplateFromSnapshotCommandIOException() { - final StoragePool pool = Mockito.mock(StoragePool.class); - final String secondaryStoragePoolURL = "nfs:/127.0.0.1/storage/secondary"; - final Long dcId = 1l; - final Long accountId = 1l; - final Long volumeId = 1l; - final String backedUpSnapshotUuid = "/run/9a0afe7c-26a7-4585-bf87-abf82ae106d9/"; - final String backedUpSnapshotName = "snap"; - final String origTemplateInstallPath = "/install/path/"; - final Long newTemplateId = 2l; - final String templateName = "templ"; - final int wait = 0; - - final CreatePrivateTemplateFromSnapshotCommand command = new CreatePrivateTemplateFromSnapshotCommand(pool, secondaryStoragePoolURL, dcId, accountId, volumeId, backedUpSnapshotUuid, backedUpSnapshotName, origTemplateInstallPath, newTemplateId, templateName, wait); - - final String templatePath = "/template/path"; - final String localPath = "/mnt/local"; - final String tmplName = "ce97bbc1-34fe-4259-9202-74bbce2562ab"; - - final KVMStoragePoolManager storagePoolMgr = Mockito.mock(KVMStoragePoolManager.class); - final KVMStoragePool secondaryPool = Mockito.mock(KVMStoragePool.class); - final KVMStoragePool snapshotPool = Mockito.mock(KVMStoragePool.class); - final KVMPhysicalDisk snapshot = Mockito.mock(KVMPhysicalDisk.class); - final StorageLayer storage = Mockito.mock(StorageLayer.class); - final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class); - final TemplateLocation location = Mockito.mock(TemplateLocation.class); - final Processor qcow2Processor = Mockito.mock(Processor.class); - final FormatInfo info = Mockito.mock(FormatInfo.class); - - when(libvirtComputingResourceMock.getStoragePoolMgr()).thenReturn(storagePoolMgr); - - String snapshotPath = command.getSnapshotUuid(); - final int index = snapshotPath.lastIndexOf("/"); - snapshotPath = snapshotPath.substring(0, index); - - when(storagePoolMgr.getStoragePoolByURI(command.getSecondaryStorageUrl() + snapshotPath)).thenReturn(snapshotPool); - when(storagePoolMgr.getStoragePoolByURI(command.getSecondaryStorageUrl())).thenReturn(secondaryPool); - when(snapshotPool.getPhysicalDisk(command.getSnapshotName())).thenReturn(snapshot); - when(secondaryPool.getLocalPath()).thenReturn(localPath); - when(libvirtComputingResourceMock.getStorage()).thenReturn(storage); - - when(libvirtComputingResourceMock.createTmplPath()).thenReturn(templatePath); - when(libvirtComputingResourceMock.getCmdsTimeout()).thenReturn(1); - - final String templateFolder = command.getAccountId() + File.separator + command.getNewTemplateId(); - final String templateInstallFolder = "template/tmpl/" + templateFolder; - final String tmplPath = secondaryPool.getLocalPath() + File.separator + templateInstallFolder; - - when(libvirtComputingResourceMock.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper); - when(libvirtUtilitiesHelper.buildTemplateLocation(storage, tmplPath)).thenReturn(location); - when(libvirtUtilitiesHelper.generateUUIDName()).thenReturn(tmplName); - - try { - when(libvirtUtilitiesHelper.buildQCOW2Processor(storage)).thenReturn(qcow2Processor); - when(qcow2Processor.process(tmplPath, null, tmplName)).thenReturn(info); - - when(location.create(1, true, tmplName)).thenThrow(IOException.class); - - } catch (final ConfigurationException e) { - fail(e.getMessage()); - } catch (final InternalErrorException e) { - fail(e.getMessage()); - } catch (final IOException e) { - fail(e.getMessage()); - } - - final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance(); - assertNotNull(wrapper); - - final Answer answer = wrapper.execute(command, libvirtComputingResourceMock); - assertFalse(answer.getResult()); - - verify(libvirtComputingResourceMock, times(1)).getStoragePoolMgr(); - verify(storagePoolMgr, times(1)).getStoragePoolByURI(command.getSecondaryStorageUrl() + snapshotPath); - verify(storagePoolMgr, times(1)).getStoragePoolByURI(command.getSecondaryStorageUrl()); - } - - @SuppressWarnings("unchecked") - @Test - public void testCreatePrivateTemplateFromSnapshotCommandCloudRuntime() { - final StoragePool pool = Mockito.mock(StoragePool.class); - final String secondaryStoragePoolURL = "nfs:/127.0.0.1/storage/secondary"; - final Long dcId = 1l; - final Long accountId = 1l; - final Long volumeId = 1l; - final String backedUpSnapshotUuid = "/run/9a0afe7c-26a7-4585-bf87-abf82ae106d9/"; - final String backedUpSnapshotName = "snap"; - final String origTemplateInstallPath = "/install/path/"; - final Long newTemplateId = 2l; - final String templateName = "templ"; - final int wait = 0; - - final CreatePrivateTemplateFromSnapshotCommand command = new CreatePrivateTemplateFromSnapshotCommand(pool, secondaryStoragePoolURL, dcId, accountId, volumeId, backedUpSnapshotUuid, backedUpSnapshotName, origTemplateInstallPath, newTemplateId, templateName, wait); - - final KVMStoragePoolManager storagePoolMgr = Mockito.mock(KVMStoragePoolManager.class); - final KVMStoragePool secondaryPool = Mockito.mock(KVMStoragePool.class); - final KVMStoragePool snapshotPool = Mockito.mock(KVMStoragePool.class); - final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class); - - final String tmplName = "ce97bbc1-34fe-4259-9202-74bbce2562ab"; - - when(libvirtComputingResourceMock.getStoragePoolMgr()).thenReturn(storagePoolMgr); - - String snapshotPath = command.getSnapshotUuid(); - final int index = snapshotPath.lastIndexOf("/"); - snapshotPath = snapshotPath.substring(0, index); - - when(libvirtComputingResourceMock.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper); - when(libvirtUtilitiesHelper.generateUUIDName()).thenReturn(tmplName); - - when(storagePoolMgr.getStoragePoolByURI(command.getSecondaryStorageUrl() + snapshotPath)).thenReturn(snapshotPool); - when(storagePoolMgr.getStoragePoolByURI(command.getSecondaryStorageUrl())).thenReturn(secondaryPool); - when(snapshotPool.getPhysicalDisk(command.getSnapshotName())).thenThrow(CloudRuntimeException.class); - - final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance(); - assertNotNull(wrapper); - - final Answer answer = wrapper.execute(command, libvirtComputingResourceMock); - assertFalse(answer.getResult()); - - verify(libvirtComputingResourceMock, times(1)).getStoragePoolMgr(); - verify(storagePoolMgr, times(1)).getStoragePoolByURI(command.getSecondaryStorageUrl() + snapshotPath); - verify(storagePoolMgr, times(1)).getStoragePoolByURI(command.getSecondaryStorageUrl()); - } - @Test public void testCopyVolumeCommand() { final StoragePool storagePool = Mockito.mock(StoragePool.class); From 2274380875b950958acef268dce10df16abccd00 Mon Sep 17 00:00:00 2001 From: Wido den Hollander Date: Tue, 22 Sep 2026 17:55:05 +0000 Subject: [PATCH 2/4] scripts: remove unused qcow2 storage scripts Nothing in the tree references these scripts any more. Nine of them have no callers at all: create_private_template.sh, createvm.sh, delvm.sh, get_domr_kernel.sh, get_iqn.sh, importmpl.sh, listvmdisk.sh, listvmdisksize.sh, managevolume.sh createvm.sh was still located at agent startup until the previous commit, but was never executed. The other three are stale copies of scripts that live in scripts/storage/secondary. The secondary storage code resolves them from that directory only, so the qcow2 copies were never run: createvolume.sh, listvmtmplt.sh, listvolume.sh createtmplt.sh, managesnapshot.sh and resizevolume.sh stay, the KVM agent still uses them. --- .../storage/qcow2/create_private_template.sh | 122 ------- scripts/storage/qcow2/createvm.sh | 310 ------------------ scripts/storage/qcow2/createvolume.sh | 210 ------------ scripts/storage/qcow2/delvm.sh | 77 ----- scripts/storage/qcow2/get_domr_kernel.sh | 131 -------- scripts/storage/qcow2/get_iqn.sh | 34 -- scripts/storage/qcow2/importmpl.sh | 223 ------------- scripts/storage/qcow2/listvmdisk.sh | 86 ----- scripts/storage/qcow2/listvmdisksize.sh | 87 ----- scripts/storage/qcow2/listvmtmplt.sh | 65 ---- scripts/storage/qcow2/listvolume.sh | 65 ---- scripts/storage/qcow2/managevolume.sh | 178 ---------- 12 files changed, 1588 deletions(-) delete mode 100755 scripts/storage/qcow2/create_private_template.sh delete mode 100755 scripts/storage/qcow2/createvm.sh delete mode 100755 scripts/storage/qcow2/createvolume.sh delete mode 100755 scripts/storage/qcow2/delvm.sh delete mode 100755 scripts/storage/qcow2/get_domr_kernel.sh delete mode 100755 scripts/storage/qcow2/get_iqn.sh delete mode 100755 scripts/storage/qcow2/importmpl.sh delete mode 100755 scripts/storage/qcow2/listvmdisk.sh delete mode 100755 scripts/storage/qcow2/listvmdisksize.sh delete mode 100755 scripts/storage/qcow2/listvmtmplt.sh delete mode 100755 scripts/storage/qcow2/listvolume.sh delete mode 100755 scripts/storage/qcow2/managevolume.sh diff --git a/scripts/storage/qcow2/create_private_template.sh b/scripts/storage/qcow2/create_private_template.sh deleted file mode 100755 index 06bf79d44cbf..000000000000 --- a/scripts/storage/qcow2/create_private_template.sh +++ /dev/null @@ -1,122 +0,0 @@ -#!/usr/bin/env bash -# Licensed to the Apache Software Foundation (ASF) under one -# or more contributor license agreements. See the NOTICE file -# distributed with this work for additional information -# regarding copyright ownership. The ASF licenses this file -# to you under the Apache License, Version 2.0 (the -# "License"); you may not use this file except in compliance -# with the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, -# software distributed under the License is distributed on an -# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -# KIND, either express or implied. See the License for the -# specific language governing permissions and limitations -# under the License. - - - -# $Id: create_private_template.sh 9804 2010-06-22 18:36:49Z alex $ $HeadURL: svn://svn.lab.vmops.com/repos/vmdev/java/scripts/storage/qcow2/create_private_template.sh $ -# create_private_template.sh -- create a private template from a snapshot -# @VERSION@ - -usage() { - printf "Usage: %s: -p -n