forked from apache/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHAProxyConfigurator.java
More file actions
658 lines (599 loc) · 31.8 KB
/
HAProxyConfigurator.java
File metadata and controls
658 lines (599 loc) · 31.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
// 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.
package com.cloud.network;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.log4j.Logger;
import com.cloud.agent.api.routing.LoadBalancerConfigCommand;
import com.cloud.agent.api.to.LoadBalancerTO;
import com.cloud.agent.api.to.LoadBalancerTO.DestinationTO;
import com.cloud.agent.api.to.LoadBalancerTO.StickinessPolicyTO;
import com.cloud.agent.api.to.PortForwardingRuleTO;
import com.cloud.network.rules.LbStickinessMethod.StickinessMethodType;
import com.cloud.utils.Pair;
import com.cloud.utils.net.NetUtils;
public class HAProxyConfigurator implements LoadBalancerConfigurator {
private static final Logger s_logger = Logger.getLogger(HAProxyConfigurator.class);
private static final String blankLine = "\t ";
private static String[] globalSection = {"global", "\tlog 127.0.0.1:3914 local0 warning", "\tmaxconn 4096", "\tmaxpipes 1024", "\tchroot /var/lib/haproxy",
"\tuser haproxy", "\tgroup haproxy", "\tdaemon"};
private static String[] defaultsSection = {"defaults", "\tlog global", "\tmode tcp", "\toption dontlognull", "\tretries 3", "\toption redispatch",
"\toption forwardfor", "\toption forceclose", "\ttimeout connect 5000", "\ttimeout client 50000", "\ttimeout server 50000"};
private static String[] defaultListen = {"listen vmops 0.0.0.0:9", "\toption transparent"};
@Override
public String[] generateConfiguration(List<PortForwardingRuleTO> fwRules) {
// Group the rules by publicip:publicport
Map<String, List<PortForwardingRuleTO>> pools = new HashMap<String, List<PortForwardingRuleTO>>();
for (PortForwardingRuleTO rule : fwRules) {
StringBuilder sb = new StringBuilder();
String poolName = sb.append(rule.getSrcIp().replace(".", "_")).append('-').append(rule.getSrcPortRange()[0]).toString();
if (!rule.revoked()) {
List<PortForwardingRuleTO> fwList = pools.get(poolName);
if (fwList == null) {
fwList = new ArrayList<PortForwardingRuleTO>();
pools.put(poolName, fwList);
}
fwList.add(rule);
}
}
List<String> result = new ArrayList<String>();
result.addAll(Arrays.asList(globalSection));
result.add(blankLine);
result.addAll(Arrays.asList(defaultsSection));
result.add(blankLine);
if (pools.isEmpty()) {
// haproxy cannot handle empty listen / frontend or backend, so add
// a dummy listener
// on port 9
result.addAll(Arrays.asList(defaultListen));
}
result.add(blankLine);
for (Map.Entry<String, List<PortForwardingRuleTO>> e : pools.entrySet()) {
List<String> poolRules = getRulesForPool(e.getKey(), e.getValue());
result.addAll(poolRules);
}
return result.toArray(new String[result.size()]);
}
private List<String> getRulesForPool(String poolName, List<PortForwardingRuleTO> fwRules) {
PortForwardingRuleTO firstRule = fwRules.get(0);
String publicIP = firstRule.getSrcIp();
String publicPort = Integer.toString(firstRule.getSrcPortRange()[0]);
// FIXEME: String algorithm = firstRule.getAlgorithm();
List<String> result = new ArrayList<String>();
// add line like this: "listen 65_37_141_30-80 65.37.141.30:80"
StringBuilder sb = new StringBuilder();
sb.append("listen ").append(poolName).append(" ").append(publicIP).append(":").append(publicPort);
result.add(sb.toString());
sb = new StringBuilder();
// FIXME sb.append("\t").append("balance ").append(algorithm);
result.add(sb.toString());
if (publicPort.equals(NetUtils.HTTP_PORT)
// && global option httpclose set (or maybe not in this spot???)
) {
sb = new StringBuilder();
sb.append("\t").append("mode http");
result.add(sb.toString());
sb = new StringBuilder();
sb.append("\t").append("option httpclose");
result.add(sb.toString());
}
int i = 0;
for (PortForwardingRuleTO rule : fwRules) {
// add line like this: "server 65_37_141_30-80_3 10.1.1.4:80 check"
if (rule.revoked()) {
continue;
}
sb = new StringBuilder();
sb.append("\t")
.append("server ")
.append(poolName)
.append("_")
.append(Integer.toString(i++))
.append(" ")
.append(rule.getDstIp())
.append(":")
.append(rule.getDstPortRange()[0])
.append(" check");
result.add(sb.toString());
}
result.add(blankLine);
return result;
}
/*
cookie <name> [ rewrite | insert | prefix ] [ indirect ] [ nocache ]
[ postonly ] [ domain <domain> ]*
Enable cookie-based persistence in a backend.
May be used in sections : defaults | frontend | listen | backend
yes | no | yes | yes
Arguments :
<name> is the name of the cookie which will be monitored, modified or
inserted in order to bring persistence. This cookie is sent to
the client via a "Set-Cookie" header in the response, and is
brought back by the client in a "Cookie" header in all requests.
Special care should be taken to choose a name which does not
conflict with any likely application cookie. Also, if the same
backends are subject to be used by the same clients (eg:
HTTP/HTTPS), care should be taken to use different cookie names
between all backends if persistence between them is not desired.
rewrite This keyword indicates that the cookie will be provided by the
server and that haproxy will have to modify its value to set the
server's identifier in it. This mode is handy when the management
of complex combinations of "Set-cookie" and "Cache-control"
headers is left to the application. The application can then
decide whether or not it is appropriate to emit a persistence
cookie. Since all responses should be monitored, this mode only
works in HTTP close mode. Unless the application behaviour is
very complex and/or broken, it is advised not to start with this
mode for new deployments. This keyword is incompatible with
"insert" and "prefix".
insert This keyword indicates that the persistence cookie will have to
be inserted by haproxy in the responses. If the server emits a
cookie with the same name, it will be replaced anyway. For this
reason, this mode can be used to upgrade existing configurations
running in the "rewrite" mode. The cookie will only be a session
cookie and will not be stored on the client's disk. Due to
caching effects, it is generally wise to add the "indirect" and
"nocache" or "postonly" keywords (see below). The "insert"
keyword is not compatible with "rewrite" and "prefix".
prefix This keyword indicates that instead of relying on a dedicated
cookie for the persistence, an existing one will be completed.
This may be needed in some specific environments where the client
does not support more than one single cookie and the application
already needs it. In this case, whenever the server sets a cookie
named <name>, it will be prefixed with the server's identifier
and a delimiter. The prefix will be removed from all client
requests so that the server still finds the cookie it emitted.
Since all requests and responses are subject to being modified,
this mode requires the HTTP close mode. The "prefix" keyword is
not compatible with "rewrite" and "insert".
indirect When this option is specified in insert mode, cookies will only
be added when the server was not reached after a direct access,
which means that only when a server is elected after applying a
load-balancing algorithm, or after a redispatch, then the cookie
will be inserted. If the client has all the required information
to connect to the same server next time, no further cookie will
be inserted. In all cases, when the "indirect" option is used in
insert mode, the cookie is always removed from the requests
transmitted to the server. The persistence mechanism then becomes
totally transparent from the application point of view.
nocache This option is recommended in conjunction with the insert mode
when there is a cache between the client and HAProxy, as it
ensures that a cacheable response will be tagged non-cacheable if
a cookie needs to be inserted. This is important because if all
persistence cookies are added on a cacheable home page for
instance, then all customers will then fetch the page from an
outer cache and will all share the same persistence cookie,
leading to one server receiving much more traffic than others.
See also the "insert" and "postonly" options.
postonly This option ensures that cookie insertion will only be performed
on responses to POST requests. It is an alternative to the
"nocache" option, because POST responses are not cacheable, so
this ensures that the persistence cookie will never get cached.
Since most sites do not need any sort of persistence before the
first POST which generally is a login request, this is a very
efficient method to optimize caching without risking to find a
persistence cookie in the cache.
See also the "insert" and "nocache" options.
domain This option allows to specify the domain at which a cookie is
inserted. It requires exactly one parameter: a valid domain
name. If the domain begins with a dot, the browser is allowed to
use it for any host ending with that name. It is also possible to
specify several domain names by invoking this option multiple
times. Some browsers might have small limits on the number of
domains, so be careful when doing that. For the record, sending
10 domains to MSIE 6 or Firefox 2 works as expected.
There can be only one persistence cookie per HTTP backend, and it can be
declared in a defaults section. The value of the cookie will be the value
indicated after the "cookie" keyword in a "server" statement. If no cookie
is declared for a given server, the cookie is not set.
Examples :
cookie JSESSIONID prefix
cookie SRV insert indirect nocache
cookie SRV insert postonly indirect
appsession <cookie> len <length> timeout <holdtime>
[request-learn] [prefix] [mode <path-parameters|query-string>]
Define session stickiness on an existing application cookie.
May be used in sections : defaults | frontend | listen | backend
no | no | yes | yes
Arguments :
<cookie> this is the name of the cookie used by the application and which
HAProxy will have to learn for each new session.
<length> this is the max number of characters that will be memorized and
checked in each cookie value.
<holdtime> this is the time after which the cookie will be removed from
memory if unused. If no unit is specified, this time is in
milliseconds.
request-learn
If this option is specified, then haproxy will be able to learn
the cookie found in the request in case the server does not
specify any in response. This is typically what happens with
PHPSESSID cookies, or when haproxy's session expires before
the application's session and the correct server is selected.
It is recommended to specify this option to improve reliability.
prefix When this option is specified, haproxy will match on the cookie
prefix (or URL parameter prefix). The appsession value is the
data following this prefix.
Example :
appsession ASPSESSIONID len 64 timeout 3h prefix
This will match the cookie ASPSESSIONIDXXXX=XXXXX,
the appsession value will be XXXX=XXXXX.
mode This option allows to change the URL parser mode.
2 modes are currently supported :
- path-parameters :
The parser looks for the appsession in the path parameters
part (each parameter is separated by a semi-colon), which is
convenient for JSESSIONID for example.
This is the default mode if the option is not set.
- query-string :
In this mode, the parser will look for the appsession in the
query string.
When an application cookie is defined in a backend, HAProxy will check when
the server sets such a cookie, and will store its value in a table, and
associate it with the server's identifier. Up to <length> characters from
the value will be retained. On each connection, haproxy will look for this
cookie both in the "Cookie:" headers, and as a URL parameter (depending on
the mode used). If a known value is found, the client will be directed to the
server associated with this value. Otherwise, the load balancing algorithm is
applied. Cookies are automatically removed from memory when they have been
unused for a duration longer than <holdtime>.
The definition of an application cookie is limited to one per backend.
Example :
appsession JSESSIONID len 52 timeout 3h
*/
private String getLbSubRuleForStickiness(LoadBalancerTO lbTO) {
int i = 0;
if (lbTO.getStickinessPolicies() == null)
return null;
StringBuilder sb = new StringBuilder();
for (StickinessPolicyTO stickinessPolicy : lbTO.getStickinessPolicies()) {
if (stickinessPolicy == null)
continue;
List<Pair<String, String>> paramsList = stickinessPolicy.getParams();
i++;
/*
* cookie <name> [ rewrite | insert | prefix ] [ indirect ] [ nocache ]
[ postonly ] [ domain <domain> ]*
*/
if (StickinessMethodType.LBCookieBased.getName().equalsIgnoreCase(stickinessPolicy.getMethodName())) {
/* Default Values */
String cookieName = null; // optional
String mode = "insert "; // optional
Boolean indirect = false; // optional
Boolean nocache = false; // optional
Boolean postonly = false; // optional
StringBuilder domainSb = null; // optional
for (Pair<String, String> paramKV : paramsList) {
String key = paramKV.first();
String value = paramKV.second();
if ("cookie-name".equalsIgnoreCase(key))
cookieName = value;
if ("mode".equalsIgnoreCase(key))
mode = value;
if ("domain".equalsIgnoreCase(key)) {
if (domainSb == null) {
domainSb = new StringBuilder();
}
domainSb = domainSb.append("domain ");
domainSb.append(value).append(" ");
}
if ("indirect".equalsIgnoreCase(key))
indirect = true;
if ("nocache".equalsIgnoreCase(key))
nocache = true;
if ("postonly".equalsIgnoreCase(key))
postonly = true;
}
if (cookieName == null) {// re-check all haproxy mandatory params
StringBuilder tempSb = new StringBuilder();
String srcip = lbTO.getSrcIp();
if (srcip == null)
srcip = "TESTCOOKIE";
tempSb.append("lbcooki_").append(srcip.hashCode()).append("_").append(lbTO.getSrcPort());
cookieName = tempSb.toString();
}
sb.append("\t").append("cookie ").append(cookieName).append(" ").append(mode).append(" ");
if (indirect)
sb.append("indirect ");
if (nocache)
sb.append("nocache ");
if (postonly)
sb.append("postonly ");
if (domainSb != null)
sb.append(domainSb).append(" ");
} else if (StickinessMethodType.SourceBased.getName().equalsIgnoreCase(stickinessPolicy.getMethodName())) {
/* Default Values */
String tablesize = "200k"; // optional
String expire = "30m"; // optional
/* overwrite default values with the stick parameters */
for (Pair<String, String> paramKV : paramsList) {
String key = paramKV.first();
String value = paramKV.second();
if ("tablesize".equalsIgnoreCase(key))
tablesize = value;
if ("expire".equalsIgnoreCase(key))
expire = value;
}
sb.append("\t").append("stick-table type ip size ").append(tablesize).append(" expire ").append(expire);
sb.append("\n\t").append("stick on src");
} else if (StickinessMethodType.AppCookieBased.getName().equalsIgnoreCase(stickinessPolicy.getMethodName())) {
/*
* FORMAT : appsession <cookie> len <length> timeout <holdtime>
* [request-learn] [prefix] [mode
* <path-parameters|query-string>]
*/
/* example: appsession JSESSIONID len 52 timeout 3h */
String cookieName = null; // optional
String length = "52"; // optional
String holdtime = "3h"; // optional
String mode = null; // optional
Boolean requestlearn = false; // optional
Boolean prefix = false; // optional
for (Pair<String, String> paramKV : paramsList) {
String key = paramKV.first();
String value = paramKV.second();
if ("cookie-name".equalsIgnoreCase(key))
cookieName = value;
if ("length".equalsIgnoreCase(key))
length = value;
if ("holdtime".equalsIgnoreCase(key))
holdtime = value;
if ("mode".equalsIgnoreCase(key))
mode = value;
if ("request-learn".equalsIgnoreCase(key))
requestlearn = true;
if ("prefix".equalsIgnoreCase(key))
prefix = true;
}
if (cookieName == null) {// re-check all haproxy mandatory params
StringBuilder tempSb = new StringBuilder();
String srcip = lbTO.getSrcIp();
if (srcip == null)
srcip = "TESTCOOKIE";
tempSb.append("appcookie_").append(srcip.hashCode()).append("_").append(lbTO.getSrcPort());
cookieName = tempSb.toString();
}
sb.append("\t").append("appsession ").append(cookieName).append(" len ").append(length).append(" timeout ").append(holdtime).append(" ");
if (prefix)
sb.append("prefix ");
if (requestlearn)
sb.append("request-learn").append(" ");
if (mode != null)
sb.append("mode ").append(mode).append(" ");
} else {
/*
* Error is silently swallowed.
* Not supposed to reach here, validation of methods are
* done at the higher layer
*/
s_logger.warn("Haproxy stickiness policy for lb rule: " + lbTO.getSrcIp() + ":" + lbTO.getSrcPort() + ": Not Applied, cause:invalid method ");
return null;
}
}
if (i == 0)
return null;
return sb.toString();
}
private List<String> getRulesForPool(LoadBalancerTO lbTO, boolean keepAliveEnabled) {
StringBuilder sb = new StringBuilder();
String poolName = sb.append(lbTO.getSrcIp().replace(".", "_")).append('-').append(lbTO.getSrcPort()).toString();
String publicIP = lbTO.getSrcIp();
String publicPort = Integer.toString(lbTO.getSrcPort());
String algorithm = lbTO.getAlgorithm();
List<String> result = new ArrayList<String>();
// add line like this: "listen 65_37_141_30-80 65.37.141.30:80"
sb = new StringBuilder();
sb.append("listen ").append(poolName).append(" ").append(publicIP).append(":").append(publicPort);
result.add(sb.toString());
sb = new StringBuilder();
sb.append("\t").append("balance ").append(algorithm);
result.add(sb.toString());
int i = 0;
Boolean destsAvailable = false;
String stickinessSubRule = getLbSubRuleForStickiness(lbTO);
List<String> dstSubRule = new ArrayList<String>();
List<String> dstWithCookieSubRule = new ArrayList<String>();
for (DestinationTO dest : lbTO.getDestinations()) {
// add line like this: "server 65_37_141_30-80_3 10.1.1.4:80 check"
if (dest.isRevoked()) {
continue;
}
sb = new StringBuilder();
sb.append("\t")
.append("server ")
.append(poolName)
.append("_")
.append(Integer.toString(i++))
.append(" ")
.append(dest.getDestIp())
.append(":")
.append(dest.getDestPort())
.append(" check");
dstSubRule.add(sb.toString());
if (stickinessSubRule != null) {
sb.append(" cookie ").append(dest.getDestIp().replace(".", "_")).append('-').append(dest.getDestPort()).toString();
dstWithCookieSubRule.add(sb.toString());
}
destsAvailable = true;
}
Boolean httpbasedStickiness = false;
/* attach stickiness sub rule only if the destinations are available */
if ((stickinessSubRule != null) && (destsAvailable == true)) {
for (StickinessPolicyTO stickinessPolicy : lbTO.getStickinessPolicies()) {
if (stickinessPolicy == null)
continue;
if (StickinessMethodType.LBCookieBased.getName().equalsIgnoreCase(stickinessPolicy.getMethodName()) ||
StickinessMethodType.AppCookieBased.getName().equalsIgnoreCase(stickinessPolicy.getMethodName())) {
httpbasedStickiness = true;
}
}
if (httpbasedStickiness) {
result.addAll(dstWithCookieSubRule);
} else {
result.addAll(dstSubRule);
}
result.add(stickinessSubRule);
} else {
result.addAll(dstSubRule);
}
if ((stickinessSubRule != null) && !destsAvailable) {
s_logger.warn("Haproxy stickiness policy for lb rule: " + lbTO.getSrcIp() + ":" + lbTO.getSrcPort() + ": Not Applied, cause: backends are unavailable");
}
if ((publicPort.equals(NetUtils.HTTP_PORT) && !keepAliveEnabled) || (httpbasedStickiness)) {
sb = new StringBuilder();
sb.append("\t").append("mode http");
result.add(sb.toString());
sb = new StringBuilder();
sb.append("\t").append("option httpclose");
result.add(sb.toString());
}
result.add(blankLine);
return result;
}
private String generateStatsRule(LoadBalancerConfigCommand lbCmd, String ruleName, String statsIp) {
StringBuilder rule = new StringBuilder("\nlisten ").append(ruleName).append(" ").append(statsIp).append(":").append(lbCmd.lbStatsPort);
// TODO DH: write test for this in both cases
if (!lbCmd.keepAliveEnabled) {
s_logger.info("Haproxy mode http enabled");
rule.append("\n\tmode http\n\toption httpclose");
}
rule.append("\n\tstats enable\n\tstats uri ")
.append(lbCmd.lbStatsUri)
.append("\n\tstats realm Haproxy\\ Statistics\n\tstats auth ")
.append(lbCmd.lbStatsAuth);
rule.append("\n");
String result = rule.toString();
if (s_logger.isDebugEnabled()) {
s_logger.debug("Haproxystats rule: " + result);
}
return result;
}
@Override
public String[] generateConfiguration(LoadBalancerConfigCommand lbCmd) {
List<String> result = new ArrayList<String>();
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);
// TODO DH: write test for this function
String pipesLine = "\tmaxpipes " + Long.toString(Long.parseLong(lbCmd.maxconn) / 4);
gSection.set(3, pipesLine);
if (s_logger.isDebugEnabled()) {
for (String s : gSection) {
s_logger.debug("global section: " + s);
}
}
result.addAll(gSection);
// TODO decide under what circumstances these options are needed
// result.add("\tnokqueue");
// result.add("\tnopoll");
result.add(blankLine);
List<String> dSection = Arrays.asList(defaultsSection);
if (lbCmd.keepAliveEnabled) {
dSection.set(6, "\t#no option set here :<");
dSection.set(7, "\tno option forceclose");
} else {
dSection.set(6, "\toption forwardfor");
dSection.set(7, "\toption forceclose");
}
if (s_logger.isDebugEnabled()) {
for (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
*/
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 (LoadBalancerTO lbTO : lbCmd.getLoadBalancers()) {
if (lbTO.isRevoked()) {
continue;
}
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()]);
}
@Override
public String[][] generateFwRules(LoadBalancerConfigCommand lbCmd) {
String[][] result = new String[3][];
Set<String> toAdd = new HashSet<String>();
Set<String> toRemove = new HashSet<String>();
Set<String> toStats = new HashSet<String>();
for (LoadBalancerTO lbTO : lbCmd.getLoadBalancers()) {
StringBuilder sb = new StringBuilder();
sb.append(lbTO.getSrcIp()).append(":");
sb.append(lbTO.getSrcPort()).append(":");
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;
}
}