Brooklyn

brooklyn.location.basic
[Java] Class SshMachineLocation

java.lang.Object
  brooklyn.location.basic.AbstractLocation
      brooklyn.location.basic.SshMachineLocation
All Implemented Interfaces:
java.io.Closeable, MachineLocation, PortSupplier, WithMutexes

public class SshMachineLocation
extends AbstractLocation

Operations on a machine that is accessible via ssh.

We expose two ways of running scripts. One (execCommands) passes lines to bash, that is lightweight but fragile. Another (execScript) creates a script on the remote machine, more portable but heavier.

Additionally there are routines to copyTo, copyFrom; and installTo (which tries a curl, and falls back to copyTo in event the source is accessible by the caller only).


Nested Class Summary
protected interface SshMachineLocation.ExecRunner

 
Field Summary
static ConfigKey ALLOCATE_PTY

static java.util.Set ALL_SSH_CONFIG_KEYS

static java.util.Set ALL_SSH_CONFIG_KEY_NAMES

static ConfigKey LOCAL_TEMP_DIR

static Logger LOG

static ConfigKey LOG_PREFIX

static ConfigKey NO_STDERR_LOGGING

static ConfigKey NO_STDOUT_LOGGING

static ConfigKey PASSWORD

static ConfigKey PRIVATE_KEY_DATA

static ConfigKey PRIVATE_KEY_FILE

static ConfigKey PRIVATE_KEY_PASSPHRASE

static java.util.Set REUSABLE_SSH_PROPS

specifies config keys where a change in the value does not require a new SshTool instance, i.e. these can be specified per command on the tool

static ConfigKey SCP_EXECUTABLE

static ConfigKey SCRIPT_DIR

static java.lang.String SSHCONFIG_PREFIX

any property that should be passed as ssh config (connection-time) can be prefixed with this and . and will be passed through (with the prefix removed), e.g.

static ConfigKey SSH_ENV_MAP

static ConfigKey SSH_EXECUTABLE

static ConfigKey SSH_HOST

static ConfigKey SSH_PORT

static ConfigKey STDERR

static ConfigKey STDOUT

static Logger logSsh

protected boolean previouslyConnected

 
Fields inherited from class AbstractLocation
LOG, PARENT_LOCATION, hostGeoInfo, name
 
Constructor Summary
SshMachineLocation()

SshMachineLocation(java.util.Map properties)

 
Method Summary
void acquireMutex(java.lang.String mutexId, java.lang.String description)

void addConfig(java.util.Map vals)

@deprecated temporary Beta method introduced in 0.5.0

void close()

void configure(java.util.Map properties)

protected SshTool connectSsh()

protected SshTool connectSsh(java.util.Map props)

int copyFrom(java.lang.String remote, java.lang.String local)

int copyFrom(java.util.Map props, java.lang.String remote, java.lang.String local)

int copyTo(java.io.File src, java.io.File destination)

int copyTo(java.util.Map props, java.io.File src, java.io.File destination)

int copyTo(java.io.File src, java.lang.String destination)

int copyTo(java.util.Map props, java.io.File src, java.lang.String destination)

int copyTo(java.io.Reader src, java.lang.String destination)

int copyTo(java.util.Map props, java.io.Reader src, java.lang.String destination)

int copyTo(java.io.InputStream src, java.lang.String destination)

int copyTo(java.io.InputStream src, long filesize, java.lang.String destination)

int copyTo(java.util.Map props, java.io.InputStream src, long filesize, java.lang.String destination)

int copyTo(java.util.Map props, java.io.InputStream src, java.lang.String destination)

int exec(java.util.List commands)

Convenience for running commands using ssh {@literal exec} mode.

int exec(java.util.Map props, java.util.List commands)

int exec(java.util.List commands, java.util.Map env)

int exec(java.util.Map props, java.util.List commands, java.util.Map env)

int execCommands(java.lang.String summaryForLogging, java.util.List commands)

executes a set of commands, directly on the target machine (no wrapping in script).

int execCommands(java.util.Map props, java.lang.String summaryForLogging, java.util.List commands)

int execCommands(java.lang.String summaryForLogging, java.util.List commands, java.util.Map env)

int execCommands(java.util.Map props, java.lang.String summaryForLogging, java.util.List commands, java.util.Map env)

int execScript(java.lang.String summaryForLogging, java.util.List commands)

executes a set of commands, wrapped as a script sent to the remote machine.

int execScript(java.util.Map props, java.lang.String summaryForLogging, java.util.List commands)

int execScript(java.lang.String summaryForLogging, java.util.List commands, java.util.Map env)

int execScript(java.util.Map props, java.lang.String summaryForLogging, java.util.List commands, java.util.Map env)

protected java.lang.Object execSsh(java.util.Map props, Function task)

protected int execWithLogging(java.util.Map props, java.lang.String summaryForLogging, java.util.List commands, java.util.Map env, groovy.lang.Closure execCommand)

protected int execWithLogging(java.util.Map props, java.lang.String summaryForLogging, java.util.List commands, java.util.Map env, SshMachineLocation.ExecRunner execCommand)

protected void finalize()

java.net.InetAddress getAddress()

