module Kitchen::Docker::Helpers::ContainerHelper

Public Instance Methods

container_env_variables(state) click to toggle source
# File lib/kitchen/docker/helpers/container_helper.rb, line 98
def container_env_variables(state)
  # Retrieves all environment variables from inside container
  vars = {}

  if state[:platform].include?('windows')
    cmd = build_powershell_command('-Command [System.Environment]::GetEnvironmentVariables() ^| ConvertTo-Json')
    cmd = build_exec_command(state, cmd)
    stdout = docker_command(cmd, suppress_output: !logger.debug?).strip
    vars = ::JSON.parse(stdout)
  else
    cmd = build_exec_command(state, 'printenv')
    stdout = docker_command(cmd, suppress_output: !logger.debug?).strip
    stdout.split("\n").each { |line| vars[line.split('=')[0]] = line.split('=')[1] }
  end

  vars
end
container_exec(state, command) click to toggle source
# File lib/kitchen/docker/helpers/container_helper.rb, line 64
def container_exec(state, command)
  cmd = build_exec_command(state, command)
  docker_command(cmd)
rescue => e
  raise "Failed to execute command on Docker container. #{e}"
end
container_exists?(state) click to toggle source
# File lib/kitchen/docker/helpers/container_helper.rb, line 60
def container_exists?(state)
  state[:container_id] && !!docker_command("top #{state[:container_id]}") rescue false
end
container_ip_address(state) click to toggle source
# File lib/kitchen/docker/helpers/container_helper.rb, line 136
def container_ip_address(state)
  cmd = "inspect --format '{{ .NetworkSettings.IPAddress }}'"
  cmd << " #{state[:container_id]}"
  docker_command(cmd).strip
rescue
  raise ActionFailed, 'Error getting internal IP of Docker container'
end
copy_file_to_container(state, local_file, remote_file) click to toggle source
# File lib/kitchen/docker/helpers/container_helper.rb, line 86
def copy_file_to_container(state, local_file, remote_file)
  debug("Copying local file #{local_file} to #{remote_file} on container")

  remote_file = replace_env_variables(state, remote_file)

  remote_file = "#{state[:container_id]}:#{remote_file}"
  cmd = build_copy_command(local_file, remote_file)
  docker_command(cmd)
rescue => e
  raise "Failed to copy file #{local_file} to container. #{e}"
end
create_dir_on_container(state, path) click to toggle source
# File lib/kitchen/docker/helpers/container_helper.rb, line 71
def create_dir_on_container(state, path)
  path = replace_env_variables(state, path)
  cmd = "mkdir -p #{path}"

  if state[:platform].include?('windows')
    psh = "-Command if(-not (Test-Path \'#{path}\')) { New-Item -Path \'#{path}\' -Force }"
    cmd = build_powershell_command(psh)
  end

  cmd = build_exec_command(state, cmd)
  docker_command(cmd)
rescue => e
  raise "Failed to create directory #{path} on container. #{e}"
end
dockerfile_path(file) click to toggle source
# File lib/kitchen/docker/helpers/container_helper.rb, line 56
def dockerfile_path(file)
  config[:build_context] ? Pathname.new(file.path).relative_path_from(Pathname.pwd).to_s : file.path
end
dockerfile_proxy_config() click to toggle source
# File lib/kitchen/docker/helpers/container_helper.rb, line 150
def dockerfile_proxy_config
  env_variables = ''
  if config[:http_proxy]
    env_variables << "ENV http_proxy #{config[:http_proxy]}\n"
    env_variables << "ENV HTTP_PROXY #{config[:http_proxy]}\n"
  end

  if config[:https_proxy]
    env_variables << "ENV https_proxy #{config[:https_proxy]}\n"
    env_variables << "ENV HTTPS_PROXY #{config[:https_proxy]}\n"
  end

  if config[:no_proxy]
    env_variables << "ENV no_proxy #{config[:no_proxy]}\n"
    env_variables << "ENV NO_PROXY #{config[:no_proxy]}\n"
  end

  env_variables
end
dockerfile_template() click to toggle source
# File lib/kitchen/docker/helpers/container_helper.rb, line 42
def dockerfile_template
  template = IO.read(File.expand_path(config[:dockerfile]))
  context = Kitchen::Docker::ERBContext.new(config.to_hash)
  ERB.new(template).result(context.get_binding)
end
parse_container_id(output) click to toggle source
# File lib/kitchen/docker/helpers/container_helper.rb, line 32
def parse_container_id(output)
  container_id = output.chomp

  unless [12, 64].include?(container_id.size)
    raise ActionFailed, 'Could not parse Docker run output for container ID'
  end

  container_id
end
remote_socket?() click to toggle source
# File lib/kitchen/docker/helpers/container_helper.rb, line 48
def remote_socket?
  config[:socket] ? socket_uri.scheme == 'tcp' : false
end
remove_container(state) click to toggle source
# File lib/kitchen/docker/helpers/container_helper.rb, line 144
def remove_container(state)
  container_id = state[:container_id]
  docker_command("stop -t 0 #{container_id}")
  docker_command("rm #{container_id}")
end
replace_env_variables(state, str) click to toggle source
# File lib/kitchen/docker/helpers/container_helper.rb, line 116
def replace_env_variables(state, str)
  if str.include?('$env:')
    key = str[/\$env:(.*?)(\\|$)/, 1]
    value = container_env_variables(state)[key].to_s.strip
    str = str.gsub("$env:#{key}", value)
  elsif str.include?('$')
    key = str[/\$(.*?)(\/|$)/, 1]
    value = container_env_variables(state)[key].to_s.strip
    str = str.gsub("$#{key}", value)
  end

  str
end
run_container(state, transport_port = nil) click to toggle source
# File lib/kitchen/docker/helpers/container_helper.rb, line 130
def run_container(state, transport_port = nil)
  cmd = build_run_command(state[:image_id], transport_port)
  output = docker_command(cmd)
  parse_container_id(output)
end
socket_uri() click to toggle source
# File lib/kitchen/docker/helpers/container_helper.rb, line 52
def socket_uri
  URI.parse(config[:socket])
end