public class SshMachineLocation extends AbstractMachineLocation implements MachineLocation, PortSupplier, WithMutexes, java.io.Closeable, CanResolveOnBoxDir
We expose two ways of running scripts. The execCommands method passes lines to bash and is lightweight but fragile. The execScript method creates a script on the remote machine. It is 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).
AbstractLocation.BasicSubscriptionSupport
BrooklynObject.RelationSupport<T extends BrooklynObject>, BrooklynObject.SubscriptionSupport, BrooklynObject.TagSupport
Configurable.ConfigurationSupport
BrooklynObjectInternal.ConfigurationSupportInternal, BrooklynObjectInternal.RelationSupportInternal<T extends BrooklynObject>, BrooklynObjectInternal.SubscriptionSupportInternal
Modifier and Type | Field and Description |
---|---|
static java.util.Set<java.lang.String> |
ALL_SSH_CONFIG_KEY_NAMES |
static java.util.Set<ConfigKey.HasConfigKey<?>> |
ALL_SSH_CONFIG_KEYS |
static ConfigKey<java.lang.Boolean> |
ALLOCATE_PTY |
static ConfigKey<java.lang.Boolean> |
CLOSE_CONNECTION |
static ConfigKey<java.lang.String> |
LOCAL_TEMP_DIR |
static ConfigKey<java.lang.String> |
LOG_PREFIX |
static ConfigKey<java.lang.Boolean> |
NO_STDERR_LOGGING |
static ConfigKey<java.lang.Boolean> |
NO_STDOUT_LOGGING |
static ConfigKey<java.lang.String> |
PASSWORD |
static ConfigKey<java.lang.Iterable<java.lang.String>> |
PRIVATE_ADDRESSES |
static ConfigKey<java.lang.String> |
PRIVATE_KEY_DATA |
static ConfigKey<java.lang.String> |
PRIVATE_KEY_FILE |
static ConfigKey<java.lang.String> |
PRIVATE_KEY_PASSPHRASE |
static java.util.Set<ConfigKey<?>> |
REUSABLE_SSH_PROPS
Specifies config keys where a change in the value does not require a new SshTool instance,
i.e.
|
static ConfigKey<java.lang.String> |
SCP_EXECUTABLE |
static ConfigKey<java.lang.String> |
SCRIPT_DIR |
static ConfigKey<Duration> |
SSH_CACHE_EXPIRY_DURATION |
static java.util.Set<ConfigKey<?>> |
SSH_CONFIG_GIVEN_TO_PROPS
The set of config keys on this location which become default values for properties when invoking an SSH
operation.
|
static ConfigKey<java.util.Map<java.lang.String,java.lang.Object>> |
SSH_ENV_MAP |
static ConfigKey<java.lang.String> |
SSH_EXECUTABLE |
static ConfigKey<java.lang.String> |
SSH_HOST |
static ConfigKey<java.lang.Integer> |
SSH_PORT |
static ConfigKey<java.lang.String> |
SSH_TOOL_CLASS |
static java.lang.String |
SSH_TOOL_CLASS_PROPERTIES_PREFIX
Prefix for config key:values to be passed to the ssh tool on construction.
|
static ConfigKey<java.io.OutputStream> |
STDERR |
static ConfigKey<java.io.OutputStream> |
STDOUT |
static ConfigKey<java.util.Map<java.lang.Integer,java.lang.String>> |
TCP_PORT_MAPPINGS |
static ConfigKey<java.lang.String> |
UNIQUE_ID |
DETECT_MACHINE_DETAILS, MACHINE_DETAILS
PARENT_LOCATION, TEMPORARY_LOCATION
FINAL_SPEC, NAMED_SPEC_NAME, ORIGINAL_SPEC
Constructor and Description |
---|
SshMachineLocation() |
SshMachineLocation(java.util.Map<?,?> properties) |
Modifier and Type | Method and Description |
---|---|
void |
acquireMutex(java.lang.String mutexId,
java.lang.String description)
Deprecated.
since 1.0.0; mutex-related methods are now accessible via
AbstractMachineLocation.mutexes() |
void |
close() |
SshMachineLocation |
configure(java.util.Map<?,?> properties)
Will set fields from flags, and put the remaining ones into the 'leftovers' map.
|
int |
copyFrom(java.util.Map<java.lang.String,?> props,
java.lang.String remote,
java.lang.String local) |
int |
copyFrom(java.lang.String remote,
java.lang.String local) |
int |
copyTo(java.io.File src,
java.io.File destination) |
int |
copyTo(java.io.File src,
java.lang.String destination) |
int |
copyTo(java.io.InputStream src,
long filesize,
java.lang.String destination) |
int |
copyTo(java.io.InputStream src,
java.lang.String destination) |
int |
copyTo(java.util.Map<java.lang.String,?> props,
java.io.File src,
java.io.File destination) |
int |
copyTo(java.util.Map<java.lang.String,?> props,
java.io.File src,
java.lang.String destination) |
int |
copyTo(java.util.Map<java.lang.String,?> props,
java.io.InputStream src,
long filesize,
java.lang.String destination) |
int |
copyTo(java.util.Map<java.lang.String,?> props,
java.io.InputStream src,
java.lang.String destination) |
int |
copyTo(java.util.Map<java.lang.String,?> props,
java.io.Reader src,
java.lang.String destination) |
int |
copyTo(java.io.Reader src,
java.lang.String destination) |
int |
execCommands(java.util.Map<java.lang.String,?> props,
java.lang.String summaryForLogging,
java.util.List<java.lang.String> commands) |
int |
execCommands(java.util.Map<java.lang.String,?> props,
java.lang.String summaryForLogging,
java.util.List<java.lang.String> commands,
java.util.Map<java.lang.String,?> env) |
int |
execCommands(java.lang.String summaryForLogging,
java.util.List<java.lang.String> commands)
Executes a set of commands, directly on the target machine (no wrapping in script).
|
int |
execCommands(java.lang.String summaryForLogging,
java.util.List<java.lang.String> commands,
java.util.Map<java.lang.String,?> env) |
int |
execScript(java.util.Map<java.lang.String,?> props,
java.lang.String summaryForLogging,
java.util.List<java.lang.String> commands) |
int |
execScript(java.util.Map<java.lang.String,?> props,
java.lang.String summaryForLogging,
java.util.List<java.lang.String> commands,
java.util.Map<java.lang.String,?> env) |
int |
execScript(java.lang.String summaryForLogging,
java.util.List<java.lang.String> commands)
Executes a set of commands, wrapped as a script sent to the remote machine.
|
int |
execScript(java.lang.String summaryForLogging,
java.util.List<java.lang.String> commands,
java.util.Map<java.lang.String,?> env) |
java.security.KeyPair |
findKeyPair()
returns the un-passphrased key-pair info if a key is being used, or else null
|
java.lang.String |
findPassword()
returns the password being used to log in, if a password is being used, or else null
|
java.net.InetAddress |
getAddress()
Return the single most appropriate address for this location.
|
java.lang.String |
getHostname()
note
|
int |
getPort()
port for SSHing
|
java.util.Set<java.lang.String> |
getPrivateAddresses()
All private IP addresses.
|
java.util.Set<java.lang.String> |
getPublicAddresses()
All public IP addresses, potentially including shared ips.
|
HostAndPort |
getSshHostAndPort() |
java.lang.String |
getUser() |
boolean |
hasMutex(java.lang.String mutexId)
Deprecated.
since 1.0.0; mutex-related methods are now accessible via
AbstractMachineLocation.mutexes() |
void |
init()
Called by framework (in new-style instances where spec was used) after configuring etc,
but before a reference to this instance is shared.
|
int |
installTo(java.util.Map<java.lang.String,?> props,
java.lang.String url,
java.lang.String destPath) |
int |
installTo(ResourceUtils utils,
java.util.Map<java.lang.String,?> props,
java.lang.String url,
java.lang.String destPath)
Installs the given URL at the indicated destination path.
|
int |
installTo(ResourceUtils loader,
java.lang.String url,
java.lang.String destPath) |
int |
installTo(java.lang.String url,
java.lang.String destPath) |
boolean |
isSshable() |
int |
obtainPort(PortRange range)
Reserve a port for your application, with a port number in a specific range.
|
boolean |
obtainSpecificPort(int portNumber)
Reserve a specific port for an application.
|
void |
releaseMutex(java.lang.String mutexId)
Deprecated.
since 1.0.0; mutex-related methods are now accessible via
AbstractMachineLocation.mutexes() |
void |
releasePort(int portNumber)
Release a previously reserved port.
|
java.lang.String |
resolveOnBoxDirFor(Entity entity,
java.lang.String unresolvedPath) |
java.lang.String |
toString()
Default String representation is simplified name of class, together with selected fields.
|
java.lang.String |
toVerboseString() |
boolean |
tryAcquireMutex(java.lang.String mutexId,
java.lang.String description)
Deprecated.
since 1.0.0; mutex-related methods are now accessible via
AbstractMachineLocation.mutexes() |
getMachineDetails, getOsDetails, mutexes
addChild, addExtension, config, containsLocation, equals, getChildren, getConfig, getConfig, getDisplayName, getExtension, getHostGeoInfo, getLocationTypeInternal, getParent, getRebindSupport, hasExtension, hashCode, isManaged, onManagementStarted, onManagementStopped, rebind, relations, removeChild, setDisplayName, setHostGeoInfo, setManagementContext, setParent, setParent, subscriptions, toMetadataRecord
addSearchPath, getCatalogItemId, getCatalogItemIdSearchPath, getId, getManagementContext, setCatalogItemId, setCatalogItemIdAndSearchPath, stackCatalogItemId, tags
getMachineDetails, getOsDetails
containsLocation, getChildren, getConfig, getDisplayName, getExtension, getId, getParent, hasExtension, relations, setParent
getCatalogItemId, getCatalogItemIdSearchPath, subscriptions, tags
config, getConfig
getManagementContext
addSearchPath, setCatalogItemId, setCatalogItemIdAndSearchPath, stackCatalogItemId
public static final ConfigKey<java.lang.String> SSH_TOOL_CLASS
public static final java.lang.String SSH_TOOL_CLASS_PROPERTIES_PREFIX
com.acme.brooklyn.MyCustomSshTool
, calling its constructor with a
Map<String, Object>
that contained the configuration. In this case, the map would
include: address=1.2.3.4
; user=myname
; and myparam=myvalue
.
brooklyn.location.named.myLocation = byon:(hosts=1.2.3.4,user=myname)
brooklyn.location.named.myLocation.sshToolClass = com.acme.brooklyn.MyCustomSshTool
brooklyn.location.named.myLocation.sshToolClass.myparam = myvalue
public static final ConfigKey<java.lang.Iterable<java.lang.String>> PRIVATE_ADDRESSES
public static final ConfigKey<java.util.Map<java.lang.Integer,java.lang.String>> TCP_PORT_MAPPINGS
public static final ConfigKey<java.lang.String> SSH_HOST
public static final ConfigKey<java.lang.Integer> SSH_PORT
public static final ConfigKey<java.lang.String> SSH_EXECUTABLE
public static final ConfigKey<java.lang.String> SCP_EXECUTABLE
public static final ConfigKey<java.lang.String> PASSWORD
public static final ConfigKey<java.lang.String> PRIVATE_KEY_FILE
public static final ConfigKey<java.lang.String> PRIVATE_KEY_DATA
public static final ConfigKey<java.lang.String> PRIVATE_KEY_PASSPHRASE
public static final ConfigKey<java.lang.String> SCRIPT_DIR
public static final ConfigKey<java.util.Map<java.lang.String,java.lang.Object>> SSH_ENV_MAP
public static final ConfigKey<java.lang.Boolean> ALLOCATE_PTY
public static final ConfigKey<java.io.OutputStream> STDOUT
public static final ConfigKey<java.io.OutputStream> STDERR
public static final ConfigKey<java.lang.Boolean> NO_STDOUT_LOGGING
public static final ConfigKey<java.lang.Boolean> NO_STDERR_LOGGING
public static final ConfigKey<java.lang.String> LOG_PREFIX
public static final ConfigKey<java.lang.String> LOCAL_TEMP_DIR
public static final ConfigKey<java.lang.Boolean> CLOSE_CONNECTION
public static final ConfigKey<java.lang.String> UNIQUE_ID
public static final java.util.Set<ConfigKey<?>> REUSABLE_SSH_PROPS
public static final java.util.Set<ConfigKey.HasConfigKey<?>> ALL_SSH_CONFIG_KEYS
public static final java.util.Set<java.lang.String> ALL_SSH_CONFIG_KEY_NAMES
public static final java.util.Set<ConfigKey<?>> SSH_CONFIG_GIVEN_TO_PROPS
public SshMachineLocation()
public SshMachineLocation(java.util.Map<?,?> properties)
public void init()
AbstractBrooklynObject
To preserve backwards compatibility for if the instance is constructed directly, one can call the code below, but that means it will be called after references to this policy have been shared with other entities.
if (isLegacyConstruction()) {
init();
}
init
in class AbstractLocation
public SshMachineLocation configure(java.util.Map<?,?> properties)
AbstractBrooklynObject
ConfigBag.getUnusedConfig()
.
To be overridden by AbstractEntity, AbstractLoation, AbstractPolicy, AbstractEnricher, etc.
But should not be overridden by specific entity types. If you do, the entity may break in subsequent releases. Also note that if you require fields to be initialized you must do that in this method. You must *not* rely on field initializers because they may not run until *after* this method (this method is invoked by the constructor in this class, so initializers in subclasses will not have run when this overridden method is invoked.)
configure
in class AbstractLocation
public void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
java.io.IOException
public java.net.InetAddress getAddress()
AddressableLocation
getAddress
in interface AddressableLocation
getAddress
in interface MachineLocation
public java.lang.String getHostname()
HasNetworkAddresses
getHostname
in interface HasNetworkAddresses
public java.util.Set<java.lang.String> getPublicAddresses()
HasNetworkAddresses
getPublicAddresses
in interface HasNetworkAddresses
public java.util.Set<java.lang.String> getPrivateAddresses()
HasNetworkAddresses
getPrivateAddresses
in interface HasNetworkAddresses
public HostAndPort getSshHostAndPort()
public java.lang.String getUser()
public int getPort()
public int execCommands(java.lang.String summaryForLogging, java.util.List<java.lang.String> commands)
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 1It 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
.)
public int execCommands(java.util.Map<java.lang.String,?> props, java.lang.String summaryForLogging, java.util.List<java.lang.String> commands)
public int execCommands(java.lang.String summaryForLogging, java.util.List<java.lang.String> commands, java.util.Map<java.lang.String,?> env)
public int execCommands(java.util.Map<java.lang.String,?> props, java.lang.String summaryForLogging, java.util.List<java.lang.String> commands, java.util.Map<java.lang.String,?> env)
public int execScript(java.lang.String summaryForLogging, java.util.List<java.lang.String> commands)
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'.
public int execScript(java.util.Map<java.lang.String,?> props, java.lang.String summaryForLogging, java.util.List<java.lang.String> commands)
public int execScript(java.lang.String summaryForLogging, java.util.List<java.lang.String> commands, java.util.Map<java.lang.String,?> env)
public int execScript(java.util.Map<java.lang.String,?> props, java.lang.String summaryForLogging, java.util.List<java.lang.String> commands, java.util.Map<java.lang.String,?> env)
public int copyTo(java.io.File src, java.io.File destination)
public int copyTo(java.util.Map<java.lang.String,?> props, java.io.File src, java.io.File destination)
public int copyTo(java.io.File src, java.lang.String destination)
public int copyTo(java.util.Map<java.lang.String,?> props, java.io.File src, java.lang.String destination)
public int copyTo(java.io.Reader src, java.lang.String destination)
public int copyTo(java.util.Map<java.lang.String,?> props, java.io.Reader src, java.lang.String destination)
public int copyTo(java.io.InputStream src, java.lang.String destination)
public int copyTo(java.io.InputStream src, long filesize, java.lang.String destination)
public int copyTo(java.util.Map<java.lang.String,?> props, java.io.InputStream src, long filesize, java.lang.String destination)
public int copyTo(java.util.Map<java.lang.String,?> props, java.io.InputStream src, java.lang.String destination)
public int copyFrom(java.lang.String remote, java.lang.String local)
public int copyFrom(java.util.Map<java.lang.String,?> props, java.lang.String remote, java.lang.String local)
public int installTo(java.lang.String url, java.lang.String destPath)
public int installTo(java.util.Map<java.lang.String,?> props, java.lang.String url, java.lang.String destPath)
public int installTo(ResourceUtils loader, java.lang.String url, java.lang.String destPath)
public int installTo(ResourceUtils utils, java.util.Map<java.lang.String,?> props, java.lang.String url, java.lang.String destPath)
Attempts to curl the source URL on the remote machine, then if that fails, loads locally (from classpath or file) and transfers.
Use ArchiveUtils
to handle directories and their contents properly.
TODO allow s3://bucket/file URIs for AWS S3 resources
TODO use PAX-URL style URIs for maven artifacts
TODO use subtasks here for greater visibility?; deprecate in favour of SshTasks.installFromUrl?
utils
- A ResourceUtils
that can resolve the source URLsurl
- The source URL to be installeddestPath
- The file to be created on the destinationArchiveUtils.deploy(String, SshMachineLocation, String)
,
ArchiveUtils.deploy(String, SshMachineLocation, String, String)
,
ResourceUtils.getResourceFromUrl(String)
public java.lang.String toString()
AbstractLocation
toString
in class AbstractLocation
public java.lang.String toVerboseString()
toVerboseString
in interface Location
toVerboseString
in class AbstractLocation
public boolean obtainSpecificPort(int portNumber)
PortSupplier
obtainSpecificPort
in interface PortSupplier
portNumber
- the required port number.true
if the port was successfully reserved; false
if it has been previously reserved.obtainPort(PortRange)
,
PortRanges.ANY_HIGH_PORT
public int obtainPort(PortRange range)
PortSupplier
obtainPort
in interface PortSupplier
range
- the range of acceptable port numbers.public void releasePort(int portNumber)
PortSupplier
releasePort
in interface PortSupplier
portNumber
- the port number from a call to PortSupplier.obtainPort(PortRange)
or PortSupplier.obtainSpecificPort(int)
public boolean isSshable()
public java.security.KeyPair findKeyPair()
public java.lang.String findPassword()
@Deprecated public void acquireMutex(java.lang.String mutexId, java.lang.String description)
AbstractMachineLocation.mutexes()
WithMutexes
acquireMutex
in interface WithMutexes
@Deprecated public boolean tryAcquireMutex(java.lang.String mutexId, java.lang.String description)
AbstractMachineLocation.mutexes()
WithMutexes
tryAcquireMutex
in interface WithMutexes
@Deprecated public void releaseMutex(java.lang.String mutexId)
AbstractMachineLocation.mutexes()
WithMutexes
releaseMutex
in interface WithMutexes
@Deprecated public boolean hasMutex(java.lang.String mutexId)
AbstractMachineLocation.mutexes()
WithMutexes
hasMutex
in interface WithMutexes
public java.lang.String resolveOnBoxDirFor(Entity entity, java.lang.String unresolvedPath)
resolveOnBoxDirFor
in interface CanResolveOnBoxDir