commandrunner

package
v1.0.16 Latest Latest
Warning

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

Go to latest
Published: Jan 2, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetCommandDescription added in v1.0.15

func GetCommandDescription(cmd string) string

GetCommandDescription returns a user-friendly description for a command. If no description is found, returns a generic description based on the command.

Types

type APIClientAdapter

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

APIClientAdapter adapts pkg/api.Client to the ProxmoxAPIClient interface

func NewAPIClientAdapter

func NewAPIClientAdapter(client *api.Client) *APIClientAdapter

NewAPIClientAdapter creates a new API client adapter

func (*APIClientAdapter) ExecuteGuestAgentCommand

func (a *APIClientAdapter) ExecuteGuestAgentCommand(ctx context.Context, vm VM, command []string, timeout time.Duration) (stdout, stderr string, exitCode int, err error)

ExecuteGuestAgentCommand executes a command via QEMU guest agent

type AllowedCommands

type AllowedCommands struct {
	Host      []string `yaml:"host"`
	Container []string `yaml:"container"`
	VM        []string `yaml:"vm"`
	VMLinux   []string `yaml:"vm_linux,omitempty"`
	VMWindows []string `yaml:"vm_windows,omitempty"`
}

AllowedCommands defines whitelisted commands for different target types

type CommandInfo added in v1.0.15

type CommandInfo struct {
	Command     string
	Description string
}

CommandInfo holds a command and its user-friendly description

type CommandTemplate

type CommandTemplate struct {
	Template   string
	Parameters []string
}

CommandTemplate represents a command with parameter placeholders

func ParseTemplate

func ParseTemplate(cmd string) CommandTemplate

ParseTemplate extracts parameter names from a command template e.g., "systemctl status {service}" -> ["service"]

func (*CommandTemplate) FillTemplate

func (t *CommandTemplate) FillTemplate(values map[string]string) (string, error)

FillTemplate replaces placeholders with actual values e.g., "systemctl status {service}" + {"service": "nginx"} -> "systemctl status nginx"

type Config

type Config struct {
	Enabled         bool            `yaml:"enabled"`
	Timeout         time.Duration   `yaml:"timeout"`
	MaxOutputSize   int             `yaml:"max_output_size"`
	AllowedCommands AllowedCommands `yaml:"allowed_commands"`
}

Config holds the configuration for the command runner plugin

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns the default configuration for the command runner plugin

func (*Config) Validate

func (c *Config) Validate() error

Validate checks if the configuration is valid

type ExecutionResult

type ExecutionResult struct {
	Command   string
	Output    string
	Error     error
	ExitCode  int
	Duration  time.Duration
	Truncated bool
}

ExecutionResult represents the result of a command execution

type Executor

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

Executor handles command execution on various targets

func NewExecutor

func NewExecutor(config Config, sshClient SSHClient, apiClient ProxmoxAPIClient) *Executor

NewExecutor creates a new command executor

func (*Executor) ExecuteContainerCommand

func (e *Executor) ExecuteContainerCommand(ctx context.Context, host string, containerID int, command string) ExecutionResult

ExecuteContainerCommand executes a command in an LXC container via SSH to the host. It uses 'pct exec' to run the command inside the container.

func (*Executor) ExecuteHostCommand

func (e *Executor) ExecuteHostCommand(ctx context.Context, host, command string) ExecutionResult

ExecuteHostCommand executes a command on a Proxmox host via SSH

func (*Executor) ExecuteTemplatedCommand

func (e *Executor) ExecuteTemplatedCommand(ctx context.Context, targetType TargetType, host string, templateCmd string, params map[string]string) ExecutionResult

ExecuteTemplatedCommand executes a command with parameters filled in

func (*Executor) ExecuteTemplatedContainerCommand

func (e *Executor) ExecuteTemplatedContainerCommand(ctx context.Context, host string, containerID int, templateCmd string, params map[string]string) ExecutionResult

ExecuteTemplatedContainerCommand executes a templated command in an LXC container.

func (*Executor) ExecuteTemplatedVMCommand

func (e *Executor) ExecuteTemplatedVMCommand(ctx context.Context, vm VM, templateCmd string, params map[string]string) ExecutionResult

ExecuteTemplatedVMCommand executes a templated command in a QEMU VM via guest agent.

func (*Executor) ExecuteVMCommand

func (e *Executor) ExecuteVMCommand(ctx context.Context, vm VM, command string) ExecutionResult

ExecuteVMCommand executes a command in a QEMU VM via guest agent

func (*Executor) GetAllowedCommands

func (e *Executor) GetAllowedCommands(targetType TargetType) []string

GetAllowedCommands returns the whitelist for a target type

func (*Executor) GetAllowedVMCommands

func (e *Executor) GetAllowedVMCommands(vm VM) []string

GetAllowedVMCommands returns the whitelist for the provided VM context.

type MockSSHClient

type MockSSHClient struct {
	ExecuteFunc          func(ctx context.Context, host, command string) (string, error)
	ExecuteContainerFunc func(ctx context.Context, host string, containerID int, command string) (string, error)
}

MockSSHClient is a mock implementation for testing

func (*MockSSHClient) ExecuteCommand

func (m *MockSSHClient) ExecuteCommand(ctx context.Context, host, command string) (string, error)

ExecuteCommand calls the mock function

func (*MockSSHClient) ExecuteContainerCommand

func (m *MockSSHClient) ExecuteContainerCommand(ctx context.Context, host string, containerID int, command string) (string, error)

ExecuteContainerCommand calls the mock container function

type OSFamily

type OSFamily string

OSFamily represents a broad operating system family for VMs.

const (
	OSFamilyUnknown OSFamily = "unknown"
	OSFamilyLinux   OSFamily = "linux"
	OSFamilyWindows OSFamily = "windows"
)

