runner

package
v5.1.2 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const MaxOutputSize = 10 * 1024 * 1024

MaxOutputSize limits the combined output size to prevent memory issues with very large command outputs (10MB default)

View Source
const MaxStderrSize = 1 * 1024 * 1024

MaxStderrSize limits stderr capture (1MB) since stderr is typically small

Variables

This section is empty.

Functions

func ExecuteSSHCommand added in v5.0.3

func ExecuteSSHCommand(ctx context.Context, client *ssh.Client, command string) (string, error)

ExecuteSSHCommand runs `command` on an existing *ssh.Client and returns combined output. When ctx is cancelled mid-flight, a watcher goroutine opens a second SSH session and kills the remote process group (TERM then KILL). This is the shared cancel-aware code path used by SSHRunner and by ad-hoc helpers (e.g. the ssh_exec workflow function).

Why this matters: closing the local SSH session does NOT kill remote processes — sshd just detaches them. The pidfile + process-group kill is the only reliable way to terminate scanning still running on the remote.

Types

type CommandResult

type CommandResult struct {
	Output   string // Combined stdout and stderr
	ExitCode int    // Exit code of the command
	Error    error  // Error if execution failed
}

CommandResult holds the output of a command execution

type DockerRunner

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

DockerRunner executes commands inside a Docker container

func NewDockerRunner

func NewDockerRunner(config *core.RunnerConfig, binaryPath string) (*DockerRunner, error)

NewDockerRunner creates a new Docker runner

func (*DockerRunner) Cleanup

func (r *DockerRunner) Cleanup(ctx context.Context) error

Cleanup stops and removes the container

func (*DockerRunner) CopyFromRemote

func (r *DockerRunner) CopyFromRemote(ctx context.Context, remotePath, localPath string) error

CopyFromRemote copies a file from the container to the local host using docker cp

func (*DockerRunner) Execute

func (r *DockerRunner) Execute(ctx context.Context, command string) (*CommandResult, error)

Execute runs a command in the Docker container

func (*DockerRunner) GetContainerID

func (r *DockerRunner) GetContainerID() string

GetContainerID returns the current container ID (for persistent mode)

func (*DockerRunner) IsRemote

func (r *DockerRunner) IsRemote() bool

IsRemote returns true since Docker container is considered remote

func (*DockerRunner) SetPIDCallbacks added in v5.0.1

func (r *DockerRunner) SetPIDCallbacks(onStart, onEnd PIDCallback)

SetPIDCallbacks sets callbacks for process lifecycle events

func (*DockerRunner) Setup

func (r *DockerRunner) Setup(ctx context.Context) error

Setup prepares the Docker container

func (*DockerRunner) Type

func (r *DockerRunner) Type() core.RunnerType

Type returns the runner type

type HostRunner

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

HostRunner executes commands on the local machine

func NewHostRunner

func NewHostRunner(binariesPath string) *HostRunner

NewHostRunner creates a new host runner

func (*HostRunner) Cleanup

func (r *HostRunner) Cleanup(ctx context.Context) error

Cleanup is a no-op for host runner

func (*HostRunner) CopyFromRemote

func (r *HostRunner) CopyFromRemote(ctx context.Context, remotePath, localPath string) error

