diff --git a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/exception/FileOperateException.java b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/exception/FileOperateException.java new file mode 100644 index 000000000000..8099ebea7349 --- /dev/null +++ b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/exception/FileOperateException.java @@ -0,0 +1,29 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.dolphinscheduler.common.exception; + +public class FileOperateException extends BaseException { + + public FileOperateException(String message) { + super(message); + } + + public FileOperateException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/FileUtils.java b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/FileUtils.java index abbfdbfcd370..a995b29dd6f4 100644 --- a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/FileUtils.java +++ b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/FileUtils.java @@ -25,6 +25,8 @@ import static org.apache.dolphinscheduler.common.constants.Constants.UTF_8; import static org.apache.dolphinscheduler.common.constants.DateConstants.YYYYMMDDHHMMSS; +import org.apache.dolphinscheduler.common.exception.FileOperateException; + import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.SystemUtils; @@ -323,23 +325,41 @@ public static String getFileChecksum(String pathName) throws IOException { return crcString; } - public static void setFileOwner(Path filePath, String fileOwner) throws InterruptedException, IOException { - // We use linux command to set the file owner, since jdk api will not use sudo. - String command = String.format("sudo chown %s %s", fileOwner, filePath.toString()); - Runtime.getRuntime().exec(command); - Process process = Runtime.getRuntime().exec(command); - if (0 != process.waitFor()) { - throw new RuntimeException("Set file: " + filePath + " to owner: " + fileOwner + " failed"); + public static void setFileOwner(Path filePath, String fileOwner) throws FileOperateException { + try { + // We use linux command to set the file owner, since jdk api will not use sudo. + String command = String.format("sudo chown %s %s", fileOwner, filePath.toString()); + Runtime.getRuntime().exec(command); + Process process = Runtime.getRuntime().exec(command); + int exitCode = process.waitFor(); + if (0 != exitCode) { + throw new FileOperateException( + "Set file: " + filePath + " to owner: " + fileOwner + " failed, existCode(" + exitCode + ")"); + } + } catch (FileOperateException ex) { + throw ex; + } catch (Exception ex) { + throw new FileOperateException("Set directory: " + filePath + " to owner: " + fileOwner + " failed"); + } } - public static void setDirectoryOwner(Path filePath, String fileOwner) throws IOException, InterruptedException { - // We use linux command to set the file owner, since jdk api will not use sudo. - String command = String.format("sudo chown -R %s %s", fileOwner, filePath.toString()); - Runtime.getRuntime().exec(command); - Process process = Runtime.getRuntime().exec(command); - if (0 != process.waitFor()) { - throw new RuntimeException("Set directory: " + filePath + " to owner: " + fileOwner + " failed"); + public static void setDirectoryOwner(Path filePath, String fileOwner) throws FileOperateException { + try { + // We use linux command to set the file owner, since jdk api will not use sudo. + String command = String.format("sudo chown -R %s %s", fileOwner, filePath.toString()); + Runtime.getRuntime().exec(command); + Process process = Runtime.getRuntime().exec(command); + int exitCode = process.waitFor(); + if (0 != exitCode) { + throw new FileOperateException("Set directory: " + filePath + " to owner: " + fileOwner + + " failed, existCode(" + exitCode + ")"); + } + } catch (FileOperateException ex) { + throw ex; + } catch (Exception ex) { + throw new FileOperateException("Set directory: " + filePath + " to owner: " + fileOwner + " failed"); + } } diff --git a/dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/main/java/org/apache/dolphinscheduler/plugin/task/api/shell/BaseLinuxShellInterceptorBuilder.java b/dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/main/java/org/apache/dolphinscheduler/plugin/task/api/shell/BaseLinuxShellInterceptorBuilder.java index 5816681e75f2..3c24977c4a09 100644 --- a/dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/main/java/org/apache/dolphinscheduler/plugin/task/api/shell/BaseLinuxShellInterceptorBuilder.java +++ b/dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/main/java/org/apache/dolphinscheduler/plugin/task/api/shell/BaseLinuxShellInterceptorBuilder.java @@ -17,6 +17,7 @@ package org.apache.dolphinscheduler.plugin.task.api.shell; +import org.apache.dolphinscheduler.common.exception.FileOperateException; import org.apache.dolphinscheduler.common.utils.FileUtils; import org.apache.dolphinscheduler.common.utils.PropertyUtils; import org.apache.dolphinscheduler.plugin.task.api.utils.AbstractCommandExecutorConstants; @@ -64,8 +65,10 @@ protected void generateShellScript() throws IOException { log.info("Final Shell file is : \n{}", finalScript); } - protected List generateBootstrapCommand() { + protected List generateBootstrapCommand() throws FileOperateException { if (sudoEnable) { + // Set the tenant owner as the working directory + FileUtils.setDirectoryOwner(Paths.get(shellDirectory), runUser); return bootstrapCommandInSudoMode(); } return bootstrapCommandInNormalMode(); diff --git a/dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/main/java/org/apache/dolphinscheduler/plugin/task/api/shell/IShellInterceptorBuilder.java b/dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/main/java/org/apache/dolphinscheduler/plugin/task/api/shell/IShellInterceptorBuilder.java index cab50e3bdf51..8e6cf5c42f6a 100644 --- a/dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/main/java/org/apache/dolphinscheduler/plugin/task/api/shell/IShellInterceptorBuilder.java +++ b/dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/main/java/org/apache/dolphinscheduler/plugin/task/api/shell/IShellInterceptorBuilder.java @@ -17,6 +17,8 @@ package org.apache.dolphinscheduler.plugin.task.api.shell; +import org.apache.dolphinscheduler.common.exception.FileOperateException; + import java.io.IOException; import java.util.Map; @@ -48,5 +50,5 @@ public interface IShellInterceptorBuilder bootstrapCommand = generateBootstrapCommand(); return new BashShellInterceptor(bootstrapCommand, shellDirectory); diff --git a/dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/main/java/org/apache/dolphinscheduler/plugin/task/api/shell/sh/ShShellInterceptorBuilder.java b/dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/main/java/org/apache/dolphinscheduler/plugin/task/api/shell/sh/ShShellInterceptorBuilder.java index 8f15b543d0bd..c74551b6c19a 100644 --- a/dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/main/java/org/apache/dolphinscheduler/plugin/task/api/shell/sh/ShShellInterceptorBuilder.java +++ b/dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/main/java/org/apache/dolphinscheduler/plugin/task/api/shell/sh/ShShellInterceptorBuilder.java @@ -17,6 +17,7 @@ package org.apache.dolphinscheduler.plugin.task.api.shell.sh; +import org.apache.dolphinscheduler.common.exception.FileOperateException; import org.apache.dolphinscheduler.plugin.task.api.shell.BaseLinuxShellInterceptorBuilder; import java.io.IOException; @@ -32,7 +33,7 @@ public ShShellInterceptorBuilder newBuilder() { } @Override - public ShShellInterceptor build() throws IOException { + public ShShellInterceptor build() throws IOException, FileOperateException { generateShellScript(); List bootstrapCommand = generateBootstrapCommand(); return new ShShellInterceptor(bootstrapCommand, shellDirectory); diff --git a/dolphinscheduler-worker/src/main/java/org/apache/dolphinscheduler/server/worker/utils/TaskExecutionCheckerUtils.java b/dolphinscheduler-worker/src/main/java/org/apache/dolphinscheduler/server/worker/utils/TaskExecutionCheckerUtils.java index 322e16bc2bbd..195e4d3a0a93 100644 --- a/dolphinscheduler-worker/src/main/java/org/apache/dolphinscheduler/server/worker/utils/TaskExecutionCheckerUtils.java +++ b/dolphinscheduler-worker/src/main/java/org/apache/dolphinscheduler/server/worker/utils/TaskExecutionCheckerUtils.java @@ -92,10 +92,6 @@ public static void createProcessLocalPathIfAbsent(TaskExecutionContext taskExecu taskExecutionContext.setAppInfoPath(FileUtils.getAppInfoPath(execLocalPath)); Path executePath = Paths.get(taskExecutionContext.getExecutePath()); FileUtils.createDirectoryIfNotPresent(executePath); - if (OSUtils.isSudoEnable() - && !TenantConstants.DEFAULT_TENANT_CODE.equals(taskExecutionContext.getTenantCode())) { - FileUtils.setDirectoryOwner(executePath, taskExecutionContext.getTenantCode()); - } } catch (Throwable ex) { throw new TaskException("Cannot create process execute dir", ex); } @@ -136,11 +132,6 @@ public static void downloadResourcesIfNeeded(StorageOperate storageOperate, Path localFileAbsolutePath = Paths.get(execLocalPath, fileName); storageOperate.download(actualTenant, fullName, localFileAbsolutePath.toString(), true); log.info("Download resource file {} under: {} successfully", fileName, localFileAbsolutePath); - if (OSUtils.isSudoEnable() && !TenantConstants.DEFAULT_TENANT_CODE.equals(tenant)) { - FileUtils.setFileOwner(localFileAbsolutePath, taskExecutionContext.getTenantCode()); - log.info("Set file {} owner to {} successfully", localFileAbsolutePath, - taskExecutionContext.getTenantCode()); - } WorkerServerMetrics .recordWorkerResourceDownloadTime(System.currentTimeMillis() - resourceDownloadStartTime); WorkerServerMetrics.recordWorkerResourceDownloadSize(Files.size(localFileAbsolutePath));