Search in sources :

Example 21 with VmwareContext

use of com.cloud.hypervisor.vmware.util.VmwareContext in project cloudstack by apache.

the class VmwareResource method initialize.

@Override
public StartupCommand[] initialize() {
    try {
        String hostApiVersion = "4.1";
        VmwareContext context = getServiceContext();
        try {
            VmwareHypervisorHost hyperHost = getHyperHost(context);
            assert (hyperHost instanceof HostMO);
            if (!((HostMO) hyperHost).isHyperHostConnected()) {
                s_logger.info("Host " + hyperHost.getHyperHostName() + " is not in connected state");
                return null;
            }
            ((HostMO) hyperHost).enableVncOnHostFirewall();
            AboutInfo aboutInfo = ((HostMO) hyperHost).getHostAboutInfo();
            hostApiVersion = aboutInfo.getApiVersion();
        } catch (Exception e) {
            String msg = "VmwareResource intialize() failed due to : " + VmwareHelper.getExceptionMessage(e);
            s_logger.error(msg);
            invalidateServiceContext();
            return null;
        }
        StartupRoutingCommand cmd = new StartupRoutingCommand();
        fillHostInfo(cmd);
        cmd.setHypervisorType(HypervisorType.VMware);
        cmd.setCluster(_cluster);
        cmd.setHypervisorVersion(hostApiVersion);
        List<StartupStorageCommand> storageCmds = initializeLocalStorage();
        StartupCommand[] answerCmds = new StartupCommand[1 + storageCmds.size()];
        answerCmds[0] = cmd;
        for (int i = 0; i < storageCmds.size(); i++) {
            answerCmds[i + 1] = storageCmds.get(i);
        }
        return answerCmds;
    } finally {
        recycleServiceContext();
    }
}
Also used : StartupCommand(com.cloud.agent.api.StartupCommand) VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) HostMO(com.cloud.hypervisor.vmware.mo.HostMO) StartupStorageCommand(com.cloud.agent.api.StartupStorageCommand) AboutInfo(com.vmware.vim25.AboutInfo) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) StartupRoutingCommand(com.cloud.agent.api.StartupRoutingCommand) 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)

Example 22 with VmwareContext

use of com.cloud.hypervisor.vmware.util.VmwareContext in project cloudstack by apache.

the class VmwareResource method execute.

