Documentation
¶
Overview ¶
Package transport runs commands on remote hosts through the OpenSSH client.
OpenSSH is driven rather than replaced, so ProxyJump, ControlMaster, Kerberos, the agent and the administrator's own configuration keep working exactly as they do from a shell.
A remote command is never built by joining words into a string. The argument vector is quoted once, handed to ssh as a single argument, and split back by the remote shell into exactly the arguments that were given, so a glob, an apostrophe or repeated whitespace survives the trip.
Index ¶
- func RemoteCommand(req Request) (string, error)
- type Call
- type Client
- func (c *Client) Args(target Target, req Request) ([]string, error)
- func (c *Client) Binary() string
- func (c *Client) ConfigPath() (string, error)
- func (c *Client) Copy(ctx context.Context, target Target, req CopyRequest) (*Result, error)
- func (c *Client) CopyArgs(target Target, req CopyRequest) ([]string, error)
- func (c *Client) Interactive(ctx context.Context, target Target, req Request) error
- func (c *Client) Run(ctx context.Context, target Target, req Request) (*Result, error)
- func (c *Client) ScpBinary() string
- type CopyRequest
- type Options
- type Recorder
- type Request
- type Result
- type Runner
- type TTY
- type Target
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RemoteCommand ¶
RemoteCommand renders a request as the single argument ssh is given.
The result is one shell command line. The remote shell splits it back into the original argument vector, because every word was quoted.
Types ¶
type Call ¶
type Call struct {
Target Target
Request Request
// Command is the shell command line the request renders to, which is
// what --dry-run prints and what the tests assert on.
Command string
}
Call is one request a Recorder was asked to run.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client drives the OpenSSH client.
func (*Client) Args ¶
Args builds the full argument vector, ssh included, that a request runs as. Commands use it for --dry-run and for reporting what they would do.
func (*Client) ConfigPath ¶
ConfigPath returns the generated ssh configuration, writing it once.
func (*Client) CopyArgs ¶
func (c *Client) CopyArgs(target Target, req CopyRequest) ([]string, error)
CopyArgs builds the scp argument vector for a transfer.
func (*Client) Interactive ¶
Interactive runs a request wired to the terminal, for a login shell or anything that needs to talk to the user.
type CopyRequest ¶
type CopyRequest struct {
// Sources are local paths when Upload is set, and remote paths
// otherwise.
Sources []string
// Destination is the remote path when Upload is set, and a local path
// otherwise.
Destination string
// Upload says which way the transfer goes.
Upload bool
// Recursive copies directories.
Recursive bool
// Preserve keeps modification times and modes.
Preserve bool
}
CopyRequest describes a file transfer.
type Options ¶
type Options struct {
// SSH is the transport configuration of the site.
SSH v1alpha1.SSHSpec
// Roles are the infrastructure roles, used for per role settings and
// for resolving a jump host by name.
Roles map[string]v1alpha1.HostRole
// StateDir holds the generated configuration and the control sockets.
StateDir string
// KnownHostsFile is the host key file, already made absolute.
KnownHostsFile string
// DefaultUser is the account used where nothing names one.
DefaultUser string
}
Options configure a client.
type Recorder ¶
type Recorder struct {
// Responses are returned in order; once they run out, an empty
// successful result is returned. A response may be keyed to a target
// name through Reply instead.
Responses []*Result
// Reply returns the result for a call. It wins over Responses.
Reply func(Target, Request) (*Result, error)
// contains filtered or unexported fields
}
Recorder is a Runner that records requests instead of running them.
It backs --dry-run, where the point is to show exactly what would be sent, and the tests, where a real ssh is neither available nor wanted.
type Request ¶
type Request struct {
// Argv is the remote command. An empty Argv and Script start an
// interactive login.
Argv []string
// Script is a shell program run with the shell's -c, as one argument.
// Argv and Script are mutually exclusive.
Script string
// Shell runs Script; it defaults to bash.
Shell string
// TTY controls pseudo terminal allocation.
TTY TTY
// Timeout bounds the command. It is enforced on the target with
// timeout(1), because killing the local ssh leaves the remote process
// running.
Timeout time.Duration
// Env is exported for the command. It is passed through env(1) rather
// than SendEnv, which needs the server to allow each name.
Env map[string]string
// Stdin is fed to the command. Secrets travel this way so that they
// never appear in an argument vector, where ps shows them.
Stdin io.Reader
// Stdout and Stderr receive the output when the command is run
// interactively. Run captures them instead.
Stdout, Stderr io.Writer
}
Request is what to run on a target.
type Result ¶
type Result struct {
Target Target `json:"target" yaml:"target"`
ExitCode int `json:"exitCode" yaml:"exitCode"`
Stdout string `json:"stdout,omitempty" yaml:"stdout,omitempty"`
Stderr string `json:"stderr,omitempty" yaml:"stderr,omitempty"`
Duration time.Duration `json:"-" yaml:"-"`
// Err is set when the command could not be run or did not exit zero.
Err error `json:"-" yaml:"-"`
}
Result is the outcome of one remote command.
type Runner ¶
Runner runs a command on a target. Commands depend on this rather than on the client, so that a dry run or a test can stand in for real ssh.
type TTY ¶
type TTY int
TTY says whether a pseudo terminal is allocated for a remote command.
const ( // TTYAuto allocates a terminal for an interactive login and none for a // command, which is what ssh does on its own. TTYAuto TTY = iota // TTYForce allocates one even for a command, for anything that prompts. TTYForce // TTYNone never allocates one, so that output is not echoed back. TTYNone )
type Target ¶
type Target struct {
// Name is the node or role as the administrator named it, used in
// output and errors.
Name string
// Host is the name ssh connects to.
Host string
// User is the remote account; empty uses what the configuration says.
User string
// Role names the infrastructure role, when the target is one.
Role string
// ForwardAgent and ForwardX11 are requested per command, on top of what
// the role already asks for.
ForwardAgent bool
ForwardX11 bool
}
Target is a host to run something on.