diff --git a/src/main/java/com/xebialabs/overthere/ssh/SshConnection.java b/src/main/java/com/xebialabs/overthere/ssh/SshConnection.java index d4c6166b..5f41803f 100644 --- a/src/main/java/com/xebialabs/overthere/ssh/SshConnection.java +++ b/src/main/java/com/xebialabs/overthere/ssh/SshConnection.java @@ -62,15 +62,7 @@ import static com.xebialabs.overthere.ConnectionOptions.PASSWORD; import static com.xebialabs.overthere.ConnectionOptions.PORT; import static com.xebialabs.overthere.ConnectionOptions.USERNAME; -import static com.xebialabs.overthere.ssh.SshConnectionBuilder.ALLOCATE_DEFAULT_PTY; -import static com.xebialabs.overthere.ssh.SshConnectionBuilder.ALLOCATE_DEFAULT_PTY_DEFAULT; -import static com.xebialabs.overthere.ssh.SshConnectionBuilder.ALLOCATE_PTY; -import static com.xebialabs.overthere.ssh.SshConnectionBuilder.CONNECTION_TYPE; -import static com.xebialabs.overthere.ssh.SshConnectionBuilder.INTERACTIVE_KEYBOARD_AUTH_PROMPT_REGEX; -import static com.xebialabs.overthere.ssh.SshConnectionBuilder.INTERACTIVE_KEYBOARD_AUTH_PROMPT_REGEX_DEFAULT; -import static com.xebialabs.overthere.ssh.SshConnectionBuilder.PASSPHRASE; -import static com.xebialabs.overthere.ssh.SshConnectionBuilder.PRIVATE_KEY_FILE; -import static com.xebialabs.overthere.ssh.SshConnectionBuilder.SSH_PORT_DEFAULT; +import static com.xebialabs.overthere.ssh.SshConnectionBuilder.*; import static java.net.InetSocketAddress.createUnresolved; /** @@ -100,6 +92,8 @@ abstract class SshConnection extends BaseOverthereConnection { protected final boolean allocateDefaultPty; + protected final boolean openShellBeforeExecute; + protected String allocatePty; protected SSHClient sshClient; @@ -129,6 +123,7 @@ public SshConnection(final String protocol, final ConnectionOptions options, fin logger.warn("The " + ALLOCATE_DEFAULT_PTY + " connection option has been deprecated in favour of the " + ALLOCATE_PTY + " option. See https://github.com/xebialabs/overthere#ssh_allocatePty"); } this.allocatePty = options.getOptional(ALLOCATE_PTY); + this.openShellBeforeExecute = options.getBoolean(OPEN_SHELL_BEFORE_EXECUTE, OPEN_SHELL_BEFORE_EXECUTE_DEFAULT); } protected void connect() { @@ -238,6 +233,12 @@ public OverthereProcess startProcess(final CmdLine commandLine) { CmdLine cmd = processCommandLine(commandLine); try { + if (openShellBeforeExecute) { + logger.debug("Creating a temporary shell to allow for deferred home dir creation."); + Session.Shell shell = getSshClient().startSession().startShell(); + shell.close(); + } + Session session = getSshClient().startSession(); if (allocatePty != null && !allocatePty.isEmpty()) { if (allocateDefaultPty) { diff --git a/src/main/java/com/xebialabs/overthere/ssh/SshConnectionBuilder.java b/src/main/java/com/xebialabs/overthere/ssh/SshConnectionBuilder.java index e1a71c96..19c20081 100644 --- a/src/main/java/com/xebialabs/overthere/ssh/SshConnectionBuilder.java +++ b/src/main/java/com/xebialabs/overthere/ssh/SshConnectionBuilder.java @@ -192,6 +192,19 @@ public class SshConnectionBuilder implements OverthereConnectionBuilder { */ public static final boolean SUDO_QUOTE_COMMAND_DEFAULT = false; + /** + * Name of the {@link ConnectionOptions connection option} used to specify whether or not to open a shell directly + * before executing a remote command. This may be necessary in case a user does not yet have a home directory on a + * machine, and this is created for him when he opens a shell. + */ + public static final String OPEN_SHELL_BEFORE_EXECUTE = "openShellBeforeExecute"; + + /** + * Default value (false) of the {@link ConnectionOptions connection option} used to specify whether or + * not to open a shell directory before executing a command. + */ + public static final boolean OPEN_SHELL_BEFORE_EXECUTE_DEFAULT = false; + protected SshConnection connection; public SshConnectionBuilder(String type, ConnectionOptions options, AddressPortMapper mapper) {