transport

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 23, 2026 License: GPL-3.0, LGPL-3.0 Imports: 17 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func RemoteCommand

func RemoteCommand(req Request) (string, error)

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.

func (Call) Describe

func (c Call) Describe() string

Describe renders a call the way --dry-run prints it.

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client drives the OpenSSH client.

func New

func New(opts Options) *Client

New returns a client. The ssh configuration is written on first use.

func (*Client) Args

func (c *Client) Args(target Target, req Request) ([]string, error)

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) Binary

func (c *Client) Binary() string

Binary returns the ssh client to run.

func (*Client) ConfigPath

func (c *Client) ConfigPath() (string, error)

ConfigPath returns the generated ssh configuration, writing it once.

func (*Client) Copy

func (c *Client) Copy(ctx context.Context, target Target, req CopyRequest) (*Result, error)

Copy runs a file transfer.

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

func (c *Client) Interactive(ctx context.Context, target Target, req Request) error

Interactive runs a request wired to the terminal, for a login shell or anything that needs to talk to the user.

func (*Client) Run

func (c *Client) Run(ctx context.Context, target Target, req Request) (*Result, error)

Run executes a request and captures its output.

func (*Client) ScpBinary

func (c *Client) ScpBinary() string

ScpBinary returns the scp client to run.

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.

func (*Recorder) Calls

func (r *Recorder) Calls() []Call

Calls returns the recorded requests.

func (*Recorder) Commands

func (r *Recorder) Commands() []string

Commands returns the rendered command line of each recorded request.

func (*Recorder) Run

func (r *Recorder) Run(_ context.Context, target Target, req Request) (*Result, error)

Run implements Runner.

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.

func (*Result) Failed

func (r *Result) Failed() bool

Failed reports whether the command did not succeed.

func (*Result) Lines

func (r *Result) Lines() []string

Lines returns the standard output split into non-empty lines.

func (*Result) Output

func (r *Result) Output() string

Output returns the standard output with trailing newlines removed.

type Runner

type Runner interface {
	Run(ctx context.Context, target Target, req Request) (*Result, error)
}

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.

func (Target) String

func (t Target) String() string

String renders the target the way errors refer to it.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL