mirror of
https://github.com/apache/cloudstack.git
synced 2025-10-26 08:42:29 +01:00
CLOUDSTACK-5779: Move loadbalancer to use routerProxy
This commit is contained in:
parent
09fa6e5d83
commit
e88cc488e5
@ -75,13 +75,10 @@ import org.apache.log4j.Logger;
|
|||||||
import javax.ejb.Local;
|
import javax.ejb.Local;
|
||||||
import javax.naming.ConfigurationException;
|
import javax.naming.ConfigurationException;
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.BufferedWriter;
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileWriter;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.io.PrintWriter;
|
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.net.URLConnection;
|
import java.net.URLConnection;
|
||||||
@ -102,7 +99,6 @@ import java.util.Map;
|
|||||||
public class VirtualRoutingResource implements Manager {
|
public class VirtualRoutingResource implements Manager {
|
||||||
private static final Logger s_logger = Logger.getLogger(VirtualRoutingResource.class);
|
private static final Logger s_logger = Logger.getLogger(VirtualRoutingResource.class);
|
||||||
private String _publicIpAddress;
|
private String _publicIpAddress;
|
||||||
private String _loadbPath;
|
|
||||||
private String _publicEthIf;
|
private String _publicEthIf;
|
||||||
private String _privateEthIf;
|
private String _privateEthIf;
|
||||||
private String _routerProxyPath;
|
private String _routerProxyPath;
|
||||||
@ -354,7 +350,20 @@ public class VirtualRoutingResource implements Manager {
|
|||||||
return new SetStaticNatRulesAnswer(cmd, results, endResult);
|
return new SetStaticNatRulesAnswer(cmd, results, endResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Answer VPCLoadBalancerConfig(final LoadBalancerConfigCommand cmd) {
|
protected boolean createFileInVR(String routerIp, String path, String filename, String content) {
|
||||||
|
File permKey = new File("/root/.ssh/id_rsa.cloud");
|
||||||
|
boolean result = true;
|
||||||
|
|
||||||
|
try {
|
||||||
|
SshHelper.scpTo(routerIp, 3922, "root", permKey, null, path, content.getBytes(), filename, null);
|
||||||
|
} catch (Exception e) {
|
||||||
|
s_logger.warn("Fail to create file " + path + filename + " in VR " + routerIp, e);
|
||||||
|
result = false;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Answer execute(LoadBalancerConfigCommand cmd) {
|
||||||
String routerIp = cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP);
|
String routerIp = cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP);
|
||||||
|
|
||||||
if (routerIp == null) {
|
if (routerIp == null) {
|
||||||
@ -368,25 +377,24 @@ public class VirtualRoutingResource implements Manager {
|
|||||||
tmpCfgFileContents += config[i];
|
tmpCfgFileContents += config[i];
|
||||||
tmpCfgFileContents += "\n";
|
tmpCfgFileContents += "\n";
|
||||||
}
|
}
|
||||||
File permKey = new File("/root/.ssh/id_rsa.cloud");
|
|
||||||
|
if (!createFileInVR(routerIp, "/etc/haproxy/", "haproxy.cfg.new", tmpCfgFileContents)) {
|
||||||
|
return new Answer(cmd, false, "Fail to copy LB config file to VR");
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
SshHelper.scpTo(routerIp, 3922, "root", permKey, null, "/etc/haproxy/", tmpCfgFileContents.getBytes(), "haproxy.cfg.new", null);
|
|
||||||
|
|
||||||
String[][] rules = cfgtr.generateFwRules(cmd);
|
String[][] rules = cfgtr.generateFwRules(cmd);
|
||||||
|
|
||||||
String[] addRules = rules[LoadBalancerConfigurator.ADD];
|
String[] addRules = rules[LoadBalancerConfigurator.ADD];
|
||||||
String[] removeRules = rules[LoadBalancerConfigurator.REMOVE];
|
String[] removeRules = rules[LoadBalancerConfigurator.REMOVE];
|
||||||
String[] statRules = rules[LoadBalancerConfigurator.STATS];
|
String[] statRules = rules[LoadBalancerConfigurator.STATS];
|
||||||
|
|
||||||
String ip = cmd.getNic().getIp();
|
String args = "";
|
||||||
String args = " -i " + ip;
|
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
if (addRules.length > 0) {
|
if (addRules.length > 0) {
|
||||||
for (int i = 0; i < addRules.length; i++) {
|
for (int i = 0; i < addRules.length; i++) {
|
||||||
sb.append(addRules[i]).append(',');
|
sb.append(addRules[i]).append(',');
|
||||||
}
|
}
|
||||||
|
|
||||||
args += " -a " + sb.toString();
|
args += " -a " + sb.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -408,7 +416,15 @@ public class VirtualRoutingResource implements Manager {
|
|||||||
args += " -s " + sb.toString();
|
args += " -s " + sb.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
String result = routerProxy("vpc_loadbalancer.sh", routerIp, args);
|
String result;
|
||||||
|
|
||||||
|
if (cmd.getVpcId() == null) {
|
||||||
|
args = " -i " + routerIp + args;
|
||||||
|
result = routerProxy("loadbalancer.sh", routerIp, args);
|
||||||
|
} else {
|
||||||
|
args = " -i " + cmd.getNic().getIp() + args;
|
||||||
|
result = routerProxy("vpc_loadbalancer.sh", routerIp, args);
|
||||||
|
}
|
||||||
|
|
||||||
if (result != null) {
|
if (result != null) {
|
||||||
return new Answer(cmd, false, "LoadBalancerConfigCommand failed");
|
return new Answer(cmd, false, "LoadBalancerConfigCommand failed");
|
||||||
@ -420,41 +436,6 @@ public class VirtualRoutingResource implements Manager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Answer execute(LoadBalancerConfigCommand cmd) {
|
|
||||||
if (cmd.getVpcId() != null) {
|
|
||||||
return VPCLoadBalancerConfig(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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected Answer execute(VmDataCommand cmd) {
|
protected Answer execute(VmDataCommand cmd) {
|
||||||
String routerIp = cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP);
|
String routerIp = cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP);
|
||||||
@ -496,44 +477,6 @@ public class VirtualRoutingResource implements Manager {
|
|||||||
return new IpAssocAnswer(cmd, results);
|
return new IpAssocAnswer(cmd, results);
|
||||||
}
|
}
|
||||||
|
|
||||||
private String setLoadBalancerConfig(final String cfgFile, final String[] addRules, final String[] removeRules, final String[] statsRules, String routerIp) {
|
|
||||||
|
|
||||||
if (routerIp == null) {
|
|
||||||
routerIp = "none";
|
|
||||||
}
|
|
||||||
|
|
||||||
final Script command = new Script(_loadbPath, _timeout, s_logger);
|
|
||||||
|
|
||||||
command.add("-i", routerIp);
|
|
||||||
command.add("-f", cfgFile);
|
|
||||||
|
|
||||||
StringBuilder sb = new StringBuilder();
|
|
||||||
if (addRules.length > 0) {
|
|
||||||
for (int i = 0; i < addRules.length; i++) {
|
|
||||||
sb.append(addRules[i]).append(',');
|
|
||||||
}
|
|
||||||
command.add("-a", sb.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
sb = new StringBuilder();
|
|
||||||
if (removeRules.length > 0) {
|
|
||||||
for (int i = 0; i < removeRules.length; i++) {
|
|
||||||
sb.append(removeRules[i]).append(',');
|
|
||||||
}
|
|
||||||
command.add("-d", sb.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
sb = new StringBuilder();
|
|
||||||
if (statsRules.length > 0) {
|
|
||||||
for (int i = 0; i < statsRules.length; i++) {
|
|
||||||
sb.append(statsRules[i]).append(',');
|
|
||||||
}
|
|
||||||
command.add("-s", sb.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
return command.execute();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected Answer execute(final SavePasswordCommand cmd) {
|
protected Answer execute(final SavePasswordCommand cmd) {
|
||||||
final String password = cmd.getPassword();
|
final String password = cmd.getPassword();
|
||||||
final String routerPrivateIPAddress = cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP);
|
final String routerPrivateIPAddress = cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP);
|
||||||
@ -1104,11 +1047,6 @@ public class VirtualRoutingResource implements Manager {
|
|||||||
s_logger.warn("Incoming public ip address is overriden. Will always be using the same ip address: " + _publicIpAddress);
|
s_logger.warn("Incoming public ip address is overriden. Will always be using the same ip address: " + _publicIpAddress);
|
||||||
}
|
}
|
||||||
|
|
||||||
_loadbPath = findScript("call_loadbalancer.sh");
|
|
||||||
if (_loadbPath == null) {
|
|
||||||
throw new ConfigurationException("Unable to find the call_loadbalancer.sh");
|
|
||||||
}
|
|
||||||
|
|
||||||
_publicEthIf = (String)params.get("public.network.device");
|
_publicEthIf = (String)params.get("public.network.device");
|
||||||
if (_publicEthIf == null) {
|
if (_publicEthIf == null) {
|
||||||
_publicEthIf = "xenbr1";
|
_publicEthIf = "xenbr1";
|
||||||
|
|||||||
@ -1035,97 +1035,22 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa
|
|||||||
return new SetStaticNatRulesAnswer(cmd, results, endResult);
|
return new SetStaticNatRulesAnswer(cmd, results, endResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Answer VPCLoadBalancerConfig(final LoadBalancerConfigCommand cmd) {
|
protected boolean createFileInVR(String routerIp, String filePath, String fileName, String content) {
|
||||||
VmwareManager mgr = getServiceContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME);
|
VmwareManager mgr = getServiceContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME);
|
||||||
File keyFile = mgr.getSystemVMKeyFile();
|
File keyFile = mgr.getSystemVMKeyFile();
|
||||||
|
boolean result = true;
|
||||||
String routerIp = cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP);
|
|
||||||
String controlIp = getRouterSshControlIp(cmd);
|
|
||||||
|
|
||||||
assert (controlIp != null);
|
|
||||||
|
|
||||||
LoadBalancerConfigurator cfgtr = new HAProxyConfigurator();
|
|
||||||
String[] config = cfgtr.generateConfiguration(cmd);
|
|
||||||
|
|
||||||
String tmpCfgFilePath = "/etc/haproxy/haproxy.cfg.new";
|
|
||||||
String tmpCfgFileContents = "";
|
|
||||||
for (int i = 0; i < config.length; i++) {
|
|
||||||
tmpCfgFileContents += config[i];
|
|
||||||
tmpCfgFileContents += "\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
SshHelper.scpTo(controlIp, DefaultDomRSshPort, "root", keyFile, null, "/etc/haproxy/", tmpCfgFileContents.getBytes(), "haproxy.cfg.new", null);
|
SshHelper.scpTo(routerIp, 3922, "root", keyFile, null, filePath, content.getBytes(), fileName, null);
|
||||||
|
} catch (Exception e) {
|
||||||
try {
|
s_logger.warn("Fail to create file " + filePath + fileName + " in VR " + routerIp, e);
|
||||||
String[][] rules = cfgtr.generateFwRules(cmd);
|
result = false;
|
||||||
|
|
||||||
String[] addRules = rules[LoadBalancerConfigurator.ADD];
|
|
||||||
String[] removeRules = rules[LoadBalancerConfigurator.REMOVE];
|
|
||||||
String[] statRules = rules[LoadBalancerConfigurator.STATS];
|
|
||||||
|
|
||||||
String args = "";
|
|
||||||
String ip = cmd.getNic().getIp();
|
|
||||||
args += " -i " + ip;
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Invoke the command
|
|
||||||
Pair<Boolean, String> result =
|
|
||||||
SshHelper.sshExecute(controlIp, DefaultDomRSshPort, "root", mgr.getSystemVMKeyFile(), null, "/opt/cloud/bin/vpc_loadbalancer.sh " + args);
|
|
||||||
|
|
||||||
if (!result.first()) {
|
|
||||||
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("VPCLoadBalancerConfigCommand on domain router " + routerIp + " completed");
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
SshHelper.sshExecute(controlIp, DefaultDomRSshPort, "root", mgr.getSystemVMKeyFile(), null, "rm " + tmpCfgFilePath);
|
|
||||||
}
|
|
||||||
return new Answer(cmd);
|
|
||||||
} catch (Throwable e) {
|
|
||||||
s_logger.error("Unexpected exception: " + e.toString(), e);
|
|
||||||
return new Answer(cmd, false, "VPCLoadBalancerConfigCommand failed due to " + VmwareHelper.getExceptionMessage(e));
|
|
||||||
}
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Answer execute(final LoadBalancerConfigCommand cmd) {
|
protected Answer execute(final LoadBalancerConfigCommand cmd) {
|
||||||
|
|
||||||
if (cmd.getVpcId() != null) {
|
|
||||||
return VPCLoadBalancerConfig(cmd);
|
|
||||||
}
|
|
||||||
|
|
||||||
VmwareManager mgr = getServiceContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME);
|
VmwareManager mgr = getServiceContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME);
|
||||||
File keyFile = mgr.getSystemVMKeyFile();
|
|
||||||
|
|
||||||
String routerIp = cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP);
|
String routerIp = cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP);
|
||||||
String controlIp = getRouterSshControlIp(cmd);
|
String controlIp = getRouterSshControlIp(cmd);
|
||||||
@ -1135,82 +1060,74 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa
|
|||||||
LoadBalancerConfigurator cfgtr = new HAProxyConfigurator();
|
LoadBalancerConfigurator cfgtr = new HAProxyConfigurator();
|
||||||
String[] config = cfgtr.generateConfiguration(cmd);
|
String[] config = cfgtr.generateConfiguration(cmd);
|
||||||
|
|
||||||
String[][] rules = cfgtr.generateFwRules(cmd);
|
|
||||||
String tmpCfgFilePath = "/tmp/" + routerIp.replace('.', '_') + ".cfg";
|
|
||||||
String tmpCfgFileContents = "";
|
String tmpCfgFileContents = "";
|
||||||
|
String tmpCfgFileName = "haproxy.cfg.new";
|
||||||
|
String tmpCfgFilePath = "/etc/haproxy/";
|
||||||
for (int i = 0; i < config.length; i++) {
|
for (int i = 0; i < config.length; i++) {
|
||||||
tmpCfgFileContents += config[i];
|
tmpCfgFileContents += config[i];
|
||||||
tmpCfgFileContents += "\n";
|
tmpCfgFileContents += "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!createFileInVR(controlIp, "/etc/haproxy/", "haproxy.cfg.new", tmpCfgFileContents)) {
|
||||||
|
return new Answer(cmd, false, "Fail to create LB config file in VR");
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
SshHelper.scpTo(controlIp, DefaultDomRSshPort, "root", keyFile, null, "/tmp/", tmpCfgFileContents.getBytes(), routerIp.replace('.', '_') + ".cfg", null);
|
|
||||||
|
|
||||||
try {
|
String[][] rules = cfgtr.generateFwRules(cmd);
|
||||||
String[] addRules = rules[LoadBalancerConfigurator.ADD];
|
|
||||||
String[] removeRules = rules[LoadBalancerConfigurator.REMOVE];
|
|
||||||
String[] statRules = rules[LoadBalancerConfigurator.STATS];
|
|
||||||
|
|
||||||
String args = "";
|
String[] addRules = rules[LoadBalancerConfigurator.ADD];
|
||||||
args += "-i " + routerIp;
|
String[] removeRules = rules[LoadBalancerConfigurator.REMOVE];
|
||||||
args += " -f " + tmpCfgFilePath;
|
String[] statRules = rules[LoadBalancerConfigurator.STATS];
|
||||||
|
|
||||||
StringBuilder sb = new StringBuilder();
|
String args = "";
|
||||||
if (addRules.length > 0) {
|
StringBuilder sb = new StringBuilder();
|
||||||
for (int i = 0; i < addRules.length; i++) {
|
if (addRules.length > 0) {
|
||||||
sb.append(addRules[i]).append(',');
|
for (int i = 0; i < addRules.length; i++) {
|
||||||
}
|
sb.append(addRules[i]).append(',');
|
||||||
|
|
||||||
args += " -a " + sb.toString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sb = new StringBuilder();
|
args += " -a " + sb.toString();
|
||||||
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, DefaultDomRSshPort, "root", mgr.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 uanble to copy haproxy configuration file");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (s_logger.isDebugEnabled()) {
|
|
||||||
s_logger.debug("Run command on domain router " + routerIp + ", /root/loadbalancer.sh " + args);
|
|
||||||
}
|
|
||||||
|
|
||||||
result = SshHelper.sshExecute(controlIp, DefaultDomRSshPort, "root", mgr.getSystemVMKeyFile(), null, "/root/loadbalancer.sh " + args);
|
|
||||||
|
|
||||||
if (!result.first()) {
|
|
||||||
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, DefaultDomRSshPort, "root", mgr.getSystemVMKeyFile(), null, "rm " + tmpCfgFilePath);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
if (cmd.getVpcId() == null) {
|
||||||
|
args = " -i " + routerIp + args;
|
||||||
|
result = SshHelper.sshExecute(controlIp, DefaultDomRSshPort, "root", mgr.getSystemVMKeyFile(), null, "/opt/cloud/bin/loadbalancer.sh " + args);
|
||||||
|
} else {
|
||||||
|
args = " -i " + cmd.getNic().getIp() + args;
|
||||||
|
result = SshHelper.sshExecute(controlIp, DefaultDomRSshPort, "root", mgr.getSystemVMKeyFile(), null, "/opt/cloud/bin/vpc_loadbalancer.sh " + args);
|
||||||
|
}
|
||||||
|
// Invoke the command
|
||||||
|
|
||||||
|
if (!result.first()) {
|
||||||
|
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");
|
||||||
|
}
|
||||||
return new Answer(cmd);
|
return new Answer(cmd);
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
s_logger.error("Unexpected exception: " + e.toString(), e);
|
s_logger.error("Unexpected exception: " + e.toString(), e);
|
||||||
|
|||||||
@ -2125,71 +2125,6 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
|
|||||||
return new SetStaticNatRulesAnswer(cmd, results, endResult);
|
return new SetStaticNatRulesAnswer(cmd, results, endResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Answer VPCLoadBalancerConfig(final LoadBalancerConfigCommand cmd) {
|
|
||||||
Connection conn = getConnection();
|
|
||||||
String routerIp = cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP);
|
|
||||||
|
|
||||||
if (routerIp == null) {
|
|
||||||
return new Answer(cmd);
|
|
||||||
}
|
|
||||||
|
|
||||||
LoadBalancerConfigurator cfgtr = new HAProxyConfigurator();
|
|
||||||
String[] config = cfgtr.generateConfiguration(cmd);
|
|
||||||
String tmpCfgFileContents = "";
|
|
||||||
for (int i = 0; i < config.length; i++) {
|
|
||||||
tmpCfgFileContents += config[i];
|
|
||||||
tmpCfgFileContents += "\n";
|
|
||||||
}
|
|
||||||
String tmpCfgFilePath = "/etc/haproxy/haproxy.cfg.new";
|
|
||||||
String result = callHostPlugin(conn, "vmops", "createFileInDomr", "domrip", routerIp, "filepath", tmpCfgFilePath, "filecontents", tmpCfgFileContents);
|
|
||||||
|
|
||||||
if (result == null || result.isEmpty()) {
|
|
||||||
return new Answer(cmd, false, "LoadBalancerConfigCommand failed to create HA proxy cfg file.");
|
|
||||||
}
|
|
||||||
|
|
||||||
String[][] rules = cfgtr.generateFwRules(cmd);
|
|
||||||
|
|
||||||
String[] addRules = rules[LoadBalancerConfigurator.ADD];
|
|
||||||
String[] removeRules = rules[LoadBalancerConfigurator.REMOVE];
|
|
||||||
String[] statRules = rules[LoadBalancerConfigurator.STATS];
|
|
||||||
|
|
||||||
String ip = cmd.getNic().getIp();
|
|
||||||
String args = "-i " + ip;
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
result = routerProxy("vpc_loadbalancer.sh", cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP), args);
|
|
||||||
|
|
||||||
if (result == null || result.isEmpty()) {
|
|
||||||
return new Answer(cmd, false, "LoadBalancerConfigCommand failed");
|
|
||||||
}
|
|
||||||
return new Answer(cmd);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected Answer execute(final CreateIpAliasCommand cmd) {
|
protected Answer execute(final CreateIpAliasCommand cmd) {
|
||||||
String routerIp = cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP);
|
String routerIp = cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP);
|
||||||
List<IpAliasTO> ipAliasTOs = cmd.getIpAliasList();
|
List<IpAliasTO> ipAliasTOs = cmd.getIpAliasList();
|
||||||
@ -2244,11 +2179,12 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Answer execute(final LoadBalancerConfigCommand cmd) {
|
protected String createFileInVR(String routerIp, String path, String content) {
|
||||||
if (cmd.getVpcId() != null) {
|
|
||||||
return VPCLoadBalancerConfig(cmd);
|
|
||||||
}
|
|
||||||
Connection conn = getConnection();
|
Connection conn = getConnection();
|
||||||
|
return callHostPlugin(conn, "vmops", "createFileInDomr", "domrip", routerIp, "filepath", path, "filecontents", content);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Answer execute(final LoadBalancerConfigCommand cmd) {
|
||||||
String routerIp = cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP);
|
String routerIp = cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP);
|
||||||
|
|
||||||
if (routerIp == null) {
|
if (routerIp == null) {
|
||||||
@ -2257,29 +2193,26 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
|
|||||||
|
|
||||||
LoadBalancerConfigurator cfgtr = new HAProxyConfigurator();
|
LoadBalancerConfigurator cfgtr = new HAProxyConfigurator();
|
||||||
String[] config = cfgtr.generateConfiguration(cmd);
|
String[] config = cfgtr.generateConfiguration(cmd);
|
||||||
|
|
||||||
String[][] rules = cfgtr.generateFwRules(cmd);
|
|
||||||
String tmpCfgFilePath = "/tmp/" + routerIp.replace('.', '_') + ".cfg";
|
|
||||||
String tmpCfgFileContents = "";
|
String tmpCfgFileContents = "";
|
||||||
for (int i = 0; i < config.length; i++) {
|
for (int i = 0; i < config.length; i++) {
|
||||||
tmpCfgFileContents += config[i];
|
tmpCfgFileContents += config[i];
|
||||||
tmpCfgFileContents += "\n";
|
tmpCfgFileContents += "\n";
|
||||||
}
|
}
|
||||||
|
String tmpCfgFilePath = "/etc/haproxy/haproxy.cfg.new";
|
||||||
String result = callHostPlugin(conn, "vmops", "createFile", "filepath", tmpCfgFilePath, "filecontents", tmpCfgFileContents);
|
String result = createFileInVR(routerIp, tmpCfgFilePath, tmpCfgFileContents);
|
||||||
|
|
||||||
if (result == null || result.isEmpty()) {
|
if (result == null || result.isEmpty()) {
|
||||||
return new Answer(cmd, false, "LoadBalancerConfigCommand failed to create HA proxy cfg file.");
|
return new Answer(cmd, false, "LoadBalancerConfigCommand failed to create HA proxy cfg file.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String[][] rules = cfgtr.generateFwRules(cmd);
|
||||||
|
|
||||||
String[] addRules = rules[LoadBalancerConfigurator.ADD];
|
String[] addRules = rules[LoadBalancerConfigurator.ADD];
|
||||||
String[] removeRules = rules[LoadBalancerConfigurator.REMOVE];
|
String[] removeRules = rules[LoadBalancerConfigurator.REMOVE];
|
||||||
String[] statRules = rules[LoadBalancerConfigurator.STATS];
|
String[] statRules = rules[LoadBalancerConfigurator.STATS];
|
||||||
|
|
||||||
String args = "";
|
String ip = cmd.getNic().getIp();
|
||||||
args += "-i " + routerIp;
|
String args = " -i " + ip;
|
||||||
args += " -f " + tmpCfgFilePath;
|
|
||||||
|
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
if (addRules.length > 0) {
|
if (addRules.length > 0) {
|
||||||
for (int i = 0; i < addRules.length; i++) {
|
for (int i = 0; i < addRules.length; i++) {
|
||||||
@ -2307,14 +2240,17 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
|
|||||||
args += " -s " + sb.toString();
|
args += " -s " + sb.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
result = callHostPlugin(conn, "vmops", "setLoadBalancerRule", "args", args);
|
if (cmd.getVpcId() == null) {
|
||||||
|
args = " -i " + routerIp + args;
|
||||||
|
result = routerProxy("loadbalancer.sh", routerIp, args);
|
||||||
|
} else {
|
||||||
|
args = " -i " + cmd.getNic().getIp() + args;
|
||||||
|
result = routerProxy("vpc_loadbalancer.sh", routerIp, args);
|
||||||
|
}
|
||||||
|
|
||||||
if (result == null || result.isEmpty()) {
|
if (result == null || result.isEmpty()) {
|
||||||
return new Answer(cmd, false, "LoadBalancerConfigCommand failed");
|
return new Answer(cmd, false, "LoadBalancerConfigCommand failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
callHostPlugin(conn, "vmops", "deleteFile", "filepath", tmpCfgFilePath);
|
|
||||||
|
|
||||||
return new Answer(cmd);
|
return new Answer(cmd);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,101 +0,0 @@
|
|||||||
#!/usr/bin/env bash
|
|
||||||
# Licensed to the Apache Software Foundation (ASF) under one
|
|
||||||
# or more contributor license agreements. See the NOTICE file
|
|
||||||
# distributed with this work for additional information
|
|
||||||
# regarding copyright ownership. The ASF licenses this file
|
|
||||||
# to you under the Apache License, Version 2.0 (the
|
|
||||||
# "License"); you may not use this file except in compliance
|
|
||||||
# with the License. You may obtain a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing,
|
|
||||||
# software distributed under the License is distributed on an
|
|
||||||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
||||||
# KIND, either express or implied. See the License for the
|
|
||||||
# specific language governing permissions and limitations
|
|
||||||
# under the License.
|
|
||||||
|
|
||||||
|
|
||||||
# $Id: call_loadbalancer.sh 9132 2010-06-04 20:17:43Z manuel $ $HeadURL: svn://svn.lab.vmops.com/repos/branches/2.0.0/java/scripts/vm/hypervisor/xenserver/patch/call_loadbalancer.sh $
|
|
||||||
# loadbalancer.sh -- reconfigure loadbalancer rules
|
|
||||||
|
|
||||||
usage() {
|
|
||||||
printf "Usage: %s: -i <domR eth1 ip> -a <added public ip address> -d <removed> -f <load balancer config> -s <stats guest ip >\n" $(basename $0) >&2
|
|
||||||
}
|
|
||||||
|
|
||||||
set -x
|
|
||||||
|
|
||||||
check_gw() {
|
|
||||||
ping -c 1 -n -q $1 > /dev/null
|
|
||||||
if [ $? -gt 0 ]
|
|
||||||
then
|
|
||||||
sleep 1
|
|
||||||
ping -c 1 -n -q $1 > /dev/null
|
|
||||||
fi
|
|
||||||
return $?;
|
|
||||||
}
|
|
||||||
|
|
||||||
copy_haproxy() {
|
|
||||||
local domRIp=$1
|
|
||||||
local cfg=$2
|
|
||||||
|
|
||||||
scp -P 3922 -q -o StrictHostKeyChecking=no -i $cert $cfg root@$domRIp:/etc/haproxy/haproxy.cfg.new
|
|
||||||
return $?
|
|
||||||
}
|
|
||||||
|
|
||||||
iflag=
|
|
||||||
aflag=
|
|
||||||
dflag=
|
|
||||||
fflag=
|
|
||||||
sflag=
|
|
||||||
|
|
||||||
while getopts 'i:a:d:f:s:' OPTION
|
|
||||||
do
|
|
||||||
case $OPTION in
|
|
||||||
i) iflag=1
|
|
||||||
domRIp="$OPTARG"
|
|
||||||
;;
|
|
||||||
a) aflag=1
|
|
||||||
addedIps="$OPTARG"
|
|
||||||
;;
|
|
||||||
d) dflag=1
|
|
||||||
removedIps="$OPTARG"
|
|
||||||
;;
|
|
||||||
f) fflag=1
|
|
||||||
cfgfile="$OPTARG"
|
|
||||||
;;
|
|
||||||
s) sflag=1
|
|
||||||
statsIps="$OPTARG"
|
|
||||||
;;
|
|
||||||
?) usage
|
|
||||||
exit 2
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
done
|
|
||||||
|
|
||||||
cert="/root/.ssh/id_rsa.cloud"
|
|
||||||
|
|
||||||
if [ "$iflag$fflag" != "11" ]
|
|
||||||
then
|
|
||||||
usage
|
|
||||||
exit 2
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Check if DomR is up and running. If it isn't, exit 1.
|
|
||||||
check_gw "$domRIp"
|
|
||||||
if [ $? -gt 0 ]
|
|
||||||
then
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
copy_haproxy $domRIp $cfgfile
|
|
||||||
|
|
||||||
if [ $? -gt 0 ]
|
|
||||||
then
|
|
||||||
printf "Reconfiguring loadbalancer failed\n"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
ssh -p 3922 -q -o StrictHostKeyChecking=no -i $cert root@$domRIp "/root/loadbalancer.sh $*"
|
|
||||||
exit $?
|
|
||||||
@ -238,21 +238,6 @@ def routerProxy(session, args):
|
|||||||
|
|
||||||
return txt
|
return txt
|
||||||
|
|
||||||
@echo
|
|
||||||
def setLoadBalancerRule(session, args):
|
|
||||||
sargs = args['args']
|
|
||||||
cmd = sargs.split(' ')
|
|
||||||
cmd.insert(0, "/opt/cloud/bin/call_loadbalancer.sh")
|
|
||||||
cmd.insert(0, "/bin/bash")
|
|
||||||
try:
|
|
||||||
txt = util.pread2(cmd)
|
|
||||||
txt = 'success'
|
|
||||||
except:
|
|
||||||
logging.debug(" set loadbalancer rule failed " )
|
|
||||||
txt = ''
|
|
||||||
|
|
||||||
return txt
|
|
||||||
|
|
||||||
@echo
|
@echo
|
||||||
def createFile(session, args):
|
def createFile(session, args):
|
||||||
file_path = args['filepath']
|
file_path = args['filepath']
|
||||||
@ -1540,7 +1525,7 @@ if __name__ == "__main__":
|
|||||||
"setIptables": setIptables, "pingdomr": pingdomr, "pingxenserver": pingxenserver,
|
"setIptables": setIptables, "pingdomr": pingdomr, "pingxenserver": pingxenserver,
|
||||||
"savePassword": savePassword,
|
"savePassword": savePassword,
|
||||||
"routerProxy": routerProxy,
|
"routerProxy": routerProxy,
|
||||||
"setLoadBalancerRule": setLoadBalancerRule, "createFile": createFile, "deleteFile": deleteFile,
|
"createFile": createFile, "deleteFile": deleteFile,
|
||||||
"network_rules":network_rules,
|
"network_rules":network_rules,
|
||||||
"can_bridge_firewall":can_bridge_firewall, "default_network_rules":default_network_rules,
|
"can_bridge_firewall":can_bridge_firewall, "default_network_rules":default_network_rules,
|
||||||
"destroy_network_rules_for_vm":destroy_network_rules_for_vm,
|
"destroy_network_rules_for_vm":destroy_network_rules_for_vm,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user