Search in sources :

Example 46 with NicTO

use of com.cloud.agent.api.to.NicTO in project CloudStack-archive by CloudStack-extras.

the class CloudZonesComputingResource method execute.

@Override
protected synchronized StartAnswer execute(StartCommand cmd) {
    VirtualMachineTO vmSpec = cmd.getVirtualMachine();
    String vmName = vmSpec.getName();
    LibvirtVMDef vm = null;
    State state = State.Stopped;
    Connect conn = null;
    try {
        conn = LibvirtConnection.getConnection();
        synchronized (_vms) {
            _vms.put(vmName, State.Starting);
        }
        vm = createVMFromSpec(vmSpec);
        createVbd(conn, vmSpec, vmName, vm);
        createVifs(conn, vmSpec, vm);
        s_logger.debug("starting " + vmName + ": " + vm.toString());
        startDomain(conn, vmName, vm.toString());
        NicTO[] nics = vmSpec.getNics();
        for (NicTO nic : nics) {
            if (nic.isSecurityGroupEnabled()) {
                if (vmSpec.getType() != VirtualMachine.Type.User) {
                    default_network_rules_for_systemvm(conn, vmName);
                } else {
                    nic.setIp(null);
                    default_network_rules(conn, vmName, nic, vmSpec.getId());
                }
            }
        }
        // attached disk
        for (DiskDef disk : vm.getDevices().getDisks()) {
            if (disk.isAttachDeferred()) {
                attachOrDetachDevice(conn, true, vmName, disk.toString());
            }
        }
        if (vmSpec.getType() == VirtualMachine.Type.User) {
            for (NicTO nic : nics) {
                if (nic.getType() == TrafficType.Guest) {
                    InetAddress ipAddr = _dhcpSnooper.getIPAddr(nic.getMac(), vmName);
                    if (ipAddr == null) {
                        s_logger.debug("Failed to get guest DHCP ip, stop it");
                        StopCommand stpCmd = new StopCommand(vmName);
                        execute(stpCmd);
                        return new StartAnswer(cmd, "Failed to get guest DHCP ip, stop it");
                    }
                    s_logger.debug(ipAddr);
                    nic.setIp(ipAddr.getHostAddress());
                    post_default_network_rules(conn, vmName, nic, vmSpec.getId(), _dhcpSnooper.getDhcpServerIP(), _hostIp, _hostMacAddress);
                    _vmDataServer.handleVmStarted(cmd.getVirtualMachine());
                }
            }
        }
        state = State.Running;
        return new StartAnswer(cmd);
    } catch (Exception e) {
        s_logger.warn("Exception ", e);
        if (conn != null) {
            handleVmStartFailure(conn, vmName, vm);
        }
        return new StartAnswer(cmd, e.getMessage());
    } finally {
        synchronized (_vms) {
            if (state != State.Stopped) {
                _vms.put(vmName, state);
            } else {
                _vms.remove(vmName);
            }
        }
    }
}
Also used : LibvirtVMDef(com.cloud.agent.resource.computing.LibvirtVMDef) DiskDef(com.cloud.agent.resource.computing.LibvirtVMDef.DiskDef) StopCommand(com.cloud.agent.api.StopCommand) StartAnswer(com.cloud.agent.api.StartAnswer) State(com.cloud.vm.VirtualMachine.State) Connect(org.libvirt.Connect) InetAddress(java.net.InetAddress) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) ConfigurationException(javax.naming.ConfigurationException) LibvirtException(org.libvirt.LibvirtException) NicTO(com.cloud.agent.api.to.NicTO)

Example 47 with NicTO

use of com.cloud.agent.api.to.NicTO in project CloudStack-archive by CloudStack-extras.

the class LibvirtComputingResource method VifHotPlug.

private void VifHotPlug(Connect conn, String vmName, String vlanId, String macAddr) throws InternalErrorException, LibvirtException {
    NicTO nicTO = new NicTO();
    nicTO.setMac(macAddr);
    nicTO.setType(TrafficType.Public);
    if (vlanId == null) {
        nicTO.setBroadcastType(BroadcastDomainType.Native);
    } else {
        nicTO.setBroadcastType(BroadcastDomainType.Vlan);
        nicTO.setBroadcastUri(BroadcastDomainType.Vlan.toUri(vlanId));
    }
    InterfaceDef nic = createVif(conn, nicTO, InterfaceDef.nicModel.VIRTIO);
    Domain vm = getDomain(conn, vmName);
    vm.attachDevice(nic.toString());
}
Also used : InterfaceDef(com.cloud.agent.resource.computing.LibvirtVMDef.InterfaceDef) Domain(org.libvirt.Domain) NicTO(com.cloud.agent.api.to.NicTO)

Example 48 with NicTO

use of com.cloud.agent.api.to.NicTO in project CloudStack-archive by CloudStack-extras.

the class JettyVmDataServer method handleVmStarted.