type Plugin

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

Plugin implements the command runner plugin

func NewPlugin

func NewPlugin(config Config, sshClient SSHClient, apiClient ProxmoxAPIClient, app UIApp) (*Plugin, error)

NewPlugin creates a new command runner plugin

func (*Plugin) Enabled

func (p *Plugin) Enabled() bool

Enabled returns whether the plugin is enabled

func (*Plugin) ExecuteHostCommand

func (p *Plugin) ExecuteHostCommand(ctx context.Context, host, command string) ExecutionResult

ExecuteHostCommand executes a command on a host and returns the result

func (*Plugin) GetAllowedHostCommands

func (p *Plugin) GetAllowedHostCommands() []string

GetAllowedHostCommands returns the list of allowed host commands

func (*Plugin) Name

func (p *Plugin) Name() string

Name returns the plugin name

func (*Plugin) ShowContainerCommandMenu

func (p *Plugin) ShowContainerCommandMenu(node string, vmid int, onClose func())

ShowContainerCommandMenu displays the command menu for a container

func (*Plugin) ShowHostCommandMenu

func (p *Plugin) ShowHostCommandMenu(host string, onClose func())

ShowHostCommandMenu displays the command menu for a host

func (*Plugin) ShowVMCommandMenu

func (p *Plugin) ShowVMCommandMenu(vm VM, onClose func())

ShowVMCommandMenu displays the command menu for a VM

type ProxmoxAPIClient

type ProxmoxAPIClient interface {
	ExecuteGuestAgentCommand(ctx context.Context, vm VM, command []string, timeout time.Duration) (stdout, stderr string, exitCode int, err error)
}

ProxmoxAPIClient interface for Proxmox API operations (abstraction for testing)

type SSHClient

type SSHClient interface {
	ExecuteCommand(ctx context.Context, host, command string) (output string, err error)
	ExecuteContainerCommand(ctx context.Context, host string, containerID int, command string) (output string, err error)
}

SSHClient interface for SSH command execution (abstraction for testing)

type SSHClientConfig

type SSHClientConfig struct {
	Username string
	Password string
	KeyPath  string
	Timeout  time.Duration
	Port     int
}

SSHClientConfig holds configuration for SSH connections

type SSHClientImpl

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

SSHClientImpl implements SSH command execution using Go's crypto/ssh library

func NewSSHClient

func NewSSHClient(config SSHClientConfig) *SSHClientImpl

NewSSHClient creates a new SSH client with the given configuration

func (*SSHClientImpl) ExecuteCommand

func (c *SSHClientImpl) ExecuteCommand(ctx context.Context, host, command string) (string, error)

ExecuteCommand executes a command on a host via SSH

func (*SSHClientImpl) ExecuteContainerCommand

func (c *SSHClientImpl) ExecuteContainerCommand(ctx context.Context, host string, containerID int, command string) (string, error)

ExecuteContainerCommand executes a command in an LXC container via SSH to the host. It uses 'pct exec' to run the command inside the container.

Parameters:

  • ctx: Context for timeout and cancellation
  • host: Proxmox node hostname or IP
  • containerID: LXC container ID (VMID)
  • command: Command to execute inside the container

Returns the command output and any error encountered.

type TargetType

type TargetType string

TargetType represents where the command will be executed

const (
	TargetHost      TargetType = "host"
	TargetContainer TargetType = "container"
	TargetVM        TargetType = "vm"
)

type UIApp

type UIApp interface {
	Pages() *tview.Pages
	SetFocus(p tview.Primitive) *tview.Application
	QueueUpdateDraw(func()) *tview.Application
}

UIApp interface defines the minimal app methods needed by UIManager

type UIManager

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

UIManager handles UI interactions for the command runner plugin

func NewUIManager

func NewUIManager(app UIApp, executor *Executor) *UIManager

NewUIManager creates a new UI manager

func (*UIManager) ShowCommandMenu

func (u *UIManager) ShowCommandMenu(targetType TargetType, target string, onClose func())

ShowCommandMenu displays a list of available commands for selection

func (*UIManager) ShowErrorModal

func (u *UIManager) ShowErrorModal(title, message string, onClose func())

ShowErrorModal displays an error message in a modal

func (*UIManager) ShowResultModal

func (u *UIManager) ShowResultModal(result ExecutionResult, onClose func())

ShowResultModal displays the command execution result in a modal

func (*UIManager) ShowVMCommandMenu

func (u *UIManager) ShowVMCommandMenu(vm VM, onClose func())

ShowVMCommandMenu displays a list of commands tailored to a VM's OS.

type VM

type VM struct {
	ID           int
	Node         string
	Type         string
	Status       string
	AgentEnabled bool
	AgentRunning bool
	OSType       string
}

VM represents a minimal VM structure needed for guest agent execution

type Validator

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

Validator handles command whitelist validation

func NewValidator

func NewValidator(config Config) *Validator

NewValidator creates a new command validator

func (*Validator) GetAllowedCommands

func (v *Validator) GetAllowedCommands(targetType TargetType) []string

GetAllowedCommands returns the list of allowed commands for a target type

func (*Validator) GetAllowedVMCommands

func (v *Validator) GetAllowedVMCommands(vm VM) []string

GetAllowedVMCommands returns the whitelist for a VM after considering its OS.

func (*Validator) ValidateCommand

func (v *Validator) ValidateCommand(targetType TargetType, command string) error

ValidateCommand checks if a command is allowed for the given target type

func (*Validator) ValidateVMCommand

func (v *Validator) ValidateVMCommand(vm VM, command string) error

ValidateVMCommand applies OS-aware validation for VM commands.

Jump to

Keyboard shortcuts

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