class Specinfra::Backend::DockerLxc

Specinfra and Serverspec backend for Docker LXC 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_lxc.rb, line 92
def docker_run!(cmd, opts = {})
  stdout, stderr, status = shell_command!(lxc_attach_command(cmd), opts)
  lxc_attach_result_assert(stderr, status)
  rspec_example_metadata(cmd, stdout, stderr)
  CommandResult.new(stdout: stdout, stderr: stderr, exit_status: status)
rescue LxcAttachError
  raise
rescue => e
  @container.kill
  erroneous_result(cmd, e, stdout, stderr, status)
end
erroneous_result(cmd, exception, stdout, stderr, status) click to toggle source

Parses a rescued exception and returns the command result.

@param exception [Exception] the exception to parse. @param stdout [String] the stdout output. @param stderr [String] the stderr output. @param status [Fixnum] the command exit status. @return [Specinfra::CommandResult] the generated result object.

# File lib/specinfra/backend/docker_lxc.rb, line 62
def erroneous_result(cmd, exception, stdout, stderr, status)
  err =
    if stderr.nil?
      [exception.message] + exception.backtrace
    else
      [stderr]
    end
  sta = status.is_a?(Fixnum) && status != 0 ? status : 1
  rspec_example_metadata(cmd, stdout, err.join)
  CommandResult.new(stdout: stdout, stderr: err.join, exit_status: sta)
end
lxc_attach_command(cmd) click to toggle source

Generates `lxc-attach` 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_lxc.rb, line 38
def lxc_attach_command(cmd)
  id = @container.id
  ['lxc-attach', '-n', id, '--', 'sh', '-c', cmd]
end
lxc_attach_result_assert(stderr, exit_status) click to toggle source

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

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

# File lib/specinfra/backend/docker_lxc.rb, line 49
def lxc_attach_result_assert(stderr, exit_status)
  return if exit_status == 0
  return if stderr.match(/\A(lxc-attach|lxc_container|sudo): /).nil?
  fail LxcAttachError, stderr
end
rspec_example_metadata(cmd, stdout, stderr) click to toggle source

Updates RSpec metadata used by Serverspec.

@param cmd [Array<String>, String] the command (without `lxc-attach`). @param stdout [String] the stdout output. @param stderr [String] the stderr output. @return nil

# File lib/specinfra/backend/docker_lxc.rb, line 80
def rspec_example_metadata(cmd, stdout, stderr)
  return unless @example
  @example.metadata[:command] = escape_command(cmd)
  @example.metadata[:stdout] = stdout
  @example.metadata[:stderr] = stderr
end