class Specinfra::Backend::DockerComposeLxc

Specinfra and Serverspec backend for Docker Compose using LXC execution driver.

Protected Instance Methods

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

Runs a command inside a Docker Compose container.

@param cmd [String] The command to run. @param opts [Hash] Options to pass to {Open3.popen3}. @return [Specinfra::CommandResult] The result. @api public

# File lib/specinfra/backend/docker_compose_lxc.rb, line 62
def docker_compose_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 DockerLxc::LxcAttachError
  raise
rescue => e
  finalize
  erroneous_result(cmd, e)
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_compose_lxc.rb, line 39
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_compose_lxc.rb, line 50
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 DockerLxc::LxcAttachError, stderr
end