10
10
package org .elasticsearch .server .cli ;
11
11
12
12
import org .elasticsearch .bootstrap .ServerArgs ;
13
+ import org .elasticsearch .cli .ExitCodes ;
13
14
import org .elasticsearch .cli .ProcessInfo ;
14
15
import org .elasticsearch .cli .Terminal ;
15
16
import org .elasticsearch .cli .UserException ;
18
19
import org .elasticsearch .core .PathUtils ;
19
20
import org .elasticsearch .core .SuppressForbidden ;
20
21
22
+ import java .io .FileNotFoundException ;
21
23
import java .io .IOException ;
22
24
import java .io .OutputStream ;
23
25
import java .io .UncheckedIOException ;
26
+ import java .nio .file .Files ;
24
27
import java .nio .file .Path ;
25
28
import java .util .HashMap ;
26
29
import java .util .List ;
@@ -44,7 +47,6 @@ public class ServerProcessBuilder {
44
47
private ServerArgs serverArgs ;
45
48
private ProcessInfo processInfo ;
46
49
private List <String > jvmOptions ;
47
- private Path workingDir ;
48
50
private Terminal terminal ;
49
51
50
52
// this allows mocking the process building by tests
@@ -84,11 +86,6 @@ public ServerProcessBuilder withJvmOptions(List<String> jvmOptions) {
84
86
return this ;
85
87
}
86
88
87
- public ServerProcessBuilder withWorkingDir (Path workingDir ) {
88
- this .workingDir = workingDir ;
89
- return this ;
90
- }
91
-
92
89
/**
93
90
* Specifies the {@link Terminal} to use for reading input and writing output from/to the cli console
94
91
*/
@@ -138,9 +135,21 @@ private String getCommand() {
138
135
* @throws UserException If the process failed during bootstrap
139
136
*/
140
137
public ServerProcess start () throws UserException {
138
+ ensureWorkingDirExists ();
141
139
return start (ProcessBuilder ::start );
142
140
}
143
141
142
+ private void ensureWorkingDirExists () throws UserException {
143
+ Path workingDir = serverArgs .logsDir ();
144
+ try {
145
+ Files .createDirectories (workingDir );
146
+ } catch (FileNotFoundException e ) {
147
+ throw new UserException (ExitCodes .CONFIG , "Logs dir [" + workingDir + "] exists but is not a directory" , e );
148
+ } catch (IOException e ) {
149
+ throw new UserException (ExitCodes .CONFIG , "Unable to create logs dir [" + workingDir + "]" , e );
150
+ }
151
+ }
152
+
144
153
private static void checkRequiredArgument (Object argument , String argumentName ) {
145
154
if (argument == null ) {
146
155
throw new IllegalStateException (
@@ -162,7 +171,7 @@ ServerProcess start(ProcessStarter processStarter) throws UserException {
162
171
163
172
boolean success = false ;
164
173
try {
165
- jvmProcess = createProcess (getCommand (), getJvmArgs (), jvmOptions , getEnvironment (), workingDir , processStarter );
174
+ jvmProcess = createProcess (getCommand (), getJvmArgs (), jvmOptions , getEnvironment (), serverArgs . logsDir () , processStarter );
166
175
errorPump = new ErrorPumpThread (terminal , jvmProcess .getErrorStream ());
167
176
errorPump .start ();
168
177
sendArgs (serverArgs , jvmProcess .getOutputStream ());
0 commit comments