Skip to content

Commit

Permalink
Issue #232: Linux connector cli not functioning locally
Browse files Browse the repository at this point in the history
* Optimized Shell command retrieval
  • Loading branch information
NassimBtk committed Jun 12, 2024
1 parent 14130e5 commit 20a0285
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 12 deletions.
7 changes: 1 addition & 6 deletions .github/workflows/maven-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ jobs:

steps:

- name: Setup tmate SSH session
uses: mxschmitt/action-tmate@v3
with:
detached: true

- name: Install gcc-multilib
if: ${{ matrix.os == 'ubuntu-latest' }}
run: |
Expand All @@ -52,7 +47,7 @@ jobs:
sonatypeSnapshots: true

- name: Build with Maven on ${{ matrix.os }}
run: mvn -X -B -U verify --file pom.xml
run: mvn -B -U verify --file pom.xml
env:
GITHUB_TOKEN: ${{ github.token }}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ public class OsCommandService {

private static final String NEGATIVE_TIMEOUT = "timeout mustn't be negative nor zero.";

private static final String[] LOCAL_SHELL_COMMAND = buildShellCommand();

/**
* Run the given command on the localhost machine.
*
Expand Down Expand Up @@ -153,13 +155,67 @@ public static String runLocalCommand(
* @return The process builder for the given command.
*/
static ProcessBuilder createProcessBuilder(final String command) {
final ProcessBuilder builder = new ProcessBuilder();
return new ProcessBuilder().command(LOCAL_SHELL_COMMAND[0], LOCAL_SHELL_COMMAND[1], command);
}

/**
* Build the shell to be used for the local command execution based on the operating system.
*
* @return The shell command to be used.
*/
private static String[] buildShellCommand() {
if (LocalOsHandler.isWindows()) {
builder.command(System.getenv("ComSpec"), "/C", command);
return new String[] { getComSpecEnvVar(), "/C" };
} else {
builder.command(System.getenv("SHELL"), "-c", command);
return new String[] { getShellEnvVar(), "-c" };
}
}

/**
* Get the shell environment variable for Linux/Unix systems.
*
* @return The shell environment variable or /bin/sh if not found.
*/
private static String getShellEnvVar() {
var shell = System.getenv("SHELL");
if (shell == null || shell.isBlank()) {
// List of common shells to check
final String[] commonShells = {
"/bin/bash",
"/usr/bin/bash",
"/bin/sh",
"/usr/bin/sh",
"/bin/zsh",
"/usr/bin/zsh",
"/bin/ksh",
"/usr/bin/ksh"
};

// Find the first common shell that exists
for (String s : commonShells) {
if (new File(s).exists()) {
shell = s;
break;
}
}
// Fallback if no common shell is found
if (shell == null || shell.isBlank()) {
shell = "/bin/sh"; // Minimal fallback
}
}
return shell;
}

/**
* Get the ComSpec environment variable for Windows systems.
* @return The ComSpec environment variable or cmd.exe if not found.
*/
private static String getComSpecEnvVar() {
var comSpec = System.getenv("ComSpec");
if (comSpec == null || comSpec.isBlank()) {
comSpec = "cmd.exe";
}
return builder;
return comSpec;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
import org.mockito.junit.jupiter.MockitoExtension;
import org.sentrysoftware.metricshub.engine.common.exception.ControlledSshException;
import org.sentrysoftware.metricshub.engine.common.exception.NoCredentialProvidedException;
import org.sentrysoftware.metricshub.engine.common.helpers.LocalOsHandler;
import org.sentrysoftware.metricshub.engine.configuration.HostConfiguration;
import org.sentrysoftware.metricshub.engine.connector.model.common.DeviceKind;
import org.sentrysoftware.metricshub.engine.connector.model.common.EmbeddedFile;
Expand Down Expand Up @@ -440,7 +439,6 @@ void testCreateProcessBuilderWindows() {
void testCreateProcessBuilderLinux() {
final ProcessBuilder processBuilder = OsCommandService.createProcessBuilder(CMD);
assertNotNull(processBuilder);
System.out.println("SHELL Env: " + System.getenv("SHELL"));
final List<String> command = processBuilder.command();
assertNotNull(command.get(0));
assertEquals("-c", command.get(1));
Expand Down

0 comments on commit 20a0285

Please sign in to comment.