private Answer execute(LoadBalancerConfigCommand cmd) { String routerIp = cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP); File tmpCfgFile = null; try { String cfgFilePath = ""; LoadBalancerConfigurator cfgtr = new HAProxyConfigurator(); String[] config = cfgtr.generateConfiguration(cmd); String[][] rules = cfgtr.generateFwRules(cmd); if (routerIp != null) { tmpCfgFile = File.createTempFile(routerIp.replace('.', '_'), "cfg"); final PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(tmpCfgFile))); for (int i = 0; i < config.length; i++) { out.println(config[i]); } out.close(); cfgFilePath = tmpCfgFile.getAbsolutePath(); } final String result = setLoadBalancerConfig( cfgFilePath, rules[LoadBalancerConfigurator.ADD], rules[LoadBalancerConfigurator.REMOVE], rules[LoadBalancerConfigurator.STATS], routerIp); return new Answer(cmd, result == null, result); } catch (final IOException e) { return new Answer(cmd, false, e.getMessage()); } finally { if (tmpCfgFile != null) { tmpCfgFile.delete(); } } }
private void createApplyLoadBalancingRulesCommands( List<LoadBalancingRule> rules, DomainRouterVO elbVm, Commands cmds, long guestNetworkId) { LoadBalancerTO[] lbs = new LoadBalancerTO[rules.size()]; int i = 0; for (LoadBalancingRule rule : rules) { boolean revoked = (rule.getState().equals(FirewallRule.State.Revoke)); String protocol = rule.getProtocol(); String algorithm = rule.getAlgorithm(); String elbIp = _networkModel.getIp(rule.getSourceIpAddressId()).getAddress().addr(); int srcPort = rule.getSourcePortStart(); String uuid = rule.getUuid(); List<LbDestination> destinations = rule.getDestinations(); LoadBalancerTO lb = new LoadBalancerTO( uuid, elbIp, srcPort, protocol, algorithm, revoked, false, false, destinations); lbs[i++] = lb; } LoadBalancerConfigCommand cmd = new LoadBalancerConfigCommand( lbs, elbVm.getPublicIpAddress(), _nicDao.getIpAddress(guestNetworkId, elbVm.getId()), elbVm.getPrivateIpAddress(), null, null); cmd.setAccessDetail(NetworkElementCommand.ROUTER_IP, elbVm.getPrivateIpAddress()); cmd.setAccessDetail(NetworkElementCommand.ROUTER_NAME, elbVm.getInstanceName()); // FIXME: why are we setting attributes directly? Ick!! There should be accessors and // the constructor should set defaults. 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()); cmds.addCommand(cmd); }