Module: Kitchen::Docker::Helpers::ContainerHelper

Includes:
Configurable, CliHelper
Included in:
Container, ImageHelper, Kitchen::Driver::Docker
Defined in:
lib/kitchen/docker/helpers/container_helper.rb

Instance Method Summary collapse

Methods included from 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

Instance Method Details

#container_env_variables(state) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# 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) ⇒ Object



64
65
66
67
68
69
# 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) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# 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) ⇒ Object



136
137
138
139
140
141
142
# 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) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
# 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) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# 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) ⇒ Object



56
57
58
# 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_configObject



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# 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_templateObject



42
43
44
45
46
# 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) ⇒ Object



32
33
34
35
36
37
38
39
40
# 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?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/kitchen/docker/helpers/container_helper.rb', line 48

def remote_socket?
  config[:socket] ? socket_uri.scheme == 'tcp' : false
end

#remove_container(state) ⇒ Object



144
145
146
147
148
# 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) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
# 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) ⇒ Object



130
131
132
133
134
# 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_uriObject



52
53
54
# File 'lib/kitchen/docker/helpers/container_helper.rb', line 52

def socket_uri
  URI.parse(config[:socket])
end