Search in sources :

Example 31 with Commands

use of com.cloud.agent.manager.Commands in project cloudstack by apache.

the class BasicNetworkVisitor method visit.

@Override
public boolean visit(final DhcpSubNetRules subnet) throws ResourceUnavailableException {
    final VirtualRouter router = subnet.getRouter();
    final Network network = subnet.getNetwork();
    final NicIpAliasVO nicAlias = subnet.getNicAlias();
    final String routerAliasIp = subnet.getRouterAliasIp();
    final Commands cmds = new Commands(Command.OnError.Stop);
    final List<IpAliasTO> ipaliasTo = new ArrayList<IpAliasTO>();
    ipaliasTo.add(new IpAliasTO(routerAliasIp, nicAlias.getNetmask(), nicAlias.getAliasCount().toString()));
    _commandSetupHelper.createIpAlias(router, ipaliasTo, nicAlias.getNetworkId(), cmds);
    // also add the required configuration to the dnsmasq for supporting
    // dhcp and dns on the new ip.
    _commandSetupHelper.configDnsMasq(router, network, cmds);
    return _networkGeneralHelper.sendCommandsToRouter(router, cmds);
}
Also used : Network(com.cloud.network.Network) Commands(com.cloud.agent.manager.Commands) ArrayList(java.util.ArrayList) IpAliasTO(com.cloud.agent.api.routing.IpAliasTO) NicIpAliasVO(com.cloud.vm.dao.NicIpAliasVO) VirtualRouter(com.cloud.network.router.VirtualRouter)

Example 32 with Commands

use of com.cloud.agent.manager.Commands in project cloudstack by apache.

the class OvsTunnelManagerImpl method checkAndCreateTunnel.

@DB
protected void checkAndCreateTunnel(Network nw, Host host) {
    s_logger.debug("Creating tunnels with OVS tunnel manager");
    long hostId = host.getId();
    int key = getGreKey(nw);
    String bridgeName = generateBridgeName(nw, key);
    List<Long> toHostIds = new ArrayList<Long>();
    List<Long> fromHostIds = new ArrayList<Long>();
    List<Long> networkSpannedHosts = _ovsNetworkToplogyGuru.getNetworkSpanedHosts(nw.getId());
    for (Long rh : networkSpannedHosts) {
        if (rh == hostId) {
            continue;
        }
        OvsTunnelNetworkVO ta = _tunnelNetworkDao.getByFromToNetwork(hostId, rh.longValue(), nw.getId());
        // Try and create the tunnel even if a previous attempt failed
        if (ta == null || ta.getState().equals(OvsTunnel.State.Failed.name())) {
            s_logger.debug("Attempting to create tunnel from:" + hostId + " to:" + rh.longValue());
            if (ta == null) {
                createTunnelRecord(hostId, rh.longValue(), nw.getId(), key);
            }
            if (!toHostIds.contains(rh)) {
                toHostIds.add(rh);
            }
        }
        ta = _tunnelNetworkDao.getByFromToNetwork(rh.longValue(), hostId, nw.getId());
        // Try and create the tunnel even if a previous attempt failed
        if (ta == null || ta.getState().equals(OvsTunnel.State.Failed.name())) {
            s_logger.debug("Attempting to create tunnel from:" + rh.longValue() + " to:" + hostId);
            if (ta == null) {
                createTunnelRecord(rh.longValue(), hostId, nw.getId(), key);
            }
            if (!fromHostIds.contains(rh)) {
                fromHostIds.add(rh);
            }
        }
    }
    //TODO: Should we propagate the exception here?
    try {
        String myIp = getGreEndpointIP(host, nw);
        if (myIp == null)
            throw new GreTunnelException("Unable to retrieve the source " + "endpoint for the GRE tunnel." + "Failure is on host:" + host.getId());
        boolean noHost = true;
        for (Long i : toHostIds) {
            HostVO rHost = _hostDao.findById(i);
            String otherIp = getGreEndpointIP(rHost, nw);
            if (otherIp == null)
                throw new GreTunnelException("Unable to retrieve the remote " + "endpoint for the GRE tunnel." + "Failure is on host:" + rHost.getId());
            Commands cmds = new Commands(new OvsCreateTunnelCommand(otherIp, key, Long.valueOf(hostId), i, nw.getId(), myIp, bridgeName, nw.getUuid()));
            s_logger.debug("Attempting to create tunnel from:" + hostId + " to:" + i + " for the network " + nw.getId());
            s_logger.debug("Ask host " + hostId + " to create gre tunnel to " + i);
            Answer[] answers = _agentMgr.send(hostId, cmds);
            handleCreateTunnelAnswer(answers);
            noHost = false;
        }
        for (Long i : fromHostIds) {
            HostVO rHost = _hostDao.findById(i);
            String otherIp = getGreEndpointIP(rHost, nw);
            Commands cmds = new Commands(new OvsCreateTunnelCommand(myIp, key, i, Long.valueOf(hostId), nw.getId(), otherIp, bridgeName, nw.getUuid()));
            s_logger.debug("Ask host " + i + " to create gre tunnel to " + hostId);
            Answer[] answers = _agentMgr.send(i, cmds);
            handleCreateTunnelAnswer(answers);
            noHost = false;
        }
        // anyway. This will ensure VIF rules will be triggered
        if (noHost) {
            Commands cmds = new Commands(new OvsSetupBridgeCommand(bridgeName, hostId, nw.getId()));
            s_logger.debug("Ask host " + hostId + " to configure bridge for network:" + nw.getId());
            Answer[] answers = _agentMgr.send(hostId, cmds);
            handleSetupBridgeAnswer(answers);
        }
    } catch (GreTunnelException | OperationTimedoutException | AgentUnavailableException e) {
        // I really thing we should do a better handling of these exceptions
        s_logger.warn("Ovs Tunnel network created tunnel failed", e);
    }
}
Also used : OvsTunnelNetworkVO(com.cloud.network.ovs.dao.OvsTunnelNetworkVO) OperationTimedoutException(com.cloud.exception.OperationTimedoutException) OvsCreateTunnelCommand(com.cloud.agent.api.OvsCreateTunnelCommand) ArrayList(java.util.ArrayList) HostVO(com.cloud.host.HostVO) OvsFetchInterfaceAnswer(com.cloud.agent.api.OvsFetchInterfaceAnswer) Answer(com.cloud.agent.api.Answer) OvsCreateTunnelAnswer(com.cloud.agent.api.OvsCreateTunnelAnswer) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) Commands(com.cloud.agent.manager.Commands) OvsSetupBridgeCommand(com.cloud.agent.api.OvsSetupBridgeCommand) DB(com.cloud.utils.db.DB)