CopyFromRemote is not supported for host runner (local execution doesn't need file copy)

func (*HostRunner) Execute

func (r *HostRunner) Execute(ctx context.Context, command string) (*CommandResult, error)

Execute runs a command on the local machine

func (*HostRunner) IsRemote

func (r *HostRunner) IsRemote() bool

IsRemote returns false since host runner runs locally

func (*HostRunner) SetPIDCallbacks added in v5.0.1

func (r *HostRunner) SetPIDCallbacks(onStart, onEnd PIDCallback)

SetPIDCallbacks sets callbacks for process lifecycle events

func (*HostRunner) Setup

func (r *HostRunner) Setup(ctx context.Context) error

Setup is a no-op for host runner

func (*HostRunner) Type

func (r *HostRunner) Type() core.RunnerType

Type returns the runner type

type LimitedBuffer added in v5.0.1

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

LimitedBuffer wraps bytes.Buffer and silently discards writes after maxSize. This prevents unbounded memory growth when a tool produces GBs of output.

func NewLimitedBuffer added in v5.0.1

func NewLimitedBuffer(maxSize int) *LimitedBuffer

NewLimitedBuffer creates a LimitedBuffer with the given max size.

func (*LimitedBuffer) Bytes added in v5.0.1

func (lb *LimitedBuffer) Bytes() []byte

Bytes returns the buffered bytes.

func (*LimitedBuffer) Len added in v5.0.1

func (lb *LimitedBuffer) Len() int

Len returns the number of buffered bytes.

func (*LimitedBuffer) Overflow added in v5.0.1

func (lb *LimitedBuffer) Overflow() bool

Overflow returns true if any writes were discarded.

func (*LimitedBuffer) Write added in v5.0.1

func (lb *LimitedBuffer) Write(p []byte) (n int, err error)

Write implements io.Writer. Writes beyond maxSize are silently discarded.

type PIDCallback added in v5.0.1

type PIDCallback func(pid int)

PIDCallback is called when a process starts or ends

type Runner

type Runner interface {
	// Execute runs a command and returns the result
	Execute(ctx context.Context, command string) (*CommandResult, error)

	// Setup prepares the runner (e.g., copy binary, start container, establish SSH)
	Setup(ctx context.Context) error

	// Cleanup tears down resources (e.g., stop container, close SSH connection)
	Cleanup(ctx context.Context) error

	// Type returns the runner type
	Type() core.RunnerType

	// IsRemote returns true if commands run on a remote machine
	IsRemote() bool

	// CopyFromRemote copies a file from the remote environment to the local host
	// For Docker: uses docker cp, for SSH: uses rsync
	CopyFromRemote(ctx context.Context, remotePath, localPath string) error

	// SetPIDCallbacks sets callbacks for process lifecycle events.
	// onStart is called when a process starts (with the PID)
	// onEnd is called when the process ends (with the PID)
	// This enables tracking of running processes for cancellation support.
	SetPIDCallbacks(onStart, onEnd PIDCallback)
}

Runner interface for executing commands in different environments

func NewRunner

func NewRunner(workflow *core.Workflow, binaryPath string) (Runner, error)

NewRunner creates a runner based on workflow configuration

func NewRunnerFromType

func NewRunnerFromType(runnerType core.RunnerType, config *core.RunnerConfig, binaryPath string) (Runner, error)

NewRunnerFromType creates a runner from type and config directly

type SSHPool

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

SSHPool manages a pool of SSH connections for reuse

func GetSSHPool

func GetSSHPool() *SSHPool

GetSSHPool returns the global SSH connection pool

func (*SSHPool) CloseAll

func (p *SSHPool) CloseAll()

CloseAll closes all pooled connections

func (*SSHPool) Get

func (p *SSHPool) Get(ctx context.Context, config *core.RunnerConfig) (*ssh.Client, SSHPoolKey, error)

Get retrieves or creates an SSH connection for the given config

func (*SSHPool) Release

func (p *SSHPool) Release(key SSHPoolKey)

Release decrements the reference count for a connection

func (*SSHPool) Stop

func (p *SSHPool) Stop()

Stop stops the cleanup goroutine

type SSHPoolKey

type SSHPoolKey struct {
	Host string
	Port int
	User string
}

SSHPoolKey uniquely identifies an SSH connection target

func (SSHPoolKey) String

func (k SSHPoolKey) String() string

String returns a string representation of the pool key

type SSHRunner

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

SSHRunner executes commands on a remote machine via SSH

func NewSSHRunner

func NewSSHRunner(config *core.RunnerConfig, binaryPath string) (*SSHRunner, error)

NewSSHRunner creates a new SSH runner

func (*SSHRunner) Cleanup

func (r *SSHRunner) Cleanup(ctx context.Context) error

Cleanup releases the SSH connection back to the pool

func (*SSHRunner) CopyFromRemote

func (r *SSHRunner) CopyFromRemote(ctx context.Context, remotePath, localPath string) error

CopyFromRemote copies a file from the SSH host to local using rsync

func (*SSHRunner) CopyFromRemoteSFTP added in v5.0.2

func (r *SSHRunner) CopyFromRemoteSFTP(remotePath, localPath string) error

CopyFromRemoteSFTP copies a file from the SSH host to local using SFTP (no rsync dependency).

func (*SSHRunner) Execute

func (r *SSHRunner) Execute(ctx context.Context, command string) (*CommandResult, error)

Execute runs a command on the remote machine

func (*SSHRunner) GetRemoteDir

func (r *SSHRunner) GetRemoteDir() string

GetRemoteDir returns the remote directory where binary is stored

func (*SSHRunner) IsRemote

func (r *SSHRunner) IsRemote() bool

IsRemote returns true since SSH runner runs on a remote machine

func (*SSHRunner) SetPIDCallbacks added in v5.0.1

func (r *SSHRunner) SetPIDCallbacks(onStart, onEnd PIDCallback)

SetPIDCallbacks records process lifecycle callbacks. Remote PIDs are NOT forwarded to the local run control plane (the control plane's PID kill is a local syscall, so a remote PID could collide with an unrelated local process). Remote cancellation is instead driven by ctx.Done() inside runCommand, which kills the remote process group out-of-band via SSH.

func (*SSHRunner) Setup

func (r *SSHRunner) Setup(ctx context.Context) error

Setup establishes SSH connection and copies the binary Uses connection pooling to reuse SSH connections across steps

func (*SSHRunner) Type

func (r *SSHRunner) Type() core.RunnerType

Type returns the runner type

Jump to

Keyboard shortcuts

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