protected Answer execute(GetVmIpAddressCommand cmd) {
    if (s_logger.isTraceEnabled()) {
        s_logger.trace("Executing resource command GetVmIpAddressCommand: " + _gson.toJson(cmd));
    }
    String details = "Unable to find IP Address of VM. ";
    String vmName = cmd.getVmName();
    boolean result = false;
    String ip = null;
    Answer answer = null;
    VmwareContext context = getServiceContext();
    VmwareHypervisorHost hyperHost = getHyperHost(context);
    if (vmName == null || vmName.isEmpty()) {
        details += "Name of instance provided is NULL or empty.";
        return new Answer(cmd, result, details);
    }
    try {
        VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(vmName);
        if (vmMo != null) {
            GuestInfo guestInfo = vmMo.getGuestInfo();
            VirtualMachineToolsStatus toolsStatus = guestInfo.getToolsStatus();
            if (toolsStatus == VirtualMachineToolsStatus.TOOLS_NOT_INSTALLED) {
                details += "Vmware tools not installed.";
            } else {
                ip = guestInfo.getIpAddress();
                if (ip != null) {
                    result = true;
                }
                details = ip;
            }
        } else {
            details += "VM " + vmName + " no longer exists on vSphere host: " + hyperHost.getHyperHostName();
            s_logger.info(details);
        }
    } catch (Throwable e) {
        if (e instanceof RemoteException) {
            s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context");
            invalidateServiceContext();
        }
        details += "Encountered exception : " + VmwareHelper.getExceptionMessage(e);
        s_logger.error(details);
    }
    answer = new Answer(cmd, result, details);
    if (s_logger.isTraceEnabled()) {
        s_logger.trace("Returning GetVmIpAddressAnswer: " + _gson.toJson(answer));
    }
    return answer;
}
Also used : ModifyTargetsAnswer(com.cloud.agent.api.ModifyTargetsAnswer) GetVncPortAnswer(com.cloud.agent.api.GetVncPortAnswer) ManageSnapshotAnswer(com.cloud.agent.api.ManageSnapshotAnswer) CreatePrivateTemplateAnswer(com.cloud.agent.api.storage.CreatePrivateTemplateAnswer) ModifyStoragePoolAnswer(com.cloud.agent.api.ModifyStoragePoolAnswer) MigrateVolumeAnswer(com.cloud.agent.api.storage.MigrateVolumeAnswer) SetupAnswer(com.cloud.agent.api.SetupAnswer) GetVmStatsAnswer(com.cloud.agent.api.GetVmStatsAnswer) StopAnswer(com.cloud.agent.api.StopAnswer) NetworkUsageAnswer(com.cloud.agent.api.NetworkUsageAnswer) Answer(com.cloud.agent.api.Answer) UnPlugNicAnswer(com.cloud.agent.api.UnPlugNicAnswer) CheckOnHostAnswer(com.cloud.agent.api.CheckOnHostAnswer) CheckHealthAnswer(com.cloud.agent.api.CheckHealthAnswer) RevertToVMSnapshotAnswer(com.cloud.agent.api.RevertToVMSnapshotAnswer) CopyVolumeAnswer(com.cloud.agent.api.storage.CopyVolumeAnswer) CreateVMSnapshotAnswer(com.cloud.agent.api.CreateVMSnapshotAnswer) DeleteVMSnapshotAnswer(com.cloud.agent.api.DeleteVMSnapshotAnswer) MaintainAnswer(com.cloud.agent.api.MaintainAnswer) GetHostStatsAnswer(com.cloud.agent.api.GetHostStatsAnswer) CheckSshAnswer(com.cloud.agent.api.check.CheckSshAnswer) RebootAnswer(com.cloud.agent.api.RebootAnswer) PrimaryStorageDownloadAnswer(com.cloud.agent.api.storage.PrimaryStorageDownloadAnswer) StartAnswer(com.cloud.agent.api.StartAnswer) GetStorageStatsAnswer(com.cloud.agent.api.GetStorageStatsAnswer) MigrateAnswer(com.cloud.agent.api.MigrateAnswer) CreateVolumeFromSnapshotAnswer(com.cloud.agent.api.CreateVolumeFromSnapshotAnswer) CheckNetworkAnswer(com.cloud.agent.api.CheckNetworkAnswer) PlugNicAnswer(com.cloud.agent.api.PlugNicAnswer) ScaleVmAnswer(com.cloud.agent.api.ScaleVmAnswer) MigrateWithStorageAnswer(com.cloud.agent.api.MigrateWithStorageAnswer) ResizeVolumeAnswer(com.cloud.agent.api.storage.ResizeVolumeAnswer) BackupSnapshotAnswer(com.cloud.agent.api.BackupSnapshotAnswer) CheckVirtualMachineAnswer(com.cloud.agent.api.CheckVirtualMachineAnswer) ValidateSnapshotAnswer(com.cloud.agent.api.ValidateSnapshotAnswer) ReadyAnswer(com.cloud.agent.api.ReadyAnswer) PrepareForMigrationAnswer(com.cloud.agent.api.PrepareForMigrationAnswer) GetVmDiskStatsAnswer(com.cloud.agent.api.GetVmDiskStatsAnswer) VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) VirtualMachineToolsStatus(com.vmware.vim25.VirtualMachineToolsStatus) GuestInfo(com.vmware.vim25.GuestInfo) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) RemoteException(java.rmi.RemoteException)

Example 23 with VmwareContext

use of com.cloud.hypervisor.vmware.util.VmwareContext in project cloudstack by apache.

the class VmwareResource method execute.

protected Answer execute(CreatePrivateTemplateFromVolumeCommand cmd) {
    if (s_logger.isInfoEnabled()) {
        s_logger.info("Executing resource CreatePrivateTemplateFromVolumeCommand: " + _gson.toJson(cmd));
    }
    try {
        VmwareContext context = getServiceContext();
        VmwareManager mgr = context.getStockObject(VmwareManager.CONTEXT_STOCK_NAME);
        return mgr.getStorageManager().execute(this, cmd);
    } catch (Throwable e) {
        if (e instanceof RemoteException) {
            s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context");
            invalidateServiceContext();
        }
        String details = "CreatePrivateTemplateFromVolumeCommand failed due to " + VmwareHelper.getExceptionMessage(e);
        s_logger.error(details, e);
        return new CreatePrivateTemplateAnswer(cmd, false, details);
    }
}
Also used : VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) VmwareManager(com.cloud.hypervisor.vmware.manager.VmwareManager) CreatePrivateTemplateAnswer(com.cloud.agent.api.storage.CreatePrivateTemplateAnswer) RemoteException(java.rmi.RemoteException)

Example 24 with VmwareContext

use of com.cloud.hypervisor.vmware.util.VmwareContext in project cloudstack by apache.

the class VmwareResource method initializeLocalStorage.

