You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dolphinscheduler.apache.org by li...@apache.org on 2020/02/16 01:52:57 UTC

[incubator-dolphinscheduler] branch dev updated: Support DS to create user and group in windows environment (#1953)

This is an automated email from the ASF dual-hosted git repository.

lidongdai pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/incubator-dolphinscheduler.git


The following commit(s) were added to refs/heads/dev by this push:
     new d674eab  Support DS to create user and group in windows environment (#1953)
d674eab is described below

commit d674eaba57cdd92b8588bdaa63b80fd2f36b5d5d
Author: liwenhe1993 <32...@users.noreply.github.com>
AuthorDate: Sun Feb 16 09:52:47 2020 +0800

    Support DS to create user and group in windows environment (#1953)
    
    * Support DS to create user and group in windows environment
    
    * Add unit test
---
 .../dolphinscheduler/common/utils/OSUtils.java     | 84 ++++++++++++++++++++--
 .../dolphinscheduler/common/utils/OSUtilsTest.java | 23 +++---
 2 files changed, 93 insertions(+), 14 deletions(-)

diff --git a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/OSUtils.java b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/OSUtils.java
index c8d33e3..acfca77 100644
--- a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/OSUtils.java
+++ b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/OSUtils.java
@@ -40,6 +40,7 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
+import java.util.regex.Pattern;
 
 /**
  * os utils
@@ -138,7 +139,7 @@ public class OSUtils {
       if (isMacOS()) {
         return getUserListFromMac();
       } else if (isWindows()) {
-        // do something
+        return getUserListFromWindows();
       } else {
         return getUserListFromLinux();
       }
@@ -186,6 +187,47 @@ public class OSUtils {
   }
 
   /**
+   *  get user list from windows
+   * @return user list
+   * @throws IOException
+   */
+  private static List<String> getUserListFromWindows() throws IOException {
+    String result = exeCmd("net user");
+    String[] lines = result.split("\n");
+
+    int startPos = 0;
+    int endPos = lines.length - 2;
+    for (int i = 0; i < lines.length; i++) {
+      if (lines[i].isEmpty()) {
+        continue;
+      }
+
+      int count = 0;
+      if (lines[i].charAt(0) == '-') {
+        for (int j = 0; j < lines[i].length(); j++) {
+          if (lines[i].charAt(i) == '-') {
+            count++;
+          }
+        }
+      }
+
+      if (count == lines[i].length()) {
+        startPos = i + 1;
+        break;
+      }
+    }
+
+    List<String> users = new ArrayList<>();
+    while (startPos <= endPos) {
+      Pattern pattern = Pattern.compile("\\s+");
+      users.addAll(Arrays.asList(pattern.split(lines[startPos])));
+      startPos++;
+    }
+
+    return users;
+  }
+
+  /**
    * create user
    * @param userName user name
    * @return true if creation was successful, otherwise false
@@ -200,7 +242,7 @@ public class OSUtils {
       if (isMacOS()) {
         createMacUser(userName, userGroup);
       } else if (isWindows()) {
-        // do something
+        createWindowsUser(userName, userGroup);
       } else {
         createLinuxUser(userName, userGroup);
       }
@@ -244,15 +286,45 @@ public class OSUtils {
   }
 
   /**
+   * create windows user
+   * @param userName user name
+   * @param userGroup user group
+   * @throws IOException in case of an I/O error
+   */
+  private static void createWindowsUser(String userName, String userGroup) throws IOException {
+    logger.info("create windows os user : {}", userName);
+    String userCreateCmd = String.format("net user \"%s\" /add", userName);
+    String appendGroupCmd = String.format("net localgroup \"%s\" \"%s\" /add", userGroup, userName);
+
+    logger.info("execute create user command : {}", userCreateCmd);
+    OSUtils.exeCmd(userCreateCmd);
+
+    logger.info("execute append user to group : {}", appendGroupCmd);
+    OSUtils.exeCmd(appendGroupCmd);
+  }
+
+  /**
    * get system group information
    * @return system group info
    * @throws IOException errors
    */
   public static String getGroup() throws IOException {
-    String result = exeCmd("groups");
-    if (StringUtils.isNotEmpty(result)) {
-      String[] groupInfo = result.split(" ");
-      return groupInfo[0];
+    if (isWindows()) {
+      String currentProcUserName = System.getProperty("user.name");
+      String result = exeCmd(String.format("net user \"%s\"", currentProcUserName));
+      String line = result.split("\n")[22];
+      String group = Pattern.compile("\\s+").split(line)[1];
+      if (group.charAt(0) == '*') {
+        return group.substring(1);
+      } else {
+        return group;
+      }
+    } else {
+      String result = exeCmd("groups");
+      if (StringUtils.isNotEmpty(result)) {
+        String[] groupInfo = result.split(" ");
+        return groupInfo[0];
+      }
     }
 
     return null;
diff --git a/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/OSUtilsTest.java b/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/OSUtilsTest.java
index 3d51aa8..7106804 100644
--- a/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/OSUtilsTest.java
+++ b/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/OSUtilsTest.java
@@ -36,9 +36,9 @@ public class OSUtilsTest {
         Assert.assertNotEquals("System user list should not be empty", userList.size(), 0);
         logger.info("OS user list : {}", userList.toString());
     }
+
     @Test
     public void testOSMetric(){
-
         double availablePhysicalMemorySize = OSUtils.availablePhysicalMemorySize();
         Assert.assertTrue(availablePhysicalMemorySize > 0.0f);
         double totalMemorySize = OSUtils.totalMemorySize();
@@ -50,17 +50,23 @@ public class OSUtilsTest {
         double cpuUsage = OSUtils.cpuUsage();
         Assert.assertTrue(cpuUsage > 0.0f);
     }
+
     @Test
     public void getGroup() {
-        if(OSUtils.isMacOS() || !OSUtils.isWindows()){
-            try {
-                String group = OSUtils.getGroup();
-                Assert.assertNotNull(group);
-            } catch (IOException e) {
-                Assert.fail("get group failed " + e.getMessage());
-            }
+        try {
+            String group = OSUtils.getGroup();
+            Assert.assertNotNull(group);
+        } catch (IOException e) {
+            Assert.fail("get group failed " + e.getMessage());
         }
     }
+
+    @Test
+    public void createUser() {
+        boolean result = OSUtils.createUser("test123");
+        Assert.assertTrue(result);
+    }
+
     @Test
     public void exeCmd() {
         if(OSUtils.isMacOS() || !OSUtils.isWindows()){
@@ -113,4 +119,5 @@ public class OSUtilsTest {
         Assert.assertFalse(resource);
 
     }
+
 }