/** * Get the current user's group list from Unix by running the command 'groups' NOTE. For non-existing user it will return EMPTY list * * @param user user name * @return the groups set that the <code>user</code> belongs to * * @throws IOException if encounter any error when running the command */ private Set<String> getUnixGroups(final String user) throws IOException { String result; try { result = shellCommandRunner.execCommand(ShellUtils.getGroupsForUserCommand(user)); } catch (ExitCodeException e) { // if we didn't get the group - just return empty list; LOG.debug("Unable to get groups for user " + user + ". ShellUtils command failed with exit code " + e.getExitCode()); return new HashSet<>(); } Set<String> groups = new HashSet<>(); for (String group : result.split(shellCommandRunner.getTokenSeparatorRegex())) { groups.add(group); } return groups; }
/** * A null config will throw IllegalArgumentException. */ @Test(expected = IllegalArgumentException.class) public void getLogHandler_nullConf() { ShellUtils.getLogHandler(null); }
ProcessBuilder builder = new ProcessBuilder(getExecString()); Timer timeOutTimer = null; ShellTimeoutTimerTask timeoutTimerTask = null; parseExecResult(inReader); // parse the output joinThread(errThread); completed.set(true); joinThread(errThread);
/** * Get the current user's group list from Unix by running the command 'groups' * NOTE. For non-existing user it will return EMPTY list * @param user user name * @return the groups set that the <code>user</code> belongs to * @throws IOException if encounter any error when running the command */ private static Set<String> getUnixGroups(final String user) throws IOException { String result; try { result = ShellUtils.execCommand(ShellUtils.getGroupsForUserCommand(user)); } catch (ExitCodeException e) { // if we didn't get the group - just return empty list; LOG.debug("unable to get groups for user " + user + ".ShellUtils command failed with exit code "+ e.getExitCode()); return new HashSet<>(); } StringTokenizer tokenizer = new StringTokenizer(result, ShellUtils.TOKEN_SEPARATOR_REGEX); Set<String> groups = new HashSet<>(); while (tokenizer.hasMoreTokens()) { groups.add(tokenizer.nextToken()); } return groups; }
/** * check to see if a command needs to be executed and execute if needed */ protected void run() throws IOException { if (lastTime + interval > System.currentTimeMillis()) { return; } exitCode = 0; // reset for next run try { runCommand(); } catch (IOException e) { numShellExceptions.mark(); throw e; } }
@Override public void run() { Process p = shell.getProcess(); try { p.exitValue(); } catch (Exception e) { //Process has not terminated. //So check if it has completed //if not just destroy it. if (p != null && !shell.completed.get()) { shell.setTimedOut(); p.destroy(); } } } }
/** * Static method to execute a shell command. * Covers most of the simple cases without requiring the user to implement * the <code>Shell</code> interface. * @param cmd shell command to execute. * @return the output of the executed command. */ public static String execCommand(String ... cmd) throws IOException { return execCommand(null, cmd, 0L); }
/** check to see if a command needs to be executed and execute if needed */ protected void run() throws IOException { if (lastTime + interval > System.currentTimeMillis()) return; exitCode = 0; // reset for next run runCommand(); }
@Override public void run() { Process p = shell.getProcess(); try { p.exitValue(); } catch (Exception e) { //Process has not terminated. //So check if it has completed //if not just destroy it. if (p != null && !shell.completed.get()) { shell.setTimedOut(); p.destroy(); } } } }
/** * Static method to execute a shell command. * Covers most of the simple cases without requiring the user to implement * the <code>Shell</code> interface. * @param env the map of environment key=value * @param cmd shell command to execute. * @return the output of the executed command. */ public static String execCommand(Map<String,String> env, String ... cmd) throws IOException { return execCommand(env, cmd, 0L); }
ProcessBuilder builder = new ProcessBuilder(getExecString()); Timer timeOutTimer = null; ShellTimeoutTimerTask timeoutTimerTask = null; } catch (IllegalStateException ise) { } try { parseExecResult(inReader); // parse the output joinThread(errThread); completed.set(true); joinThread(errThread);
public void open(Map<String, Object> topoConf, TopologyContext context, SpoutOutputCollector collector) { _collector = collector; _context = context; if (topoConf.containsKey(Config.TOPOLOGY_SUBPROCESS_TIMEOUT_SECS)) { workerTimeoutMills = 1000 * ObjectReader.getInt(topoConf.get(Config.TOPOLOGY_SUBPROCESS_TIMEOUT_SECS)); } else { workerTimeoutMills = 1000 * ObjectReader.getInt(topoConf.get(Config.SUPERVISOR_WORKER_TIMEOUT_SECS)); } _process = new ShellProcess(_command); if (!env.isEmpty()) { _process.setEnv(env); } Number subpid = _process.launch(topoConf, context, changeDirectory); LOG.info("Launched subprocess with pid " + subpid); _logHandler = ShellUtils.getLogHandler(topoConf); _logHandler.setUpContext(ShellSpout.class, _process, _context); heartBeatExecutorService = MoreExecutors.getExitingScheduledExecutorService(new ScheduledThreadPoolExecutor(1)); }
@Test public void testCanGetGroups() throws Exception { try (SimulatedTime t = new SimulatedTime()) { groupsMapping.prepare(topoConf); when(mockShell.execCommand(ShellUtils.getGroupsForUserCommand(TEST_USER_1))).thenReturn(TEST_TWO_GROUPS); Set<String> groups = groupsMapping.getGroups(TEST_USER_1); assertThat(groups, containsInAnyOrder(TEST_TWO_GROUPS.split(GROUP_SEPARATOR_REGEX))); } }
String resultFromMonitor = ShellUtils.execCommand(commands.toArray(new String[0]));
/** * If a log handler is not configured, {@link DefaultShellLogHandler} * will be returned. */ @Test public void getLogHandler_notConfigured() { ShellLogHandler logHandler = ShellUtils.getLogHandler(new HashMap<String, Object>()); assertTrue(logHandler.getClass() == DefaultShellLogHandler.class); }
@Test public void testWillCacheGroups() throws Exception { try (SimulatedTime t = new SimulatedTime()) { groupsMapping.prepare(topoConf); when(mockShell.execCommand(ShellUtils.getGroupsForUserCommand(TEST_USER_1))).thenReturn(TEST_TWO_GROUPS, TEST_NO_GROUPS); Set<String> firstGroups = groupsMapping.getGroups(TEST_USER_1); Set<String> secondGroups = groupsMapping.getGroups(TEST_USER_1); assertThat(firstGroups, is(secondGroups)); } }
LOG.info("Launched subprocess with pid " + subpid); _logHandler = ShellUtils.getLogHandler(topoConf); _logHandler.setUpContext(ShellBolt.class, _process, _context);
@Test public void testWillExpireCache() throws Exception { try (SimulatedTime t = new SimulatedTime()) { groupsMapping.prepare(topoConf); when(mockShell.execCommand(ShellUtils.getGroupsForUserCommand(TEST_USER_1))).thenReturn(TEST_TWO_GROUPS, TEST_NO_GROUPS); Set<String> firstGroups = groupsMapping.getGroups(TEST_USER_1); Time.advanceTimeSecs(CACHE_EXPIRATION_SECS * 2); Set<String> secondGroups = groupsMapping.getGroups(TEST_USER_1); assertThat(firstGroups, not(secondGroups)); assertThat(secondGroups, contains(TEST_NO_GROUPS)); } }
/** * If a log handler is correctly configured, it will be returned. */ @Test public void getLogHandler_customHandler() { Map<String, Object> conf = configureLogHandler("org.apache.storm.utils.ShellUtilsTest$CustomShellLogHandler"); ShellLogHandler logHandler = ShellUtils.getLogHandler(conf); assertTrue(logHandler.getClass() == CustomShellLogHandler.class); }
public void open(Map stormConf, TopologyContext context, SpoutOutputCollector collector) { _collector = collector; _context = context; if (stormConf.containsKey(Config.TOPOLOGY_SUBPROCESS_TIMEOUT_SECS)) { workerTimeoutMills = 1000 * RT.intCast(stormConf.get(Config.TOPOLOGY_SUBPROCESS_TIMEOUT_SECS)); } else { workerTimeoutMills = 1000 * RT.intCast(stormConf.get(Config.SUPERVISOR_WORKER_TIMEOUT_SECS)); } _process = new ShellProcess(_command); if (!env.isEmpty()) { _process.setEnv(env); } Number subpid = _process.launch(stormConf, context, changeDirectory); LOG.info("Launched subprocess with pid " + subpid); _logHandler = ShellUtils.getLogHandler(stormConf); _logHandler.setUpContext(ShellSpout.class, _process, _context); heartBeatExecutorService = MoreExecutors.getExitingScheduledExecutorService(new ScheduledThreadPoolExecutor(1)); }