Example 33 with Commands

use of com.cloud.agent.manager.Commands in project cloudstack by apache.

the class SshKeysDistriMonitor method processConnect.

@Override
public void processConnect(Host host, StartupCommand cmd, boolean forRebalance) throws ConnectionException {
    if (cmd instanceof StartupRoutingCommand) {
        if (((StartupRoutingCommand) cmd).getHypervisorType() == HypervisorType.KVM || ((StartupRoutingCommand) cmd).getHypervisorType() == HypervisorType.XenServer || ((StartupRoutingCommand) cmd).getHypervisorType() == HypervisorType.LXC) {
            /*TODO: Get the private/public keys here*/
            String pubKey = _configDao.getValue("ssh.publickey");
            String prvKey = _configDao.getValue("ssh.privatekey");
            try {
                ModifySshKeysCommand cmds = new ModifySshKeysCommand(pubKey, prvKey);
                Commands c = new Commands(cmds);
                _agentMgr.send(host.getId(), c, this);
            } catch (AgentUnavailableException e) {
                s_logger.debug("Failed to send keys to agent: " + host.getId());
            }
        }
    }
}
Also used : AgentUnavailableException(com.cloud.exception.AgentUnavailableException) ModifySshKeysCommand(com.cloud.agent.api.ModifySshKeysCommand) Commands(com.cloud.agent.manager.Commands) StartupRoutingCommand(com.cloud.agent.api.StartupRoutingCommand)

Example 34 with Commands

use of com.cloud.agent.manager.Commands in project cloudstack by apache.

the class CloudZonesNetworkElement method addPasswordAndUserdata.