OsDetails getOsDetails()

java.lang.String getUser()

boolean hasMutex(java.lang.String mutexId)

int installTo(ResourceUtils loader, java.lang.String url, java.lang.String destination)

installs the given URL at the indicated destination.

boolean isSshable()

protected WithMutexes newMutexSupport()

int obtainPort(PortRange range)

boolean obtainSpecificPort(int portNumber)

@see #obtainPort(PortRange)

void releaseMutex(java.lang.String mutexId)

void releasePort(int portNumber)

int run(java.lang.String command)

int run(java.util.Map props, java.lang.String command)

int run(java.lang.String command, java.util.Map env)

int run(java.util.Map props, java.lang.String command, java.util.Map env)

int run(java.util.List commands)

deprecated:
in 1.4.1,
@see execCommand and execScript

int run(java.util.Map props, java.util.List commands)

int run(java.util.List commands, java.util.Map env)

int run(java.util.Map props, java.util.List commands, java.util.Map env)

java.lang.String toString()

boolean tryAcquireMutex(java.lang.String mutexId, java.lang.String description)

 
Methods inherited from class AbstractLocation
addChildLocation, configure, configure, containsLocation, equals, findLocationProperty, getAllConfig, getChildLocations, getConfig, getConfigBag, getHostGeoInfo, getId, getLocationProperty, getName, getParentLocation, getRebindSupport, hasConfig, hasLocationProperty, hashCode, removeChildLocation, setConfig, setHostGeoInfo, setName, setParentLocation, string, toString
 

Field Detail

ALLOCATE_PTY

public static final ConfigKey ALLOCATE_PTY


ALL_SSH_CONFIG_KEYS

public static final java.util.Set ALL_SSH_CONFIG_KEYS


ALL_SSH_CONFIG_KEY_NAMES

public static final java.util.Set ALL_SSH_CONFIG_KEY_NAMES


LOCAL_TEMP_DIR

public static final ConfigKey LOCAL_TEMP_DIR


LOG

public static final Logger LOG


LOG_PREFIX

public static final ConfigKey LOG_PREFIX


NO_STDERR_LOGGING

public static final ConfigKey NO_STDERR_LOGGING


NO_STDOUT_LOGGING

public static final ConfigKey NO_STDOUT_LOGGING


PASSWORD

public static final ConfigKey PASSWORD


PRIVATE_KEY_DATA

public static final ConfigKey PRIVATE_KEY_DATA


PRIVATE_KEY_FILE

public static final ConfigKey PRIVATE_KEY_FILE


PRIVATE_KEY_PASSPHRASE

public static final ConfigKey PRIVATE_KEY_PASSPHRASE


REUSABLE_SSH_PROPS

public static final java.util.Set REUSABLE_SSH_PROPS
specifies config keys where a change in the value does not require a new SshTool instance, i.e. these can be specified per command on the tool


SCP_EXECUTABLE

public static final ConfigKey SCP_EXECUTABLE


SCRIPT_DIR

public static final ConfigKey SCRIPT_DIR


SSHCONFIG_PREFIX

@Deprecated
public static final java.lang.String SSHCONFIG_PREFIX
any property that should be passed as ssh config (connection-time) can be prefixed with this and . and will be passed through (with the prefix removed), e.g. (SSHCONFIG_PREFIX+"."+"StrictHostKeyChecking"):"yes"
deprecated:
use SshTool#BROOKLYN_CONFIG_KEY_PREFIX#BROOKLYN_CONFIG_KEY_PREFIX


SSH_ENV_MAP

public static final ConfigKey SSH_ENV_MAP


SSH_EXECUTABLE

public static final ConfigKey SSH_EXECUTABLE


SSH_HOST

public static final ConfigKey SSH_HOST


SSH_PORT

public static final ConfigKey SSH_PORT


STDERR

public static final ConfigKey STDERR


STDOUT

public static final ConfigKey STDOUT


logSsh

public static final Logger logSsh


previouslyConnected

protected boolean previouslyConnected


 
Constructor Detail

SshMachineLocation

public SshMachineLocation()


SshMachineLocation

public SshMachineLocation(java.util.Map properties)


 
Method Detail

acquireMutex

@Override
public void acquireMutex(java.lang.String mutexId, java.lang.String description)


addConfig

public void addConfig(java.util.Map vals)
deprecated:
temporary Beta method introduced in 0.5.0


close

@Override
public void close()


configure

public void configure(java.util.Map properties)


connectSsh

protected SshTool connectSsh()


connectSsh

protected SshTool connectSsh(java.util.Map props)


copyFrom

public int copyFrom(java.lang.String remote, java.lang.String local)


copyFrom

public int copyFrom(java.util.Map props, java.lang.String remote, java.lang.String local)


copyTo

public int copyTo(java.io.File src, java.io.File destination)


copyTo

public int copyTo(java.util.Map props, java.io.File src, java.io.File destination)


copyTo

public int copyTo(java.io.File src, java.lang.String destination)


copyTo

public int copyTo(java.util.Map props, java.io.File src, java.lang.String destination)


copyTo

public int copyTo(java.io.Reader src, java.lang.String destination)


