Search in sources :

Example 1 with AttachIsoAnswer

use of com.cloud.agent.api.AttachIsoAnswer in project cloudstack by apache.

the class VmwareResource method execute.

protected AttachIsoAnswer execute(AttachIsoCommand cmd) {
    try {
        VmwareHypervisorHost hyperHost = getHyperHost(getServiceContext());
        VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(cmd.getVmName());
        if (vmMo == null) {
            String msg = "Unable to find VM in vSphere to execute AttachIsoCommand, vmName: " + cmd.getVmName();
            s_logger.error(msg);
            throw new Exception(msg);
        }
        String storeUrl = cmd.getStoreUrl();
        if (storeUrl == null) {
            if (!cmd.getIsoPath().equalsIgnoreCase(TemplateManager.VMWARE_TOOLS_ISO)) {
                String msg = "ISO store root url is not found in AttachIsoCommand";
                s_logger.error(msg);
                throw new Exception(msg);
            } else {
                if (cmd.isAttach()) {
                    vmMo.mountToolsInstaller();
                } else {
                    try {
                        if (!vmMo.unmountToolsInstaller()) {
                            return new AttachIsoAnswer(cmd, false, "Failed to unmount vmware-tools installer ISO as the corresponding CDROM device is locked by VM. Please unmount the CDROM device inside the VM and ret-try.");
                        }
                    } catch (Throwable e) {
                        vmMo.detachIso(null, cmd.isForce());
                    }
                }
                return new AttachIsoAnswer(cmd);
            }
        }
        ManagedObjectReference morSecondaryDs = prepareSecondaryDatastoreOnHost(storeUrl);
        String isoPath = cmd.getIsoPath();
        if (!isoPath.startsWith(storeUrl)) {
            assert (false);
            String msg = "ISO path does not start with the secondary storage root";
            s_logger.error(msg);
            throw new Exception(msg);
        }
        int isoNameStartPos = isoPath.lastIndexOf('/');
        String isoFileName = isoPath.substring(isoNameStartPos + 1);
        String isoStorePathFromRoot = isoPath.substring(storeUrl.length() + 1, isoNameStartPos + 1);
        // TODO, check if iso is already attached, or if there is a previous
        // attachment
        DatastoreMO secondaryDsMo = new DatastoreMO(getServiceContext(), morSecondaryDs);
        String storeName = secondaryDsMo.getName();
        String isoDatastorePath = String.format("[%s] %s%s", storeName, isoStorePathFromRoot, isoFileName);
        if (cmd.isAttach()) {
            vmMo.attachIso(isoDatastorePath, morSecondaryDs, true, false, cmd.getDeviceKey(), cmd.isForce());
            return new AttachIsoAnswer(cmd);
        } else {
            int key = vmMo.detachIso(isoDatastorePath, cmd.isForce());
            return new AttachIsoAnswer(cmd, key);
        }
    } catch (Throwable e) {
        if (e instanceof RemoteException) {
            s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context");
            invalidateServiceContext();
        }
        String message = String.format("AttachIsoCommand(%s) failed due to [%s].", cmd.isAttach() ? "attach" : "detach", VmwareHelper.getExceptionMessage(e));
        s_logger.error(message, e);
        return new AttachIsoAnswer(cmd, false, message);
    }
}
Also used : AttachIsoAnswer(com.cloud.agent.api.AttachIsoAnswer) VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) RemoteException(java.rmi.RemoteException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) RemoteException(java.rmi.RemoteException) InternalErrorException(com.cloud.exception.InternalErrorException) CloudException(com.cloud.exception.CloudException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ConfigurationException(javax.naming.ConfigurationException) DatastoreMO(com.cloud.hypervisor.vmware.mo.DatastoreMO) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 2 with AttachIsoAnswer

use of com.cloud.agent.api.AttachIsoAnswer in project cloudstack by apache.

the class LibvirtAttachIsoCommandWrapper method execute.

@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());
}
Also used : LibvirtException(org.libvirt.LibvirtException) AttachIsoAnswer(com.cloud.agent.api.AttachIsoAnswer) Connect(org.libvirt.Connect) URISyntaxException(java.net.URISyntaxException) InternalErrorException(com.cloud.exception.InternalErrorException)

Aggregations

AttachIsoAnswer (com.cloud.agent.api.AttachIsoAnswer)2 InternalErrorException (com.cloud.exception.InternalErrorException)2 CloudException (com.cloud.exception.CloudException)1 DatastoreMO (com.cloud.hypervisor.vmware.mo.DatastoreMO)1 VirtualMachineMO (com.cloud.hypervisor.vmware.mo.VirtualMachineMO)1 VmwareHypervisorHost (com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost)1 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)1 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ConnectException (java.net.ConnectException)1 URISyntaxException (java.net.URISyntaxException)1 RemoteException (java.rmi.RemoteException)1 ConfigurationException (javax.naming.ConfigurationException)1 Connect (org.libvirt.Connect)1 LibvirtException (org.libvirt.LibvirtException)1