client

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2022 License: BSD-3-Clause Imports: 29 Imported by: 0

Documentation

Overview

Package client provides an exec.Command and ssh like interface for cpu sessions. It attempts to cleave as much as possible to the original. The choice between options and environment variables mirrors this effort. For example, the namespace for the remote process is an environment variable, CPU_NAMESPACE. The nonce for the mount protocol back is also an environment variable. command name and arguments are passed in os.Args The only required parameter for Command() is a host name; if os.Args is empty, the remote server reads SHELL and starts a shell. Similarly, because the root for the client namespace is known only to the client. it is settable in the Cmd struct.

Index

Constants

View Source
const (

	// DefaultNameSpace is the default used if the user does not request
	// something else.
	DefaultNameSpace = "/lib:/lib64:/usr:/bin:/etc:/home"
)
View Source
const (
	// DefaultPort is the default cpu port.
	DefaultPort = "17010"
)
View Source
const DsDefault = "dnssd:"

Variables

View Source
var (
	// DefaultKeyFile is the default key for cpu users.
	DefaultKeyFile = filepath.Join(os.Getenv("HOME"), ".ssh/cpu_rsa")
	// Debug9p enables 9p debugging.
	Debug9p bool
	// Dump9p enables dumping 9p packets.
	Dump9p bool
	// DumpWriter is an io.Writer to which dump packets are written.
	DumpWriter io.Writer = os.Stderr
)
View Source
var V = func(string, ...interface{}) {}

V allows debug printing.

Functions

func DsLookup

func DsLookup(query dsQuery) (string, string, error)

lookup based on hostname, return resolved host, port, network, and error uri currently supported dnssd://domain/_service._network/instance?reqkey=reqvalue default for domain is local, first path element is _ncpu._tcp, and instance is wildcard can omit to underspecify, e.g. dnssd:?arch=arm64 to pick any arm64 cpu server

func DsParse

func DsParse(uri string) (dsQuery, error)

parse DNS-SD URI to dnssd struct we could subtype BrowseEntry or Service, but why?

func GetHostName

func GetHostName(host string) string

GetHostName reads the host name from the ssh config file, if needed. If it is not found, the host name is returned.

func GetKeyFile

func GetKeyFile(host, kf string) string

GetKeyFile picks a keyfile if none has been set. It will use ssh config, else use a default.

func GetPort

func GetPort(host, port string) (string, error)

GetPort gets a port. It verifies that the port fits in 16-bit space. The rules here are messy, since config.Get will return "22" if there is no entry in .ssh/config. 22 is not allowed. So in the case of "22", convert to defaultPort.

func SetVerbose

func SetVerbose(f func(string, ...interface{}))

Types

type Cmd

type Cmd struct {

	// CPU-specific options.
	// As in exec.Command, these controls are exposed and can
	// be set directly.
	Host string
	// HostName as found in .ssh/config; set to Host if not found
	HostName       string
	Args           []string
	Root           string
	HostKeyFile    string
	PrivateKeyFile string
	Port           string
	Timeout        time.Duration
	Env            []string
	Stdin          io.WriteCloser
	Stdout         io.Reader
	Stderr         io.Reader
	Row            int
	Col            int

	// NameSpace is a string as defined in the cpu documentation.
	NameSpace string
	// FSTab is an fstab(5)-format string
	FSTab string
	// Ninep determines if client will run a 9P server
	Ninep bool

	TmpMnt string
	// contains filtered or unexported fields
}

Cmd is a cpu client. It implements as much of exec.Command as makes sense.

func Command

func Command(host string, args ...string) *Cmd

Command implements exec.Command. The required parameter is a host. The args arg args to $SHELL. If there are no args, then starting $SHELL is assumed.

func (*Cmd) Close

func (c *Cmd) Close() error

Close ends a cpu session, doing whatever is needed.

func (*Cmd) Dial

func (c *Cmd) Dial() error

Dial implements ssh.Dial for cpu. Additionaly, if Cmd.Root is not "", it starts up a server for 9p requests.

func (*Cmd) HostKeyConfig

func (c *Cmd) HostKeyConfig(hostKeyFile string) error

HostKeyConfig sets the host key. It is optional.

func (*Cmd) Outputs

func (c *Cmd) Outputs() ([]bytes.Buffer, error)

Outputs returns a slice of bytes.Buffer for stdout and stderr, and an error if either had trouble being read.

func (*Cmd) Run

func (c *Cmd) Run() error

Run runs a command with Start, and waits for it to finish with Wait.

func (*Cmd) SSHStdin

func (c *Cmd) SSHStdin(w io.WriteCloser, r io.Reader)

SSHStdin implements an ssh-like reader, honoring ~ commands.

func (*Cmd) SetEnv

func (c *Cmd) SetEnv(envs ...string) error

SetEnv sets zero or more environment variables for a Session.

func (*Cmd) SetOptions

func (c *Cmd) SetOptions(opts ...Set) error

SetOptions sets various options into the Command.

func (*Cmd) SetupInteractive

func (c *Cmd) SetupInteractive() error

SetupInteractive sets up a cpu client for interactive access. It returns a function to be run when the session ends.

func (*Cmd) Signal

func (c *Cmd) Signal(s ssh.Signal) error

Signal implements ssh.Signal

func (*Cmd) Start

func (c *Cmd) Start() error

Start implements exec.Start for CPU.

func (*Cmd) TTYIn

func (c *Cmd) TTYIn(s *ssh.Session, w io.WriteCloser, r io.Reader)

TTYIn manages tty input for a cpu session. It exists mainly to deal with ~.

func (*Cmd) UserKeyConfig

func (c *Cmd) UserKeyConfig() error

UserKeyConfig sets up authentication for a User Key. It is required in almost all cases.

func (*Cmd) Wait

func (c *Cmd) Wait() error

Wait waits for a Cmd to finish.

type Set

type Set func(*Cmd) error

Set is the type of function used to set options in SetOptions.

func With9P

func With9P(p9 bool) Set

With9P enables the 9P2000 server in cpu. The server is by default disabled. Ninep is sticky; if set by, e.g., WithNameSpace, the Principle of Least Confusion argues that it should remain set. Hence, we || it with its current value.

func WithCpudCommand

func WithCpudCommand(cmd string) Set

WithCpudCommand sets the initial command to run on the remote side. This is extremely helpful when testing new implementations of cpud, of little use otherwise.

func WithFSTab

func WithFSTab(fstab string) Set

WithFSTab reads a file for the FSTab member.

func WithHostKeyFile

func WithHostKeyFile(key string) Set

WithHostKeyFile adds a host key to a Cmd

func WithNameSpace

func WithNameSpace(ns string) Set

WithNameSpace sets the namespace to Cmd.There is no default: having some default violates the principle of least surprise for package users. If ns is non-empty the Ninep is forced on.

func WithNetwork

func WithNetwork(network string) Set

WithNetwork sets the network. This almost never needs to be set, save for vsock.

func WithPort

func WithPort(port string) Set

WithPort sets the port in the Cmd. It calls GetPort with the passed-in port before assigning it.

func WithPrivateKeyFile

func WithPrivateKeyFile(key string) Set

WithPrivateKeyFile adds a private key file to a Cmd

func WithRoot

func WithRoot(root string) Set

WithRoot adds a root to a Cmd

func WithTempMount

func WithTempMount(tmpMnt string) Set

WithTempMount sets the private namespace mount point.

func WithTimeout

func WithTimeout(timeout string) Set

WithTimeout sets the 9p timeout.

Jump to

Keyboard shortcuts

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