private <T extends JvmOptions> ProcessBuilder create(JavaCommand<T> javaCommand) { List<String> commands = new ArrayList<>(); commands.add(buildJavaPath()); commands.addAll(javaCommand.getJvmOptions().getAll()); commands.addAll(buildClasspath(javaCommand)); commands.add(javaCommand.getClassName()); if (javaCommand.getReadsArgumentsFromFile()) { commands.add(buildPropertiesFile(javaCommand).getAbsolutePath()); } else { javaCommand.getArguments().forEach((key, value) -> { if (value != null && !value.isEmpty()) { commands.add("-E" + key + "=" + value); } }); } return create(javaCommand, commands); }
public ProcessMonitor launch(AbstractCommand command) { EsInstallation fileSystem = command.getEsInstallation(); if (fileSystem != null) { cleanupOutdatedEsData(fileSystem); writeConfFiles(fileSystem); process = launchExternal((EsScriptCommand) command); } else if (command instanceof JavaCommand) { process = launchJava((JavaCommand) command); } else { throw new IllegalStateException("Unexpected type of command: " + command.getClass());
private <T extends JvmOptions> Process launchJava(JavaCommand<T> javaCommand) { ProcessId processId = javaCommand.getProcessId(); try { ProcessBuilder processBuilder = create(javaCommand); logLaunchedCommand(javaCommand, processBuilder); return processBuilder.start(); } catch (Exception e) { throw new IllegalStateException(format("Fail to launch process [%s]", processId.getKey()), e); } }
@Test public void clean_up_old_es_data() throws Exception { File tempDir = temp.newFolder(); File homeDir = temp.newFolder(); File dataDir = temp.newFolder(); File logDir = temp.newFolder(); ProcessLauncher underTest = new ProcessLauncherImpl(tempDir, commands, () -> new TestProcessBuilder()); EsScriptCommand command = createEsScriptCommand(tempDir, homeDir, dataDir, logDir); File outdatedEsDir = new File(dataDir, "es"); assertThat(outdatedEsDir.mkdir()).isTrue(); assertThat(outdatedEsDir.exists()).isTrue(); underTest.launch(command); assertThat(outdatedEsDir.exists()).isFalse(); }
private ProcessBuilder create(EsScriptCommand esScriptCommand) { List<String> commands = new ArrayList<>(); EsInstallation esInstallation = esScriptCommand.getEsInstallation(); requireNonNull(esInstallation, () -> "No Elasticsearch installation configuration is available for the command of type " + esScriptCommand.getClass()); commands.add(esInstallation.getExecutable().getAbsolutePath()); commands.addAll(esScriptCommand.getOptions()); return create(esScriptCommand, commands); }
@Test public void do_not_fail_if_outdated_es_directory_does_not_exist() throws Exception { File tempDir = temp.newFolder(); File homeDir = temp.newFolder(); File dataDir = temp.newFolder(); File logDir = temp.newFolder(); ProcessLauncher underTest = new ProcessLauncherImpl(tempDir, commands, () -> new TestProcessBuilder()); EsScriptCommand command = createEsScriptCommand(tempDir, homeDir, dataDir, logDir); File outdatedEsDir = new File(dataDir, "es"); assertThat(outdatedEsDir.exists()).isFalse(); underTest.launch(command); assertThat(outdatedEsDir.exists()).isFalse(); }
private ProcessBuilder create(EsScriptCommand esScriptCommand) { List<String> commands = new ArrayList<>(); EsInstallation esInstallation = esScriptCommand.getEsInstallation(); requireNonNull(esInstallation, () -> "No Elasticsearch installation configuration is available for the command of type " + esScriptCommand.getClass()); commands.add(esInstallation.getExecutable().getAbsolutePath()); commands.addAll(esScriptCommand.getOptions()); return create(esScriptCommand, commands); }
private <T extends JvmOptions> ProcessBuilder create(JavaCommand<T> javaCommand) { List<String> commands = new ArrayList<>(); commands.add(buildJavaPath()); commands.addAll(javaCommand.getJvmOptions().getAll()); commands.addAll(buildClasspath(javaCommand)); commands.add(javaCommand.getClassName()); if (javaCommand.getReadsArgumentsFromFile()) { commands.add(buildPropertiesFile(javaCommand).getAbsolutePath()); } else { javaCommand.getArguments().forEach((key, value) -> { if (value != null && !value.isEmpty()) { commands.add("-E" + key + "=" + value); } }); } return create(javaCommand, commands); }
public ProcessMonitor launch(AbstractCommand command) { EsInstallation fileSystem = command.getEsInstallation(); if (fileSystem != null) { cleanupOutdatedEsData(fileSystem); writeConfFiles(fileSystem); } Process process; if (command instanceof EsScriptCommand) { process = launchExternal((EsScriptCommand) command); } else if (command instanceof JavaCommand) { process = launchJava((JavaCommand) command); } else { throw new IllegalStateException("Unexpected type of command: " + command.getClass()); } ProcessId processId = command.getProcessId(); try { if (processId == ProcessId.ELASTICSEARCH) { return new EsProcessMonitor(process, processId, command.getEsInstallation(), new EsConnectorImpl()); } else { ProcessCommands commands = allProcessesCommands.createAfterClean(processId.getIpcIndex()); return new ProcessCommandsProcessMonitor(process, processId, commands); } } catch (Exception e) { // just in case if (process != null) { process.destroyForcibly(); } throw new IllegalStateException(format("Fail to launch monitor of process [%s]", processId.getKey()), e); } }
private Process launchExternal(EsScriptCommand esScriptCommand) { try { ProcessBuilder processBuilder = create(esScriptCommand); logLaunchedCommand(esScriptCommand, processBuilder); return processBuilder.start(); } catch (Exception e) { throw new IllegalStateException(format("Fail to launch process [%s]", esScriptCommand.getProcessId().getKey()), e); } }
@Test public void throw_ISE_if_command_fails() throws IOException { File tempDir = temp.newFolder(); ProcessLauncherImpl.ProcessBuilder processBuilder = mock(ProcessLauncherImpl.ProcessBuilder.class, RETURNS_MOCKS); when(processBuilder.start()).thenThrow(new IOException("error")); ProcessLauncher underTest = new ProcessLauncherImpl(tempDir, commands, () -> processBuilder); expectedException.expect(IllegalStateException.class); expectedException.expectMessage("Fail to launch process [es]"); underTest.launch(new JavaCommand(ProcessId.ELASTICSEARCH, temp.newFolder())); }
private Process launchExternal(EsScriptCommand esScriptCommand) { try { ProcessBuilder processBuilder = create(esScriptCommand); logLaunchedCommand(esScriptCommand, processBuilder); return processBuilder.start(); } catch (Exception e) { throw new IllegalStateException(format("Fail to launch process [%s]", esScriptCommand.getProcessId().getKey()), e); } }
@Test public void temporary_properties_file_can_be_avoided() throws Exception { File tempDir = temp.newFolder(); TestProcessBuilder processBuilder = new TestProcessBuilder(); ProcessLauncher underTest = new ProcessLauncherImpl(tempDir, commands, () -> processBuilder); JavaCommand<JvmOptions> command = new JavaCommand<>(ProcessId.WEB_SERVER, temp.newFolder()); command.setReadsArgumentsFromFile(false); command.setArgument("foo", "bar"); command.setArgument("baz", "woo"); command.setJvmOptions(new JvmOptions<>()); underTest.launch(command); String propsFilePath = processBuilder.commands.get(processBuilder.commands.size() - 1); File file = new File(propsFilePath); assertThat(file).doesNotExist(); }
private <T extends JvmOptions> Process launchJava(JavaCommand<T> javaCommand) { ProcessId processId = javaCommand.getProcessId(); try { ProcessBuilder processBuilder = create(javaCommand); logLaunchedCommand(javaCommand, processBuilder); return processBuilder.start(); } catch (Exception e) { throw new IllegalStateException(format("Fail to launch process [%s]", processId.getKey()), e); } }
File tempDir = temp.newFolder(); TestProcessBuilder processBuilder = new TestProcessBuilder(); ProcessLauncher underTest = new ProcessLauncherImpl(tempDir, commands, () -> processBuilder); JavaCommand<JvmOptions> command = new JavaCommand<>(ProcessId.ELASTICSEARCH, temp.newFolder()); command.addClasspath("lib/*.class");
@Test public void properties_are_passed_to_command_via_a_temporary_properties_file() throws Exception { File tempDir = temp.newFolder(); TestProcessBuilder processBuilder = new TestProcessBuilder(); ProcessLauncher underTest = new ProcessLauncherImpl(tempDir, commands, () -> processBuilder); JavaCommand<JvmOptions> command = new JavaCommand<>(ProcessId.WEB_SERVER, temp.newFolder()); command.setReadsArgumentsFromFile(true); command.setArgument("foo", "bar"); command.setArgument("baz", "woo"); command.setJvmOptions(new JvmOptions<>()); underTest.launch(command); String propsFilePath = processBuilder.commands.get(processBuilder.commands.size() - 1); File file = new File(propsFilePath); assertThat(file).exists().isFile(); try (FileReader reader = new FileReader(file)) { Properties props = new Properties(); props.load(reader); assertThat(props).containsOnly( entry("foo", "bar"), entry("baz", "woo"), entry("process.terminationTimeout", "60000"), entry("process.key", ProcessId.WEB_SERVER.getKey()), entry("process.index", String.valueOf(ProcessId.WEB_SERVER.getIpcIndex())), entry("process.sharedDir", tempDir.getAbsolutePath())); } }
public void start(String[] cliArguments) throws IOException { AppSettingsLoader settingsLoader = new AppSettingsLoaderImpl(cliArguments); AppSettings settings = settingsLoader.load(); // order is important - logging must be configured before any other components (AppFileSystem, ...) AppLogging logging = new AppLogging(settings); logging.configure(); AppFileSystem fileSystem = new AppFileSystem(settings); try (AppState appState = new AppStateFactory(settings).create()) { appState.registerSonarQubeVersion(getSonarqubeVersion()); appState.registerClusterName(settings.getProps().nonNullValue(CLUSTER_NAME.getKey())); AppReloader appReloader = new AppReloaderImpl(settingsLoader, fileSystem, appState, logging); fileSystem.reset(); CommandFactory commandFactory = new CommandFactoryImpl(settings.getProps(), fileSystem.getTempDir(), System2.INSTANCE); try (ProcessLauncher processLauncher = new ProcessLauncherImpl(fileSystem.getTempDir())) { Scheduler scheduler = new SchedulerImpl(settings, appReloader, commandFactory, processLauncher, appState); // intercepts CTRL-C Runtime.getRuntime().addShutdownHook(new ShutdownHook(scheduler)); scheduler.schedule(); stopRequestWatcher = StopRequestWatcherImpl.create(settings, scheduler, fileSystem); stopRequestWatcher.startWatching(); scheduler.awaitTermination(); stopRequestWatcher.stopWatching(); } } systemExit.exit(0); }