-
Notifications
You must be signed in to change notification settings - Fork 6
/
generateInventory.js
1222 lines (950 loc) · 42.5 KB
/
generateInventory.js
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
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var fs = require('fs');
var fp = require("lodash/fp");
var gu = require("./generateUtilities")
// TODO: move to utils
function rgb(r, g, b){
function c(d){
var h = d.toString(16);
return h.length ===1 ? "0" + h : h;
}
return "#" + c(r) + c(g) + c(b);
}
var ipT = fp.template( "IPDC<%= dcid %>N<%= ('0'+nodeid).slice(-2) %>" );
var drT = fp.template( "$<%= ipref %>:<%= dcid %>,<%= rackid %>" );
var ndT = fp.template( "<%= nodeipref %> # <%= roles %>" );
function getIpRefObject( ipref ){
// ignore "regexpmatch" using tail
return fp.zipObject( [ "dcid", "nodeid" ] )(fp.tail(/\/dc\/(\d+)\/n\/(\d+)/.exec( ipref )).map(i=>Number(i)));
}
function getNodeByIpRef( nodes, ipref){
return fp.find( getIpRefObject( ipref ) )( nodes );
}
// supported compType:
// * all components
// * nodes node list
// * XX component
function gatherComp( topology, compType, compList ){
var genNodeId = gu.genRegionIdNodeId( topology.regions.length === 1 );
var comps = [];
fp.map(
region => fp.map(
subnet =>
fp.reduce( (css, node) => {
// compType:
// "all" to return for any component; no third parameter
// "byNodes" to return for a list of nodes, in this case, third parameter is a list of nodes
// "XX" component type to return for specific type of component; no third parameter
//
if( compType === "*"
|| (compType === "nodes" && fp.find( { dcid: region.id, nid: node.id} )( compList ) )
|| fp.find( { comp: compType } )(node.components) ){
var c = {
dcid: region.id,
nodeid: node.id,
rackid: (typeof node.rack === "undefined"? 1 : node.rack),
ip: node.ip
};
c.regionidnodeidref = genNodeId( c.dcid, c.nodeid );
c.ipref = ipT({ dcid: c.dcid, nodeid: c.nodeid });
c.drref = drT({ ipref: c.ipref, dcid: c.dcid, rackid: c.rackid } );
// "Invert" components
c.components = fp.keyBy("comp")(node.components);
c.roles = fp.map(comp => comp.comp)(node.components).join(", ");
c.nodeipref = ndT({ nodeipref: fp.padCharsEnd(' ')(34)( c.ipref + "=" + c.ip ), roles: c.roles });
comps.push( c );
}
return comps;
}, comps )(subnet.nodes)
)(region.subnets)
)(topology.regions);
return fp.sortBy(["dcid","nodeid"])(comps);
}
var compName = {
"MS": "edge-managementserver",
"MS": "edge-management-server",
"UI": "edge-sap-ui",
"R": "edge-router",
"MP": "edge-message-processor",
"QS": "edge-qpid-server",
"PS": "edge-postgres-server",
"ZK": "apigee-zookeeper",
"CS": "apigee-cassandra",
"OL": "apigee-openldap",
"QD": "apigee-qpidd",
"PG": "apigee-postgresql",
"PGm": "apigee-postgresql",
"PGs": "apigee-postgresql",
"DP": "apigee-drupal-devportal",
"BS": "baas-sap-usergrid",
"BP": "baas-sap-portal",
"ES": "apigee-elasticsearch",
"TC": "apigee-tomcat",
"IF": "apigee-influx",
"TG": "apigee-telegraf",
"GF": "apigee-grafana"
}
// [ ] Install Rule: for v. 17.01+ dp might be different
// [ ] Install Rule: ps installs PS and PG*
// [ ] Install Rule: qs installs QS and QID
// [ ] Install Rule: if MS and OL on different nodes, USER_LDAP_REMOTE_HOST=y
// [ ] Install Rule: if MS and UI on same node, do MS, ignore UI for this node
// TODO: WIP: REFACTOR: finish with IF, TG, FG, PGd
var compInst = {
"ZK": { comp: "zk", ansible: "edge-comp-setup.yml" },
"CS": { comp: "c", ansible: "edge-comp-setup.yml" },
"OL": { comp: "ld", ansible: "edge-comp-setup.yml" },
"MS": { comp: "ms", ansible: "edge-comp-setup.yml" },
"UI": { comp: "ui", ansible: "edge-comp-setup.yml" },
"QS": { comp: "qs", ansible: "edge-comp-setup.yml" },
"PS": { comp: "ps", ansible: "edge-comp-setup.yml" },
"R": { comp: "r", ansible: "edge-comp-setup.yml" },
"MP": { comp: "mp", ansible: "edge-comp-setup.yml" },
"DP": { comp: "dp", ansible: "edge-comp-setup.yml" },
"PGd": { comp: "pdb", ansible: "edge-comp-setup.yml" },
"ES": { comp: "e", ansible: "edge-comp-setup.yml" },
"BS": { comp: "b", ansible: "edge-comp-setup.yml" },
"BP": { comp: "p", ansible: "edge-comp-setup.yml" },
"IF": { comp: "influxdb", ansible: "edge-comp.yml" },
"TG": { comp: "telegraf", ansible: "edge-comp.yml" },
"GF": { comp: "grafana", ansible: "edge-comp.yml" }
}
var getTopologyPropertyFromTopologyPortdefs = fp.curry( ( portdefs, topology, property ) => {
var val = fp.get( property, topology )
return typeof val === "undefined" ? fp.get( property, portdefs ) : val;
})
var getComponentPropertyFromTopologyPortdefs = fp.curry( ( portdefs, topology, component, property ) => {
var val = fp.get( property, topology )
return typeof val === "undefined" ? fp.get( property, portdefs ) : val;
})
// TODO: move to utils
// TODo: DOCO:
// wrapper over <stream>.write that implements Optional/Required for environment parameters
// if flag [O/R] is O, nothing is written in in the stream and therefore the default for Edge
// install command is used. If needed to be overwritten, then define it in customer.<parameter> in
// topology file. if flag is missed or "R", whatever is passed is generated.
//
//
function streamProperty( stream, parameter, value, optional ){
var optional = (typeof optional === "undefined" )? "R" : optional;
// WARNING: Required parameters undefined
if( typeof value === "undefined" && optional === "R" ){
ruleWarning( `Required Parameter: ${parameter} is undefined.`)
}
if( !( typeof value === "undefined" && optional === "O") ){
stream.write( `${parameter}=${value}\n` );
}
}
// TODO: move to utils
// TODO: Refactor
function ruleWarning( w ){
if( typeof w === 'string' && w.length != 0 ){
console.log( "WARNING: " + w);
}if( Array.isArray(w) && w.length != 0 ){
// array of string
console.log( "WARNING: " + w );
}
}
// If component requires custum configuration file, the table provides parametrization information for it
// NOTE: in array to guarantee traversal order; shifted to object for lookup retrieval
// in case of multiple MSs, For MS .primary = true
var compConfigurations = [
[ "OLMSUI", {
install: "ms",
ansible: "edge-comp-setup.yml",
comps: [ "OL", "MS", "UI"],
pred: true,
config: [ "hosts", "hostip", "bind", "ms-host", "ms-creds", "license", "ldap-host", "dc", "rmp-pod", "zk-hosts", "cs-creds", "cs-hosts", "pg-creds" ]
} ],
[ "RMP", {
install: "rmp",
ansible: "edge-comp-setup.yml",
comps: [ "R", "MP" ],
pred: true,
config: [ "hosts", "dc", "rmp-pod" ]
} ],
[ "DS", {
install: "ds",
ansible: "edge-comp-setup.yml",
comps: [ "ZK", "CS" ],
pred: true,
config: [ "hosts", "hostip", "run-as", "cs-creds", "zk-hosts", "cs-hosts" ]
} ],
[ "SAX", {
install: "sax",
ansible: "edge-comp-setup.yml",
comps: [ "QS", "PS" ],
pred: true,
config: [ "hosts", "hostip", "dc", "rmp-pod", "run-as", "pg-conf", "pg-creds" ]
} ],
[ "MS", {
install: "ms",
ansible: "edge-comp-setup.yml",
comps: [ "MS" ],
pred: true,
config: [ "hosts", "hostip", "ms-host", "ms-creds", "license", "ldap-host", "ldap-creds", "dc", "rmp-pod", "zk-hosts", "cs-creds", "cs-hosts", "pg-creds" ]
} ],
[ "OL", {
install: "ld",
ansible: "edge-comp-setup.yml",
comps: [ "OL" ],
pred: true,
config: [ "hosts","ldap-host", "ldap-creds", "ldap-conf", "run-as" ]
} ],
[ "UI", {
install: "ui",
ansible: "edge-comp-setup.yml",
comps: [ "UI" ],
pred: true,
config: [ "hosts", "brand", "hostip", "ms-host", "ms-creds", "run-as", "ldap-host", "smtp" ]
} ],
[ "CS", {
install: "c",
ansible: "edge-comp-setup.yml",
comps: [ "CS" ],
pred: true,
config: [ "hosts", "hostip", "run-as", "cs-creds", "cs-hosts" ]
} ],
[ "ZK", {
install: "zk",
ansible: "edge-comp-setup.yml",
comps: [ "ZK" ],
pred: true,
config: [ "hosts", "hostip", "run-as", "zk-hosts" ]
} ],
[ "R", {
install: "r",
ansible: "edge-comp-setup.yml",
comps: [ "R" ],
pred: true,
config: [ "hosts", "dc", "rmp-pod" ]
} ],
[ "MP", {
install: "mp",
ansible: "edge-comp-setup.yml",
comps: [ "MP" ],
pred: true,
config: [ "hosts", "dc", "rmp-pod" ]
} ],
[ "PS", {
install: "ps",
ansible: "edge-comp-setup.yml",
comps: [ "PS" ],
pred: true,
config: [ "hosts", "hostip", "dc", "rmp-pod", "run-as", "pg-conf", "pg-creds" ]
} ],
[ "QS", {
install: "qs",
ansible: "edge-comp-setup.yml",
comps: [ "QS" ],
pred: true,
config: [ "hosts", "dc", "rmp-pod" ]
} ],
[ "UG", {
install: "e",
ansible: "edge-comp-setup.yml",
comps: [ "UG" ],
pred: true,
config: [ "hosts", "brand" ]
} ],
[ "BS", {
install: "b",
ansible: "edge-comp-setup.yml",
comps: [ "BS" ],
pred: true,
config: [ "hosts", "smtp" ]
} ],
[ "DP", {
install: "dp",
ansible: "edge-comp-setup.yml",
comps: [ "DP" ],
pred: true,
config: [ "hosts","brand" ]
} ]
];
// Index values for lookup operations
var compConfigurationsIdx = fp.fromPairs(compConfigurations);
module.exports = function ( topologyFile, outputFile, program ){
var portdefs = require("./edge-defs.json");
var topology = require( topologyFile );
var genNodeId = gu.genRegionIdNodeId( topology.regions.length === 1 );
// I.E.: ...= getTopologyProperty("customer.cassPassword");
var getTopologyProperty= getTopologyPropertyFromTopologyPortdefs( portdefs, topology );
var getComponentProperty= getComponentPropertyFromTopologyPortdefs( portdefs, topology );
//--- TODO: Refactor to utils.js -------------
// TODO: refactor to utils: CREF: generateSvgDiagram
// make lookup table for component:isApigee check
var isApigee = fp(portdefs.mappings.edge).reduce( (comps, comp) => {
comps[comp.client.component] = comp.client.apigee;
return comps;
}, {});
//------------------------------
// Collect Node List; ordered by Node ID
var nodes = fp.flatMap( region =>
fp.flatMap( subnet =>
fp.map( node => {
node.roles = fp.map(comp=>comp.comp)(node.components);
node.dcid = region.id;
node.subnet = subnet.name;
return node;
} )(subnet.nodes)
)(region.subnets)
)(topology.regions);
// Templates
var versionT = fp.template( "<%= comment%> Planet: <%= planet %>; Version: <%= version %>. Generated by etp at: <%= genat %>" );
var htmlDocHeader = `
<head>
<style>
html * {
font-size: small;
font-family: "Arial Narrow", Arial, sans-serif !important;
}
table {
border-collapse: collapse;
}
th {
background-color: #dddddd;
}
th,
td {
border: 1px solid lightgrey;
}
tr:nth-of-type(odd) {
background-color: #f9f9f9;
}
.roleApigee {
font-weight: bold;
color: #f83100;
}
.role3rdParty {
font-weight: bold;
color: blue;s
}
.nodeID {
font-weight: bold;
}
</style>
</head>`;
var htmlInventoryHeader = `<table>\n
<thead>
<tr>
<th>DC</th>
<th>Node #</th>
<th>Role(s)</th>
<th>Rack</th>
<th>Subnet</th>
<th>Hostname</th>
<th>IP</th>
</tr>
</thead>
<tbody>
`;
var htmlInventoryFooter = "</tbody></table>\n\n"
var htmlTDTemplate = fp.template(
`<tr>
<td>dc-<%= dcid %></td>
<td>Node <bold><%= id %></bold></td>
<td><%= roles %></td>
<%= rackcell %>
<td><%= subnet %></td>
<td><%= hostname %></td>
<td><%= ip %></td>
</tr>`);
// https://s-media-cache-ak0.pinimg.com/564x/be/54/8f/be548f0f7eb1a29259a1e5c980b0d1e7.jpg
var rackColor = {
1: rgb( 21, 151, 2 ),
2: rgb( 121, 235, 14 ),
3: rgb( 198, 255, 72 ),
4: rgb( 217, 255, 134 ),
5: rgb( 164, 255, 84 ),
6: rgb( 126, 239, 125 )
}
// http://img.bhs4.com/e1/8/e18d5f8ec497dc8baf51ca1d2462932e71d972ce_large.jpg
var subnetColor = {
1: rgb( 84, 87, 72 ),
2: rgb( 180, 189, 131 ),
3: rgb( 216, 208, 123 ),
4: rgb( 242, 216, 109 ),
5: rgb( 254, 196, 44 )
}
// Generate html table
var htmlInventoryNodes = [];
fp.reduce( (list, node) => {
//console.log(node);
list.push( htmlTDTemplate({
dcid: node.dcid,
id: '<span class="nodeID">'+node.id+'</span>',
roles: fp.map( role => isApigee[role]?'<span class="roleApigee">'+role+'</span>':'<span class="role3rdParty">'+role+'</span>' )(node.roles).join(','),
rackcell: typeof node.rack === "undefined" ? '<td/>' : '<td style="text-align:right; background-color:'+rackColor[node.rack]+';">'+node.rack+'</td>',
subnet: node.subnet,
hostname: node.hostname,
ip: node.ip}) );
return list;
}, htmlInventoryNodes)(fp.sortBy(["dcid","id"])(nodes));
//
//---------------------------------------
var svgstream = fs.createWriteStream( outputFile );
svgstream.write( htmlDocHeader );
svgstream.write( htmlInventoryHeader );
svgstream.write( htmlInventoryNodes.join('\n') );
svgstream.write( htmlInventoryFooter );
svgstream.end();
//---------------------------------------
// generate ansible file and ansible playbook invocation script
// I.E.:
// [edge]
// n01 ansible_host=10.119.131.11 ansible_user=opapiadmin ansible_ssh_private_key_file=~/.ssh/id_ansible
var ansibleHosts = [ "[edge]" ];
var ansibleHostT = fp.template( '<%= id %> ansible_host=<%= ip %><%= ansible_user %><%= ansible_key %>' );
fp.reduce( (list, node) => {
//console.log(node);
list.push( ansibleHostT({
id: genNodeId( node.dcid, node.id),
ip: node.ip,
ansible_user: ((program.ansible_user || "") === "") ? "" : " ansible_user="+program.ansible_user,
ansible_key: ((program.ansible_key || "") === "") ? "" : " ansible_ssh_private_key_file="+program.ansible_key
}) );
return list;
}, ansibleHosts)(fp.sortBy(["dcid","id"])(nodes));
var svgstream = fs.createWriteStream( program.directory + '/' + "hosts" );
svgstream.write( ansibleHosts.join('\n') );
svgstream.end();
//--------------------------------------------------------------------------
//---------------------------------------
// generate ssh config file
// TODO: Refactor to re-use with ansibleHosts: practically same code
// I.E.:
// Host n02
// HostName 10.119.132.11
// IdentityFile ~/.ssh/id_ansible
var sshConfig = [];
var sshConfigT = fp.template( "Host <%= id %>\n" +
" HostName <%= ip %>\n" +
" IdentityFile <%= ansible_key %>\n" );
fp.reduce( (list, node) => {
//console.log(node);
list.push( sshConfigT({
id: genNodeId( node.dcid, node.id),
ip: node.ip,
ansible_key: ((program.ansible_key || "") === "") ? "" : program.ansible_key
}) );
return list;
}, sshConfig)(fp.sortBy(["dcid","id"])(nodes));
var svgstream = fs.createWriteStream( program.directory + '/' + "config" );
svgstream.write( sshConfig.join('\n') );
svgstream.end();
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
// TODO:
// silent .cfg file generation
/*
ZK can either be Voter [ZKv] or Observer [ZKo].
For 1 DC: 3 ZK nodes, all Voters.
For 2 DC : 3 ZK nodes in each DC, 3 [ZKv] in DC-1 and 2 [ZKv]+1 [ZKo] in DC-2.
Total number of [ZKv] across all DC's needs to be always odd. Number of [ZKo] nodes is not important, they are all read-only replicas.
CS ZK the relevant portions of the silent files for each DC in DU:
My naming convention has $IPA* for DC-1, $IPB* for DC-2.
DC-1:
ZK_HOSTS="$IPA13 $IPA14 $IPA15 $IPB13 $IPB14 $IPB15:observer"
ZK_CLIENT_HOSTS="$IPA13 $IPA14 $IPA15"
CASS_HOSTS="$IPA13:1,1 $IPA14:1,1 $IPA15:1,1 $IPB13:2,1 $IPB14:2,1 $IPB15:2,1"
DC-2:
ZK_HOSTS="$IPB13 $IPB14 $IPB15:observer $IPA13 $IPA14 $IPA15"
ZK_CLIENT_HOSTS="$IPB13 $IPB14 $IPB15"
CASS_HOSTS="$IPB13:2,1 $IPB14:2,1 $IPB15:2,1 $IPA13:1,1 $IPA14:1,1 $IPA15:1,1"
C* syntax is IP:DC-number,Rack-number.
NB first character is : second is ,
$IPB15:2,3 this would be the C* node in DC2 placed on the third rack of the DC.
*/
// Generate .cfg per dc
// collect nodes with CS comp in all DCs
// var css = fp.filter(
// ["subnets" tier", "dmz"]
// // {subnets: {"tier":"dmz"} }
// // { subnets: [ { nodes: [ "components", "R" ]}]}
// )(topology.regions);
// )(topology.regions[0].subnets);
// var cs1 = fp.map( ({id, components}) => {
// console.log(id, components);
// } )(topology.regions);
var genCfgFileName = function( planet, regionId, nodeId, compType, isPrimaryMS ){
var cfgT = fp.template( "<%= planet %>-dc<%= dcid %>-<%= nodeidref %>-<%= compprefix %>.cfg" );
// Define config file prefix
var nodeidref = "all";
var comp = compType.toLowerCase();
if( compType === "MS" && isPrimaryMS ){
}else if( compConfigurationsIdx[compType] === undefined ){
comp = "ms";
}else{
nodeidref = 'n'+('0'+nodeId).slice(-2);
}
return cfgT({planet: planet.toLowerCase(), dcid: regionId, nodeidref: nodeidref, compprefix: comp })
}
var all = gatherComp(topology, "*");
var mss = gatherComp(topology, "MS");
var ols = gatherComp(topology, "OL");
var uis = gatherComp(topology, "UI");
// TODO: PG, PGm, PGs
// Then add: ,{compType:"PG", comps:pgs}
// TODO: Refactor eventually to PG with master: property
var pss = gatherComp(topology, "PS");
var pgms = gatherComp(topology, "PGm");
var pgss = gatherComp(topology, "PGs");
var css = gatherComp(topology, "CS");
var zks = gatherComp(topology, "CS");
var bss = gatherComp(topology, "BL");
/* --------------------------------------------------------
WIP
Gathering of component combinations
-------------------------------------------------------- */
// (1) collect component combinations
// step 1: populate compPlanet with comp info
var compPlanet = {};
fp.map( region => {
fp.map( subnet => {
fp.reduce( (compPlanet, node ) => {
// console.log(node.components);
compPlanet[ `/dc/${node.dcid}/n/${node.id}` ] = {
components: fp.map('comp')(node.components),
node: {
dcid: node.dcid,
nid: node.id,
}
};
return compPlanet;
}, compPlanet )(subnet.nodes);
})(region.subnets)
})(topology.regions);
// step 2: check if a collection of components is on a node then exclude it from compPlanet
var compConfList =[];
fp.map( compConf => {
fp.reduce( (complist, key ) => {
if( fp.difference(compConf[1].comps,compPlanet[key].components).length === 0 ){
compPlanet[key].components = fp.difference(compPlanet[key].components, compConf[1].comps);
complist.push( {
compType: compConf[0],
node: compPlanet[key].node
} );
var x = 0;
}
return complist
}, compConfList )(fp.keys(compPlanet))
} )(compConfigurations);
// and
// step 3: pivot nodes by compType; collect comps using gatherComp()
var configLayout = {};
fp.reduce( (configL, compConf)=>{
(configL[compConf.compType]|| (configL[compConf.compType]=[])).push(compConf.node);
return configL;
}, configLayout )(compConfList);
// and
// step 4: collect comps by type
// .comps are in format: [ { dcid: 1, nid: 2 }, {}... ]
var configCompNodesByType = fp.map(
comparr => {
return {
compType: comparr[0],
compNodes: gatherComp( topology, "nodes", comparr[1] ) }
}
)(fp.toPairs(configLayout))
//
//
// Edge Components Integrity and Best Practices Rules
//
//
// MS
// iRULE: If there are MS nodes defined in a region, one of them should be nominated as .primary
var noOfMSs = mss.length;
fp( mss ).filter()
fp.map( dc => {
var msPrimaryCounts = fp(mss).filter(
{dcid: dc.id}
).map( n => {
return {nodeid: n.nodeid, primary: n.components["MS"].primary }
} ).countBy({primary: true}).value();
if( msPrimaryCounts.true != 1 ){
ruleWarning( `Rule: each DC with MSs should have 1 .primary==true MS. Found dc: ${dc.id} has ${msPrimaryCounts.true} MSs` )
}
}
)(topology.regions)
// iRULE: odd number of non-observer nodes per planet
var noOfZKs = zks.length;
var noOfZKos = fp(zks).reduce( (no, n ) => { return no + (n.components["ZK"].observer==true? 1: 0) }, 0 )
if( (noOfZKs - noOfZKos) % 2 == 0 ){ // if even
ruleWarning( `Rule: Should have odd number of voter nodes. Found ${noOfZKos} observers out of ${noOfZKs} total. Mark some ZK(s) as .observer:true` )
}
// PS -- PGm, PGs
// each PS should have its PGm/PGs counterpart on its node
// TODO: let's evolve the language!! DSL to describe topology integrity rules
// iRULE: single PGm across all Data centers
var noOfPGms = pgms.length;
if( noOfPGms !== 1 ){
ruleWarning( `Rule: single PGm across Data Centers. Found ${noOfPGms}` )
}
// iRULE: Each node must have unique IP address
var dupIPs = fp(all).groupBy("ip").pickBy(x => x.length > 1).value()
if( !fp.isEmpty(dupIPs) ){
ruleWarning(
"Rule: Duplicate IP addresses assigned: "
+
fp.toPairs(dupIPs).map(v=> {return v[0] + ":" + v[1].map(n=>n.ipref).join(",")} ).join("; ")
)
}
// iRULE: Each PS should have a single PGm or PGs on the same node
ruleWarning(
fp.reduce(
( rerrors, ps ) => {
if( (ps.components.PGm === undefined? 0 : 1) + (ps.components.PGs === undefined? 0 : 1) !== 1 ) {
rerrors.push( `Rule: Each node containing PS component must have either PGm or PGs.` );
}
return rerrors;
}, []
)(pss)
);
// TODO: add an option for a type of artifact to be generated
if( true ){
// top-level iteration by region/DC
fp.map( region => {
// next-level iteration by list of nodes for a component type
fp.map( configurations => {
fp.map( compnode => {
// TODO: Refactor .primary marker with relation to the generated file name.
// original value : compnode.components[configurations.compType].primary )
var cfgstream = fs.createWriteStream( program.directory + '/' + genCfgFileName( topology.planet, region.id, compnode.nodeid, configurations.compType, false ) );
cfgstream.write( versionT({ comment: '#', planet: topology.planet, version: topology.version, genat: new Date() } ));
cfgstream.write("\n\n");
// list nodes
if( fp.includes("hosts")(compConfigurationsIdx[configurations.compType].config) ){
fp.toPairs(fp.groupBy("dcid")(all)).map(
dc => {
cfgstream.write( "#--------------------------------------------------------------------------\n");
cfgstream.write( "# Datacentre: " + dc[0] + "\n" );
cfgstream.write( "#--------------------------------------------------------------------------\n");
cfgstream.write(dc[1].map(n=>n.nodeipref).join('\n'));
cfgstream.write("\n\n");
})
cfgstream.write("");
cfgstream.write("HOSTIP=$(hostname -i)\n");
}
// BRAND: generated if present
if( fp.includes("brand")(compConfigurationsIdx[configurations.compType].config) ){
streamProperty( cfgstream, "BRAND", getTopologyProperty("customer.brand"), "O" );
}
// MS section:
/*
-- if more than one MS in a datacentre,
seek for a primary,
if absent, use first found for ALL,
use <current> for each other -ms-
*/
/*
-- if MS override present, use override
xx
-- if MS and OL on the same node, pair them
*/
if( fp.includes("ms-host")(compConfigurationsIdx[configurations.compType].config) ){
var msip = null;
if( compnode.components["MS"].primary ){
msip = compnode.ip;
}else{
// for this dc, locate MS with .primary=true and use its ipref for MSIP
msip = fp( mss ).filter({dcid: region.id}).map(
n=> { return {ip: n.ip, primary: n.components["MS"].primary }}
).filter({primary: true}).value()[0].ip
}
streamProperty( cfgstream, "MSIP", msip );
streamProperty( cfgstream, "APIGEE_PORT_HTTP_MS", getTopologyProperty("customer.msPort"), "O" );
}
if( fp.includes("ms-creds")(compConfigurationsIdx[configurations.compType].config) ){
cfgstream.write( `ADMIN_EMAIL=${getTopologyProperty("customer.adminEmail")}\n` );
cfgstream.write( `APIGEE_ADMINPW=${getTopologyProperty("customer.adminPassword")}\n` );
}
if( fp.includes("license")(compConfigurationsIdx[configurations.compType].config) ){
streamProperty( cfgstream, "LICENSE_FILE", getTopologyProperty("customer.licenseFile") );
}
if( fp.includes("bind")(compConfigurationsIdx[configurations.compType].config) ){
streamProperty( cfgstream, "BIND_ON_ALL_INTERFACES", getTopologyProperty("customer.bindOnAllInterfaces"), "O" );
}
if( fp.includes("run-as")(compConfigurationsIdx[configurations.compType].config) ){
streamProperty( cfgstream, "RUN_GROUP", getTopologyProperty("customer.runGroup"), "O" );
streamProperty( cfgstream, "RUN_GROUP", getTopologyProperty("customer.runGroup"), "O" );
}
// MP_POD: generated if present
if( fp.includes("rmp-pod")(compConfigurationsIdx[configurations.compType].config) ){
streamProperty( cfgstream, "MP_POD", getTopologyProperty("customer.mpPod"), "O" );
}
// REGION
if( fp.includes("region")(compConfigurationsIdx[configurations.compType].config) ){
streamProperty( cfgstream, "REGION", region.name, "O" );
}
// TODO: LDAP
/*------------------------
LDAP properties affected are:
USE_LDAP_REMOTE_HOST=y
LDAP_HOST=
APIGEE_LDAPPW=
LDAP_PORT=
for multi DC: LDAP_TYPE=2; if standalone, i..e no replication needed: LDAP_TYPE=1
LDAP_SID=1,2,3 etc for the ID of each node in sequence
LDAP_PEER=
for up to 2 LDAP nodes LDAP_PEER point to each other. For more I have the logic. Limit to 2 LDAP peers for now in your logic
for total >2 nodes is LDAP multimaster replication across 3 or more nodes.
*/
if( fp.includes("zk-hosts")(compConfigurationsIdx[configurations.compType].config) ){
// TODO: change the behavior of observer so that if some ZK nodes are marked as observer in the topology, then this choice marking is respected.
cfgstream.write( "\n" );
// TODO: [ ] RIP: ZK -- explicit observers
var observers = (zks.length % 2)===0 ? 1: 0
var _zks = fp(zks).take(zks.length-observers).map(n=> ({dcid: n.dcid, zkref: "$"+n.ipref}) ).value().concat(
fp(zks).takeRight(observers).map(n=> ({ dcid: n.dcid, zkref: "$"+n.ipref + ":observer" }) ).value()
);
var ZK_HOSTS = fp(_zks).map(n=>n.zkref).value().join(" ");
var ZK_CLIENT_HOSTS = fp(zks).filter({dcid: region.id}).map(n=> "$"+n.ipref).value().join(" ")
streamProperty( cfgstream, "ZK_HOSTS", "\""+ZK_HOSTS+"\"", "R" );
streamProperty( cfgstream, "ZK_CLIENT_HOSTS", "\""+ZK_CLIENT_HOSTS+"\"", "R" );
}
if( fp.includes("cs-hosts")(compConfigurationsIdx[configurations.compType].config) ){
var CASS_HOSTS = fp(css).filter({dcid: region.id}).map(n=>n.drref).value().join(" ")
+ " " + fp(css).reject({dcid: region.id}).map(n=>n.drref).value().join(" ");
streamProperty( cfgstream, "CASS_HOSTS", "\""+CASS_HOSTS+"\"", "R" );
}
if( fp.includes("ldap-host")(compConfigurationsIdx[configurations.compType].config) ){
cfgstream.write( "\n" );
// TODO: MSIP for MS section also LDAP_REMOTE_HOST=y if it is not collocated with an MS
// TODO: MS
// TODO: check USE_LDAP_REMOTE_HOST
if( configurations.compType == "MS"){
if( typeof compnode.components["MS"].ldapHost == "undefined" ){
// OL is collocated with MS
cfgstream.write( `USE_LDAP_REMOTE_HOST=n\n` );
}else{
// OL is remote
cfgstream.write( `USE_LDAP_REMOTE_HOST=y\n` );
// Deref OL for MS
var ldapNode = getNodeByIpRef( all, compnode.components["MS"].ldapHost );
cfgstream.write( `LDAP_HOST=${ldapNode.ipref}\n` );
cfgstream.write( `LDAP_TYPE=${ldapNode.components["OL"].ldapType}\n` );
cfgstream.write( `LDAP_SID=${ldapNode.components["OL"].ldapSid}\n` );
}
}
}
if( fp.includes("ldap-conf")(compConfigurationsIdx[configurations.compType].config) ){
cfgstream.write( "\n" );
cfgstream.write( `LDAP_TYPE=${compnode.components["OL"].ldapType}\n` );
cfgstream.write( `LDAP_SID=${compnode.components["OL"].ldapSid}\n` );
if( typeof compnode.components["OL"].ldapPeer !== "undefined" ){
cfgstream.write( `LDAP_PEER=$${
ipT( fp.zipObject( [ "regexpmatch", "dcid", "nodeid" ] )
( /\/dc\/(\d+)\/n\/(\d+)/.exec( compnode.components["OL"].ldapPeer ) )
)
}\n` );
}
}
if( fp.includes("ldap-creds")(compConfigurationsIdx[configurations.compType].config) ){
// TODO: add other creds parameters
streamProperty( cfgstream, "APIGEE_LDAPPW", getTopologyProperty("customer.ldapPassword"), "R" );
}
//-------------------
// TODO: PG/PGm/PGs
if( fp.includes("pg-conf")(compConfigurationsIdx[configurations.compType].config) ){
cfgstream.write( "\n" );
// PGm - a single master across Data Centres; checked by iRules;
// PGs - either a current one or any, ie, a first from PGs collection
if( typeof compnode.components["PG"] !== 'undefined' ){
// skip section generation
}else{
var pgmnodeipref = (typeof compnode.components["PGm"] === 'undefined' ? pgms[0].ipref : compnode.ipref );
var pgsnodeipref = (typeof compnode.components["PGs"] === 'undefined' ? pgss[0].ipref : compnode.ipref );
streamProperty( cfgstream, "PG_MASTER", "$"+ pgmnodeipref );
streamProperty( cfgstream, "PG_STANDBY", "$" + pgsnodeipref );
}
}
if( fp.includes("pg-creds")(compConfigurationsIdx[configurations.compType].config) ){
cfgstream.write( "\n" );
streamProperty( cfgstream, "PG_USER", getTopologyProperty("customer.pgUsername"), "R" );
streamProperty( cfgstream, "PG_PWD", getTopologyProperty("customer.pgPassword"), "R" );
}
if( fp.includes("smtp")(compConfigurationsIdx[configurations.compType].config) ){
cfgstream.write( "\n" );
streamProperty( cfgstream, "SKIP_SMTP", getTopologyProperty("customer.skipSmtp"), "R" );
streamProperty( cfgstream, "SMTPHOST", getTopologyProperty("customer.smtpHostp"), "O" );
streamProperty( cfgstream, "SMTPPORT", getTopologyProperty("customer.smtpPort"), "O" );
streamProperty( cfgstream, "SMTPUSER", getTopologyProperty("customer.smtpUser"), "O" );
streamProperty( cfgstream, "SMTPPASSWORD", getTopologyProperty("customer.smtpPassword"), "O" );
streamProperty( cfgstream, "SMTPMAILFROM", getTopologyProperty("customer.smtpMailFrom"), "R" );
streamProperty( cfgstream, "SMTPSSL", getTopologyProperty("customer.smtpSsl"), "O" );
}
cfgstream.end();
})(configurations.compNodes)
})(configCompNodesByType)
})(topology.regions)
}
// end: program.prefix