use of com.cloud.network.HAProxyConfigurator 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);
}
use of com.cloud.network.HAProxyConfigurator 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());
}
}
use of com.cloud.network.HAProxyConfigurator 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);
}
Aggregations