executor

package
v2.3.2 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2026 License: MIT Imports: 28 Imported by: 0

Documentation

Overview

Package executor provides the command execution framework for Alpamon

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CommandDispatcher

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

CommandDispatcher manages command execution through registered handlers

func InitDispatcher

func InitDispatcher(
	pool *pool.Pool,
	ctxManager *agent.ContextManager,
	session *scheduler.Session,
	wsClient common.WSClient,
	callbacks SystemInfoCallbacks,
) (*CommandDispatcher, error)

InitDispatcher initializes and configures the command dispatching system with all handlers

func NewCommandDispatcher

func NewCommandDispatcher(pool *pool.Pool, ctxManager *agent.ContextManager) *CommandDispatcher

NewCommandDispatcher creates a new command dispatcher

func (*CommandDispatcher) Execute

func (e *CommandDispatcher) Execute(ctx context.Context, cmd string, args *common.CommandArgs) (int, string, error)

Execute runs a command with the appropriate handler

func (*CommandDispatcher) HasHandler

func (e *CommandDispatcher) HasHandler(cmd string) bool

HasHandler checks if a handler exists for the given command

func (*CommandDispatcher) RegisterHandler

func (e *CommandDispatcher) RegisterHandler(h common.Handler) error

RegisterHandler registers a command handler

func (*CommandDispatcher) Shutdown

func (e *CommandDispatcher) Shutdown(timeout time.Duration) error

Shutdown gracefully shuts down the executor

type CommandOptions

type CommandOptions struct {
	Args       []string          // Command and arguments
	Username   string            // Username to run as (empty = current user)
	Groupname  string            // Group to run as
	Env        map[string]string // Environment variables
	WorkingDir string            // Working directory
	Timeout    time.Duration     // Command timeout
	Input      string            // Input to provide via stdin

	// PIDHook, if non-nil, receives the child's pid after Start so the
	// shell handler can register it with the PAM tracker before the
	// child execs sudo. Panics are recovered and logged.
	PIDHook func(pid int)

	// ChunkCallback, if non-nil, receives streamed stdout/stderr chunks.
	// Sequencing is the caller's responsibility.
	ChunkCallback func(content string)
}

CommandOptions defines options for command execution

type Executor

type Executor struct{}

Executor provides system command execution with privilege management

func NewExecutor

func NewExecutor() *Executor

NewExecutor creates a new system command executor

func (*Executor) Exec

func (e *Executor) Exec(ctx context.Context, args []string, username, groupname string, env map[string]string, timeout time.Duration) (int, string, error)

Exec executes a command with all options

func (*Executor) ExecWithHook

func (e *Executor) ExecWithHook(ctx context.Context, args []string, username, groupname string, env map[string]string, timeout time.Duration, pidHook func(pid int)) (int, string, error)

ExecWithHook is like Exec but registers a PIDHook that receives the child's pid immediately after cmd.Start() succeeds. This is used by the shell handler to track the root pid of a deploy shell Command so sudo calls made inside the command can be authorized via command_id.

func (*Executor) ExecWithStreamingHook added in v2.3.1

func (e *Executor) ExecWithStreamingHook(ctx context.Context, args []string, username, groupname string, env map[string]string, timeout time.Duration, pidHook func(pid int), chunkCallback func(content string)) (int, string, error)

ExecWithStreamingHook combines PIDHook and ChunkCallback. Either may be nil.

func (*Executor) Execute

func (e *Executor) Execute(ctx context.Context, opts CommandOptions) (int, string, error)

Execute runs a command with full control over execution parameters

func (*Executor) Run

func (e *Executor) Run(ctx context.Context, name string, args ...string) (int, string, error)

Run executes a command with the given arguments

func (*Executor) RunAsUser

func (e *Executor) RunAsUser(ctx context.Context, username string, name string, args ...string) (int, string, error)

RunAsUser executes a command as a specific user

func (*Executor) RunWithInput

func (e *Executor) RunWithInput(ctx context.Context, input string, name string, args ...string) (int, string, error)

RunWithInput executes a command with stdin input

func (*Executor) RunWithTimeout

func (e *Executor) RunWithTimeout(ctx context.Context, timeout time.Duration, name string, args ...string) (int, string, error)

RunWithTimeout executes a command with a timeout

type HandlerFactory

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

HandlerFactory encapsulates handler instantiation and registration

func NewHandlerFactory

func NewHandlerFactory(dispatcher *CommandDispatcher, cmdExec common.CommandExecutor) *HandlerFactory

NewHandlerFactory creates a new handler factory

func (*HandlerFactory) RegisterAll

func (f *HandlerFactory) RegisterAll(
	pool *pool.Pool,
	ctxManager *agent.ContextManager,
	session *scheduler.Session,
	wsClient common.WSClient,
	callbacks SystemInfoCallbacks,
) error

RegisterAll registers all handlers with the provided callbacks

type Registry

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

Registry manages the registration and lookup of command handlers

func NewRegistry

func NewRegistry() *Registry

NewRegistry creates a new handler registry

func (*Registry) Clear

func (r *Registry) Clear()

Clear removes all handlers from the registry

func (*Registry) Get

func (r *Registry) Get(cmd string) (common.Handler, error)

Get retrieves a handler by command name

func (*Registry) GetHandler

func (r *Registry) GetHandler(name string) (common.Handler, error)

GetHandler retrieves a handler by handler name

func (*Registry) IsCommandRegistered

func (r *Registry) IsCommandRegistered(cmd string) bool

IsCommandRegistered checks if a command is registered

func (*Registry) List

func (r *Registry) List() []string

List returns all registered handlers

func (*Registry) ListCommands

func (r *Registry) ListCommands() []string

ListCommands returns all registered commands

func (*Registry) Register

func (r *Registry) Register(h common.Handler) error

Register adds a new handler to the registry

func (*Registry) Unregister

func (r *Registry) Unregister(name string) error

Unregister removes a handler from the registry

type SystemInfoAdapter

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

SystemInfoAdapter implements the common.SystemInfoManager interface for handlers

func NewSystemInfoAdapter

func NewSystemInfoAdapter(
	session *scheduler.Session,
	commitFunc func(),
	syncFunc func(*scheduler.Session, []string),
) *SystemInfoAdapter

NewSystemInfoAdapter creates a new system info adapter with function callbacks

func (*SystemInfoAdapter) CommitSystemInfo

func (a *SystemInfoAdapter) CommitSystemInfo()

CommitSystemInfo implements common.SystemInfoManager

func (*SystemInfoAdapter) SyncSystemInfo

func (a *SystemInfoAdapter) SyncSystemInfo(keys []string)

SyncSystemInfo implements common.SystemInfoManager

type SystemInfoCallbacks

type SystemInfoCallbacks struct {
	CommitFunc func()
	SyncFunc   func(*scheduler.Session, []string)
}

SystemInfoCallbacks contains function callbacks for system info operations

Directories

Path Synopsis
handlers

Jump to

Keyboard shortcuts

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