@Override
public boolean addPasswordAndUserdata(Network network, NicProfile nic, VirtualMachineProfile vm, DeployDestination dest, ReservationContext context) throws ConcurrentOperationException, InsufficientCapacityException, ResourceUnavailableException {
    if (canHandle(dest, network.getTrafficType())) {
        if (vm.getType() != VirtualMachine.Type.User) {
            return false;
        }
        @SuppressWarnings("unchecked") UserVmVO uservm = _userVmDao.findById(vm.getId());
        _userVmDao.loadDetails(uservm);
        String password = (String) vm.getParameter(VirtualMachineProfile.Param.VmPassword);
        String userData = uservm.getUserData();
        String sshPublicKey = uservm.getDetail("SSH.PublicKey");
        Commands cmds = new Commands(Command.OnError.Continue);
        if (password != null && nic.isDefaultNic()) {
            SavePasswordCommand cmd = new SavePasswordCommand(password, nic.getIPv4Address(), uservm.getHostName(), _networkMgr.getExecuteInSeqNtwkElmtCmd());
            cmds.addCommand("password", cmd);
        }
        String serviceOffering = _serviceOfferingDao.findByIdIncludingRemoved(uservm.getServiceOfferingId()).getDisplayText();
        String zoneName = _dcDao.findById(network.getDataCenterId()).getName();
        cmds.addCommand("vmdata", generateVmDataCommand(nic.getIPv4Address(), userData, serviceOffering, zoneName, nic.getIPv4Address(), uservm.getHostName(), uservm.getInstanceName(), uservm.getId(), uservm.getUuid(), sshPublicKey));
        try {
            _agentManager.send(dest.getHost().getId(), cmds);
        } catch (OperationTimedoutException e) {
            s_logger.debug("Unable to send vm data command to host " + dest.getHost());
            return false;
        }
        Answer dataAnswer = cmds.getAnswer("vmdata");
        if (dataAnswer != null && dataAnswer.getResult()) {
            s_logger.info("Sent vm data successfully to vm " + uservm.getInstanceName());
            return true;
        }
        s_logger.info("Failed to send vm data to vm " + uservm.getInstanceName());
        return false;
    }
    return false;
}
Also used : Answer(com.cloud.agent.api.Answer) UserVmVO(com.cloud.vm.UserVmVO) OperationTimedoutException(com.cloud.exception.OperationTimedoutException) SavePasswordCommand(com.cloud.agent.api.routing.SavePasswordCommand) Commands(com.cloud.agent.manager.Commands)

Example 35 with Commands

use of com.cloud.agent.manager.Commands in project cloudstack by apache.

the class RemoteHostEndPoint method sendMessageAsync.

@Override
public void sendMessageAsync(Command cmd, AsyncCompletionCallback<Answer> callback) {
    try {
        long newHostId = _hvGuruMgr.getGuruProcessedCommandTargetHost(hostId, cmd);
        if (newHostId != hostId) {
            // update endpoint with new host if changed
            setId(newHostId);
        }
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Sending command " + cmd.toString() + " to host: " + newHostId);
        }
        agentMgr.send(newHostId, new Commands(cmd), new CmdRunner(callback));
    } catch (AgentUnavailableException e) {
        throw new CloudRuntimeException("Unable to send message", e);
    }
}
Also used : AgentUnavailableException(com.cloud.exception.AgentUnavailableException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Commands(com.cloud.agent.manager.Commands)

Aggregations

Commands (com.cloud.agent.manager.Commands)64 AgentUnavailableException (com.cloud.exception.AgentUnavailableException)22 VirtualRouter (com.cloud.network.router.VirtualRouter)19 OperationTimedoutException (com.cloud.exception.OperationTimedoutException)17 Answer (com.cloud.agent.api.Answer)16 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)13 Network (com.cloud.network.Network)13 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)11 NicVO (com.cloud.vm.NicVO)10 UnPlugNicAnswer (com.cloud.agent.api.UnPlugNicAnswer)9 ArrayList (java.util.ArrayList)9 PlugNicAnswer (com.cloud.agent.api.PlugNicAnswer)8 UserVmVO (com.cloud.vm.UserVmVO)8 DataCenter (com.cloud.dc.DataCenter)7 NoTransitionException (com.cloud.utils.fsm.NoTransitionException)7 VirtualMachineProfile (com.cloud.vm.VirtualMachineProfile)7 AgentControlAnswer (com.cloud.agent.api.AgentControlAnswer)6 RestoreVMSnapshotAnswer (com.cloud.agent.api.RestoreVMSnapshotAnswer)6 StartAnswer (com.cloud.agent.api.StartAnswer)6 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)6