/** * Gets the primary group name from the user name. * * @param userName the user name * @return group name */ public static String getGroupName(String userName) throws IOException { return ShellUtils.execCommand("id", "-ng", userName).trim(); }
/** * Gets the user name from the user id. * * @param uid user id * @return user name */ public static String getUserName(long uid) throws IOException { return ShellUtils.execCommand("id", "-nu", Long.toString(uid)).trim(); }
/** * Runs the "id" command with the given options on the passed username. * * @param option option to pass to id (either -u or -g) * @param username the username on which to run the command * @return the uid (-u) or gid (-g) of username */ private static long getIdInfo(String option, String username) { String output; try { output = ShellUtils.execCommand("id", option, username).trim(); } catch (IOException e) { LOG.error("Failed to get id from {} with option {}", username, option); return -1; } return Long.parseLong(output); }
/** * Gets the group name from the group id. * * @param gid the group id * @return group name */ public static String getGroupName(long gid) throws IOException { if (OSUtils.isLinux()) { String script = "getent group " + gid + " | cut -d: -f1"; return ShellUtils.execCommand("bash", "-c", script).trim(); } else if (OSUtils.isMacOS()) { String script = "dscl . list /Groups PrimaryGroupID | awk '($2 == \"" + gid + "\") { print $1 }'"; return ShellUtils.execCommand("bash", "-c", script).trim(); } return ""; }
/** * Retrieves the gid of the given group. * * @param groupName the group name * @return gid */ public static long getGidFromGroupName(String groupName) throws IOException { String result = ""; if (OSUtils.isLinux()) { String script = "getent group " + groupName + " | cut -d: -f3"; result = ShellUtils.execCommand("bash", "-c", script).trim(); } else if (OSUtils.isMacOS()) { String script = "dscl . -read /Groups/" + groupName + " | awk '($1 == \"PrimaryGroupID:\") { print $2 }'"; result = ShellUtils.execCommand("bash", "-c", script).trim(); } try { return Long.parseLong(result); } catch (NumberFormatException e) { LOG.error("Failed to get gid from group name {}.", groupName); return -1; } }
/** * Checks whether fuse is installed in local file system. * Alluxio-Fuse only support mac and linux. * * @return true if fuse is installed, false otherwise */ public static boolean isFuseInstalled() { try { if (OSUtils.isLinux()) { String result = ShellUtils.execCommand("fusermount", "-V"); return !result.isEmpty(); } else if (OSUtils.isMacOS()) { String result = ShellUtils.execCommand("bash", "-c", "mount | grep FUSE"); return !result.isEmpty(); } } catch (Exception e) { return false; } return false; }
/** * Gets system mount information. This method should only be attempted on Unix systems. * * @return system mount information */ public static List<UnixMountInfo> getUnixMountInfo() throws IOException { Preconditions.checkState(OSUtils.isLinux() || OSUtils.isMacOS()); String output = execCommand(MOUNT_COMMAND); List<UnixMountInfo> mountInfo = new ArrayList<>(); for (String line : output.split("\n")) { mountInfo.add(parseMountInfo(line)); } return mountInfo; }
/** * Gets the current user's group list from Unix by running the command 'groups' NOTE. For * non-existing user it will return EMPTY list. This method may return duplicate groups. * * @param user user name * @return the groups list that the {@code user} belongs to. The primary group is returned first */ public static List<String> getUnixGroups(String user) throws IOException { String result; List<String> groups = new ArrayList<>(); try { result = ShellUtils.execCommand(ShellUtils.getGroupsForUserCommand(user)); } catch (ExitCodeException e) { // if we didn't get the group - just return empty list LOG.warn("got exception trying to get groups for user " + user + ": " + e.getMessage()); return groups; } StringTokenizer tokenizer = new StringTokenizer(result, ShellUtils.TOKEN_SEPARATOR_REGEX); while (tokenizer.hasMoreTokens()) { groups.add(tokenizer.nextToken()); } return groups; }
String identityString; try { identityString = ShellUtils.execCommand(script.toString()); } catch (IOException e) { throw new RuntimeException(
ShellUtils.COMMON_SSH_OPTS, uri, host, targetUriParent); LOG.debug("Executing: {}", rsyncCmd); String output = ShellUtils.execCommand("bash", "-c", rsyncCmd); LOG.debug("Succeeded w/ output: {}", output); } catch (IOException e) {
@Override public int run(CommandLine cl) { String uri = cl.getArgs()[0]; String extensionsDir = ServerConfiguration.get(PropertyKey.EXTENSIONS_DIR); List<String> failedHosts = new ArrayList<>(); for (String host : ExtensionsShellUtils.getServerHostnames()) { try { LOG.info("Attempting to uninstall extension on host {}", host); String rmCmd = String.format("ssh %s %s rm %s", ShellUtils.COMMON_SSH_OPTS, host, PathUtils.concatPath(extensionsDir, uri)); LOG.debug("Executing: {}", rmCmd); String output = ShellUtils.execCommand("bash", "-c", rmCmd); LOG.debug("Succeeded w/ output: {}", output); } catch (IOException e) { LOG.error("Error uninstalling extension on host {}.", host, e); failedHosts.add(host); } } if (failedHosts.size() != 0) { System.err.println("Failed to uninstall extension on hosts:"); for (String failedHost : failedHosts) { System.err.println(failedHost); } return -1; } System.out.println("Extension uninstalled successfully."); return 0; }
+ "awk '{ print $2; }'"); LOG.debug("Executing: {}", cmd); String output = ShellUtils.execCommand("bash", "-c", cmd); if (output.isEmpty()) { throw new IllegalStateException(
/** * Tests the {@link ShellUtils#execCommand(String...)} method. * * @throws Throwable when the execution of the command fails */ @Test public void execCommand() throws Exception { String testString = "alluxio"; // Execute echo for testing command execution. String result = ShellUtils.execCommand("bash", "-c", "echo " + testString); Assert.assertEquals(testString + "\n", result); }
private void setupShellMocks(String username, List<String> groups) throws IOException { PowerMockito.mockStatic(ShellUtils.class); String shellResult = ""; for (String group: groups) { shellResult = shellResult + " " + group; } PowerMockito.when( ShellUtils.execCommand(ShellUtils.getGroupsForUserCommand(Mockito.eq(username)))) .thenReturn(shellResult); }
/** * Tests the {@link ShellUtils#execCommand(String...)} method for a group of commands. * * @throws Throwable when the execution of the commands fails */ @Test public void execGetGroupCommand() throws Exception { String result = ShellUtils.execCommand(ShellUtils.getGroupsForUserCommand("root")); // On Linux user "root" will be a part of the group "root". On OSX it will be a part of "admin". Assert.assertTrue(result.contains("root") || result.contains("admin")); }
/** * Gets system mount information. This method should only be attempted on Unix systems. * * @return system mount information */ public static List<UnixMountInfo> getUnixMountInfo() throws IOException { Preconditions.checkState(OSUtils.isLinux() || OSUtils.isMacOS()); String output = execCommand(MOUNT_COMMAND); List<UnixMountInfo> mountInfo = new ArrayList<>(); for (String line : output.split("\n")) { mountInfo.add(parseMountInfo(line)); } return mountInfo; }
/** * @return a tiered identity created from running the user-provided script */ @Nullable private static TieredIdentity fromScript() { String scriptName = Configuration.get(PropertyKey.LOCALITY_SCRIPT); Path script = Paths.get(scriptName); if (!Files.exists(script)) { URL resource = TieredIdentityFactory.class.getClassLoader().getResource(scriptName); if (resource != null) { script = Paths.get(resource.getPath()); } else { return null; } } LOG.debug("Found tiered identity script at {}", script); String identityString; try { identityString = ShellUtils.execCommand(script.toString()); } catch (IOException e) { throw new RuntimeException( String.format("Failed to run script %s: %s", script, e.toString()), e); } try { return fromString(identityString); } catch (IOException e) { throw new RuntimeException( String.format("Failed to parse output of running %s: %s", script, e.getMessage()), e); } }
/** * Gets the current user's group list from Unix by running the command 'groups' NOTE. For * non-existing user it will return EMPTY list. This method may return duplicate groups. * * @param user user name * @return the groups list that the {@code user} belongs to. The primary group is returned first */ public static List<String> getUnixGroups(String user) throws IOException { String result; List<String> groups = new ArrayList<>(); try { result = ShellUtils.execCommand(ShellUtils.getGroupsForUserCommand(user)); } catch (ExitCodeException e) { // if we didn't get the group - just return empty list LOG.warn("got exception trying to get groups for user " + user + ": " + e.getMessage()); return groups; } StringTokenizer tokenizer = new StringTokenizer(result, ShellUtils.TOKEN_SEPARATOR_REGEX); while (tokenizer.hasMoreTokens()) { groups.add(tokenizer.nextToken()); } return groups; }
ShellUtils.COMMON_SSH_OPTS, uri, host, targetUriParent); LOG.debug("Executing: {}", rsyncCmd); String output = ShellUtils.execCommand("bash", "-c", rsyncCmd); LOG.debug("Succeeded w/ output: {}", output); } catch (IOException e) {
@Override public int run(CommandLine cl) { String uri = cl.getArgs()[0]; String extensionsDir = Configuration.get(PropertyKey.EXTENSIONS_DIR); List<String> failedHosts = new ArrayList<>(); for (String host : ExtensionsShellUtils.getServerHostnames()) { try { LOG.info("Attempting to uninstall extension on host {}", host); String rmCmd = String.format("ssh %s %s rm %s", ShellUtils.COMMON_SSH_OPTS, host, PathUtils.concatPath(extensionsDir, uri)); LOG.debug("Executing: {}", rmCmd); String output = ShellUtils.execCommand("bash", "-c", rmCmd); LOG.debug("Succeeded w/ output: {}", output); } catch (IOException e) { LOG.error("Error uninstalling extension on host {}.", host, e); failedHosts.add(host); } } if (failedHosts.size() != 0) { System.err.println("Failed to uninstall extension on hosts:"); for (String failedHost : failedHosts) { System.err.println(failedHost); } return -1; } System.out.println("Extension uninstalled successfully."); return 0; }