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
|
/*
* SonarQube
* Copyright (C) 2009-2017 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.application;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import org.apache.commons.io.FilenameUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.process.Lifecycle;
import org.sonar.process.ProcessId;
import org.sonar.process.ProcessProperties;
import org.sonar.process.Props;
import org.sonar.process.Stoppable;
import org.sonar.process.monitor.JavaCommand;
import org.sonar.process.monitor.Monitor;
import static org.sonar.process.Lifecycle.State;
import static org.sonar.process.ProcessId.APP;
import static org.sonar.process.ProcessProperties.HTTPS_PROXY_HOST;
import static org.sonar.process.ProcessProperties.HTTPS_PROXY_PORT;
import static org.sonar.process.ProcessProperties.HTTP_PROXY_HOST;
import static org.sonar.process.ProcessProperties.HTTP_PROXY_PORT;
/**
* Entry-point of process that starts and monitors ElasticSearch, the Web Server and the Compute Engine.
*/
public class App implements Stoppable {
/**
* Properties about proxy that must be set as system properties
*/
private static final String[] PROXY_PROPERTY_KEYS = new String[] {
HTTP_PROXY_HOST,
HTTP_PROXY_PORT,
"http.nonProxyHosts",
HTTPS_PROXY_HOST,
HTTPS_PROXY_PORT,
"http.auth.ntlm.domain",
"socksProxyHost",
"socksProxyPort"};
private final Monitor monitor;
public App(AppFileSystem appFileSystem, boolean watchForHardStop) {
this(Monitor.create(APP.getIpcIndex(), appFileSystem, watchForHardStop, new AppLifecycleListener()));
}
App(Monitor monitor) {
this.monitor = monitor;
}
public void start(Props props) throws InterruptedException {
monitor.start(createCommands(props));
monitor.awaitTermination();
}
private static List<JavaCommand> createCommands(Props props) {
File homeDir = props.nonNullValueAsFile(ProcessProperties.PATH_HOME);
List<JavaCommand> commands = new ArrayList<>(3);
if (isProcessEnabled(props, ProcessProperties.CLUSTER_SEARCH_DISABLED)) {
commands.add(createESCommand(props, homeDir));
}
if (isProcessEnabled(props, ProcessProperties.CLUSTER_WEB_DISABLED)) {
commands.add(createWebServerCommand(props, homeDir));
}
if (isProcessEnabled(props, ProcessProperties.CLUSTER_CE_DISABLED)) {
commands.add(createCeServerCommand(props, homeDir));
}
return commands;
}
private static boolean isProcessEnabled(Props props, String disabledPropertyKey) {
return !props.valueAsBoolean(ProcessProperties.CLUSTER_ENABLED) ||
!props.valueAsBoolean(disabledPropertyKey);
}
private static JavaCommand createESCommand(Props props, File homeDir) {
return newJavaCommand(ProcessId.ELASTICSEARCH, props, homeDir)
.addJavaOptions("-Djava.awt.headless=true")
.addJavaOptions(props.nonNullValue(ProcessProperties.SEARCH_JAVA_OPTS))
.addJavaOptions(props.nonNullValue(ProcessProperties.SEARCH_JAVA_ADDITIONAL_OPTS))
.setClassName("org.sonar.search.SearchServer")
.addClasspath("./lib/common/*")
.addClasspath("./lib/search/*");
}
private static JavaCommand createWebServerCommand(Props props, File homeDir) {
JavaCommand command = newJavaCommand(ProcessId.WEB_SERVER, props, homeDir)
.addJavaOptions(ProcessProperties.WEB_ENFORCED_JVM_ARGS)
.addJavaOptions(props.nonNullValue(ProcessProperties.WEB_JAVA_OPTS))
.addJavaOptions(props.nonNullValue(ProcessProperties.WEB_JAVA_ADDITIONAL_OPTS))
// required for logback tomcat valve
.setEnvVariable(ProcessProperties.PATH_LOGS, props.nonNullValue(ProcessProperties.PATH_LOGS))
.setClassName("org.sonar.server.app.WebServer")
.addClasspath("./lib/common/*")
.addClasspath("./lib/server/*");
String driverPath = props.value(ProcessProperties.JDBC_DRIVER_PATH);
if (driverPath != null) {
command.addClasspath(driverPath);
}
return command;
}
private static JavaCommand createCeServerCommand(Props props, File homeDir) {
JavaCommand command = newJavaCommand(ProcessId.COMPUTE_ENGINE, props, homeDir)
.addJavaOptions(ProcessProperties.CE_ENFORCED_JVM_ARGS)
.addJavaOptions(props.nonNullValue(ProcessProperties.CE_JAVA_OPTS))
.addJavaOptions(props.nonNullValue(ProcessProperties.CE_JAVA_ADDITIONAL_OPTS))
.setClassName("org.sonar.ce.app.CeServer")
.addClasspath("./lib/common/*")
.addClasspath("./lib/server/*")
.addClasspath("./lib/ce/*");
String driverPath = props.value(ProcessProperties.JDBC_DRIVER_PATH);
if (driverPath != null) {
command.addClasspath(driverPath);
}
return command;
}
private static JavaCommand newJavaCommand(ProcessId id, Props props, File homeDir) {
JavaCommand command = new JavaCommand(id)
.setWorkDir(homeDir)
.setArguments(props.rawProperties());
for (String key : PROXY_PROPERTY_KEYS) {
if (props.contains(key)) {
command.addJavaOption("-D" + key + "=" + props.value(key));
}
}
// defaults of HTTPS are the same than HTTP defaults
setSystemPropertyToDefaultIfNotSet(command, props, HTTPS_PROXY_HOST, HTTP_PROXY_HOST);
setSystemPropertyToDefaultIfNotSet(command, props, HTTPS_PROXY_PORT, HTTP_PROXY_PORT);
return command;
}
private static void setSystemPropertyToDefaultIfNotSet(JavaCommand command, Props props, String httpsProperty, String httpProperty) {
if (!props.contains(httpsProperty) && props.contains(httpProperty)) {
command.addJavaOption("-D" + httpsProperty + "=" + props.value(httpProperty));
}
}
static String starPath(File homeDir, String relativePath) {
File dir = new File(homeDir, relativePath);
return FilenameUtils.concat(dir.getAbsolutePath(), "*");
}
public static void main(String[] args) throws InterruptedException {
CommandLineParser cli = new CommandLineParser();
Properties rawProperties = cli.parseArguments(args);
Props props = new PropsBuilder(rawProperties, new JdbcSettings()).build();
AppFileSystem appFileSystem = new AppFileSystem(props);
appFileSystem.verifyProps();
AppLogging logging = new AppLogging();
logging.configure(props);
// used by orchestrator
boolean watchForHardStop = props.valueAsBoolean(ProcessProperties.ENABLE_STOP_COMMAND, false);
App app = new App(appFileSystem, watchForHardStop);
app.start(props);
}
@Override
public void stopAsync() {
monitor.stop();
}
private static class AppLifecycleListener implements Lifecycle.LifecycleListener {
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
@Override
public void successfulTransition(State from, State to) {
if (to == State.STARTED) {
LOGGER.info("SonarQube is up");
}
}
}
}
|