private ProcessBuilder create(AbstractCommand<?> javaCommand, List<String> commands) { ProcessBuilder processBuilder = processBuilderSupplier.get(); processBuilder.command(commands); processBuilder.directory(javaCommand.getWorkDir()); Map<String, String> environment = processBuilder.environment(); environment.putAll(javaCommand.getEnvVariables()); javaCommand.getSuppressedEnvVariables().forEach(environment::remove); processBuilder.redirectErrorStream(true); return processBuilder; }
private static <T extends AbstractCommand> void logLaunchedCommand(AbstractCommand<T> command, ProcessBuilder processBuilder) { if (LOG.isInfoEnabled()) { LOG.info("Launch process[{}] from [{}]: {}", command.getProcessId(), command.getWorkDir().getAbsolutePath(), String.join(" ", processBuilder.command())); } }
public T setEsInstallation(EsInstallation esInstallation) { this.esInstallation = esInstallation; return castThis(); }
@Test public void createEsCommand_for_unix_returns_command_for_default_settings() throws Exception { System2 system2 = Mockito.mock(System2.class); when(system2.isOsWindows()).thenReturn(false); prepareEsFileSystem(); Properties props = new Properties(); props.setProperty("sonar.search.host", "localhost"); AbstractCommand esCommand = newFactory(props, system2).createEsCommand(); EsInstallation esConfig = esCommand.getEsInstallation(); assertThat(esCommand).isInstanceOf(EsScriptCommand.class); assertThat(esConfig.getClusterName()).isEqualTo("sonarqube"); assertThat(esConfig.getHost()).isNotEmpty(); assertThat(esConfig.getPort()).isEqualTo(9001); assertThat(esConfig.getEsJvmOptions().getAll()) // enforced values .contains("-XX:+UseConcMarkSweepGC", "-server", "-Dfile.encoding=UTF-8") // default settings .contains("-Xms512m", "-Xmx512m", "-XX:+HeapDumpOnOutOfMemoryError"); File esConfDir = new File(tempDir, "conf/es"); assertThat(esCommand.getEnvVariables()) .contains(entry("ES_JVM_OPTIONS", new File(esConfDir, "jvm.options").getAbsolutePath())) .containsKey("JAVA_HOME"); assertThat(esConfig.getEsYmlSettings()).isNotNull(); assertThat(esConfig.getLog4j2Properties()) .contains(entry("appender.file_es.fileName", new File(logsDir, "es.log").getAbsolutePath())); assertThat(esCommand.getSuppressedEnvVariables()).containsOnly("JAVA_TOOL_OPTIONS", "ES_JAVA_OPTS"); }
@Test public void suppressEnvVariable_remove_existing_env_variable_and_add_variable_to_set_of_suppressed_variables() throws IOException { File workDir = temp.newFolder(); System2 system2 = Mockito.mock(System2.class); Map<String, String> env = new HashMap<>(); String key1 = randomAlphanumeric(3); env.put(key1, randomAlphanumeric(9)); when(system2.getenv()).thenReturn(env); AbstractCommand underTest = new AbstractCommand(ProcessId.ELASTICSEARCH, workDir, system2) { }; underTest.suppressEnvVariable(key1); assertThat(underTest.getEnvVariables()).doesNotContainKey(key1); assertThat(underTest.getSuppressedEnvVariables()).containsOnly(key1); }
public ProcessMonitor launch(AbstractCommand command) { EsInstallation fileSystem = command.getEsInstallation(); if (fileSystem != null) { cleanupOutdatedEsData(fileSystem); ProcessId processId = command.getProcessId(); try { if (processId == ProcessId.ELASTICSEARCH) { EsInstallation esInstallation = command.getEsInstallation(); checkArgument(esInstallation != null, "Incorrect configuration EsInstallation is null"); EsConnectorImpl esConnector = new EsConnectorImpl(esInstallation.getClusterName(), singleton(HostAndPort.fromParts(esInstallation.getHost(), esInstallation.getPort())));
private ProcessMonitor launchImpl(AbstractCommand<?> javaCommand) { commands.add(javaCommand); if (makeStartupFail == javaCommand.getProcessId()) { throw new IllegalStateException("cannot start " + javaCommand.getProcessId()); } TestProcess process = new TestProcess(javaCommand.getProcessId()); processes.put(javaCommand.getProcessId(), process); return process; }
@Test public void constructor_puts_System_getEnv_into_map_of_env_variables() throws IOException { File workDir = temp.newFolder(); System2 system2 = Mockito.mock(System2.class); Map<String, String> env = IntStream.range(0, 1 + new Random().nextInt(99)).mapToObj(String::valueOf).collect(Collectors.toMap(i -> "key" + i, j -> "value" + j)); when(system2.getenv()).thenReturn(env); AbstractCommand underTest = new AbstractCommand(ProcessId.ELASTICSEARCH, workDir, system2) { }; assertThat(underTest.getEnvVariables()).isEqualTo(env); }
@Test public void createEsCommand_returns_command_for_overridden_settings() throws Exception { prepareEsFileSystem(); Properties props = new Properties(); props.setProperty("sonar.search.host", "localhost"); props.setProperty("sonar.cluster.name", "foo"); props.setProperty("sonar.search.port", "1234"); props.setProperty("sonar.search.javaOpts", "-Xms10G -Xmx10G"); AbstractCommand esCommand = newFactory(props).createEsCommand(); EsInstallation esConfig = esCommand.getEsInstallation(); assertThat(esConfig.getClusterName()).isEqualTo("foo"); assertThat(esConfig.getPort()).isEqualTo(1234); assertThat(esConfig.getEsJvmOptions().getAll()) // enforced values .contains("-XX:+UseConcMarkSweepGC", "-server", "-Dfile.encoding=UTF-8") // user settings .contains("-Xms10G", "-Xmx10G") // default values disabled .doesNotContain("-XX:+HeapDumpOnOutOfMemoryError"); }
@Test public void setEnvVariable_fails_with_NPE_if_key_is_null() throws IOException { File workDir = temp.newFolder(); AbstractCommand underTest = new AbstractCommand(ProcessId.ELASTICSEARCH, workDir, System2.INSTANCE) { }; expectedException.expect(NullPointerException.class); expectedException.expectMessage("key can't be null"); underTest.setEnvVariable(null, randomAlphanumeric(30)); }
@Test public void createEsCommand_for_windows_returns_command_for_default_settings() throws Exception { System2 system2 = Mockito.mock(System2.class); when(system2.isOsWindows()).thenReturn(true); prepareEsFileSystem(); Properties props = new Properties(); props.setProperty("sonar.search.host", "localhost"); AbstractCommand esCommand = newFactory(props, system2).createEsCommand(); EsInstallation esConfig = esCommand.getEsInstallation(); assertThat(esCommand).isInstanceOf(JavaCommand.class); assertThat(esConfig.getClusterName()).isEqualTo("sonarqube"); assertThat(esConfig.getHost()).isNotEmpty(); assertThat(esConfig.getPort()).isEqualTo(9001); assertThat(esConfig.getEsJvmOptions().getAll()) // enforced values .contains("-XX:+UseConcMarkSweepGC", "-server", "-Dfile.encoding=UTF-8") // default settings .contains("-Xms512m", "-Xmx512m", "-XX:+HeapDumpOnOutOfMemoryError"); File esConfDir = new File(tempDir, "conf/es"); assertThat(esCommand.getEnvVariables()) .contains(entry("ES_JVM_OPTIONS", new File(esConfDir, "jvm.options").getAbsolutePath())) .containsKey("JAVA_HOME"); assertThat(esConfig.getEsYmlSettings()).isNotNull(); assertThat(esConfig.getLog4j2Properties()) .contains(entry("appender.file_es.fileName", new File(logsDir, "es.log").getAbsolutePath())); assertThat(esCommand.getSuppressedEnvVariables()).containsOnly("JAVA_TOOL_OPTIONS", "ES_JAVA_OPTS"); }
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); } }
@Test public void setEnvVariable_fails_with_NPE_if_value_is_null() throws IOException { File workDir = temp.newFolder(); AbstractCommand underTest = new AbstractCommand(ProcessId.ELASTICSEARCH, workDir, System2.INSTANCE) { }; expectedException.expect(NullPointerException.class); expectedException.expectMessage("value can't be null"); underTest.setEnvVariable(randomAlphanumeric(30), null); }
private ProcessBuilder create(AbstractCommand<?> javaCommand, List<String> commands) { ProcessBuilder processBuilder = processBuilderSupplier.get(); processBuilder.command(commands); processBuilder.directory(javaCommand.getWorkDir()); Map<String, String> environment = processBuilder.environment(); environment.putAll(javaCommand.getEnvVariables()); javaCommand.getSuppressedEnvVariables().forEach(environment::remove); processBuilder.redirectErrorStream(true); return processBuilder; }
private static <T extends AbstractCommand> void logLaunchedCommand(AbstractCommand<T> command, ProcessBuilder processBuilder) { if (LOG.isInfoEnabled()) { LOG.info("Launch process[{}] from [{}]: {}", command.getProcessId(), command.getWorkDir().getAbsolutePath(), String.join(" ", processBuilder.command())); } }
public T setEnvVariable(String key, String value) { envVariables.put( requireNonNull(key, "key can't be null"), requireNonNull(value, "value can't be null")); return castThis(); }
public T suppressEnvVariable(String key) { requireNonNull(key, "key can't be null"); suppressedEnvVariables.add(key); envVariables.remove(key); return castThis(); }
public T setEsInstallation(EsInstallation esInstallation) { this.esInstallation = esInstallation; return castThis(); }
public T suppressEnvVariable(String key) { requireNonNull(key, "key can't be null"); suppressedEnvVariables.add(key); envVariables.remove(key); return castThis(); }
public T setEnvVariable(String key, String value) { envVariables.put( requireNonNull(key, "key can't be null"), requireNonNull(value, "value can't be null")); return castThis(); }