Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[HotFix] Set the tenant as the owner in final stage #15256

Merged
merged 1 commit into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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");

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -64,8 +65,10 @@ protected void generateShellScript() throws IOException {
log.info("Final Shell file is : \n{}", finalScript);
}

protected List<String> generateBootstrapCommand() {
protected List<String> generateBootstrapCommand() throws FileOperateException {
if (sudoEnable) {
// Set the tenant owner as the working directory
FileUtils.setDirectoryOwner(Paths.get(shellDirectory), runUser);
return bootstrapCommandInSudoMode();
}
return bootstrapCommandInNormalMode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -48,5 +50,5 @@ public interface IShellInterceptorBuilder<T extends IShellInterceptorBuilder<T,

T appendScript(String script);

Y build() throws IOException;
Y build() throws IOException, InterruptedException, FileOperateException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

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 java.io.IOException;
Expand All @@ -32,7 +33,7 @@ public BashShellInterceptorBuilder newBuilder() {
}

@Override
public BashShellInterceptor build() throws IOException {
public BashShellInterceptor build() throws FileOperateException, IOException {
generateShellScript();
List<String> bootstrapCommand = generateBootstrapCommand();
return new BashShellInterceptor(bootstrapCommand, shellDirectory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,7 +33,7 @@ public ShShellInterceptorBuilder newBuilder() {
}

@Override
public ShShellInterceptor build() throws IOException {
public ShShellInterceptor build() throws IOException, FileOperateException {
generateShellScript();
List<String> bootstrapCommand = generateBootstrapCommand();
return new ShShellInterceptor(bootstrapCommand, shellDirectory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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));
Expand Down
Loading