Search in sources :

Example 51 with NicTO

use of com.cloud.legacymodel.to.NicTO in project cosmic by MissionCriticalCloud.

the class UserVmManagerImpl method finalizeStart.

@Override
public boolean finalizeStart(final VirtualMachineProfile profile, final long hostId, final Commands cmds, final ReservationContext context) {
    final UserVmVO vm = _vmDao.findById(profile.getId());
    final Answer[] answersToCmds = cmds.getAnswers();
    if (answersToCmds == null) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Returning from finalizeStart() since there are no answers to read");
        }
        return true;
    }
    final Answer startAnswer = cmds.getAnswer(StartAnswer.class);
    String returnedIp = null;
    String originalIp = null;
    if (startAnswer != null) {
        final StartAnswer startAns = (StartAnswer) startAnswer;
        final VirtualMachineTO vmTO = startAns.getVirtualMachine();
        for (final NicTO nicTO : vmTO.getNics()) {
            if (nicTO.getType() == TrafficType.Guest) {
                returnedIp = nicTO.getIp();
            }
        }
    }
    final List<NicVO> nics = _nicDao.listByVmId(vm.getId());
    NicVO guestNic = null;
    NetworkVO guestNetwork = null;
    for (final NicVO nic : nics) {
        final NetworkVO network = _networkDao.findById(nic.getNetworkId());
        final long isDefault = nic.isDefaultNic() ? 1 : 0;
        if (network.getTrafficType() == TrafficType.Guest) {
            originalIp = nic.getIPv4Address();
            guestNic = nic;
            guestNetwork = network;
            if (nic.getBroadcastUri().getScheme().equals("pvlan")) {
                final NicProfile nicProfile = new NicProfile(nic, network, nic.getBroadcastUri(), nic.getIsolationUri(), 0, "pvlan-nic");
                if (!setupVmForPvlan(true, hostId, nicProfile)) {
                    return false;
                }
            }
        }
    }
    boolean ipChanged = false;
    if (originalIp != null && !originalIp.equalsIgnoreCase(returnedIp)) {
        if (returnedIp != null && guestNic != null) {
            guestNic.setIPv4Address(returnedIp);
            ipChanged = true;
        }
    }
    if (returnedIp != null && !returnedIp.equalsIgnoreCase(originalIp)) {
        if (guestNic != null) {
            guestNic.setIPv4Address(returnedIp);
            ipChanged = true;
        }
    }
    // get system ip and create static nat rule for the vm
    try {
        _rulesMgr.getSystemIpAndEnableStaticNatForVm(profile.getVirtualMachine(), false);
    } catch (final Exception ex) {
        s_logger.warn("Failed to get system ip and enable static nat for the vm " + profile.getVirtualMachine() + " due to exception ", ex);
        return false;
    }
    final Answer answer = cmds.getAnswer("restoreVMSnapshot");
    if (answer != null && answer instanceof RestoreVMSnapshotAnswer) {
        final RestoreVMSnapshotAnswer restoreVMSnapshotAnswer = (RestoreVMSnapshotAnswer) answer;
        if (!restoreVMSnapshotAnswer.getResult()) {
            s_logger.warn("Unable to restore the vm snapshot from image file to the VM: " + restoreVMSnapshotAnswer.getDetails());
        }
    }
    return true;
}
Also used : NetworkVO(com.cloud.network.dao.NetworkVO) StartAnswer(com.cloud.legacymodel.communication.answer.StartAnswer) RestoreVMSnapshotAnswer(com.cloud.legacymodel.communication.answer.RestoreVMSnapshotAnswer) VirtualMachineTO(com.cloud.legacymodel.to.VirtualMachineTO) PermissionDeniedException(com.cloud.legacymodel.exceptions.PermissionDeniedException) TransactionCallbackWithException(com.cloud.utils.db.TransactionCallbackWithException) ConcurrentOperationException(com.cloud.legacymodel.exceptions.ConcurrentOperationException) OperationTimedoutException(com.cloud.legacymodel.exceptions.OperationTimedoutException) InsufficientAddressCapacityException(com.cloud.legacymodel.exceptions.InsufficientAddressCapacityException) VirtualMachineMigrationException(com.cloud.legacymodel.exceptions.VirtualMachineMigrationException) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) ExecutionException(com.cloud.legacymodel.exceptions.ExecutionException) ResourceAllocationException(com.cloud.legacymodel.exceptions.ResourceAllocationException) CloudException(com.cloud.legacymodel.exceptions.CloudException) NoTransitionException(com.cloud.legacymodel.exceptions.NoTransitionException) InsufficientCapacityException(com.cloud.legacymodel.exceptions.InsufficientCapacityException) AgentUnavailableException(com.cloud.legacymodel.exceptions.AgentUnavailableException) ConfigurationException(javax.naming.ConfigurationException) StorageUnavailableException(com.cloud.legacymodel.exceptions.StorageUnavailableException) ResourceUnavailableException(com.cloud.legacymodel.exceptions.ResourceUnavailableException) ManagementServerException(com.cloud.legacymodel.exceptions.ManagementServerException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) GetVmDiskStatsAnswer(com.cloud.legacymodel.communication.answer.GetVmDiskStatsAnswer) GetVmStatsAnswer(com.cloud.legacymodel.communication.answer.GetVmStatsAnswer) RestoreVMSnapshotAnswer(com.cloud.legacymodel.communication.answer.RestoreVMSnapshotAnswer) StartAnswer(com.cloud.legacymodel.communication.answer.StartAnswer) Answer(com.cloud.legacymodel.communication.answer.Answer) NicTO(com.cloud.legacymodel.to.NicTO)

Aggregations

NicTO (com.cloud.legacymodel.to.NicTO)51 VirtualMachineTO (com.cloud.legacymodel.to.VirtualMachineTO)28 Answer (com.cloud.legacymodel.communication.answer.Answer)26 Test (org.junit.Test)25 LibvirtException (org.libvirt.LibvirtException)23 AttachAnswer (com.cloud.legacymodel.communication.answer.AttachAnswer)21 LibvirtRequestWrapper (com.cloud.agent.resource.kvm.wrapper.LibvirtRequestWrapper)19 LibvirtUtilitiesHelper (com.cloud.agent.resource.kvm.wrapper.LibvirtUtilitiesHelper)19 CheckRouterAnswer (com.cloud.legacymodel.communication.answer.CheckRouterAnswer)19 Connect (org.libvirt.Connect)19 ArrayList (java.util.ArrayList)16 KvmStoragePoolManager (com.cloud.agent.resource.kvm.storage.KvmStoragePoolManager)14 Connection (com.xensource.xenapi.Connection)14 InternalErrorException (com.cloud.legacymodel.exceptions.InternalErrorException)11 URISyntaxException (java.net.URISyntaxException)11 VifDriver (com.cloud.agent.resource.kvm.vif.VifDriver)10 LibvirtVmDef (com.cloud.agent.resource.kvm.xml.LibvirtVmDef)10 Host (com.cloud.legacymodel.dc.Host)10 UnPlugNicCommand (com.cloud.legacymodel.communication.command.UnPlugNicCommand)9 HashMap (java.util.HashMap)8