private List<StartupStorageCommand> initializeLocalStorage() {
    List<StartupStorageCommand> storageCmds = new ArrayList<StartupStorageCommand>();
    VmwareContext context = getServiceContext();
    try {
        VmwareHypervisorHost hyperHost = getHyperHost(context);
        if (hyperHost instanceof HostMO) {
            HostMO hostMo = (HostMO) hyperHost;
            List<Pair<ManagedObjectReference, String>> dsList = hostMo.getLocalDatastoreOnHost();
            for (Pair<ManagedObjectReference, String> dsPair : dsList) {
                DatastoreMO dsMo = new DatastoreMO(context, dsPair.first());
                String poolUuid = dsMo.getCustomFieldValue(CustomFieldConstants.CLOUD_UUID);
                if (poolUuid == null || poolUuid.isEmpty()) {
                    poolUuid = UUID.randomUUID().toString();
                    dsMo.setCustomFieldValue(CustomFieldConstants.CLOUD_UUID, poolUuid);
                }
                DatastoreSummary dsSummary = dsMo.getSummary();
                String address = hostMo.getHostName();
                StoragePoolInfo pInfo = new StoragePoolInfo(poolUuid, address, dsMo.getMor().getValue(), "", StoragePoolType.VMFS, dsSummary.getCapacity(), dsSummary.getFreeSpace());
                StartupStorageCommand cmd = new StartupStorageCommand();
                cmd.setName(poolUuid);
                cmd.setPoolInfo(pInfo);
                // give storage host the same UUID as the local storage pool itself
                cmd.setGuid(poolUuid);
                cmd.setResourceType(Storage.StorageResourceType.STORAGE_POOL);
                cmd.setDataCenter(_dcId);
                cmd.setPod(_pod);
                cmd.setCluster(_cluster);
                s_logger.info("Add local storage startup command: " + _gson.toJson(cmd));
                storageCmds.add(cmd);
            }
        } else {
            s_logger.info("Cluster host does not support local storage, skip it");
        }
    } catch (Exception e) {
        String msg = "initializing local storage failed due to : " + VmwareHelper.getExceptionMessage(e);
        s_logger.error(msg);
        invalidateServiceContext();
        throw new CloudRuntimeException(msg);
    }
    return storageCmds;
}
Also used : HostMO(com.cloud.hypervisor.vmware.mo.HostMO) StartupStorageCommand(com.cloud.agent.api.StartupStorageCommand) ArrayList(java.util.ArrayList) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) DatastoreMO(com.cloud.hypervisor.vmware.mo.DatastoreMO) 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) VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) DatastoreSummary(com.vmware.vim25.DatastoreSummary) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) StoragePoolInfo(com.cloud.agent.api.StoragePoolInfo) Pair(com.cloud.utils.Pair) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 25 with VmwareContext

use of com.cloud.hypervisor.vmware.util.VmwareContext in project cloudstack by apache.

the class VmwareResource method execute.