@Override
public void handleVmStarted(VirtualMachineTO vm) {
    for (NicTO nic : vm.getNics()) {
        if (nic.getType() == TrafficType.Guest) {
            if (nic.getIp() != null) {
                String ipv4File = _vmDataDir + File.separator + vm.getName() + File.separator + "local-ipv4";
                try {
                    _fs.create(_vmDataDir + File.separator + vm.getName(), "local-ipv4");
                    BufferedWriter writer = new BufferedWriter(new FileWriter(ipv4File));
                    writer.write(nic.getIp());
                    _ipVmMap.put(nic.getIp(), vm.getName());
                    writer.close();
                } catch (IOException e) {
                    s_logger.warn("Failed to create or write to local-ipv4 file " + ipv4File, e);
                }
            }
        }
    }
}
Also used : FileWriter(java.io.FileWriter) IOException(java.io.IOException) NicTO(com.cloud.agent.api.to.NicTO) BufferedWriter(java.io.BufferedWriter)

Example 49 with NicTO

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

the class CommandSetupHelper method createNetworkACLsCommands.

public void createNetworkACLsCommands(final List<? extends NetworkACLItem> rules, final VirtualRouter router, final Commands cmds, final long guestNetworkId, final boolean privateGateway) {
    final List<NetworkACLTO> rulesTO = new ArrayList<NetworkACLTO>();
    String guestVlan = null;
    final Network guestNtwk = _networkDao.findById(guestNetworkId);
    final URI uri = guestNtwk.getBroadcastUri();
    if (uri != null) {
        guestVlan = BroadcastDomainType.getValue(uri);
    }
    if (rules != null) {
        for (final NetworkACLItem rule : rules) {
            final NetworkACLTO ruleTO = new NetworkACLTO(rule, guestVlan, rule.getTrafficType());
            rulesTO.add(ruleTO);
        }
    }
    NicTO nicTO = _networkHelper.getNicTO(router, guestNetworkId, null);
    final SetNetworkACLCommand cmd = new SetNetworkACLCommand(rulesTO, nicTO);
    cmd.setAccessDetail(NetworkElementCommand.ROUTER_IP, _routerControlHelper.getRouterControlIp(router.getId()));
    cmd.setAccessDetail(NetworkElementCommand.ROUTER_GUEST_IP, _routerControlHelper.getRouterIpInNetwork(guestNetworkId, router.getId()));
    cmd.setAccessDetail(NetworkElementCommand.GUEST_VLAN_TAG, guestVlan);
    cmd.setAccessDetail(NetworkElementCommand.ROUTER_NAME, router.getInstanceName());
    final DataCenterVO dcVo = _dcDao.findById(router.getDataCenterId());
    cmd.setAccessDetail(NetworkElementCommand.ZONE_NETWORK_TYPE, dcVo.getNetworkType().toString());
    if (privateGateway) {
        cmd.setAccessDetail(NetworkElementCommand.VPC_PRIVATE_GATEWAY, String.valueOf(VpcGateway.Type.Private));
    }
    cmds.addCommand(cmd);
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) NetworkACLItem(com.cloud.network.vpc.NetworkACLItem) NetworkACLTO(com.cloud.agent.api.to.NetworkACLTO) Network(com.cloud.network.Network) ArrayList(java.util.ArrayList) SetNetworkACLCommand(com.cloud.agent.api.routing.SetNetworkACLCommand) URI(java.net.URI) NicTO(com.cloud.agent.api.to.NicTO)

Example 50 with NicTO

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

the class XenServer610MigrateWithStorageSendCommandWrapper method execute.

