Class: Kitchen::Transport::Docker::Connection

Inherits:
Base::Connection
  • Object
show all
Includes:
Docker::Helpers::InspecHelper
Defined in:
lib/kitchen/transport/docker.rb

Overview

A connection to one container.

The superclass is named in full rather than relying on Ruby resolving the bare Connection constant through this class's ancestors.

Instance Method Summary collapse

Instance Method Details

#build_login_commandArray<String> (private)

Builds the argv array for an interactive docker exec session.

Kitchen hands the result to Kernel.exec in its multi-argument form, which bypasses the shell entirely. Every flag and its value therefore has to be its own token -- a packed "-H unix:///var/run/docker.sock" would reach Docker as a single argument -- and values must not be quoted, since there is no shell to strip the quotes back off.

Returns:

  • (Array<String>)

    the docker command and its arguments



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/kitchen/transport/docker.rb', line 176

def 
  docker = [@options[:binary]]
  docker.push("-H", @options[:socket]) if @options[:socket]
  docker << "--tls" if @options[:tls]
  docker << "--tlsverify" if @options[:tls_verify]
  docker << "--tlscacert=#{@options[:tls_cacert]}" if @options[:tls_cacert]
  docker << "--tlscert=#{@options[:tls_cert]}" if @options[:tls_cert]
  docker << "--tlskey=#{@options[:tls_key]}" if @options[:tls_key]

  # Always attached, always a TTY: a detached or non-interactive exec
  # would hand back a session the user cannot type into.
  cmd = ["exec"]
  cmd << "--privileged" if @options[:privileged]
  cmd.push("-t", "-i")
  Hash(@options[:env_variables]).each { |key, value| cmd.push("-e", "#{key}=#{value}") }
  cmd.push("-u", @options[:username]) if @options[:username]
  cmd.push("-w", @options[:working_dir]) if @options[:working_dir]
  cmd << @options[:container_id]
  cmd.concat()

  logger.debug("build_login_command: #{(docker + cmd).join(" ")}")
  docker + cmd
end

#containerKitchen::Docker::Container

The container implementation for this platform.

Returns:



139
140
141
142
143
144
145
146
# File 'lib/kitchen/transport/docker.rb', line 139

def container
  @container ||= if windows_container?
                   Kitchen::Docker::Container::Windows.new(@options)
                 else
                   Kitchen::Docker::Container::Linux.new(@options)
                 end
  @container
end

#execute(command) ⇒ void

This method returns an undefined value.

Runs a command inside the container.

Parameters:

  • command (String)

    the command to run; nil is a no-op

Raises:



116
117
118
119
120
121
122
123
124
125
# File 'lib/kitchen/transport/docker.rb', line 116

def execute(command)
  return if command.nil?

  debug("[Docker] Executing command: #{command}")
  info("[Docker] Executing command on container")

  container.execute(command)
rescue => e
  raise DockerFailed, "Docker failed to execute command on container. Error Details: #{e}"
end

#login_commandKitchen::LoginCommand

The command kitchen login execs to open a shell in the container.

Documented here rather than inherited via (see ...), because the superclass lives in the test-kitchen gem and YARD cannot resolve a reference into it from this project's docs.

Returns:

  • (Kitchen::LoginCommand)

    an interactive docker exec session



155
156
157
158
# File 'lib/kitchen/transport/docker.rb', line 155

def 
  argv = 
  LoginCommand.new(argv.first, argv.drop(1))
end

#login_shellArray<String> (private)

Returns the shell to drop the user into, PowerShell on Windows and an interactive login bash elsewhere.

Returns:

  • (Array<String>)

    the shell to drop the user into, PowerShell on Windows and an interactive login bash elsewhere



202
203
204
# File 'lib/kitchen/transport/docker.rb', line 202

def 
  windows_container? ? ["powershell"] : ["/bin/bash", "--login", "-i"]
end

#upload(locals, remote) ⇒ Array<String>

Copies local files into the container.

Parameters:

  • locals (String, Array<String>)

    one path or several

  • remote (String)

    destination path inside the container

Returns:

  • (Array<String>)

    the files copied



132
133
134
# File 'lib/kitchen/transport/docker.rb', line 132

def upload(locals, remote)
  container.upload(locals, remote)
end

#windows_container?Boolean (private)

Returns whether the platform under test is Windows.

Returns:

  • (Boolean)

    whether the platform under test is Windows



163
164
165
# File 'lib/kitchen/transport/docker.rb', line 163

def windows_container?
  @options[:platform].to_s.include?("windows")
end