@ -257,6 +257,23 @@ public class OSUtils {
return users ;
}
/ * *
* create user
*
* @param userName user name
* /
public static void createUserIfAbsent ( String userName ) {
// if not exists this user, then create
taskLoggerThreadLocal . set ( taskLoggerThreadLocal . get ( ) ) ;
if ( ! getUserList ( ) . contains ( userName ) ) {
boolean isSuccess = createUser ( userName ) ;
String infoLog = String . format ( "create user %s %s" , userName , isSuccess ? "success" : "fail" ) ;
LoggerUtils . logInfo ( Optional . ofNullable ( logger ) , infoLog ) ;
LoggerUtils . logInfo ( Optional . ofNullable ( taskLoggerThreadLocal . get ( ) ) , infoLog ) ;
}
taskLoggerThreadLocal . remove ( ) ;
}
/ * *
* create user
*
@ -265,7 +282,7 @@ public class OSUtils {
* /
public static boolean createUser ( String userName ) {
try {
String userGroup = OSUtils . getGroup ( ) ;
String userGroup = getGroup ( ) ;
if ( StringUtils . isEmpty ( userGroup ) ) {
String errorLog = String . format ( "%s group does not exist for this operating system." , userGroup ) ;
LoggerUtils . logError ( Optional . ofNullable ( logger ) , errorLog ) ;
@ -304,7 +321,7 @@ public class OSUtils {
String infoLog2 = String . format ( "execute cmd : %s" , cmd ) ;
LoggerUtils . logInfo ( Optional . ofNullable ( logger ) , infoLog2 ) ;
LoggerUtils . logInfo ( Optional . ofNullable ( taskLoggerThreadLocal . get ( ) ) , infoLog2 ) ;
OSUtils . exeCmd ( cmd ) ;
exeCmd ( cmd ) ;
}
/ * *
@ -315,7 +332,6 @@ public class OSUtils {
* @throws IOException in case of an I / O error
* /
private static void createMacUser ( String userName , String userGroup ) throws IOException {
Optional < Logger > optionalLogger = Optional . ofNullable ( logger ) ;
Optional < Logger > optionalTaskLogger = Optional . ofNullable ( taskLoggerThreadLocal . get ( ) ) ;
@ -327,13 +343,13 @@ public class OSUtils {
String infoLog2 = String . format ( "create user command : %s" , createUserCmd ) ;
LoggerUtils . logInfo ( optionalLogger , infoLog2 ) ;
LoggerUtils . logInfo ( optionalTaskLogger , infoLog2 ) ;
OSUtils . exeCmd ( createUserCmd ) ;
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 ) ;
exeCmd ( appendGroupCmd ) ;
}
/ * *
@ -352,13 +368,13 @@ public class OSUtils {
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 ) ;
exeCmd ( userCreateCmd ) ;
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 ) ;
exeCmd ( appendGroupCmd ) ;
}
/ * *
@ -472,9 +488,9 @@ public class OSUtils {
* /
public static Boolean checkResource ( double systemCpuLoad , double systemReservedMemory ) {
// system load average
double loadAverage = OSUtils . loadAverage ( ) ;
double loadAverage = loadAverage ( ) ;
// system available physical memory
double availablePhysicalMemorySize = OSUtils . availablePhysicalMemorySize ( ) ;
double availablePhysicalMemorySize = availablePhysicalMemorySize ( ) ;
if ( loadAverage > systemCpuLoad | | availablePhysicalMemorySize < systemReservedMemory ) {
logger . warn ( "load is too high or availablePhysicalMemorySize(G) is too low, it's availablePhysicalMemorySize(G):{},loadAvg:{}" , availablePhysicalMemorySize , loadAverage ) ;