|
|
|
@ -16,16 +16,6 @@
|
|
|
|
|
*/ |
|
|
|
|
package org.apache.dolphinscheduler.common.utils; |
|
|
|
|
|
|
|
|
|
import org.apache.dolphinscheduler.common.Constants; |
|
|
|
|
import org.apache.dolphinscheduler.common.shell.ShellExecutor; |
|
|
|
|
import org.apache.commons.configuration.Configuration; |
|
|
|
|
import org.slf4j.Logger; |
|
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
|
import oshi.SystemInfo; |
|
|
|
|
import oshi.hardware.CentralProcessor; |
|
|
|
|
import oshi.hardware.GlobalMemory; |
|
|
|
|
import oshi.hardware.HardwareAbstractionLayer; |
|
|
|
|
|
|
|
|
|
import java.io.BufferedReader; |
|
|
|
|
import java.io.FileInputStream; |
|
|
|
|
import java.io.IOException; |
|
|
|
@ -40,8 +30,21 @@ import java.util.ArrayList;
|
|
|
|
|
import java.util.Arrays; |
|
|
|
|
import java.util.Collections; |
|
|
|
|
import java.util.List; |
|
|
|
|
import java.util.Optional; |
|
|
|
|
import java.util.StringTokenizer; |
|
|
|
|
import java.util.regex.Pattern; |
|
|
|
|
|
|
|
|
|
import org.apache.commons.configuration.Configuration; |
|
|
|
|
import org.apache.dolphinscheduler.common.Constants; |
|
|
|
|
import org.apache.dolphinscheduler.common.shell.ShellExecutor; |
|
|
|
|
import org.slf4j.Logger; |
|
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
|
|
|
|
|
|
import oshi.SystemInfo; |
|
|
|
|
import oshi.hardware.CentralProcessor; |
|
|
|
|
import oshi.hardware.GlobalMemory; |
|
|
|
|
import oshi.hardware.HardwareAbstractionLayer; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* os utils |
|
|
|
|
* |
|
|
|
@ -50,6 +53,8 @@ public class OSUtils {
|
|
|
|
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(OSUtils.class); |
|
|
|
|
|
|
|
|
|
public static final ThreadLocal<Logger> taskLoggerThreadLocal = new ThreadLocal<>(); |
|
|
|
|
|
|
|
|
|
private static final SystemInfo SI = new SystemInfo(); |
|
|
|
|
public static final String TWO_DECIMAL = "0.00"; |
|
|
|
|
|
|
|
|
@ -251,7 +256,9 @@ public class OSUtils {
|
|
|
|
|
try { |
|
|
|
|
String userGroup = OSUtils.getGroup(); |
|
|
|
|
if (StringUtils.isEmpty(userGroup)) { |
|
|
|
|
logger.error("{} group does not exist for this operating system.", userGroup); |
|
|
|
|
String errorLog = String.format("%s group does not exist for this operating system.", userGroup); |
|
|
|
|
LoggerUtils.logError(Optional.ofNullable(logger), errorLog); |
|
|
|
|
LoggerUtils.logError(Optional.ofNullable(taskLoggerThreadLocal.get()), errorLog); |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
if (isMacOS()) { |
|
|
|
@ -263,7 +270,8 @@ public class OSUtils {
|
|
|
|
|
} |
|
|
|
|
return true; |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
logger.error(e.getMessage(), e); |
|
|
|
|
LoggerUtils.logError(Optional.ofNullable(logger), e); |
|
|
|
|
LoggerUtils.logError(Optional.ofNullable(taskLoggerThreadLocal.get()), e); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return false; |
|
|
|
@ -276,10 +284,14 @@ public class OSUtils {
|
|
|
|
|
* @throws IOException in case of an I/O error |
|
|
|
|
*/ |
|
|
|
|
private static void createLinuxUser(String userName, String userGroup) throws IOException { |
|
|
|
|
logger.info("create linux os user : {}", userName); |
|
|
|
|
String cmd = String.format("sudo useradd -g %s %s", userGroup, userName); |
|
|
|
|
String infoLog1 = String.format("create linux os user : %s", userName); |
|
|
|
|
LoggerUtils.logInfo(Optional.ofNullable(logger), infoLog1); |
|
|
|
|
LoggerUtils.logInfo(Optional.ofNullable(taskLoggerThreadLocal.get()), infoLog1); |
|
|
|
|
|
|
|
|
|
logger.info("execute cmd : {}", cmd); |
|
|
|
|
String cmd = String.format("sudo useradd -g %s %s", userGroup, userName); |
|
|
|
|
String infoLog2 = String.format("execute cmd : %s", cmd); |
|
|
|
|
LoggerUtils.logInfo(Optional.ofNullable(logger), infoLog2); |
|
|
|
|
LoggerUtils.logInfo(Optional.ofNullable(taskLoggerThreadLocal.get()), infoLog2); |
|
|
|
|
OSUtils.exeCmd(cmd); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -290,13 +302,24 @@ public class OSUtils {
|
|
|
|
|
* @throws IOException in case of an I/O error |
|
|
|
|
*/ |
|
|
|
|
private static void createMacUser(String userName, String userGroup) throws IOException { |
|
|
|
|
logger.info("create mac os user : {}", userName); |
|
|
|
|
String userCreateCmd = String.format("sudo sysadminctl -addUser %s -password %s", userName, userName); |
|
|
|
|
String appendGroupCmd = String.format("sudo dseditgroup -o edit -a %s -t user %s", userName, userGroup); |
|
|
|
|
|
|
|
|
|
logger.info("create user command : {}", userCreateCmd); |
|
|
|
|
OSUtils.exeCmd(userCreateCmd); |
|
|
|
|
logger.info("append user to group : {}", appendGroupCmd); |
|
|
|
|
Optional<Logger> optionalLogger = Optional.ofNullable(logger); |
|
|
|
|
Optional<Logger> optionalTaskLogger = Optional.ofNullable(taskLoggerThreadLocal.get()); |
|
|
|
|
|
|
|
|
|
String infoLog1 = String.format("create mac os user : %s", userName); |
|
|
|
|
LoggerUtils.logInfo(optionalLogger, infoLog1); |
|
|
|
|
LoggerUtils.logInfo(optionalTaskLogger, infoLog1); |
|
|
|
|
|
|
|
|
|
String createUserCmd = String.format("sudo sysadminctl -addUser %s -password %s", userName, userName); |
|
|
|
|
String infoLog2 = String.format("create user command : %s", createUserCmd); |
|
|
|
|
LoggerUtils.logInfo(optionalLogger, infoLog2); |
|
|
|
|
LoggerUtils.logInfo(optionalTaskLogger, infoLog2); |
|
|
|
|
OSUtils.exeCmd(createUserCmd); |
|
|
|
|
|
|
|
|
|
String appendGroupCmd = String.format("sudo dseditgroup -o edit -a %s -t user %s", userName, userGroup); |
|
|
|
|
String infoLog3 = String.format("append user to group : %s", appendGroupCmd); |
|
|
|
|
LoggerUtils.logInfo(optionalLogger, infoLog3); |
|
|
|
|
LoggerUtils.logInfo(optionalTaskLogger, infoLog3); |
|
|
|
|
OSUtils.exeCmd(appendGroupCmd); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -307,14 +330,20 @@ public class OSUtils {
|
|
|
|
|
* @throws IOException in case of an I/O error |
|
|
|
|
*/ |
|
|
|
|
private static void createWindowsUser(String userName, String userGroup) throws IOException { |
|
|
|
|
logger.info("create windows os user : {}", userName); |
|
|
|
|
String userCreateCmd = String.format("net user \"%s\" /add", userName); |
|
|
|
|
String appendGroupCmd = String.format("net localgroup \"%s\" \"%s\" /add", userGroup, userName); |
|
|
|
|
String infoLog1 = String.format("create windows os user : %s", userName); |
|
|
|
|
LoggerUtils.logInfo(Optional.ofNullable(logger), infoLog1); |
|
|
|
|
LoggerUtils.logInfo(Optional.ofNullable(taskLoggerThreadLocal.get()), infoLog1); |
|
|
|
|
|
|
|
|
|
logger.info("execute create user command : {}", userCreateCmd); |
|
|
|
|
String userCreateCmd = String.format("net user \"%s\" /add", userName); |
|
|
|
|
String infoLog2 = String.format("execute create user command : %s", userCreateCmd); |
|
|
|
|
LoggerUtils.logInfo(Optional.ofNullable(logger), infoLog2); |
|
|
|
|
LoggerUtils.logInfo(Optional.ofNullable(taskLoggerThreadLocal.get()), infoLog2); |
|
|
|
|
OSUtils.exeCmd(userCreateCmd); |
|
|
|
|
|
|
|
|
|
logger.info("execute append user to group : {}", appendGroupCmd); |
|
|
|
|
String appendGroupCmd = String.format("net localgroup \"%s\" \"%s\" /add", userGroup, userName); |
|
|
|
|
String infoLog3 = String.format("execute append user to group : %s", appendGroupCmd); |
|
|
|
|
LoggerUtils.logInfo(Optional.ofNullable(logger), infoLog3); |
|
|
|
|
LoggerUtils.logInfo(Optional.ofNullable(taskLoggerThreadLocal.get()), infoLog3); |
|
|
|
|
OSUtils.exeCmd(appendGroupCmd); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -353,22 +382,12 @@ public class OSUtils {
|
|
|
|
|
* @throws IOException errors |
|
|
|
|
*/ |
|
|
|
|
public static String exeCmd(String command) throws IOException { |
|
|
|
|
BufferedReader br = null; |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
Process p = Runtime.getRuntime().exec(command); |
|
|
|
|
br = new BufferedReader(new InputStreamReader(p.getInputStream())); |
|
|
|
|
String line; |
|
|
|
|
StringBuilder sb = new StringBuilder(); |
|
|
|
|
|
|
|
|
|
while ((line = br.readLine()) != null) { |
|
|
|
|
sb.append(line + "\n"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return sb.toString(); |
|
|
|
|
} finally { |
|
|
|
|
IOUtils.closeQuietly(br); |
|
|
|
|
StringTokenizer st = new StringTokenizer(command); |
|
|
|
|
String[] cmdArray = new String[st.countTokens()]; |
|
|
|
|
for (int i = 0; st.hasMoreTokens(); i++) { |
|
|
|
|
cmdArray[i] = st.nextToken(); |
|
|
|
|
} |
|
|
|
|
return exeShell(cmdArray); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -377,7 +396,7 @@ public class OSUtils {
|
|
|
|
|
* @return result of execute the shell |
|
|
|
|
* @throws IOException errors |
|
|
|
|
*/ |
|
|
|
|
public static String exeShell(String command) throws IOException { |
|
|
|
|
public static String exeShell(String[] command) throws IOException { |
|
|
|
|
return ShellExecutor.execCommand(command); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|