public class ShellCommandUtil extends Object
| Modifier and Type | Class and Description |
|---|---|
static interface |
ShellCommandUtil.InteractiveHandler
InteractiveHandler is a handler for interactive sessions with command line commands.
|
static class |
ShellCommandUtil.Result |
| Modifier and Type | Field and Description |
|---|---|
static boolean |
LINUX
Set to true when run on Linux platforms
|
static boolean |
MAC
Set to true when run on Mac OS platforms
|
static String |
MASK_EVERYBODY_RWX
Permission mask 777 allows everybody to read/modify/execute file
|
static String |
MASK_OWNER_ONLY_RW
Permission mask 600 allows only owner to read and modify file.
|
static String |
MASK_OWNER_ONLY_RWX
Permission mask 700 allows only owner to read, modify and execute file.
|
static boolean |
UNIX_LIKE
Set to true when run if platform is detected to be UNIX compatible
|
static boolean |
WINDOWS
Set to true when run on Windows platforms
|
| Constructor and Description |
|---|
ShellCommandUtil() |
| Modifier and Type | Method and Description |
|---|---|
static ShellCommandUtil.Result |
copyFile(String srcFile,
String destFile,
boolean force,
boolean sudo)
Copies a source file to the specified destination.
|
static ShellCommandUtil.Result |
delete(String file,
boolean force,
boolean sudo)
Deletes the
file. |
static String |
getOpenSslCommandResult(String command,
int exitCode) |
static String |
getUnixFilePermissions(String path)
Gets file permissions on Linux systems.
|
static String |
hideOpenSslPassword(String command) |
static void |
logOpenSslExitCode(String command,
int exitCode) |
static ShellCommandUtil.Result |
mkdir(String directoryPath,
boolean sudo)
Creates the specified directory and any directories in the path
|
static ShellCommandUtil.Result |
pathExists(String path,
boolean sudo)
Test if a file or directory exists
|
static ShellCommandUtil.Result |
runCommand(List<String> args,
Map<String,String> vars,
ShellCommandUtil.InteractiveHandler interactiveHandler,
boolean sudo) |
static ShellCommandUtil.Result |
runCommand(String[] args)
Run a command
|
static ShellCommandUtil.Result |
runCommand(String[] args,
Map<String,String> vars)
Runs a command with a given set of environment variables
|
static ShellCommandUtil.Result |
runCommand(String[] args,
Map<String,String> vars,
ShellCommandUtil.InteractiveHandler interactiveHandler,
boolean sudo)
Runs a command with a given set of environment variables
|
static ShellCommandUtil.Result |
setFileGroup(String path,
String groupName)
Sets the group for a file.
|
static ShellCommandUtil.Result |
setFileMode(String path,
boolean ownerReadable,
boolean ownerWritable,
boolean ownerExecutable,
boolean groupReadable,
boolean groupWritable,
boolean groupExecutable,
boolean otherReadable,
boolean otherWritable,
boolean otherExecutable)
Set the access modes for a file
|
static ShellCommandUtil.Result |
setFileOwner(String path,
String ownerName)
Sets the owner for a file.
|
static void |
setUnixFilePermissions(String mode,
String path)
Sets file permissions to a given value on Linux systems.
|
public static final boolean WINDOWS
public static final boolean LINUX
public static final boolean MAC
public static final boolean UNIX_LIKE
public static final String MASK_OWNER_ONLY_RW
public static final String MASK_OWNER_ONLY_RWX
public static final String MASK_EVERYBODY_RWX
public static void logOpenSslExitCode(String command, int exitCode)
public static String getUnixFilePermissions(String path)
path - public static void setUnixFilePermissions(String mode, String path)
mode - path - public static ShellCommandUtil.Result setFileOwner(String path, String ownerName)
path - the path to the fileownerName - the owner's local usernamepublic static ShellCommandUtil.Result setFileGroup(String path, String groupName)
path - the path to the filegroupName - the group namepublic static ShellCommandUtil.Result setFileMode(String path, boolean ownerReadable, boolean ownerWritable, boolean ownerExecutable, boolean groupReadable, boolean groupWritable, boolean groupExecutable, boolean otherReadable, boolean otherWritable, boolean otherExecutable)
path - the path to the fileownerWritable - true if the owner should be able to write to this file; otherwise falseownerReadable - true if the owner should be able to read this file; otherwise falseownerExecutable - true if the owner should be able to execute this file; otherwise falsegroupWritable - true if the group should be able to write to this file; otherwise falsegroupReadable - true if the group should be able to read this file; otherwise falsegroupExecutable - true if the group should be able to execute this file; otherwise falseotherReadable - true if other users should be able to read this file; otherwise falseotherWritable - true if other users should be able to write to this file; otherwise falseotherExecutable - true if other users should be able to execute this file; otherwise falsepublic static ShellCommandUtil.Result pathExists(String path, boolean sudo) throws IOException, InterruptedException
path - the path to testsudo - true to execute the command using sudo (ambari-sudo); otherwise falseIOExceptionInterruptedExceptionpublic static ShellCommandUtil.Result mkdir(String directoryPath, boolean sudo) throws IOException, InterruptedException
directoryPath - the directory to createsudo - true to execute the command using sudo (ambari-sudo); otherwise falseIOExceptionInterruptedExceptionpublic static ShellCommandUtil.Result copyFile(String srcFile, String destFile, boolean force, boolean sudo) throws IOException, InterruptedException
srcFile - the path to the source filedestFile - the path to the destination fileforce - true to force copy even if the file existssudo - true to execute the command using sudo (ambari-sudo); otherwise falseIOExceptionInterruptedExceptionpublic static ShellCommandUtil.Result delete(String file, boolean force, boolean sudo) throws IOException, InterruptedException
file.file - the path to the file to be deletedforce - true to force copy even if the file existssudo - true to execute the command using sudo (ambari-sudo); otherwise falseIOExceptionInterruptedExceptionpublic static ShellCommandUtil.Result runCommand(List<String> args, Map<String,String> vars, ShellCommandUtil.InteractiveHandler interactiveHandler, boolean sudo) throws IOException, InterruptedException
public static ShellCommandUtil.Result runCommand(String[] args, Map<String,String> vars, ShellCommandUtil.InteractiveHandler interactiveHandler, boolean sudo) throws IOException, InterruptedException
args - a String[] of the command and its argumentsvars - a Map of String,String setting an environment variable to run the command withinteractiveHandler - a handler to provide responses to queries from the command,
or null if no queries are expectedsudo - true to execute the command using sudo (ambari-sudo); otherwise falseIOExceptionInterruptedExceptionpublic static ShellCommandUtil.Result runCommand(String[] args, Map<String,String> vars) throws IOException, InterruptedException
args - a String[] of the command and its argumentsvars - a Map of String,String setting an environment variable to run the command withIOExceptionInterruptedExceptionpublic static ShellCommandUtil.Result runCommand(String[] args) throws IOException, InterruptedException
args - A String[] of the command and its argumentsIOExceptionInterruptedExceptionCopyright © 2022 Apache Software Foundation. All rights reserved.