@Override
public Answer execute(final MigrateWithStorageSendCommand command, final XenServer610Resource xenServer610Resource) {
    final Connection connection = xenServer610Resource.getConnection();
    final VirtualMachineTO vmSpec = command.getVirtualMachine();
    final List<Pair<VolumeTO, Object>> volumeToSr = command.getVolumeToSr();
    final List<Pair<NicTO, Object>> nicToNetwork = command.getNicToNetwork();
    final Map<String, String> token = command.getToken();
    final String vmName = vmSpec.getName();
    Task task = null;
    try {
        // In a cluster management server setup, the migrate with storage receive and send
        // commands and answers may have to be forwarded to another management server. This
        // happens when the host/resource on which the command has to be executed is owned
        // by the second management server. The serialization/deserialization of the command
        // and answers fails as the xapi SR and Network class type isn't understand by the
        // agent attache. Seriliaze the SR and Network objects here to a string and pass in
        // the answer object. It'll be deserialzed and object created in migrate with
        // storage send command execution.
        Gson gson = new Gson();
        final Map<String, String> other = new HashMap<String, String>();
        other.put("live", "true");
        // Create the vdi map which tells what volumes of the vm need to go
        // on which sr on the destination.
        final Map<VDI, SR> vdiMap = new HashMap<VDI, SR>();
        for (final Pair<VolumeTO, Object> entry : volumeToSr) {
            if (entry.second() instanceof SR) {
                final SR sr = (SR) entry.second();
                final VDI vdi = xenServer610Resource.getVDIbyUuid(connection, entry.first().getPath());
                vdiMap.put(vdi, sr);
            } else {
                throw new CloudRuntimeException("The object " + entry.second() + " passed is not of type SR.");
            }
        }
        final Set<VM> vms = VM.getByNameLabel(connection, vmSpec.getName());
        VM vmToMigrate = null;
        if (vms != null) {
            vmToMigrate = vms.iterator().next();
        }
        // Create the vif map.
        final Map<VIF, Network> vifMap = new HashMap<VIF, Network>();
        for (final Pair<NicTO, Object> entry : nicToNetwork) {
            if (entry.second() instanceof Network) {
                final Network network = (Network) entry.second();
                final VIF vif = xenServer610Resource.getVifByMac(connection, vmToMigrate, entry.first().getMac());
                vifMap.put(vif, network);
            } else {
                throw new CloudRuntimeException("The object " + entry.second() + " passed is not of type Network.");
            }
        }
        // Check migration with storage is possible.
        task = vmToMigrate.assertCanMigrateAsync(connection, token, true, vdiMap, vifMap, other);
        try {
            // poll every 1 seconds.
            final long timeout = xenServer610Resource.getMigrateWait() * 1000L;
            xenServer610Resource.waitForTask(connection, task, 1000, timeout);
            xenServer610Resource.checkForSuccess(connection, task);
        } catch (final Types.HandleInvalid e) {
            s_logger.error("Error while checking if vm " + vmName + " can be migrated.", e);
            throw new CloudRuntimeException("Error while checking if vm " + vmName + " can be migrated.", e);
        }
        // Migrate now.
        task = vmToMigrate.migrateSendAsync(connection, token, true, vdiMap, vifMap, other);
        try {
            // poll every 1 seconds.
            final long timeout = xenServer610Resource.getMigrateWait() * 1000L;
            xenServer610Resource.waitForTask(connection, task, 1000, timeout);
            xenServer610Resource.checkForSuccess(connection, task);
        } catch (final Types.HandleInvalid e) {
            s_logger.error("Error while migrating vm " + vmName, e);
            throw new CloudRuntimeException("Error while migrating vm " + vmName, e);
        }
        final Set<VolumeTO> volumeToSet = null;
        return new MigrateWithStorageSendAnswer(command, volumeToSet);
    } catch (final CloudRuntimeException e) {
        s_logger.error("Migration of vm " + vmName + " with storage failed due to " + e.toString(), e);
        return new MigrateWithStorageSendAnswer(command, e);
    } catch (final Exception e) {
        s_logger.error("Migration of vm " + vmName + " with storage failed due to " + e.toString(), e);
        return new MigrateWithStorageSendAnswer(command, e);
    } finally {
        if (task != null) {
            try {
                task.destroy(connection);
            } catch (final Exception e) {
                s_logger.debug("Unable to destroy task " + task.toString() + " on host " + xenServer610Resource.getHost().getUuid() + " due to " + e.toString());
            }
        }
    }
}
Also used : Types(com.xensource.xenapi.Types) Task(com.xensource.xenapi.Task) MigrateWithStorageSendAnswer(com.cloud.agent.api.MigrateWithStorageSendAnswer) HashMap(java.util.HashMap) Gson(com.google.gson.Gson) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) VolumeTO(com.cloud.agent.api.to.VolumeTO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Network(com.xensource.xenapi.Network) VDI(com.xensource.xenapi.VDI) Pair(com.cloud.utils.Pair) SR(com.xensource.xenapi.SR) NicTO(com.cloud.agent.api.to.NicTO) Connection(com.xensource.xenapi.Connection) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VIF(com.xensource.xenapi.VIF) VM(com.xensource.xenapi.VM)

Aggregations

NicTO (com.cloud.agent.api.to.NicTO)99 VirtualMachineTO (com.cloud.agent.api.to.VirtualMachineTO)42 Answer (com.cloud.agent.api.Answer)31 Test (org.junit.Test)30 InternalErrorException (com.cloud.exception.InternalErrorException)28 LibvirtException (org.libvirt.LibvirtException)27 ArrayList (java.util.ArrayList)25 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)24 ConfigurationException (javax.naming.ConfigurationException)23 Connect (org.libvirt.Connect)23 URISyntaxException (java.net.URISyntaxException)22 AttachAnswer (org.apache.cloudstack.storage.command.AttachAnswer)21 IOException (java.io.IOException)20 CheckRouterAnswer (com.cloud.agent.api.CheckRouterAnswer)19 LibvirtRequestWrapper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper)19 LibvirtUtilitiesHelper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper)19 Connection (com.xensource.xenapi.Connection)18 KVMStoragePoolManager (com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager)14 UnPlugNicCommand (com.cloud.agent.api.UnPlugNicCommand)13 URI (java.net.URI)12