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



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/kitchen/transport/docker.rb', line 170

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:



133
134
135
136
137
138
139
140
# File 'lib/kitchen/transport/docker.rb', line 133

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:



110
111
112
113
114
115
116
117
118
119
# File 'lib/kitchen/transport/docker.rb', line 110

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



149
150
151
152
# File 'lib/kitchen/transport/docker.rb', line 149

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



196
197
198
# File 'lib/kitchen/transport/docker.rb', line 196

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



126
127
128
# File 'lib/kitchen/transport/docker.rb', line 126

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



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

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