Change command file permission to 755 (#12678)

(cherry picked from commit 95484d930762712f4ab68ece27ca3b65a2073ab2)
This commit is contained in:
Wenjun Ruan 2022-11-03 22:51:37 +08:00 committed by GitHub
parent fdb02d486c
commit eaef68856b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 93 additions and 57 deletions

View File

@ -17,21 +17,21 @@
package org.apache.dolphinscheduler.plugin.task.api;
import org.apache.commons.io.FileUtils;
import org.apache.dolphinscheduler.plugin.task.api.utils.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.SystemUtils;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.function.Consumer;
import org.slf4j.Logger;
import com.google.common.base.Strings;
/**
* shell command executor
*/
@ -80,42 +80,45 @@ public class ShellCommandExecutor extends AbstractCommandExecutor {
*/
@Override
protected void createCommandFileIfNotExists(String execCommand, String commandFile) throws IOException {
logger.info("tenantCode user:{}, task dir:{}", taskRequest.getTenantCode(),
taskRequest.getTaskAppId());
// create if non existence
if (!Files.exists(Paths.get(commandFile))) {
logger.info("create command file:{}", commandFile);
logger.info("Begin to create command file:{}", commandFile);
StringBuilder sb = new StringBuilder();
if (SystemUtils.IS_OS_WINDOWS) {
sb.append("@echo off\n");
sb.append("cd /d %~dp0\n");
if (!Strings.isNullOrEmpty(taskRequest.getEnvironmentConfig())) {
sb.append(taskRequest.getEnvironmentConfig()).append("\n");
} else {
if (taskRequest.getEnvFile() != null) {
sb.append("call ").append(taskRequest.getEnvFile()).append("\n");
}
}
Path commandFilePath = Paths.get(commandFile);
if (Files.exists(commandFilePath)) {
logger.warn("The command file: {} is already exist, will not create a again", commandFile);
return;
}
StringBuilder sb = new StringBuilder();
if (SystemUtils.IS_OS_WINDOWS) {
sb.append("@echo off\n");
sb.append("cd /d %~dp0\n");
if (StringUtils.isNotBlank(taskRequest.getEnvironmentConfig())) {
sb.append(taskRequest.getEnvironmentConfig()).append("\n");
} else {
sb.append("#!/bin/bash\n");
sb.append("BASEDIR=$(cd `dirname $0`; pwd)\n");
sb.append("cd $BASEDIR\n");
if (!Strings.isNullOrEmpty(taskRequest.getEnvironmentConfig())) {
sb.append(taskRequest.getEnvironmentConfig()).append("\n");
} else {
if (taskRequest.getEnvFile() != null) {
sb.append("source ").append(taskRequest.getEnvFile()).append("\n");
}
if (taskRequest.getEnvFile() != null) {
sb.append("call ").append(taskRequest.getEnvFile()).append("\n");
}
}
} else {
sb.append("#!/bin/sh\n");
sb.append("BASEDIR=$(cd `dirname $0`; pwd)\n");
sb.append("cd $BASEDIR\n");
if (StringUtils.isNotBlank(taskRequest.getEnvironmentConfig())) {
sb.append(taskRequest.getEnvironmentConfig()).append("\n");
} else {
if (taskRequest.getEnvFile() != null) {
sb.append("source ").append(taskRequest.getEnvFile()).append("\n");
}
}
sb.append(execCommand);
logger.info("command : {}", sb);
// write data to file
FileUtils.writeStringToFile(new File(commandFile), sb.toString(), StandardCharsets.UTF_8);
}
sb.append(execCommand);
String commandContent = sb.toString();
FileUtils.createFileWith755(commandFilePath);
Files.write(commandFilePath, commandContent.getBytes(), StandardOpenOption.APPEND);
logger.info("Success create command file, command: {}", commandContent);
}
@Override

View File

@ -0,0 +1,51 @@
/*
* 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.plugin.task.api.utils;
import static org.apache.dolphinscheduler.plugin.task.api.TaskConstants.RWXR_XR_X;
import org.apache.commons.lang3.SystemUtils;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.FileAttribute;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions;
import java.util.Set;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class FileUtils {
private static final FileAttribute<Set<PosixFilePermission>> PERMISSION_755 =
PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString(RWXR_XR_X));
/**
* Create a file with '755'.
*/
public static void createFileWith755(@NonNull Path path) throws IOException {
if (SystemUtils.IS_OS_WINDOWS) {
Files.createFile(path);
} else {
Files.createFile(path, PERMISSION_755);
}
}
}

View File

@ -18,7 +18,6 @@
package org.apache.dolphinscheduler.plugin.task.shell;
import static org.apache.dolphinscheduler.plugin.task.api.TaskConstants.EXIT_CODE_FAILURE;
import static org.apache.dolphinscheduler.plugin.task.api.TaskConstants.RWXR_XR_X;
import org.apache.dolphinscheduler.common.utils.JSONUtils;
import org.apache.dolphinscheduler.plugin.task.api.AbstractTask;
@ -31,19 +30,15 @@ import org.apache.dolphinscheduler.plugin.task.api.model.TaskResponse;
import org.apache.dolphinscheduler.plugin.task.api.parameters.AbstractParameters;
import org.apache.dolphinscheduler.plugin.task.api.parser.ParamUtils;
import org.apache.dolphinscheduler.plugin.task.api.parser.ParameterUtils;
import org.apache.dolphinscheduler.plugin.task.api.utils.FileUtils;
import org.apache.commons.lang3.SystemUtils;
import java.io.File;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.nio.file.attribute.FileAttribute;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions;
import java.util.Map;
import java.util.Set;
/**
* shell task
@ -137,6 +132,8 @@ public class ShellTask extends AbstractTask {
Path path = file.toPath();
if (Files.exists(path)) {
// this shouldn't happen
logger.warn("The command file: {} is already exist", path);
return fileName;
}
@ -147,22 +144,7 @@ public class ShellTask extends AbstractTask {
logger.info("raw script : {}", shellParameters.getRawScript());
logger.info("task execute path : {}", taskExecutionContext.getExecutePath());
Set<PosixFilePermission> perms = PosixFilePermissions.fromString(RWXR_XR_X);
FileAttribute<Set<PosixFilePermission>> attr = PosixFilePermissions.asFileAttribute(perms);
if (SystemUtils.IS_OS_WINDOWS) {
Files.createFile(path);
} else {
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
try {
Files.createFile(path, attr);
} catch (FileAlreadyExistsException ex) {
// this is expected
}
}
FileUtils.createFileWith755(path);
Files.write(path, shellParameters.getRawScript().getBytes(), StandardOpenOption.APPEND);
return fileName;