Skip to content

Commit b196782

Browse files
committed
Ensure logs dir exists before using as working dir
With the change to using the logs dir as the working dir of the Elasticsearch process we need to ensure the logs dir exists within the CLI instead of later during startup. relates elastic#124966
1 parent 36280d2 commit b196782

File tree

3 files changed

+20
-16
lines changed

3 files changed

+20
-16
lines changed

Diff for: distribution/tools/server-cli/src/main/java/org/elasticsearch/server/cli/ServerCli.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,7 @@ protected ServerProcess startServer(Terminal terminal, ProcessInfo processInfo,
271271
.withProcessInfo(processInfo)
272272
.withServerArgs(args)
273273
.withTempDir(tempDir)
274-
.withJvmOptions(jvmOptions)
275-
.withWorkingDir(args.logsDir());
274+
.withJvmOptions(jvmOptions);
276275
return serverProcessBuilder.start();
277276
}
278277

Diff for: distribution/tools/server-cli/src/main/java/org/elasticsearch/server/cli/ServerProcessBuilder.java

+16-7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
package org.elasticsearch.server.cli;
1111

1212
import org.elasticsearch.bootstrap.ServerArgs;
13+
import org.elasticsearch.cli.ExitCodes;
1314
import org.elasticsearch.cli.ProcessInfo;
1415
import org.elasticsearch.cli.Terminal;
1516
import org.elasticsearch.cli.UserException;
@@ -18,9 +19,11 @@
1819
import org.elasticsearch.core.PathUtils;
1920
import org.elasticsearch.core.SuppressForbidden;
2021

22+
import java.io.FileNotFoundException;
2123
import java.io.IOException;
2224
import java.io.OutputStream;
2325
import java.io.UncheckedIOException;
26+
import java.nio.file.Files;
2427
import java.nio.file.Path;
2528
import java.util.HashMap;
2629
import java.util.List;
@@ -44,7 +47,6 @@ public class ServerProcessBuilder {
4447
private ServerArgs serverArgs;
4548
private ProcessInfo processInfo;
4649
private List<String> jvmOptions;
47-
private Path workingDir;
4850
private Terminal terminal;
4951

5052
// this allows mocking the process building by tests
@@ -84,11 +86,6 @@ public ServerProcessBuilder withJvmOptions(List<String> jvmOptions) {
8486
return this;
8587
}
8688

87-
public ServerProcessBuilder withWorkingDir(Path workingDir) {
88-
this.workingDir = workingDir;
89-
return this;
90-
}
91-
9289
/**
9390
* Specifies the {@link Terminal} to use for reading input and writing output from/to the cli console
9491
*/
@@ -138,9 +135,21 @@ private String getCommand() {
138135
* @throws UserException If the process failed during bootstrap
139136
*/
140137
public ServerProcess start() throws UserException {
138+
ensureWorkingDirExists();
141139
return start(ProcessBuilder::start);
142140
}
143141

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+
144153
private static void checkRequiredArgument(Object argument, String argumentName) {
145154
if (argument == null) {
146155
throw new IllegalStateException(
@@ -162,7 +171,7 @@ ServerProcess start(ProcessStarter processStarter) throws UserException {
162171

163172
boolean success = false;
164173
try {
165-
jvmProcess = createProcess(getCommand(), getJvmArgs(), jvmOptions, getEnvironment(), workingDir, processStarter);
174+
jvmProcess = createProcess(getCommand(), getJvmArgs(), jvmOptions, getEnvironment(), serverArgs.logsDir(), processStarter);
166175
errorPump = new ErrorPumpThread(terminal, jvmProcess.getErrorStream());
167176
errorPump.start();
168177
sendArgs(serverArgs, jvmProcess.getOutputStream());

Diff for: distribution/tools/server-cli/src/test/java/org/elasticsearch/server/cli/ServerProcessTests.java

+3-7
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ public class ServerProcessTests extends ESTestCase {
6565
protected final Map<String, String> sysprops = new HashMap<>();
6666
protected final Map<String, String> envVars = new HashMap<>();
6767
Path esHomeDir;
68-
Path workingDir;
6968
Settings.Builder nodeSettings;
7069
ProcessValidator processValidator;
7170
MainMethod mainCallback;
@@ -95,7 +94,6 @@ public void resetEnv() {
9594
sysprops.put("java.home", "javahome");
9695
sysprops.put("es.path.home", esHomeDir.toString());
9796
envVars.clear();
98-
workingDir = createTempDir();
9997
nodeSettings = Settings.builder();
10098
processValidator = null;
10199
mainCallback = null;
@@ -231,8 +229,7 @@ ServerProcess startProcess(boolean daemonize, boolean quiet) throws Exception {
231229
.withProcessInfo(pinfo)
232230
.withServerArgs(createServerArgs(daemonize, quiet))
233231
.withJvmOptions(List.of())
234-
.withTempDir(ServerProcessUtils.setupTempDir(pinfo))
235-
.withWorkingDir(workingDir);
232+
.withTempDir(ServerProcessUtils.setupTempDir(pinfo));
236233
return serverProcessBuilder.start(starter);
237234
}
238235

@@ -241,7 +238,7 @@ public void testProcessBuilder() throws Exception {
241238
assertThat(pb.redirectInput(), equalTo(ProcessBuilder.Redirect.PIPE));
242239
assertThat(pb.redirectOutput(), equalTo(ProcessBuilder.Redirect.INHERIT));
243240
assertThat(pb.redirectError(), equalTo(ProcessBuilder.Redirect.PIPE));
244-
assertThat(String.valueOf(pb.directory()), equalTo(workingDir.toString())); // leave default, which is working directory
241+
assertThat(String.valueOf(pb.directory()), equalTo(esHomeDir.resolve("logs").toString()));
245242
};
246243
mainCallback = (args, stdin, stderr, exitCode) -> {
247244
try (PrintStream err = new PrintStream(stderr, true, StandardCharsets.UTF_8)) {
@@ -315,8 +312,7 @@ public void testCommandLineSysprops() throws Exception {
315312
.withProcessInfo(createProcessInfo())
316313
.withServerArgs(createServerArgs(false, false))
317314
.withJvmOptions(List.of("-Dfoo1=bar", "-Dfoo2=baz"))
318-
.withTempDir(Path.of("."))
319-
.withWorkingDir(workingDir);
315+
.withTempDir(Path.of("."));
320316
serverProcessBuilder.start(starter).waitFor();
321317
}
322318

0 commit comments

Comments
 (0)