private PlugNicAnswer execute(PlugNicCommand cmd) {
    if (s_logger.isInfoEnabled()) {
        s_logger.info("Executing resource PlugNicCommand " + _gson.toJson(cmd));
    }
    getServiceContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME);
    VmwareContext context = getServiceContext();
    try {
        VmwareHypervisorHost hyperHost = getHyperHost(context);
        String vmName = cmd.getVmName();
        VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(vmName);
        if (vmMo == null) {
            if (hyperHost instanceof HostMO) {
                ClusterMO clusterMo = new ClusterMO(hyperHost.getContext(), ((HostMO) hyperHost).getParentMor());
                vmMo = clusterMo.findVmOnHyperHost(vmName);
            }
        }
        if (vmMo == null) {
            String msg = "Router " + vmName + " no longer exists to execute PlugNic command";
            s_logger.error(msg);
            throw new Exception(msg);
        }
        /*
            if(!isVMWareToolsInstalled(vmMo)){
                String errMsg = "vmware tools is not installed or not running, cannot add nic to vm " + vmName;
                s_logger.debug(errMsg);
                return new PlugNicAnswer(cmd, false, "Unable to execute PlugNicCommand due to " + errMsg);
            }
             */
        // Fallback to E1000 if no specific nicAdapter is passed
        VirtualEthernetCardType nicDeviceType = VirtualEthernetCardType.E1000;
        Map<String, String> details = cmd.getDetails();
        if (details != null) {
            nicDeviceType = VirtualEthernetCardType.valueOf((String) details.get("nicAdapter"));
        }
        // find a usable device number in VMware environment
        VirtualDevice[] nicDevices = vmMo.getNicDevices();
        int deviceNumber = -1;
        for (VirtualDevice device : nicDevices) {
            if (device.getUnitNumber() > deviceNumber)
                deviceNumber = device.getUnitNumber();
        }
        deviceNumber++;
        NicTO nicTo = cmd.getNic();
        VirtualDevice nic;
        Pair<ManagedObjectReference, String> networkInfo = prepareNetworkFromNicInfo(vmMo.getRunningHost(), nicTo, false, cmd.getVMType());
        String dvSwitchUuid = null;
        if (VmwareHelper.isDvPortGroup(networkInfo.first())) {
            ManagedObjectReference dcMor = hyperHost.getHyperHostDatacenter();
            DatacenterMO dataCenterMo = new DatacenterMO(context, dcMor);
            ManagedObjectReference dvsMor = dataCenterMo.getDvSwitchMor(networkInfo.first());
            dvSwitchUuid = dataCenterMo.getDvSwitchUuid(dvsMor);
            s_logger.info("Preparing NIC device on dvSwitch : " + dvSwitchUuid);
            nic = VmwareHelper.prepareDvNicDevice(vmMo, networkInfo.first(), nicDeviceType, networkInfo.second(), dvSwitchUuid, nicTo.getMac(), deviceNumber, deviceNumber + 1, true, true);
        } else {
            s_logger.info("Preparing NIC device on network " + networkInfo.second());
            nic = VmwareHelper.prepareNicDevice(vmMo, networkInfo.first(), nicDeviceType, networkInfo.second(), nicTo.getMac(), deviceNumber, deviceNumber + 1, true, true);
        }
        VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec();
        //VirtualDeviceConfigSpec[] deviceConfigSpecArray = new VirtualDeviceConfigSpec[1];
        VirtualDeviceConfigSpec deviceConfigSpec = new VirtualDeviceConfigSpec();
        deviceConfigSpec.setDevice(nic);
        deviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.ADD);
        vmConfigSpec.getDeviceChange().add(deviceConfigSpec);
        setNuageVspVrIpInExtraConfig(vmConfigSpec.getExtraConfig(), nicTo, dvSwitchUuid);
        if (!vmMo.configureVm(vmConfigSpec)) {
            throw new Exception("Failed to configure devices when running PlugNicCommand");
        }
        return new PlugNicAnswer(cmd, true, "success");
    } catch (Exception e) {
        s_logger.error("Unexpected exception: ", e);
        return new PlugNicAnswer(cmd, false, "Unable to execute PlugNicCommand due to " + e.toString());
    }
}
Also used : HostMO(com.cloud.hypervisor.vmware.mo.HostMO) VirtualDeviceConfigSpec(com.vmware.vim25.VirtualDeviceConfigSpec) VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) VirtualDevice(com.vmware.vim25.VirtualDevice) VirtualEthernetCardType(com.cloud.hypervisor.vmware.mo.VirtualEthernetCardType) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) ClusterMO(com.cloud.hypervisor.vmware.mo.ClusterMO) 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) VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) VirtualMachineConfigSpec(com.vmware.vim25.VirtualMachineConfigSpec) UnPlugNicAnswer(com.cloud.agent.api.UnPlugNicAnswer) PlugNicAnswer(com.cloud.agent.api.PlugNicAnswer) NicTO(com.cloud.agent.api.to.NicTO) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) DatacenterMO(com.cloud.hypervisor.vmware.mo.DatacenterMO)

Aggregations

VmwareContext (com.cloud.hypervisor.vmware.util.VmwareContext)76 RemoteException (java.rmi.RemoteException)58 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)47 VmwareHypervisorHost (com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost)45 UnsupportedEncodingException (java.io.UnsupportedEncodingException)35 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)32 VirtualMachineMO (com.cloud.hypervisor.vmware.mo.VirtualMachineMO)28 IOException (java.io.IOException)26 CloudException (com.cloud.exception.CloudException)25 ConfigurationException (javax.naming.ConfigurationException)25 InternalErrorException (com.cloud.exception.InternalErrorException)23 ConnectException (java.net.ConnectException)23 DatastoreMO (com.cloud.hypervisor.vmware.mo.DatastoreMO)16 VmwareManager (com.cloud.hypervisor.vmware.manager.VmwareManager)14 HostMO (com.cloud.hypervisor.vmware.mo.HostMO)13 VolumeObjectTO (org.apache.cloudstack.storage.to.VolumeObjectTO)13 DataStoreTO (com.cloud.agent.api.to.DataStoreTO)12 PrimaryDataStoreTO (org.apache.cloudstack.storage.to.PrimaryDataStoreTO)12 DatacenterMO (com.cloud.hypervisor.vmware.mo.DatacenterMO)11 CreatePrivateTemplateAnswer (com.cloud.agent.api.storage.CreatePrivateTemplateAnswer)10