16
16
*/
17
17
package org .graylog2 .bindings .providers ;
18
18
19
- import com .google .common .base .Joiner ;
20
- import com .google .common .collect .ImmutableList ;
21
- import com .google .common .collect .Maps ;
22
- import javax .inject .Inject ;
23
- import javax .inject .Provider ;
24
- import javax .inject .Singleton ;
25
- import org .elasticsearch .common .settings .loader .YamlSettingsLoader ;
19
+ import org .elasticsearch .common .settings .Settings ;
20
+ import org .elasticsearch .common .settings .SettingsException ;
21
+ import org .elasticsearch .node .GraylogNode ;
26
22
import org .elasticsearch .node .Node ;
27
- import org .elasticsearch .node . NodeBuilder ;
23
+ import org .elasticsearch .plugins . Plugin ;
28
24
import org .graylog2 .configuration .ElasticsearchConfiguration ;
25
+ import org .graylog2 .indexer .esplugin .MonitorPlugin ;
29
26
import org .slf4j .Logger ;
30
27
import org .slf4j .LoggerFactory ;
31
28
32
- import java .io .File ;
33
- import java .io .IOException ;
34
- import java .nio .file .Files ;
35
- import java .util .Map ;
29
+ import javax .inject .Inject ;
30
+ import javax .inject .Provider ;
31
+ import javax .inject .Singleton ;
32
+ import java .nio .file .Path ;
33
+ import java .util .Collections ;
34
+ import java .util .List ;
36
35
37
36
import static com .google .common .base .Strings .isNullOrEmpty ;
38
- import static org .elasticsearch .node .NodeBuilder .nodeBuilder ;
39
37
38
+ @ Singleton
40
39
public class EsNodeProvider implements Provider <Node > {
41
40
private static final Logger LOG = LoggerFactory .getLogger (EsNodeProvider .class );
42
41
@@ -48,43 +47,45 @@ public EsNodeProvider(ElasticsearchConfiguration configuration) {
48
47
}
49
48
50
49
@ Override
51
- @ Singleton
52
50
public Node get () {
53
- final NodeBuilder builder = nodeBuilder ().client (configuration .isClientNode ());
54
- Map <String , String > settings = readNodeSettings (configuration );
55
-
56
- builder .settings ().put (settings );
57
- return builder .build ();
51
+ return new GraylogNode (
52
+ readNodeSettings (configuration ),
53
+ Collections .<Class <? extends Plugin >>singleton (MonitorPlugin .class ));
58
54
}
59
55
60
- public static Map < String , String > readNodeSettings (ElasticsearchConfiguration conf ) {
61
- Map < String , String > settings = Maps . newHashMap ();
56
+ public static Settings readNodeSettings (ElasticsearchConfiguration conf ) {
57
+ final Settings . Builder settings = Settings . builder ();
62
58
63
59
// Standard Configuration.
64
60
settings .put ("cluster.name" , conf .getClusterName ());
65
61
66
62
settings .put ("node.name" , conf .getNodeName ());
67
- settings .put ("node.master" , Boolean .toString (conf .isMasterNode ()));
68
- settings .put ("node.data" , Boolean .toString (conf .isDataNode ()));
63
+ settings .put ("node.master" , conf .isMasterNode ());
64
+ settings .put ("node.data" , conf .isDataNode ());
65
+ settings .put ("node.client" , true );
66
+
69
67
68
+ settings .put ("path.home" , conf .getPathHome ());
70
69
if (!isNullOrEmpty (conf .getPathData ())) {
71
70
settings .put ("path.data" , conf .getPathData ());
72
71
}
73
72
74
- settings .put ("action.auto_create_index" , Boolean . toString ( false ) );
73
+ settings .put ("action.auto_create_index" , false );
75
74
76
- settings .put ("http.enabled" , Boolean . toString ( conf .isHttpEnabled () ));
77
- settings .put ("transport.tcp.port" , String . valueOf ( conf .getTransportTcpPort () ));
75
+ settings .put ("http.enabled" , conf .isHttpEnabled ());
76
+ settings .put ("transport.tcp.port" , conf .getTransportTcpPort ());
78
77
79
78
settings .put ("discovery.initial_state_timeout" , conf .getInitialStateTimeout ());
80
- settings .put ("discovery.zen.ping.multicast.enabled" , Boolean .toString (conf .isMulticastDiscovery ()));
81
-
82
- if (conf .getUnicastHosts () != null && !conf .getUnicastHosts ().isEmpty ()) {
83
- final ImmutableList .Builder <String > trimmedHosts = ImmutableList .builder ();
84
- for (String host : conf .getUnicastHosts ()) {
85
- trimmedHosts .add (host .trim ());
79
+ settings .put ("discovery.zen.ping.multicast.enabled" , conf .isMulticastDiscovery ());
80
+
81
+ final List <String > unicastHosts = conf .getUnicastHosts ();
82
+ if (unicastHosts != null && !unicastHosts .isEmpty ()) {
83
+ final String [] trimmedHosts = new String [unicastHosts .size ()];
84
+ for (int i = 0 ; i < unicastHosts .size (); i ++) {
85
+ final String host = unicastHosts .get (i );
86
+ trimmedHosts [i ] = host .trim ();
86
87
}
87
- settings .put ("discovery.zen.ping.unicast.hosts" , Joiner . on ( "," ). join ( trimmedHosts . build ()) );
88
+ settings .putArray ("discovery.zen.ping.unicast.hosts" , trimmedHosts );
88
89
}
89
90
90
91
if (!isNullOrEmpty (conf .getNetworkHost ())) {
@@ -97,19 +98,16 @@ public static Map<String, String> readNodeSettings(ElasticsearchConfiguration co
97
98
settings .put ("network.publish_host" , conf .getNetworkPublishHost ());
98
99
}
99
100
100
- settings .put ("plugins.mandatory" , "graylog2-monitor" );
101
-
102
101
// Overwrite from a custom ElasticSearch config file.
103
- final File esConfigFile = conf .getConfigFile ();
102
+ final Path esConfigFile = conf .getConfigFile ();
104
103
if (esConfigFile != null ) {
105
104
try {
106
- final byte [] esSettings = Files .readAllBytes (esConfigFile .toPath ());
107
- settings .putAll (new YamlSettingsLoader ().load (esSettings ));
108
- } catch (IOException e ) {
109
- LOG .warn ("Cannot read Elasticsearch configuration." );
105
+ settings .loadFromPath (esConfigFile );
106
+ } catch (SettingsException e ) {
107
+ LOG .warn ("Cannot read Elasticsearch configuration from " + esConfigFile , e );
110
108
}
111
109
}
112
110
113
- return settings ;
111
+ return settings . build () ;
114
112
}
115
113
}
0 commit comments