class Specinfra::Backend::DockerNsenter

Specinfra and Serverspec backend for Docker `nsenter` execution driver.

Protected Instance Methods

docker_run!(cmd, opts = {}) click to toggle source

Runs a command inside a Docker container.

@param cmd [String] the command to run. @param opts [Hash] options to pass to {Open3.popen3}. @return [Specinfra::CommandResult] the result.

# File lib/specinfra/backend/docker_nsenter.rb, line 58
def docker_run!(cmd, opts = {})
  stdout, stderr, status = shell_command!(nsenter_command(cmd), opts)
  nsenter_result_assert(stderr, status)
  rspec_example_metadata(cmd, stdout, stderr)
  CommandResult.new(stdout: stdout, stderr: stderr, exit_status: status)
rescue NsenterError
  raise
rescue => e
  @container.kill
  erroneous_result(cmd, e, stdout, stderr, status)
end
nsenter_command(cmd) click to toggle source

Generates `nsenter` command to run.

@param cmd [String] the commands to run inside docker. @return [Array] the command to run as unescaped array.

# File lib/specinfra/backend/docker_nsenter.rb, line 36
def nsenter_command(cmd)
  pid = @container.json['State']['Pid']
  ['nsenter', '-t', pid, '-m', '-u', '-i', '-n', '-p', 'sh', '-c', cmd]
end
nsenter_result_assert(stderr, exit_status) click to toggle source

Parses `nsenter` command output and raises an exception if it is an error from the `nsenter` program.

@param stderr [String] command stderr output. @param exit_status [Fixnum] command exit status. @return nil

# File lib/specinfra/backend/docker_nsenter.rb, line 47
def nsenter_result_assert(stderr, exit_status)
  return if exit_status == 0
  return if stderr.match(/\A(nsenter|sudo): /).nil?
  fail NsenterError, stderr
end