Search in sources :

Example 1 with LoadBalancerConfigurator

use of com.cloud.network.LoadBalancerConfigurator in project cloudstack by apache.

the class LoadBalancerConfigItem method generateConfig.

@Override
public List<ConfigItem> generateConfig(final NetworkElementCommand cmd) {
    final LoadBalancerConfigCommand command = (LoadBalancerConfigCommand) cmd;
    final LoadBalancerConfigurator cfgtr = new HAProxyConfigurator();
    final String[] configuration = cfgtr.generateConfiguration(command);
    String routerIp = command.getNic().getIp();
    if (command.getVpcId() == null) {
        routerIp = cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP);
    }
    final String tmpCfgFilePath = "/etc/haproxy/";
    final String tmpCfgFileName = "haproxy.cfg.new." + String.valueOf(System.currentTimeMillis());
    final String[][] allRules = cfgtr.generateFwRules(command);
    final String[] addRules = allRules[LoadBalancerConfigurator.ADD];
    final String[] removeRules = allRules[LoadBalancerConfigurator.REMOVE];
    final String[] statRules = allRules[LoadBalancerConfigurator.STATS];
    final LoadBalancerRule loadBalancerRule = new LoadBalancerRule(configuration, tmpCfgFilePath, tmpCfgFileName, addRules, removeRules, statRules, routerIp);
    final List<LoadBalancerRule> rules = new LinkedList<LoadBalancerRule>();
    rules.add(loadBalancerRule);
    final LoadBalancerRules configRules = new LoadBalancerRules(rules);
    return generateConfigItems(configRules);
}
Also used : HAProxyConfigurator(com.cloud.network.HAProxyConfigurator) LoadBalancerRule(com.cloud.agent.resource.virtualnetwork.model.LoadBalancerRule) LoadBalancerRules(com.cloud.agent.resource.virtualnetwork.model.LoadBalancerRules) LoadBalancerConfigurator(com.cloud.network.LoadBalancerConfigurator) LinkedList(java.util.LinkedList) LoadBalancerConfigCommand(com.cloud.agent.api.routing.LoadBalancerConfigCommand)

Example 2 with LoadBalancerConfigurator

use of com.cloud.network.LoadBalancerConfigurator in project cloudstack by apache.

the class HypervDirectConnectResource method execute.

