Browse Source

[fix-4128][worker] Dolphin executes the command using sudo to specify the -u parameter (#4217)

* update sudo cmd.

* update sudo cmd.

* update OSUtils code style.

* add OSUtils test method.
zhuangchong 4 years ago
parent
commit
a5443f1173

+ 10 - 0
dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/OSUtils.java

@@ -389,6 +389,16 @@ public class OSUtils {
         return null;
     }
 
+    /**
+     *  get sudo command
+     * @param tenantCode tenantCode
+     * @param command command
+     * @return result of sudo execute command
+     */
+    public static String getSudoCmd(String tenantCode, String command) {
+        return StringUtils.isEmpty(tenantCode) ? command : "sudo -u " + tenantCode + " " + command;
+    }
+
     /**
      * Execute the corresponding command of Linux or Windows
      *

+ 7 - 0
dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/OSUtilsTest.java

@@ -75,6 +75,13 @@ public class OSUtilsTest {
         }
     }
 
+    @Test
+    public void testGetSudoCmd() {
+        String cmd = "kill -9 1234";
+        String sudoCmd = OSUtils.getSudoCmd("test123", cmd);
+        Assert.assertEquals("sudo -u test123 " + cmd, sudoCmd);
+    }
+
     @Test
     public void exeCmd() {
         if(OSUtils.isMacOS() || !OSUtils.isWindows()){

+ 3 - 6
dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/utils/ProcessUtils.java

@@ -344,10 +344,7 @@ public class ProcessUtils {
             }
 
             String runCmd = String.format("%s %s", Constants.SH, commandFile);
-            if (StringUtils.isNotEmpty(tenantCode)) {
-                runCmd = "sudo -u " + tenantCode + " " + runCmd;
-            }
-
+            runCmd = OSUtils.getSudoCmd(tenantCode, runCmd);
             logger.info("kill cmd:{}", runCmd);
             OSUtils.exeCmd(runCmd);
         } catch (Exception e) {
@@ -369,8 +366,8 @@ public class ProcessUtils {
                 return;
             }
 
-            String cmd = String.format("sudo kill -9 %s", getPidsStr(processId));
-
+            String cmd = String.format("kill -9 %s", getPidsStr(processId));
+            cmd = OSUtils.getSudoCmd(taskExecutionContext.getTenantCode(), cmd);
             logger.info("process id:{}, cmd:{}", processId, cmd);
 
             OSUtils.exeCmd(cmd);

+ 2 - 2
dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/worker/processor/TaskKillProcessor.java

@@ -115,8 +115,8 @@ public class TaskKillProcessor implements NettyRequestProcessor {
                 return Pair.of(true, appIds);
             }
 
-            String cmd = String.format("sudo kill -9 %s", ProcessUtils.getPidsStr(taskExecutionContext.getProcessId()));
-
+            String cmd = String.format("kill -9 %s", ProcessUtils.getPidsStr(taskExecutionContext.getProcessId()));
+            cmd = OSUtils.getSudoCmd(taskExecutionContext.getTenantCode(), cmd);
             logger.info("process id:{}, cmd:{}", taskExecutionContext.getProcessId(), cmd);
 
             OSUtils.exeCmd(cmd);

+ 5 - 4
dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/worker/task/AbstractCommandExecutor.java

@@ -27,6 +27,7 @@ import org.apache.dolphinscheduler.common.thread.Stopper;
 import org.apache.dolphinscheduler.common.thread.ThreadUtils;
 import org.apache.dolphinscheduler.common.utils.HadoopUtils;
 import org.apache.dolphinscheduler.common.utils.LoggerUtils;
+import org.apache.dolphinscheduler.common.utils.OSUtils;
 import org.apache.dolphinscheduler.common.utils.StringUtils;
 import org.apache.dolphinscheduler.server.entity.TaskExecutionContext;
 import org.apache.dolphinscheduler.server.utils.ProcessUtils;
@@ -265,8 +266,8 @@ public abstract class AbstractCommandExecutor {
         if (processId != 0 && process.isAlive()) {
             try {
                 // sudo -u user command to run command
-                String cmd = String.format("sudo kill %d", processId);
-
+                String cmd = String.format("kill %d", processId);
+                cmd = OSUtils.getSudoCmd(taskExecutionContext.getTenantCode(), cmd);
                 logger.info("soft kill task:{}, process id:{}, cmd:{}", taskExecutionContext.getTaskAppId(), processId, cmd);
 
                 Runtime.getRuntime().exec(cmd);
@@ -286,8 +287,8 @@ public abstract class AbstractCommandExecutor {
     private void hardKill(int processId) {
         if (processId != 0 && process.isAlive()) {
             try {
-                String cmd = String.format("sudo kill -9 %d", processId);
-
+                String cmd = String.format("kill -9 %d", processId);
+                cmd = OSUtils.getSudoCmd(taskExecutionContext.getTenantCode(), cmd);
                 logger.info("hard kill task:{}, process id:{}, cmd:{}", taskExecutionContext.getTaskAppId(), processId, cmd);
 
                 Runtime.getRuntime().exec(cmd);

+ 2 - 1
dolphinscheduler-server/src/test/java/org/apache/dolphinscheduler/server/utils/ProcessUtilsTest.java

@@ -99,12 +99,13 @@ public class ProcessUtilsTest {
         try {
             when(OSUtils.exeCmd(String.format("%s -sp %d", Constants.PSTREE, 1))).thenReturn("1111");
             when(OSUtils.exeCmd(String.format("%s -p %d", Constants.PSTREE, 1))).thenReturn("1111");
-            when(OSUtils.exeCmd("sudo kill -9")).thenReturn("1111");
+            when(OSUtils.exeCmd("sudo -u tenantCode kill -9")).thenReturn("1111");
         } catch (Exception e) {
             e.printStackTrace();
         }
         taskExecutionContext.setHost("127.0.0.1:8888");
         taskExecutionContext.setLogPath("/log/1.log");
+        taskExecutionContext.setTenantCode("tenantCode");
         ProcessUtils.kill(taskExecutionContext);
         Assert.assertEquals(1, taskExecutionContext.getProcessId());
     }