7575import javax .ejb .Local ;
7676import javax .naming .ConfigurationException ;
7777import java .io .BufferedReader ;
78- import java .io .BufferedWriter ;
7978import java .io .File ;
80- import java .io .FileWriter ;
8179import java .io .IOException ;
8280import java .io .InputStream ;
8381import java .io .InputStreamReader ;
84- import java .io .PrintWriter ;
8582import java .net .InetSocketAddress ;
8683import java .net .URL ;
8784import java .net .URLConnection ;
10299public class VirtualRoutingResource implements Manager {
103100 private static final Logger s_logger = Logger .getLogger (VirtualRoutingResource .class );
104101 private String _publicIpAddress ;
105- private String _loadbPath ;
106102 private String _publicEthIf ;
107103 private String _privateEthIf ;
108104 private String _routerProxyPath ;
@@ -354,7 +350,20 @@ private Answer execute(SetStaticNatRulesCommand cmd) {
354350 return new SetStaticNatRulesAnswer (cmd , results , endResult );
355351 }
356352
357- protected Answer VPCLoadBalancerConfig (final LoadBalancerConfigCommand cmd ) {
353+ protected boolean createFileInVR (String routerIp , String path , String filename , String content ) {
354+ File permKey = new File ("/root/.ssh/id_rsa.cloud" );
355+ boolean result = true ;
356+
357+ try {
358+ SshHelper .scpTo (routerIp , 3922 , "root" , permKey , null , path , content .getBytes (), filename , null );
359+ } catch (Exception e ) {
360+ s_logger .warn ("Fail to create file " + path + filename + " in VR " + routerIp , e );
361+ result = false ;
362+ }
363+ return result ;
364+ }
365+
366+ private Answer execute (LoadBalancerConfigCommand cmd ) {
358367 String routerIp = cmd .getAccessDetail (NetworkElementCommand .ROUTER_IP );
359368
360369 if (routerIp == null ) {
@@ -368,25 +377,24 @@ protected Answer VPCLoadBalancerConfig(final LoadBalancerConfigCommand cmd) {
368377 tmpCfgFileContents += config [i ];
369378 tmpCfgFileContents += "\n " ;
370379 }
371- File permKey = new File ("/root/.ssh/id_rsa.cloud" );
372380
373- try {
374- SshHelper .scpTo (routerIp , 3922 , "root" , permKey , null , "/etc/haproxy/" , tmpCfgFileContents .getBytes (), "haproxy.cfg.new" , null );
381+ if (!createFileInVR (routerIp , "/etc/haproxy/" , "haproxy.cfg.new" , tmpCfgFileContents )) {
382+ return new Answer (cmd , false , "Fail to copy LB config file to VR" );
383+ }
375384
385+ try {
376386 String [][] rules = cfgtr .generateFwRules (cmd );
377387
378388 String [] addRules = rules [LoadBalancerConfigurator .ADD ];
379389 String [] removeRules = rules [LoadBalancerConfigurator .REMOVE ];
380390 String [] statRules = rules [LoadBalancerConfigurator .STATS ];
381391
382- String ip = cmd .getNic ().getIp ();
383- String args = " -i " + ip ;
392+ String args = "" ;
384393 StringBuilder sb = new StringBuilder ();
385394 if (addRules .length > 0 ) {
386395 for (int i = 0 ; i < addRules .length ; i ++) {
387396 sb .append (addRules [i ]).append (',' );
388397 }
389-
390398 args += " -a " + sb .toString ();
391399 }
392400
@@ -408,7 +416,15 @@ protected Answer VPCLoadBalancerConfig(final LoadBalancerConfigCommand cmd) {
408416 args += " -s " + sb .toString ();
409417 }
410418
411- String result = routerProxy ("vpc_loadbalancer.sh" , routerIp , args );
419+ String result ;
420+
421+ if (cmd .getVpcId () == null ) {
422+ args = " -i " + routerIp + args ;
423+ result = routerProxy ("loadbalancer.sh" , routerIp , args );
424+ } else {
425+ args = " -i " + cmd .getNic ().getIp () + args ;
426+ result = routerProxy ("vpc_loadbalancer.sh" , routerIp , args );
427+ }
412428
413429 if (result != null ) {
414430 return new Answer (cmd , false , "LoadBalancerConfigCommand failed" );
@@ -420,41 +436,6 @@ protected Answer VPCLoadBalancerConfig(final LoadBalancerConfigCommand cmd) {
420436 }
421437 }
422438
423- private Answer execute (LoadBalancerConfigCommand cmd ) {
424- if (cmd .getVpcId () != null ) {
425- return VPCLoadBalancerConfig (cmd );
426- }
427-
428- String routerIp = cmd .getAccessDetail (NetworkElementCommand .ROUTER_IP );
429- File tmpCfgFile = null ;
430- try {
431- String cfgFilePath = "" ;
432- LoadBalancerConfigurator cfgtr = new HAProxyConfigurator ();
433- String [] config = cfgtr .generateConfiguration (cmd );
434- String [][] rules = cfgtr .generateFwRules (cmd );
435- if (routerIp != null ) {
436- tmpCfgFile = File .createTempFile (routerIp .replace ('.' , '_' ), "cfg" );
437- final PrintWriter out = new PrintWriter (new BufferedWriter (new FileWriter (tmpCfgFile )));
438- for (int i = 0 ; i < config .length ; i ++) {
439- out .println (config [i ]);
440- }
441- out .close ();
442- cfgFilePath = tmpCfgFile .getAbsolutePath ();
443- }
444-
445- final String result =
446- setLoadBalancerConfig (cfgFilePath , rules [LoadBalancerConfigurator .ADD ], rules [LoadBalancerConfigurator .REMOVE ], rules [LoadBalancerConfigurator .STATS ],
447- routerIp );
448-
449- return new Answer (cmd , result == null , result );
450- } catch (final IOException e ) {
451- return new Answer (cmd , false , e .getMessage ());
452- } finally {
453- if (tmpCfgFile != null ) {
454- tmpCfgFile .delete ();
455- }
456- }
457- }
458439
459440 protected Answer execute (VmDataCommand cmd ) {
460441 String routerIp = cmd .getAccessDetail (NetworkElementCommand .ROUTER_IP );
@@ -496,44 +477,6 @@ protected Answer execute(final IpAssocCommand cmd) {
496477 return new IpAssocAnswer (cmd , results );
497478 }
498479
499- private String setLoadBalancerConfig (final String cfgFile , final String [] addRules , final String [] removeRules , final String [] statsRules , String routerIp ) {
500-
501- if (routerIp == null ) {
502- routerIp = "none" ;
503- }
504-
505- final Script command = new Script (_loadbPath , _timeout , s_logger );
506-
507- command .add ("-i" , routerIp );
508- command .add ("-f" , cfgFile );
509-
510- StringBuilder sb = new StringBuilder ();
511- if (addRules .length > 0 ) {
512- for (int i = 0 ; i < addRules .length ; i ++) {
513- sb .append (addRules [i ]).append (',' );
514- }
515- command .add ("-a" , sb .toString ());
516- }
517-
518- sb = new StringBuilder ();
519- if (removeRules .length > 0 ) {
520- for (int i = 0 ; i < removeRules .length ; i ++) {
521- sb .append (removeRules [i ]).append (',' );
522- }
523- command .add ("-d" , sb .toString ());
524- }
525-
526- sb = new StringBuilder ();
527- if (statsRules .length > 0 ) {
528- for (int i = 0 ; i < statsRules .length ; i ++) {
529- sb .append (statsRules [i ]).append (',' );
530- }
531- command .add ("-s" , sb .toString ());
532- }
533-
534- return command .execute ();
535- }
536-
537480 protected Answer execute (final SavePasswordCommand cmd ) {
538481 final String password = cmd .getPassword ();
539482 final String routerPrivateIPAddress = cmd .getAccessDetail (NetworkElementCommand .ROUTER_IP );
@@ -1104,11 +1047,6 @@ public boolean configure(final String name, final Map<String, Object> params) th
11041047 s_logger .warn ("Incoming public ip address is overriden. Will always be using the same ip address: " + _publicIpAddress );
11051048 }
11061049
1107- _loadbPath = findScript ("call_loadbalancer.sh" );
1108- if (_loadbPath == null ) {
1109- throw new ConfigurationException ("Unable to find the call_loadbalancer.sh" );
1110- }
1111-
11121050 _publicEthIf = (String )params .get ("public.network.device" );
11131051 if (_publicEthIf == null ) {
11141052 _publicEthIf = "xenbr1" ;
0 commit comments