Browse Source

Set the tenant as the owner in final stage (#15256)

3.2.1-prepare
Wenjun Ruan 12 months ago committed by GitHub
parent
commit
ef2e9c9b67
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 29
      dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/exception/FileOperateException.java
  2. 48
      dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/FileUtils.java
  3. 5
      dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/main/java/org/apache/dolphinscheduler/plugin/task/api/shell/BaseLinuxShellInterceptorBuilder.java
  4. 4
      dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/main/java/org/apache/dolphinscheduler/plugin/task/api/shell/IShellInterceptorBuilder.java
  5. 3
      dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/main/java/org/apache/dolphinscheduler/plugin/task/api/shell/bash/BashShellInterceptorBuilder.java
  6. 3
      dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/main/java/org/apache/dolphinscheduler/plugin/task/api/shell/sh/ShShellInterceptorBuilder.java
  7. 9
      dolphinscheduler-worker/src/main/java/org/apache/dolphinscheduler/server/worker/utils/TaskExecutionCheckerUtils.java

29
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);
}
}

48
dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/FileUtils.java

@ -25,6 +25,8 @@ import static org.apache.dolphinscheduler.common.constants.Constants.RESOURCE_VI
import static org.apache.dolphinscheduler.common.constants.Constants.UTF_8; import static org.apache.dolphinscheduler.common.constants.Constants.UTF_8;
import static org.apache.dolphinscheduler.common.constants.DateConstants.YYYYMMDDHHMMSS; 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.io.IOUtils;
import org.apache.commons.lang3.SystemUtils; import org.apache.commons.lang3.SystemUtils;
@ -323,23 +325,41 @@ public class FileUtils {
return crcString; return crcString;
} }
public static void setFileOwner(Path filePath, String fileOwner) throws InterruptedException, IOException { public static void setFileOwner(Path filePath, String fileOwner) throws FileOperateException {
// We use linux command to set the file owner, since jdk api will not use sudo. try {
String command = String.format("sudo chown %s %s", fileOwner, filePath.toString()); // We use linux command to set the file owner, since jdk api will not use sudo.
Runtime.getRuntime().exec(command); String command = String.format("sudo chown %s %s", fileOwner, filePath.toString());
Process process = Runtime.getRuntime().exec(command); Runtime.getRuntime().exec(command);
if (0 != process.waitFor()) { Process process = Runtime.getRuntime().exec(command);
throw new RuntimeException("Set file: " + filePath + " to owner: " + fileOwner + " failed"); 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 { public static void setDirectoryOwner(Path filePath, String fileOwner) throws FileOperateException {
// We use linux command to set the file owner, since jdk api will not use sudo. try {
String command = String.format("sudo chown -R %s %s", fileOwner, filePath.toString()); // We use linux command to set the file owner, since jdk api will not use sudo.
Runtime.getRuntime().exec(command); String command = String.format("sudo chown -R %s %s", fileOwner, filePath.toString());
Process process = Runtime.getRuntime().exec(command); Runtime.getRuntime().exec(command);
if (0 != process.waitFor()) { Process process = Runtime.getRuntime().exec(command);
throw new RuntimeException("Set directory: " + filePath + " to owner: " + fileOwner + " failed"); 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");
} }
} }

5
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; 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.FileUtils;
import org.apache.dolphinscheduler.common.utils.PropertyUtils; import org.apache.dolphinscheduler.common.utils.PropertyUtils;
import org.apache.dolphinscheduler.plugin.task.api.utils.AbstractCommandExecutorConstants; import org.apache.dolphinscheduler.plugin.task.api.utils.AbstractCommandExecutorConstants;
@ -64,8 +65,10 @@ public abstract class BaseLinuxShellInterceptorBuilder<T extends BaseLinuxShellI
log.info("Final Shell file is : \n{}", finalScript); log.info("Final Shell file is : \n{}", finalScript);
} }
protected List<String> generateBootstrapCommand() { protected List<String> generateBootstrapCommand() throws FileOperateException {
if (sudoEnable) { if (sudoEnable) {
// Set the tenant owner as the working directory
FileUtils.setDirectoryOwner(Paths.get(shellDirectory), runUser);
return bootstrapCommandInSudoMode(); return bootstrapCommandInSudoMode();
} }
return bootstrapCommandInNormalMode(); return bootstrapCommandInNormalMode();

4
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; package org.apache.dolphinscheduler.plugin.task.api.shell;
import org.apache.dolphinscheduler.common.exception.FileOperateException;
import java.io.IOException; import java.io.IOException;
import java.util.Map; import java.util.Map;
@ -48,5 +50,5 @@ public interface IShellInterceptorBuilder<T extends IShellInterceptorBuilder<T,
T appendScript(String script); T appendScript(String script);
Y build() throws IOException; Y build() throws IOException, InterruptedException, FileOperateException;
} }