protected Answer execute(final LoadBalancerConfigCommand cmd) {
    if (cmd.getVpcId() != null) {
    // return VPCLoadBalancerConfig(cmd);
    }
    final File keyFile = getSystemVMKeyFile();
    final String routerIp = cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP);
    final String controlIp = getRouterSshControlIp(cmd);
    assert controlIp != null;
    final LoadBalancerConfigurator cfgtr = new HAProxyConfigurator();
    final String[] config = cfgtr.generateConfiguration(cmd);
    final String[][] rules = cfgtr.generateFwRules(cmd);
    final String tmpCfgFilePath = "/tmp/" + routerIp.replace('.', '_') + ".cfg";
    final StringBuilder tmpCfgFileContents = new StringBuilder();
    for (int i = 0; i < config.length; i++) {
        tmpCfgFileContents.append(config[i]);
        tmpCfgFileContents.append("\n");
    }
    try {
        SshHelper.scpTo(controlIp, DEFAULT_DOMR_SSHPORT, "root", keyFile, null, "/tmp/", tmpCfgFileContents.toString().getBytes(Charset.forName("UTF-8")), routerIp.replace('.', '_') + ".cfg", null);
        try {
            final String[] addRules = rules[LoadBalancerConfigurator.ADD];
            final String[] removeRules = rules[LoadBalancerConfigurator.REMOVE];
            final String[] statRules = rules[LoadBalancerConfigurator.STATS];
            String args = "";
            args += "-i " + routerIp;
            args += " -f " + tmpCfgFilePath;
            StringBuilder sb = new StringBuilder();
            if (addRules.length > 0) {
                for (int i = 0; i < addRules.length; i++) {
                    sb.append(addRules[i]).append(',');
                }
                args += " -a " + sb.toString();
            }
            sb = new StringBuilder();
            if (removeRules.length > 0) {
                for (int i = 0; i < removeRules.length; i++) {
                    sb.append(removeRules[i]).append(',');
                }
                args += " -d " + sb.toString();
            }
            sb = new StringBuilder();
            if (statRules.length > 0) {
                for (int i = 0; i < statRules.length; i++) {
                    sb.append(statRules[i]).append(',');
                }
                args += " -s " + sb.toString();
            }
            Pair<Boolean, String> result = SshHelper.sshExecute(controlIp, DEFAULT_DOMR_SSHPORT, "root", getSystemVMKeyFile(), null, "scp " + tmpCfgFilePath + " /etc/haproxy/haproxy.cfg.new");
            if (!result.first()) {
                s_logger.error("Unable to copy haproxy configuration file");
                return new Answer(cmd, false, "LoadBalancerConfigCommand failed due to unable to copy haproxy configuration file");
            }
            final String command = String.format("%s%s %s", "/root/", VRScripts.LB, args);
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("Run command on domain router " + routerIp + command);
            }
            result = SshHelper.sshExecute(controlIp, DEFAULT_DOMR_SSHPORT, "root", getSystemVMKeyFile(), null, command);
            if (!result.first()) {
                final String msg = "LoadBalancerConfigCommand on domain router " + routerIp + " failed. message: " + result.second();
                s_logger.error(msg);
                return new Answer(cmd, false, msg);
            }
            if (s_logger.isInfoEnabled()) {
                s_logger.info("LoadBalancerConfigCommand on domain router " + routerIp + " completed");
            }
        } finally {
            SshHelper.sshExecute(controlIp, DEFAULT_DOMR_SSHPORT, "root", getSystemVMKeyFile(), null, "rm " + tmpCfgFilePath);
        }
        return new Answer(cmd);
    } catch (final Throwable e) {
        s_logger.error("Unexpected exception: " + e.toString(), e);
        return new Answer(cmd, false, "LoadBalancerConfigCommand failed due to " + e.getMessage());
    }
}
Also used : HAProxyConfigurator(com.cloud.network.HAProxyConfigurator) UnsupportedAnswer(com.cloud.agent.api.UnsupportedAnswer) CheckSshAnswer(com.cloud.agent.api.check.CheckSshAnswer) GetDomRVersionAnswer(com.cloud.agent.api.GetDomRVersionAnswer) CheckS2SVpnConnectionsAnswer(com.cloud.agent.api.CheckS2SVpnConnectionsAnswer) SetPortForwardingRulesAnswer(com.cloud.agent.api.routing.SetPortForwardingRulesAnswer) SetSourceNatAnswer(com.cloud.agent.api.routing.SetSourceNatAnswer) PlugNicAnswer(com.cloud.agent.api.PlugNicAnswer) GetVmConfigAnswer(com.cloud.agent.api.GetVmConfigAnswer) NetworkUsageAnswer(com.cloud.agent.api.NetworkUsageAnswer) Answer(com.cloud.agent.api.Answer) UnPlugNicAnswer(com.cloud.agent.api.UnPlugNicAnswer) SetStaticNatRulesAnswer(com.cloud.agent.api.routing.SetStaticNatRulesAnswer) IpAssocAnswer(com.cloud.agent.api.routing.IpAssocAnswer) SetFirewallRulesAnswer(com.cloud.agent.api.routing.SetFirewallRulesAnswer) CheckRouterAnswer(com.cloud.agent.api.CheckRouterAnswer) SetStaticRouteAnswer(com.cloud.agent.api.routing.SetStaticRouteAnswer) LoadBalancerConfigurator(com.cloud.network.LoadBalancerConfigurator) File(java.io.File)

Example 3 with LoadBalancerConfigurator

