Class: AgentHarness::DockerCommandExecutor

Inherits:
CommandExecutor show all
Defined in:
lib/agent_harness/docker_command_executor.rb

Overview

Executes commands inside a Docker container

Wraps commands with ‘docker exec` so they run inside the specified container rather than on the host.

Examples:

Basic usage

executor = AgentHarness::DockerCommandExecutor.new(container_id: "abc123")
result = executor.execute(["python", "script.py"])

With environment variables

result = executor.execute("echo $FOO", env: { "FOO" => "bar" })

Instance Attribute Summary collapse

Attributes inherited from CommandExecutor

#logger

Instance Method Summary collapse

Methods inherited from CommandExecutor

#available?

Constructor Details

#initialize(container_id:, logger: nil) ⇒ DockerCommandExecutor

Initialize the Docker command executor

Parameters:

  • container_id (String)

    the Docker container ID or name

  • logger (Logger, nil) (defaults to: nil)

    optional logger

Raises:



23
24
25
26
27
28
29
# File 'lib/agent_harness/docker_command_executor.rb', line 23

def initialize(container_id:, logger: nil)
  raise ArgumentError, "container_id cannot be nil or empty" if container_id.nil? || container_id.empty?

  super(logger: logger)
  @container_id = container_id
  validate_docker!
end

Instance Attribute Details

#container_idObject (readonly)

Returns the value of attribute container_id.



16
17
18
# File 'lib/agent_harness/docker_command_executor.rb', line 16

def container_id
  @container_id
end

Instance Method Details

#execute(command, timeout: nil, env: {}, stdin_data: nil) ⇒ Result

Execute a command inside the Docker container

Wraps the given command with ‘docker exec` and delegates to the parent class for actual process execution.

Parameters:

  • command (Array<String>, String)

    command to execute

  • timeout (Integer, nil) (defaults to: nil)

    timeout in seconds

  • env (Hash) (defaults to: {})

    environment variables to set in the container

  • stdin_data (String, nil) (defaults to: nil)

    data to send to stdin

Returns:

  • (Result)

    execution result



41
42
43
44
# File 'lib/agent_harness/docker_command_executor.rb', line 41

def execute(command, timeout: nil, env: {}, stdin_data: nil)
  docker_cmd = build_docker_command(command, env: env, stdin_data: stdin_data)
  super(docker_cmd, timeout: timeout, env: {}, stdin_data: stdin_data)
end

#which(binary) ⇒ String?

Check if a binary exists inside the container

Parameters:

  • binary (String)

    binary name

Returns:

  • (String, nil)

    full path or nil



50
51
52
53
# File 'lib/agent_harness/docker_command_executor.rb', line 50

def which(binary)
  result = execute(["which", binary], timeout: 5)
  result.success? ? result.stdout.strip : nil
end