sshutils

package
v0.0.0-...-21311fb Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2023 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// TestRegularFile file exists and is a regular file
	TestRegularFile = "-f"
	// TestDir file exists and is a directory
	TestDir = "-d"
)
View Source
const (
	SUDO = true
)

Variables

This section is empty.

Functions

func CheckTimeSync

func CheckTimeSync(ctx context.Context, nodes []SshNode) error

CheckTimeSync verifies current time is in sync across nodes

func Client

func Client(addr, user string, signer ssh.Signer) (*ssh.Client, error)

Client creates a new SSH client specified by addr and user. keyInput defines the SSH key to use for authentication. Returns a SSH client

func Connect

func Connect(addr, user string, signer ssh.Signer) (*ssh.Session, error)

Connect connects to remote SSH server and returns new session

func IsExitMissingError

func IsExitMissingError(err error) bool

func MakePrivateKeySignerFromFile

func MakePrivateKeySignerFromFile(path string) (ssh.Signer, error)

MakePrivateKeySignerFromFile creates a singer from the specified path

func MakePrivateKeySignerFromReader

func MakePrivateKeySignerFromReader(r io.Reader) (ssh.Signer, error)

MakePrivateKeySignerFromReader creates a singer from the specified reader

func ParseDiscard

func ParseDiscard(r *bufio.Reader) error

func PipeCommand

func PipeCommand(ctx context.Context, client *ssh.Client, log logrus.FieldLogger, cmd, dst string) error

PipeCommand will run a remote command and store as local file

func PutFile

func PutFile(ctx context.Context, client *ssh.Client, log logrus.FieldLogger, srcPath, dstDir string) (remotePath string, err error)

PutFile transfers local file to remote host directory

func Run

func Run(ctx context.Context, client *ssh.Client, log logrus.FieldLogger, cmd string, env map[string]string) error

Run is a simple method to run external program and don't care about its output or exit status

func RunAndParse

func RunAndParse(
	ctx context.Context,
	client *ssh.Client,
	log logrus.FieldLogger,
	cmd string,
	env map[string]string,
	parse OutputParseFn,
) (err error)

RunAndParse runs remote SSH command cmd with environment variables set with env. parse if set, will be provided the reader that consumes stdout of the command. Returns *ssh.ExitError if the command has completed with a non-0 exit code, *ssh.ExitMissingError if the other side has terminated the session without providing the exit code and nil for no errors

func RunCommandWithOutput

func RunCommandWithOutput(session *ssh.Session, log logrus.FieldLogger, command string, w io.Writer) (err error)

RunCommandWithOutput executes the specified command in given session and streams session's Stderr/Stdout into w. The function takes ownership of session and will destroy it upon completion of the command

func RunCommands

func RunCommands(ctx context.Context, client *ssh.Client, log logrus.FieldLogger, commands []Cmd) error

RunCommands executes commands sequentially

func RunScript

func RunScript(ctx context.Context, client *ssh.Client, log logrus.FieldLogger, scriptPath string, sudo bool) error

RunScript will run a .sh script on remote host if script should not be executed it should have internal flag files and terminate

func TestFile

func TestFile(ctx context.Context, client *ssh.Client, log logrus.FieldLogger, path, test string) error

TestFile tests remote file using `test` command. It returns trace.NotFound in case test fails, nil is test passes, and unspecified error otherwise

func TransferFile

func TransferFile(ctx context.Context, client *ssh.Client, log logrus.FieldLogger, fileUrl, dstDir string, env map[string]string) (path string, err error)

TransferFile takes file URL which may be S3 or HTTP or local file and transfers it to remote the machine fileUrl - file to download, could be S3:// or http(s)://

func WaitForFile

func WaitForFile(ctx context.Context, client *ssh.Client, log logrus.FieldLogger, path, test string) error

WaitForFile waits for a test to become true against a remote file (or context to expire)

func WaitTimeSync

func WaitTimeSync(ctx context.Context, nodes []SshNode) error

WaitTimeSync will ensure time is synchronized between the nodes provided otherwise an installation might fail we do not try to cross check times between nodes, but rather check their system clock drift from NTP source

Types

type Cmd

type Cmd struct {
	// Command is what is passed to remote shell
	Command string
	// Env are any environment variables to be set
	// Note that environment vars must be explicitly enabled in /etc/ssh/sshd.conf `AcceptEnv` directive
	Env map[string]string
}

Cmd defines command to execute on remote host

type ExitStatusError

type ExitStatusError interface {
	// ExitStatus reports the exist status of an operation
	ExitStatus() int
}

ExitStatusError describes the class of errors that report exit status

type OutputParseFn

type OutputParseFn func(r *bufio.Reader) error

func ParseAsString

func ParseAsString(out *string) OutputParseFn

type SshNode

type SshNode struct {
	Client *ssh.Client
	Log    logrus.FieldLogger
}

SshNode is ssh node client and logger

Jump to

Keyboard shortcuts

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