use of com.cloud.legacymodel.to.LoadBalancerTO in project cosmic by MissionCriticalCloud.
the class HAProxyConfigurator method generateConfiguration.
@Override
public String[] generateConfiguration(final LoadBalancerConfigCommand lbCmd) {
final List<String> result = new ArrayList<>();
final List<String> gSection = Arrays.asList(globalSection);
// Note that this is overwritten on the String in the static ArrayList<String>
gSection.set(2, "\tmaxconn " + lbCmd.maxconn);
final String pipesLine = "\tmaxpipes " + Long.toString(Long.parseLong(lbCmd.maxconn) / 4);
gSection.set(3, pipesLine);
if (s_logger.isDebugEnabled()) {
for (final String s : gSection) {
s_logger.debug("global section: " + s);
}
}
result.addAll(gSection);
result.add(blankLine);
final List<String> dSection = Arrays.asList(defaultsSection);
if (lbCmd.keepAliveEnabled) {
dSection.set(7, "\tno option forceclose");
}
if (s_logger.isDebugEnabled()) {
for (final String s : dSection) {
s_logger.debug("default section: " + s);
}
}
result.addAll(dSection);
if (!lbCmd.lbStatsVisibility.equals("disabled")) {
// new rule : listen admin_page guestip/link-local:8081
if (lbCmd.lbStatsVisibility.equals("global")) {
result.add(generateStatsRule(lbCmd, "stats_on_public", lbCmd.lbStatsPublicIP));
} else if (lbCmd.lbStatsVisibility.equals("guest-network")) {
result.add(generateStatsRule(lbCmd, "stats_on_guest", lbCmd.lbStatsGuestIP));
} else if (lbCmd.lbStatsVisibility.equals("link-local")) {
result.add(generateStatsRule(lbCmd, "stats_on_private", lbCmd.lbStatsPrivateIP));
} else if (lbCmd.lbStatsVisibility.equals("all")) {
result.add(generateStatsRule(lbCmd, "stats_on_public", lbCmd.lbStatsPublicIP));
result.add(generateStatsRule(lbCmd, "stats_on_guest", lbCmd.lbStatsGuestIP));
result.add(generateStatsRule(lbCmd, "stats_on_private", lbCmd.lbStatsPrivateIP));
} else {
// Stats will be available on the default http serving port, no special stats port
final StringBuilder subRule = new StringBuilder("\tstats enable\n\tstats uri ").append(lbCmd.lbStatsUri).append("\n\tstats realm Haproxy\\ Statistics\n\tstats auth ").append(lbCmd.lbStatsAuth);
result.add(subRule.toString());
}
}
result.add(blankLine);
boolean has_listener = false;
for (final LoadBalancerTO lbTO : lbCmd.getLoadBalancers()) {
if (lbTO.isRevoked()) {
continue;
}
final List<String> poolRules = getRulesForPool(lbTO, lbCmd.keepAliveEnabled);
result.addAll(poolRules);
has_listener = true;
}
result.add(blankLine);
if (!has_listener) {
// HAproxy cannot handle empty listen / frontend or backend, so add a dummy listener on port 9
result.addAll(Arrays.asList(defaultListen));
}
return result.toArray(new String[result.size()]);
}
use of com.cloud.legacymodel.to.LoadBalancerTO in project cosmic by MissionCriticalCloud.
the class HAProxyConfigurator method generateFwRules.
@Override
public String[][] generateFwRules(final LoadBalancerConfigCommand lbCmd) {
final String[][] result = new String[3][];
final Set<String> toAdd = new HashSet<>();
final Set<String> toRemove = new HashSet<>();
final Set<String> toStats = new HashSet<>();
for (final LoadBalancerTO lbTO : lbCmd.getLoadBalancers()) {
final StringBuilder sb = new StringBuilder();
sb.append(lbTO.getSrcIp()).append(":");
sb.append(lbTO.getSrcPort()).append(":");
sb.append(lbTO.getProtocol()).append(":");
for (final DestinationTO dest : lbTO.getDestinations()) {
// Add dst entry like this: "10.1.1.4:80"
if (dest.isRevoked()) {
continue;
}
sb.append(dest.getDestIp()).append(":");
sb.append(dest.getDestPort()).append(":");
}
final String lbRuleEntry = sb.toString();
if (!lbTO.isRevoked()) {
toAdd.add(lbRuleEntry);
} else {
toRemove.add(lbRuleEntry);
}
}
StringBuilder sb = new StringBuilder("");
if (lbCmd.lbStatsVisibility.equals("guest-network")) {
sb = new StringBuilder(lbCmd.lbStatsGuestIP).append(":").append(lbCmd.lbStatsPort).append(":").append(lbCmd.lbStatsSrcCidrs).append(":,");
} else if (lbCmd.lbStatsVisibility.equals("link-local")) {
sb = new StringBuilder(lbCmd.lbStatsPrivateIP).append(":").append(lbCmd.lbStatsPort).append(":").append(lbCmd.lbStatsSrcCidrs).append(":,");
} else if (lbCmd.lbStatsVisibility.equals("global")) {
sb = new StringBuilder(lbCmd.lbStatsPublicIP).append(":").append(lbCmd.lbStatsPort).append(":").append(lbCmd.lbStatsSrcCidrs).append(":,");
} else if (lbCmd.lbStatsVisibility.equals("all")) {
sb = new StringBuilder("0.0.0.0/0").append(":").append(lbCmd.lbStatsPort).append(":").append(lbCmd.lbStatsSrcCidrs).append(":,");
}
toStats.add(sb.toString());
toRemove.removeAll(toAdd);
result[ADD] = toAdd.toArray(new String[toAdd.size()]);
result[REMOVE] = toRemove.toArray(new String[toRemove.size()]);
result[STATS] = toStats.toArray(new String[toStats.size()]);
return result;
}
use of com.cloud.legacymodel.to.LoadBalancerTO in project cosmic by MissionCriticalCloud.
the class CommandSetupHelper method createNetworkOverviewFromRouter.
public NetworkOverviewTO createNetworkOverviewFromRouter(final VirtualRouter router, final List<Nic> nicsToExclude, final List<Ip> ipsToExclude, final List<StaticRouteProfile> staticRoutesToExclude, final RemoteAccessVpn remoteAccessVpnToExclude, final Site2SiteVpnConnection site2siteVpnToExclude, final LoadBalancer loadBalancerExclude) {
final NetworkOverviewTO networkOverviewTO = new NetworkOverviewTO();
final List<NetworkOverviewTO.InterfaceTO> interfacesTO = new ArrayList<>();
final NetworkOverviewTO.ServiceTO servicesTO = new NetworkOverviewTO.ServiceTO();
final List<NetworkOverviewTO.ServiceTO.ServiceSourceNatTO> serviceSourceNatsTO = new ArrayList<>();
configureInterfacesAndIps(router, nicsToExclude, ipsToExclude, networkOverviewTO, interfacesTO, serviceSourceNatsTO);
configureStaticRoutes(router, staticRoutesToExclude, networkOverviewTO);
servicesTO.setSourceNat(serviceSourceNatsTO.toArray(new NetworkOverviewTO.ServiceTO.ServiceSourceNatTO[serviceSourceNatsTO.size()]));
networkOverviewTO.setServices(servicesTO);
final NetworkOverviewTO.VPNTO vpnTO = new NetworkOverviewTO.VPNTO();
configureRemoteAccessVpn(router, remoteAccessVpnToExclude, vpnTO);
configureSite2SiteVpn(router, site2siteVpnToExclude, vpnTO);
networkOverviewTO.setVpn(vpnTO);
configureSyslog(router, networkOverviewTO);
final NetworkOverviewTO.LoadBalancerTO loadBalancerTO = new NetworkOverviewTO.LoadBalancerTO();
configureLoadBalancer(router, loadBalancerTO);
networkOverviewTO.setLoadbalancer(loadBalancerTO);
return networkOverviewTO;
}
use of com.cloud.legacymodel.to.LoadBalancerTO in project cosmic by MissionCriticalCloud.
the class CommandSetupHelper method createApplyLoadBalancingRulesCommands.
public void createApplyLoadBalancingRulesCommands(final List<LoadBalancingRule> rules, final VirtualRouter router, final Commands cmds, final long guestNetworkId) {
final LoadBalancerTO[] lbs = new LoadBalancerTO[rules.size()];
int i = 0;
// We don't support VR to be inline currently
final boolean inline = false;
for (final LoadBalancingRule rule : rules) {
final boolean revoked = rule.getState().equals(FirewallRule.State.Revoke);
final String protocol = rule.getProtocol();
final String lb_protocol = rule.getLbProtocol();
final String algorithm = rule.getAlgorithm();
final String uuid = rule.getUuid();
final String srcIp = rule.getSourceIp().addr();
final int srcPort = rule.getSourcePortStart();
final List<LbDestination> destinations = rule.getDestinations();
final List<LbStickinessPolicy> stickinessPolicies = rule.getStickinessPolicies();
// Load default values and fallback to hardcoded if not available
final Integer defaultClientTimeout = NumbersUtil.parseInt(_configDao.getValue(Config.DefaultLoadBalancerClientTimeout.key()), 60000);
final Integer defaultServerTimeout = NumbersUtil.parseInt(_configDao.getValue(Config.DefaultLoadBalancerServerTimeout.key()), 60000);
// set timeouts, use defaults if not available
Integer clientTimeout = rule.getClientTimeout();
if (clientTimeout != null) {
clientTimeout = NumbersUtil.parseInt(clientTimeout.toString(), defaultClientTimeout);
} else {
clientTimeout = defaultClientTimeout;
}
Integer serverTimeout = rule.getServerTimeout();
if (serverTimeout != null) {
serverTimeout = NumbersUtil.parseInt(serverTimeout.toString(), defaultServerTimeout);
} else {
serverTimeout = defaultServerTimeout;
}
final LoadBalancerTO lb = new LoadBalancerTO(uuid, srcIp, srcPort, protocol, algorithm, revoked, false, inline, destinations, stickinessPolicies, clientTimeout, serverTimeout);
lb.setLbProtocol(lb_protocol);
lbs[i++] = lb;
}
String routerPublicIp = null;
if (router instanceof DomainRouterVO) {
final DomainRouterVO domr = _routerDao.findById(router.getId());
routerPublicIp = domr.getPublicIpAddress();
if (routerPublicIp == null) {
routerPublicIp = router.getPublicIpAddress();
}
}
final Network guestNetwork = _networkModel.getNetwork(guestNetworkId);
final Nic nic = _nicDao.findByNtwkIdAndInstanceId(guestNetwork.getId(), router.getId());
final NicProfile nicProfile = new NicProfile(nic, guestNetwork, nic.getBroadcastUri(), nic.getIsolationUri(), _networkModel.getNetworkRate(guestNetwork.getId(), router.getId()), _networkModel.getNetworkTag(router.getHypervisorType(), guestNetwork));
final NetworkOffering offering = _networkOfferingDao.findById(guestNetwork.getNetworkOfferingId());
final String maxconn;
if (offering.getConcurrentConnections() == null) {
maxconn = _configDao.getValue(Config.NetworkLBHaproxyMaxConn.key());
} else {
maxconn = offering.getConcurrentConnections().toString();
}
final LoadBalancerConfigCommand cmd = new LoadBalancerConfigCommand(lbs, routerPublicIp, _routerControlHelper.getRouterIpInNetwork(guestNetworkId, router.getId()), router.getPrivateIpAddress(), _itMgr.toNicTO(nicProfile, router.getHypervisorType()), router.getVpcId(), maxconn, offering.isKeepAliveEnabled());
cmd.lbStatsVisibility = _configDao.getValue(Config.NetworkLBHaproxyStatsVisbility.key());
cmd.lbStatsUri = _configDao.getValue(Config.NetworkLBHaproxyStatsUri.key());
cmd.lbStatsAuth = _configDao.getValue(Config.NetworkLBHaproxyStatsAuth.key());
cmd.lbStatsPort = _configDao.getValue(Config.NetworkLBHaproxyStatsPort.key());
cmd.setAccessDetail(NetworkElementCommand.ROUTER_IP, _routerControlHelper.getRouterControlIp(router.getId()));
cmd.setAccessDetail(NetworkElementCommand.ROUTER_NAME, router.getInstanceName());
final Zone zone = zoneRepository.findById(router.getDataCenterId()).orElse(null);
cmd.setAccessDetail(NetworkElementCommand.ZONE_NETWORK_TYPE, zone.getNetworkType().toString());
cmds.addCommand(cmd);
}
use of com.cloud.legacymodel.to.LoadBalancerTO in project cosmic by MissionCriticalCloud.
the class HAProxyConfiguratorTest method testGenerateConfigurationLoadBalancerConfigCommand.
/**
* Test method for {@link HAProxyConfigurator#generateConfiguration(LoadBalancerConfigCommand)}.
*/
@Test
public void testGenerateConfigurationLoadBalancerConfigCommand() {
final LoadBalancerTO lb = new LoadBalancerTO("1", "10.2.0.1", 80, "http", "bla", false, false, false, null, 60000, 60000);
final LoadBalancerTO[] lba = new LoadBalancerTO[1];
lba[0] = lb;
final HAProxyConfigurator hpg = new HAProxyConfigurator();
LoadBalancerConfigCommand cmd = new LoadBalancerConfigCommand(lba, "10.0.0.1", "10.1.0.1", "10.1.1.1", null, 1L, "12", false);
String result = genConfig(hpg, cmd);
assertTrue("keepalive disabled should result in 'mode http' in the resulting haproxy config", result.contains("mode http"));
cmd = new LoadBalancerConfigCommand(lba, "10.0.0.1", "10.1.0.1", "10.1.1.1", null, 1L, "4", true);
result = genConfig(hpg, cmd);
assertTrue("keepalive enabled should not result in 'mode http' in the resulting haproxy config", !result.contains("mode http"));
// TODO
// create lb command
// setup tests for
// maxconn (test for maxpipes as well)
// httpmode
}
Aggregations