| 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 |
1
1
1
1
1
74
74
1
73
62
62
11
73
14
2
14
32
133
133
108
25
32
28
32
32
30
24
30
30
24
30
6
6
6
16
4
16
16
16
16
9
7
16
122
122
122
122
122
48
48
48
48
48
48
47
1
48
37
37
4
19
33
37
22
15
37
37
74
37
37
159
159
112
47
25
6
6
6
| "use strict";
;require.register("utils/configs/hosts_based_initializer_mixin", function (exports, require, module) {
/**
* 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.
*/
var App = require('app');
/**
* Regexp for host with port ('hostName:1234')
*
* @type {string}
*/
var hostWithPort = "([\\w|\\.]*)(?=:)";
/**
* Regexp for host with port and protocol ('://hostName:1234')
*
* @type {string}
*/
var hostWithPrefix = ":\/\/" + hostWithPort;
/**
* Mixin describes host name computations initializers and handlers.
*
* @mixin App.HostsBasedInitializerMixin
*/
App.HostsBasedInitializerMixin = Em.Mixin.create({
initializerTypes: [{ name: 'host_with_component', method: '_initAsHostWithComponent' }, { name: 'hosts_with_components', method: '_initAsHostsWithComponents' }, { name: 'hosts_list_with_component', method: '_initAsHostsListWithComponent' }],
/**
* Initializer for configs with value equal to hostName with needed component
* Value example: 'hostName'
*
* @param {configProperty} configProperty
* @param {topologyLocalDB} localDB
* @param {object} dependencies
* @param {object} initializer
* @returns {Object}
* @private
*/
_initAsHostWithComponent: function _initAsHostWithComponent(configProperty, localDB, dependencies, initializer) {
var component = localDB.masterComponentHosts.findProperty('component', initializer.component);
if (!component) {
return configProperty;
}
if (initializer.modifier) {
var replaceWith = Em.getWithDefault(initializer.modifier, 'prefix', '') + component.hostName + Em.getWithDefault(initializer.modifier, 'suffix', '');
this.setRecommendedValue(configProperty, initializer.modifier.regex, replaceWith);
} else {
Em.setProperties(configProperty, {
recommendedValue: component.hostName,
value: component.hostName
});
}
return configProperty;
},
/**
* Settings for <code>hosts_with_components</code>-initializer
* Used for configs with value equal to the hosts list
* May set value as array (if <code>asArray</code> is true) or as comma-sepratated string (if <code>asArray</code> is false)
*
* @see _initAsHostsWithComponents
* @param {string|string[]} components
* @param {boolean} [asArray=false]
* @param {boolean|undefined} [isInstalled=undefined]
* @returns {{type: string, components: string[], asArray: boolean}}
*/
getComponentsHostsConfig: function getComponentsHostsConfig(components, asArray, isInstalled) {
if (1 === arguments.length) {
asArray = false;
}
return {
type: 'hosts_with_components',
components: Em.makeArray(components),
asArray: asArray,
isInstalled: isInstalled
};
},
/**
* Initializer for configs with value equal to hostNames with needed components
* May be array or comma-separated list
* Depends on <code>initializer.asArray</code> (true - array, false - string)
* Value example: 'hostName1,hostName2,hostName3' or ['hostName1', 'hostName2', 'hostName3']
*
* @param {configProperty} configProperty
* @param {topologyLocalDB} localDB
* @param {object} dependencies
* @param {object} initializer
* @return {Object}
* @private
*/
_initAsHostsWithComponents: function _initAsHostsWithComponents(configProperty, localDB, dependencies, initializer) {
var hostNames = localDB.masterComponentHosts.filter(function (masterComponent) {
var hasFound = initializer.components.contains(masterComponent.component);
if (Em.isNone(initializer.isInstalled)) {
return hasFound;
}
return hasFound && masterComponent.isInstalled === initializer.isInstalled;
}).sortProperty('hostName').mapProperty('hostName');
if (!initializer.asArray) {
hostNames = hostNames.uniq().join(',');
}
Em.setProperties(configProperty, {
value: hostNames,
recommendedValue: hostNames
});
return configProperty;
},
/**
* Settings for <code>host_with_component</code>-initializer
* Used for configs with value equal to hostName that has <code>component</code>
* Value may be modified with if <code>withModifier</code> is true (it is by default)
* <code>hostWithPort</code>-regexp will be used in this case
*
* @see _initAsHostWithComponent
* @param {string} component
* @param {boolean} [withModifier=true]
* @return {object}
*/
getSimpleComponentConfig: function getSimpleComponentConfig(component, withModifier) {
if (arguments.length === 1) {
withModifier = true;
}
var config = {
type: 'host_with_component',
component: component
};
if (withModifier) {
config.modifier = {
type: 'regexp',
regex: hostWithPort
};
}
return config;
},
/**
* Almost the same to <code>getSimpleComponentConfig</code>, but with possibility to modify <code>replaceWith</code>-value
* <code>prefix</code> is added before it
* <code>suffix</code> is added after it
* <code>hostWithPrefix</code>-regexp is used
*
* @see _initAsHostWithComponent
* @param {string} component
* @param {string} [prefix]
* @param {string} [suffix]
* @returns {object}
*/
getComponentConfigWithAffixes: function getComponentConfigWithAffixes(component, prefix, suffix) {
prefix = prefix || '';
suffix = suffix || '';
return {
type: 'host_with_component',
component: component,
modifier: {
type: 'regexp',
regex: hostWithPrefix,
prefix: prefix,
suffix: suffix
}
};
},
/**
* Settings for <code>host_with_port</code>-initializer
* Used for configs with value equal to hostName where some component exists concatenated with port-value
* Port-value is calculated according to <code>port</code> and <code>portFromDependencies</code> values
* If <code>portFromDependencies</code> is <code>true</code>, <code>port</code>-value is used as key of the <code>dependencies</code> (where real port-value is)
* Otherwise - <code>port</code>-value used as is
* If calculated port-value is empty, it will be skipped (and ':' too)
* Value also may be customized with prefix and suffix
*
* @param {string} component needed component
* @param {boolean} componentExists component already exists or just going to be installed
* @param {string} prefix=''
* @param {string} suffix=''
* @param {string} port
* @param {boolean} [portFromDependencies=false]
* @returns {{type: string, component: string, componentExists: boolean, modifier: {prefix: (string), suffix: (string)}}}
* @method getHostWithPortConfig
* @static
*/
getHostWithPortConfig: function getHostWithPortConfig(component, componentExists, prefix, suffix, port, portFromDependencies) {
if (arguments.length < 6) {
portFromDependencies = false;
}
prefix = prefix || '';
suffix = suffix || '';
var ret = {
type: 'host_with_port',
component: component,
componentExists: componentExists,
modifier: {
prefix: prefix,
suffix: suffix
}
};
if (portFromDependencies) {
ret.portKey = port;
} else {
ret.port = port;
}
return ret;
},
/**
* Initializer for configs with value equal to the hostName where some component exists
* Value may be customized with prefix and suffix (see <code>initializer.modifier</code>)
* Port-value is calculated according to <code>initializer.portKey</code> or <code>initializer.port</code> values
* If calculated port-value is empty, it will be skipped (and ':' too)
* Value-examples: 'SOME_COOL_PREFIXhost1:port1SOME_COOL_SUFFIX', 'host1:port2'
*
* @param {configProperty} configProperty
* @param {extendedTopologyLocalDB} localDB
* @param {object} dependencies
* @param {object} initializer
* @returns {object}
* @private
* @method _initAsHostWithPort
*/
_initAsHostWithPort: function _initAsHostWithPort(configProperty, localDB, dependencies, initializer) {
var hostName = localDB.masterComponentHosts.filterProperty('component', initializer.component).findProperty('isInstalled', initializer.componentExists).hostName;
var port = this.__getPort(dependencies, initializer);
var value = initializer.modifier.prefix + hostName + (port ? ':' + port : '') + initializer.modifier.suffix;
Em.setProperties(configProperty, {
value: value,
recommendedValue: value
});
return configProperty;
},
/**
* Settings for <code>hosts_with_port</code>-initializer
* Used for configs with value equal to the list of hostNames with port
* Value also may be customized with prefix, suffix and delimiter between host:port elements
* Port-value is calculated according to <code>port</code> and <code>portFromDependencies</code> values
* If <code>portFromDependencies</code> is <code>true</code>, <code>port</code>-value is used as key of the <code>dependencies</code> (where real port-value is)
* Otherwise - <code>port</code>-value used as is
* If calculated port-value is empty, it will be skipped (and ':' too)
*
* @param {string|string[]} component hosts where this component(s) exists are used as config-value
* @param {string} prefix='' substring added before hosts-list
* @param {string} suffix='' substring added after hosts-list
* @param {string} delimiter=',' delimiter between hosts in the value
* @param {string} port if <code>portFromDependencies</code> is <code>false</code> this value is used as port for hosts
* if <code>portFromDependencies</code> is <code>true</code> `port` is used as key in the <code>dependencies</code> to get real port-value
* @param {boolean} portFromDependencies=false true - use <code>port</code> as key for <code>dependencies</code> to get real port-value,
* false - use <code>port</code> as port-value
* @returns {{type: string, component: string, modifier: {prefix: (string), suffix: (string), delimiter: (string)}}}
* @method getHostsWithPortConfig
* @static
*/
getHostsWithPortConfig: function getHostsWithPortConfig(component, prefix, suffix, delimiter, port, portFromDependencies) {
Iif (arguments.length < 6) {
portFromDependencies = false;
}
prefix = prefix || '';
suffix = suffix || '';
delimiter = delimiter || ',';
var ret = {
type: 'hosts_with_port',
component: component,
modifier: {
prefix: prefix,
suffix: suffix,
delimiter: delimiter
}
};
if (portFromDependencies) {
ret.portKey = port;
} else {
ret.port = port;
}
return ret;
},
/**
* Initializer for configs with value equal to the list of hosts where some component exists
* Value may be customized with prefix and suffix (see <code>initializer.modifier</code>)
* Delimiter between hostNames also may be customized in the <code>initializer.modifier</code>
* Port-value is calculated according to <code>initializer.portKey</code> or <code>initializer.port</code> values
* If calculated port-value is empty, it will be skipped (and ':' too)
* Value examples: 'SOME_COOL_PREFIXhost1:port,host2:port,host2:portSOME_COOL_SUFFIX', 'host1:port|||host2:port|||host2:port'
*
* @param {configProperty} configProperty
* @param {topologyLocalDB} localDB
* @param {object} dependencies
* @param {object} initializer
* @returns {object}
* @private
* @method _initAsHostsWithPort
*/
_initAsHostsWithPort: function _initAsHostsWithPort(configProperty, localDB, dependencies, initializer) {
var hostNames, hosts;
if (Em.isArray(initializer.component)) {
hosts = localDB.masterComponentHosts.filter(function (masterComponent) {
return initializer.component.contains(masterComponent.component);
}).sortProperty('hostName');
} else {
hosts = localDB.masterComponentHosts.filterProperty('component', initializer.component);
}
if (Em.isNone(initializer.componentExists)) {
hostNames = hosts.mapProperty('hostName');
} else {
hostNames = hosts.filterProperty('isInstalled', initializer.componentExists).mapProperty('hostName');
}
var port = this.__getPort(dependencies, initializer);
var value = initializer.modifier.prefix + hostNames.uniq().map(function (hostName) {
return hostName + (port ? ':' + port : '');
}).join(initializer.modifier.delimiter) + initializer.modifier.suffix;
Em.setProperties(configProperty, {
value: value,
recommendedValue: value
});
return configProperty;
},
/**
* Returns port-value from <code>dependencies</code> accorfing to <code>initializer.portKey</code> or <code>initializer.port</code> values
*
* @param {object} dependencies
* @param {object} initializer
* @returns {string|number}
* @private
* @method __getPort
*/
__getPort: function __getPort(dependencies, initializer) {
var portKey = initializer.portKey;
if (portKey) {
return dependencies[portKey];
}
return initializer.port;
},
/**
*
* @param {string} component component name
* @param {boolean} componentExists
* @returns {object}
*/
getHostsListComponentConfig: function getHostsListComponentConfig(component, componentExists, delimiter) {
return {
type: 'hosts_list_with_component',
component: component,
componentExists: componentExists,
modifier: {
delimiter: Em.isNone(delimiter) ? ',' : delimiter
}
};
},
/**
*
* @param {configProperty} configProperty
* @param {topologyLocalDB} localDB
* @param {object} dependencies
* @param {object} initializer
* @returns {configProperty}
*/
_initAsHostsListWithComponent: function _initAsHostsListWithComponent(configProperty, localDB, dependencies, initializer) {
var hostNames = localDB.masterComponentHosts.filterProperty('component', initializer.component).filterProperty('isInstalled', initializer.componentExists).mapProperty('hostName').join(initializer.modifier.delimiter);
Em.setProperties(configProperty, {
value: hostNames,
recommendedValue: hostNames
});
return configProperty;
}
});
}); |