private File buildPropertiesFile(JavaCommand javaCommand) { File propertiesFile = null; try { propertiesFile = File.createTempFile("sq-process", "properties", tempDir); Properties props = new Properties(); props.putAll(javaCommand.getArguments()); props.setProperty(PROPERTY_PROCESS_KEY, javaCommand.getProcessId().getKey()); props.setProperty(PROPERTY_PROCESS_INDEX, Integer.toString(javaCommand.getProcessId().getIpcIndex())); props.setProperty(PROPERTY_TERMINATION_TIMEOUT_MS, "60000"); props.setProperty(PROPERTY_SHARED_PATH, tempDir.getAbsolutePath()); try (OutputStream out = new FileOutputStream(propertiesFile)) { props.store(out, format("Temporary properties file for command [%s]", javaCommand.getProcessId().getKey())); } return propertiesFile; } catch (Exception e) { throw new IllegalStateException("Cannot write temporary settings to " + propertiesFile, e); } }
@Override public void handle(Request wsRequest, Response wsResponse) throws Exception { userSession.checkIsSystemAdministrator(); String processKey = wsRequest.mandatoryParam(PROCESS_PROPERTY); ProcessId processId = ProcessId.fromKey(processKey); File logsDir = serverLogging.getLogsDir(); File file = new File(logsDir, processId.getLogFilenamePrefix() + ".log"); // filenames are defined in the enum LogProcess. Still to prevent any vulnerability, // path is double-checked to prevent returning any file present on the file system. if (file.exists() && file.getParentFile().equals(logsDir)) { wsResponse.stream().setMediaType(MediaTypes.TXT); FileUtils.copyFile(file, wsResponse.stream().output()); } else { wsResponse.stream().setStatus(HttpURLConnection.HTTP_NOT_FOUND); } } }
@Test public void all_values_are_unique() { Set<Integer> ipcIndices = new HashSet<>(); Set<String> keys = new HashSet<>(); for (ProcessId processId : ProcessId.values()) { ipcIndices.add(processId.getIpcIndex()); keys.add(processId.getKey()); } assertThat(ipcIndices).hasSize(ProcessId.values().length); assertThat(keys).hasSize(ProcessId.values().length); }
StopWatcher() { // this name is different than Thread#toString(), which includes name, priority // and thread group // -> do not override toString() super(format("StopWatcher[%s]", processId.getKey())); }
@Override public boolean isCeOperational() { return call(IS_OPERATIONAL, ProcessId.COMPUTE_ENGINE.getIpcIndex()); }
public static MemberSelector selectorForProcessIds(ProcessId... processIds) { List<ProcessId> processIdList = asList(processIds); return member -> { ProcessId memberProcessId = fromKey(member.getStringAttribute(PROCESS_KEY.getKey())); return processIdList.contains(memberProcessId); }; } }
private String writeFileAppender(RootLoggerConfig config, File logDir, String logPattern) { String appenderName = "file_" + config.getProcessId().getLogFilenamePrefix(); RollingPolicy rollingPolicy = createRollingPolicy(logDir, config.getProcessId().getLogFilenamePrefix()); Appender appender = new Appender(appenderName, rollingPolicy, logPattern); appender.writeAppenderProperties(); return appender.getAppenderRef(); }
@Override public int hashCode() { int result = processId.hashCode(); result = 31 * result + nodeUuid.hashCode(); return result; } }
@Override public boolean isOperational(ProcessId processId, boolean local) { if (local) { return operationalLocalProcesses.computeIfAbsent(processId, p -> false); } if (processId.equals(ProcessId.ELASTICSEARCH)) { return isElasticSearchAvailable(); } for (Map.Entry<ClusterProcess, Boolean> entry : operationalProcesses.entrySet()) { if (entry.getKey().getProcessId().equals(processId) && entry.getValue()) { return true; } } return false; }
EventWatcher() { // this name is different than Thread#toString(), which includes name, priority // and thread group // -> do not override toString() super(format("EventWatcher[%s]", processId.getKey())); }
private boolean isOperational() { try (DefaultProcessCommands processCommands = DefaultProcessCommands.secondary(sharedDir, ProcessId.WEB_SERVER.getIpcIndex())) { return processCommands.isOperational(); } }
@Override public void define(WebService.NewController controller) { WebService.NewAction action = controller.createAction("logs") .setDescription("Get system logs in plain-text format. Requires system administration permission.") .setResponseExample(getClass().getResource("logs-example.log")) .setSince("5.2") .setHandler(this); action .createParam(PROCESS_PROPERTY) .setPossibleValues(stream(ProcessId.values()) .map(ProcessId::getKey) .sorted() .collect(MoreCollectors.toList(ProcessId.values().length))) .setDefaultValue(ProcessId.APP.getKey()) .setSince("6.2") .setDescription("Process to get logs from"); }
@Test public void fromKey_searches_process_by_its_key() { assertThat(ProcessId.fromKey("app")).isEqualTo(ProcessId.APP); assertThat(ProcessId.fromKey("ce")).isEqualTo(ProcessId.COMPUTE_ENGINE); assertThat(ProcessId.fromKey("es")).isEqualTo(ProcessId.ELASTICSEARCH); assertThat(ProcessId.fromKey("web")).isEqualTo(ProcessId.WEB_SERVER); }
public FileAppender<ILoggingEvent> newFileAppender(LoggerContext ctx, Props props, RootLoggerConfig config, String logPattern) { RollingPolicy rollingPolicy = createRollingPolicy(ctx, props, config.getProcessId().getLogFilenamePrefix()); FileAppender<ILoggingEvent> fileAppender = rollingPolicy.createAppender("file_" + config.getProcessId().getLogFilenamePrefix()); fileAppender.setContext(ctx); PatternLayoutEncoder fileEncoder = new PatternLayoutEncoder(); fileEncoder.setContext(ctx); fileEncoder.setPattern(logPattern); fileEncoder.start(); fileAppender.setEncoder(fileEncoder); fileAppender.start(); return fileAppender; }
@Override public int hashCode() { int result = processId.hashCode(); result = 31 * result + nodeUuid.hashCode(); return result; } }
@Override public boolean isOperational(ProcessId processId, boolean local) { if (local) { return operationalLocalProcesses.computeIfAbsent(processId, p -> false); } for (Map.Entry<ClusterProcess, Boolean> entry : operationalProcesses.entrySet()) { if (entry.getKey().getProcessId().equals(processId) && entry.getValue()) { return true; } } return false; }
@Override public String toString() { return format("Process[%s]", processId.getKey()); }
@Test public void test_constants() { assertThat(ProcessId.COMPUTE_ENGINE.getKey()).isEqualTo("ce"); assertThat(ProcessId.COMPUTE_ENGINE.getIpcIndex()).isEqualTo(3); }
public static StopRequestWatcherImpl create(AppSettings settings, Scheduler scheduler, FileSystem fs) { DefaultProcessCommands commands = DefaultProcessCommands.secondary(fs.getTempDir(), ProcessId.APP.getIpcIndex()); return new StopRequestWatcherImpl(settings, scheduler, commands); }