-
Notifications
You must be signed in to change notification settings - Fork 24.3k
/
CloudGcePlugin.java
128 lines (110 loc) · 4.34 KB
/
CloudGcePlugin.java
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
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch 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 org.elasticsearch.plugin.cloud.gce;
import org.elasticsearch.cloud.gce.GceComputeService;
import org.elasticsearch.cloud.gce.GceModule;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.component.LifecycleComponent;
import org.elasticsearch.common.inject.Module;
import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.logging.Loggers;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.discovery.DiscoveryModule;
import org.elasticsearch.discovery.gce.GceDiscovery;
import org.elasticsearch.discovery.gce.GceUnicastHostsProvider;
import org.elasticsearch.plugins.Plugin;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
*
*/
public class CloudGcePlugin extends Plugin {
private final Settings settings;
protected final ESLogger logger = Loggers.getLogger(CloudGcePlugin.class);
public CloudGcePlugin(Settings settings) {
this.settings = settings;
}
@Override
public String name() {
return "cloud-gce";
}
@Override
public String description() {
return "Cloud Google Compute Engine Plugin";
}
@Override
public Collection<Module> nodeModules() {
List<Module> modules = new ArrayList<>();
if (isDiscoveryAlive(settings, logger)) {
modules.add(new GceModule());
}
return modules;
}
@Override
public Collection<Class<? extends LifecycleComponent>> nodeServices() {
Collection<Class<? extends LifecycleComponent>> services = new ArrayList<>();
if (isDiscoveryAlive(settings, logger)) {
services.add(GceModule.getComputeServiceImpl());
}
return services;
}
public void onModule(DiscoveryModule discoveryModule) {
if (isDiscoveryAlive(settings, logger)) {
discoveryModule.addDiscoveryType("gce", GceDiscovery.class);
discoveryModule.addUnicastHostProvider(GceUnicastHostsProvider.class);
}
}
/**
* Check if discovery is meant to start
*
* @return true if we can start gce discovery features
*/
public static boolean isDiscoveryAlive(Settings settings, ESLogger logger) {
// User set discovery.type: gce
if (GceDiscovery.GCE.equalsIgnoreCase(settings.get("discovery.type")) == false) {
logger.debug("discovery.type not set to {}", GceDiscovery.GCE);
return false;
}
if (checkProperty(GceComputeService.Fields.PROJECT, settings.get(GceComputeService.Fields.PROJECT), logger) == false ||
checkProperty(GceComputeService.Fields.ZONE, settings.getAsArray(GceComputeService.Fields.ZONE), logger) == false) {
logger.debug("one or more gce discovery settings are missing. " +
"Check elasticsearch.yml file. Should have [{}] and [{}].",
GceComputeService.Fields.PROJECT,
GceComputeService.Fields.ZONE);
return false;
}
logger.trace("all required properties for gce discovery are set!");
return true;
}
private static boolean checkProperty(String name, String value, ESLogger logger) {
if (!Strings.hasText(value)) {
logger.warn("{} is not set.", name);
return false;
}
return true;
}
private static boolean checkProperty(String name, String[] values, ESLogger logger) {
if (values == null || values.length == 0) {
logger.warn("{} is not set.", name);
return false;
}
return true;
}
}