use of com.cloud.network.LoadBalancerConfigurator in project cosmic by MissionCriticalCloud.

the class LoadBalancerConfigItem method generateConfig.

@Override
public List<ConfigItem> generateConfig(final NetworkElementCommand cmd) {
    final LoadBalancerConfigCommand command = (LoadBalancerConfigCommand) cmd;
    final LoadBalancerConfigurator cfgtr = new HAProxyConfigurator();
    final String[] configuration = cfgtr.generateConfiguration(command);
    String routerIp = command.getNic().getIp();
    if (command.getVpcId() == null) {
        routerIp = cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP);
    }
    final String tmpCfgFilePath = "/etc/haproxy/";
    final String tmpCfgFileName = "haproxy.cfg.new." + String.valueOf(System.currentTimeMillis());
    final String[][] allRules = cfgtr.generateFwRules(command);
    final String[] addRules = allRules[LoadBalancerConfigurator.ADD];
    final String[] removeRules = allRules[LoadBalancerConfigurator.REMOVE];
    final String[] statRules = allRules[LoadBalancerConfigurator.STATS];
    final LoadBalancerRule loadBalancerRule = new LoadBalancerRule(configuration, tmpCfgFilePath, tmpCfgFileName, addRules, removeRules, statRules, routerIp);
    final List<LoadBalancerRule> rules = new LinkedList<>();
    rules.add(loadBalancerRule);
    final LoadBalancerRules configRules = new LoadBalancerRules(rules);
    return generateConfigItems(configRules);
}
Also used : HAProxyConfigurator(com.cloud.network.HAProxyConfigurator) LoadBalancerRule(com.cloud.agent.resource.virtualnetwork.model.LoadBalancerRule) LoadBalancerRules(com.cloud.agent.resource.virtualnetwork.model.LoadBalancerRules) LoadBalancerConfigurator(com.cloud.network.LoadBalancerConfigurator) LinkedList(java.util.LinkedList) LoadBalancerConfigCommand(com.cloud.agent.api.routing.LoadBalancerConfigCommand)

Aggregations

HAProxyConfigurator (com.cloud.network.HAProxyConfigurator)3 LoadBalancerConfigurator (com.cloud.network.LoadBalancerConfigurator)3 LoadBalancerConfigCommand (com.cloud.agent.api.routing.LoadBalancerConfigCommand)2 LoadBalancerRule (com.cloud.agent.resource.virtualnetwork.model.LoadBalancerRule)2 LoadBalancerRules (com.cloud.agent.resource.virtualnetwork.model.LoadBalancerRules)2 LinkedList (java.util.LinkedList)2 Answer (com.cloud.agent.api.Answer)1 CheckRouterAnswer (com.cloud.agent.api.CheckRouterAnswer)1 CheckS2SVpnConnectionsAnswer (com.cloud.agent.api.CheckS2SVpnConnectionsAnswer)1 GetDomRVersionAnswer (com.cloud.agent.api.GetDomRVersionAnswer)1 GetVmConfigAnswer (com.cloud.agent.api.GetVmConfigAnswer)1 NetworkUsageAnswer (com.cloud.agent.api.NetworkUsageAnswer)1 PlugNicAnswer (com.cloud.agent.api.PlugNicAnswer)1 UnPlugNicAnswer (com.cloud.agent.api.UnPlugNicAnswer)1 UnsupportedAnswer (com.cloud.agent.api.UnsupportedAnswer)1 CheckSshAnswer (com.cloud.agent.api.check.CheckSshAnswer)1 IpAssocAnswer (com.cloud.agent.api.routing.IpAssocAnswer)1 SetFirewallRulesAnswer (com.cloud.agent.api.routing.SetFirewallRulesAnswer)1 SetPortForwardingRulesAnswer (com.cloud.agent.api.routing.SetPortForwardingRulesAnswer)1 SetSourceNatAnswer (com.cloud.agent.api.routing.SetSourceNatAnswer)1