3
dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/main/java/org/apache/dolphinscheduler/plugin/task/api/shell/bash/BashShellInterceptorBuilder.java

@ -17,6 +17,7 @@
package org.apache.dolphinscheduler.plugin.task.api.shell.bash; package org.apache.dolphinscheduler.plugin.task.api.shell.bash;
import org.apache.dolphinscheduler.common.exception.FileOperateException;
import org.apache.dolphinscheduler.plugin.task.api.shell.BaseLinuxShellInterceptorBuilder; import org.apache.dolphinscheduler.plugin.task.api.shell.BaseLinuxShellInterceptorBuilder;
import java.io.IOException; import java.io.IOException;
@ -32,7 +33,7 @@ public class BashShellInterceptorBuilder
} }
@Override @Override
public BashShellInterceptor build() throws IOException { public BashShellInterceptor build() throws FileOperateException, IOException {
generateShellScript(); generateShellScript();
List<String> bootstrapCommand = generateBootstrapCommand(); List<String> bootstrapCommand = generateBootstrapCommand();
return new BashShellInterceptor(bootstrapCommand, shellDirectory); return new BashShellInterceptor(bootstrapCommand, shellDirectory);

3
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; 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 org.apache.dolphinscheduler.plugin.task.api.shell.BaseLinuxShellInterceptorBuilder;
import java.io.IOException; import java.io.IOException;
@ -32,7 +33,7 @@ public class ShShellInterceptorBuilder
} }
@Override @Override
public ShShellInterceptor build() throws IOException { public ShShellInterceptor build() throws IOException, FileOperateException {
generateShellScript(); generateShellScript();
List<String> bootstrapCommand = generateBootstrapCommand(); List<String> bootstrapCommand = generateBootstrapCommand();
return new ShShellInterceptor(bootstrapCommand, shellDirectory); return new ShShellInterceptor(bootstrapCommand, shellDirectory);

9
dolphinscheduler-worker/src/main/java/org/apache/dolphinscheduler/server/worker/utils/TaskExecutionCheckerUtils.java

@ -92,10 +92,6 @@ public class TaskExecutionCheckerUtils {
taskExecutionContext.setAppInfoPath(FileUtils.getAppInfoPath(execLocalPath)); taskExecutionContext.setAppInfoPath(FileUtils.getAppInfoPath(execLocalPath));
Path executePath = Paths.get(taskExecutionContext.getExecutePath()); Path executePath = Paths.get(taskExecutionContext.getExecutePath());
FileUtils.createDirectoryIfNotPresent(executePath); FileUtils.createDirectoryIfNotPresent(executePath);
if (OSUtils.isSudoEnable()
&& !TenantConstants.DEFAULT_TENANT_CODE.equals(taskExecutionContext.getTenantCode())) {
FileUtils.setDirectoryOwner(executePath, taskExecutionContext.getTenantCode());
}
} catch (Throwable ex) { } catch (Throwable ex) {
throw new TaskException("Cannot create process execute dir", ex); throw new TaskException("Cannot create process execute dir", ex);
} }
@ -136,11 +132,6 @@ public class TaskExecutionCheckerUtils {
Path localFileAbsolutePath = Paths.get(execLocalPath, fileName); Path localFileAbsolutePath = Paths.get(execLocalPath, fileName);
storageOperate.download(actualTenant, fullName, localFileAbsolutePath.toString(), true); storageOperate.download(actualTenant, fullName, localFileAbsolutePath.toString(), true);
log.info("Download resource file {} under: {} successfully", fileName, localFileAbsolutePath); 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 WorkerServerMetrics
.recordWorkerResourceDownloadTime(System.currentTimeMillis() - resourceDownloadStartTime); .recordWorkerResourceDownloadTime(System.currentTimeMillis() - resourceDownloadStartTime);
WorkerServerMetrics.recordWorkerResourceDownloadSize(Files.size(localFileAbsolutePath)); WorkerServerMetrics.recordWorkerResourceDownloadSize(Files.size(localFileAbsolutePath));

Loading…
Cancel
Save