copyTo

public int copyTo(java.util.Map props, java.io.Reader src, java.lang.String destination)


copyTo

public int copyTo(java.io.InputStream src, java.lang.String destination)


copyTo

public int copyTo(java.io.InputStream src, long filesize, java.lang.String destination)


copyTo

public int copyTo(java.util.Map props, java.io.InputStream src, long filesize, java.lang.String destination)


copyTo

public int copyTo(java.util.Map props, java.io.InputStream src, java.lang.String destination)


exec

public int exec(java.util.List commands)
Convenience for running commands using ssh {
literal:
exec} mode.
deprecated:
in 1.4.1,
See Also:
execCommand


exec

public int exec(java.util.Map props, java.util.List commands)


exec

public int exec(java.util.List commands, java.util.Map env)


exec

public int exec(java.util.Map props, java.util.List commands, java.util.Map env)


execCommands

public int execCommands(java.lang.String summaryForLogging, java.util.List commands)
executes a set of commands, directly on the target machine (no wrapping in script). joined using ' ; ' by default.

Stdout and stderr will be logged automatically to brooklyn.SSH logger, unless the flags 'noStdoutLogging' and 'noStderrLogging' are set. To set a logging prefix, use the flag 'logPrefix'.

Currently runs the commands in an interactive/login shell by passing each as a line to bash. To terminate early, use:

 foo || exit 1
 
It may be desirable instead, in some situations, to wrap as:
 { line1 ; } && { line2 ; } ... 
 
and run as a single command (possibly not as an interacitve/login shell) causing the script to exit on the first command which fails.

Currently this has to be done by the caller. (If desired we can add a flag exitIfAnyNonZero to support this mode, and/or commandPrepend and commandAppend similar to (currently supported in SshjTool) separator.)


execCommands

public int execCommands(java.util.Map props, java.lang.String summaryForLogging, java.util.List commands)


execCommands

public int execCommands(java.lang.String summaryForLogging, java.util.List commands, java.util.Map env)


execCommands

public int execCommands(java.util.Map props, java.lang.String summaryForLogging, java.util.List commands, java.util.Map env)


execScript

public int execScript(java.lang.String summaryForLogging, java.util.List commands)
executes a set of commands, wrapped as a script sent to the remote machine.

Stdout and stderr will be logged automatically to brooklyn.SSH logger, unless the flags 'noStdoutLogging' and 'noStderrLogging' are set. To set a logging prefix, use the flag 'logPrefix'.


execScript

public int execScript(java.util.Map props, java.lang.String summaryForLogging, java.util.List commands)


execScript

public int execScript(java.lang.String summaryForLogging, java.util.List commands, java.util.Map env)


execScript

public int execScript(java.util.Map props, java.lang.String summaryForLogging, java.util.List commands, java.util.Map env)


execSsh

protected java.lang.Object execSsh(java.util.Map props, Function task)


execWithLogging

protected int execWithLogging(java.util.Map props, java.lang.String summaryForLogging, java.util.List commands, java.util.Map env, groovy.lang.Closure execCommand)


execWithLogging

@SuppressWarnings("resource")
protected int execWithLogging(java.util.Map props, java.lang.String summaryForLogging, java.util.List commands, java.util.Map env, SshMachineLocation.ExecRunner execCommand)


finalize

@Override
protected void finalize()


getAddress

public java.net.InetAddress getAddress()


getOsDetails

@Override
public OsDetails getOsDetails()


getUser

public java.lang.String getUser()


hasMutex

@Override
public boolean hasMutex(java.lang.String mutexId)


installTo

public int installTo(ResourceUtils loader, java.lang.String url, java.lang.String destination)
installs the given URL at the indicated destination. attempts to curl the sourceUrl on the remote machine, then if that fails, loads locally (from classpath or file) and transfers.

accepts either a path (terminated with /) or filename for the destination.


isSshable

public boolean isSshable()


newMutexSupport

protected WithMutexes newMutexSupport()


obtainPort

public int obtainPort(PortRange range)


obtainSpecificPort

public boolean obtainSpecificPort(int portNumber)
See Also:
obtainPort(PortRange)
BasicPortRange#ANY_HIGH_PORT#ANY_HIGH_PORT


releaseMutex

@Override
public void releaseMutex(java.lang.String mutexId)


releasePort

public void releasePort(int portNumber)


run

public int run(java.lang.String command)


run

public int run(java.util.Map props, java.lang.String command)


run

public int run(java.lang.String command, java.util.Map env)


run

public int run(java.util.Map props, java.lang.String command, java.util.Map env)


run

public int run(java.util.List commands)
deprecated:
in 1.4.1,
See Also:
execCommand


run

public int run(java.util.Map props, java.util.List commands)


run

public int run(java.util.List commands, java.util.Map env)


run

public int run(java.util.Map props, java.util.List commands, java.util.Map env)


toString

@Override
public java.lang.String toString()


tryAcquireMutex

@Override
public boolean tryAcquireMutex(java.lang.String mutexId, java.lang.String description)


 

Brooklyn Multi-Cloud Application Management Platform
brooklyncentral.github.com. Apache License. © 2012.