Class: Kitchen::Docker::Container::Linux

Inherits:
Kitchen::Docker::Container show all
Includes:
Helpers::DockerfileHelper
Defined in:
lib/kitchen/docker/container/linux.rb

Constant Summary collapse

MUTEX_FOR_SSH_KEYS =
Mutex.new

Instance Method Summary collapse

Methods included from Helpers::DockerfileHelper

#arch_platform, #debian_platform, #dockerfile_base_linux, #dockerfile_platform, #fedora_platform, #gentoo_paludis_platform, #gentoo_platform, #opensuse_platform, #rhel_platform

Methods inherited from Kitchen::Docker::Container

#destroy, #hostname, #upload

Methods included from Helpers::ImageHelper

#build_image, #image_exists?, #parse_image_id, #remove_image

Methods included from Helpers::ContainerHelper

#container_env_variables, #container_exec, #container_exists?, #container_ip_address, #copy_file_to_container, #create_dir_on_container, #dockerfile_path, #dockerfile_proxy_config, #dockerfile_template, #parse_container_id, #remote_socket?, #remove_container, #replace_env_variables, #run_container, #socket_uri

Methods included from Helpers::CliHelper

#build_copy_command, #build_env_variable_args, #build_exec_command, #build_powershell_command, #build_run_command, #config_to_options, #dev_null, #docker_command, #docker_shell_opts, #run_command

Methods included from Helpers::FileHelper

#create_temp_file

Constructor Details

#initialize(config) ⇒ Linux

Returns a new instance of Linux.


30
31
32
# File 'lib/kitchen/docker/container/linux.rb', line 30

def initialize(config)
  super
end

Instance Method Details

#create(state) ⇒ Object


34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/kitchen/docker/container/linux.rb', line 34

def create(state)
  super

  debug('Creating Linux container')
  generate_keys

  state[:ssh_key] = @config[:private_key]
  state[:image_id] = build_image(state, dockerfile) unless state[:image_id]
  state[:container_id] = run_container(state, 22) unless state[:container_id]
  state[:hostname] = hostname(state)
  state[:port] = container_ssh_port(state)
end

#execute(command) ⇒ Object


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/kitchen/docker/container/linux.rb', line 47

def execute(command)
  # Create temp script file and upload files to container
  debug("Executing command on Linux container (Platform: #{@config[:platform]})")
  filename = "docker-#{::SecureRandom.uuid}.sh"
  temp_file = "./.kitchen/temp/#{filename}"
  create_temp_file(temp_file, command)

  remote_path = @config[:temp_dir]
  debug("Creating directory #{remote_path} on container")
  create_dir_on_container(@config, remote_path)

  debug("Uploading temp file #{temp_file} to #{remote_path} on container")
  upload(temp_file, remote_path)

  debug('Deleting temp file from local filesystem')
  ::File.delete(temp_file)

  # Replace any environment variables used in the path and execute script file
  debug("Executing temp script #{remote_path}/#{filename} on container")
  remote_path = replace_env_variables(@config, remote_path)

  container_exec(@config, "/bin/bash #{remote_path}/#{filename}")
rescue => e
  raise "Failed to execute command on Linux container. #{e}"
end