executor

package
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2024 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Overview

Copyright © 2020 Marvin

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Index

Constants

View Source
const (
	// DefaultExecuteTimeout default SSH command execution timeout, unit: seconds
	DefaultExecuteTimeout = 60
	// DefaultConnectTimeout default SSH connection timeout, unit: seconds
	DefaultConnectTimeout = 10
)

Variables

View Source
var (
	ErrPropSSHCommand     = errorx.RegisterPrintableProperty("ssh_command")
	ErrPropSSHStdout      = errorx.RegisterPrintableProperty("ssh_stdout")
	ErrPropSSHStderr      = errorx.RegisterPrintableProperty("ssh_stderr")
	ErrSSHExecuteFailed   = errNSSSH.NewType("execute_failed")
	ErrSSHExecuteTimedout = errNSSSH.NewType("execute_timeout")
)

Functions

func FindSSHAuthorizedKeysFile

func FindSSHAuthorizedKeysFile(ctx context.Context, exec Executor) string

FindSSHAuthorizedKeysFile Find the correct path to the SSH authorization key file

func ScpDownload

func ScpDownload(session *ssh.Session, client *ssh.Client, src, dst string, limit int) error

ScpDownload uses SCP to download files remotely refer: github.com/dtylman/scp

Types

type EasySSHExecutor

type EasySSHExecutor struct {
	Config *easyssh.MakeConfig
	Locale string // The locale to use when executing the command
	Sudo   bool   // Whether all commands run using this executor use sudo
}

EasySSHExecutor implements EasySSH Executor as the SSH transport protocol layer

func (*EasySSHExecutor) Execute

func (e *EasySSHExecutor) Execute(ctx context.Context, cmd string, sudo bool, execTimeout ...time.Duration) ([]byte, []byte, error)

Execute the run command over SSH, which does not invoke any specific shell by default

func (*EasySSHExecutor) Transfer

func (e *EasySSHExecutor) Transfer(ctx context.Context, src, dst string, download bool, limit int) error

Transfer the copy file via SCP This function depends on `scp` (tools from OpenSSH or other SSH implementations) This function is based on easyssh.MakeConfig.Scp() but supports copying to remote files

type Executor

type Executor interface {
	// Execute the run command and return stdout and stderr
	// If cmd times out and cannot exit, an error will be returned. The default timeout is 60 seconds.
	Execute(ctx context.Context, cmd string, sudo bool, timeout ...time.Duration) (stdout []byte, stderr []byte, err error)

	// Transfer copy files from or to target
	Transfer(ctx context.Context, src, dst string, download bool, limit int) error
}

Executor is the SSH executor interface, all tasks will be executed through the SSH executor

func New

func New(etype SSHType, sudo bool, c SSHConfig) (Executor, error)

type Local

type Local struct {
	Config *SSHConfig
	Sudo   bool   // all commands run with this executor will be using sudo
	Locale string // the locale used when executing the command
}

Local execute the command at local host.

func (*Local) Execute

func (l *Local) Execute(ctx context.Context, cmd string, sudo bool, execTimeout ...time.Duration) ([]byte, []byte, error)

Execute implements Executor interface.

func (*Local) Transfer

func (l *Local) Transfer(ctx context.Context, src, dst string, download bool, limit int) error

Transfer implements Executer interface.

type SSHConfig

type SSHConfig struct {
	Host           string        // SSH Host
	Port           int           // SSH Host Port
	User           string        // SSH Host Username
	Password       string        // SSH Host Username Password
	KeyFile        string        // SSH Private Key File
	Passphrase     string        // SSH Private Key Password
	ConnectTimeout time.Duration // TCP Connect timeout
	ExecuteTimeout time.Duration // Command execute timeout
	Proxy          *SSHConfig    // ssh proxy config
}

SSHConfig is the configuration required to establish an SSH connection

type SSHType

type SSHType string

SSHType represent the type of the chanel used by ssh

var (

	// SSHTypeBuiltin is the type of easy ssh executor
	SSHTypeBuiltin SSHType = "builtin"

	// SSHTypeNone is the type of local executor (no ssh will be used)
	SSHTypeNone SSHType = "none"
)

Jump to

Keyboard shortcuts

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