Class: Kitchen::Transport::Docker::Connection
- Inherits:
-
Base::Connection
- Object
- Base::Connection
- Kitchen::Transport::Docker::Connection
- 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
-
#build_login_command ⇒ Array<String>
private
Builds the argv array for an interactive
docker execsession. -
#container ⇒ Kitchen::Docker::Container
The container implementation for this platform.
-
#execute(command) ⇒ void
Runs a command inside the container.
-
#login_command ⇒ Kitchen::LoginCommand
The command
kitchen loginexecs to open a shell in the container. -
#login_shell ⇒ Array<String>
private
The shell to drop the user into, PowerShell on Windows and an interactive login bash elsewhere.
-
#upload(locals, remote) ⇒ Array<String>
Copies local files into the container.
-
#windows_container? ⇒ Boolean
private
Whether the platform under test is Windows.
Instance Method Details
#build_login_command ⇒ Array<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.
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 build_login_command 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(login_shell) logger.debug("build_login_command: #{(docker + cmd).join(" ")}") docker + cmd end |
#container ⇒ Kitchen::Docker::Container
The container implementation for this platform.
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.
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_command ⇒ Kitchen::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.
149 150 151 152 |
# File 'lib/kitchen/transport/docker.rb', line 149 def login_command argv = build_login_command LoginCommand.new(argv.first, argv.drop(1)) end |
#login_shell ⇒ Array<String> (private)
Returns 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 login_shell windows_container? ? ["powershell"] : ["/bin/bash", "--login", "-i"] end |
#upload(locals, remote) ⇒ Array<String>
Copies local files into the container.
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.
157 158 159 |
# File 'lib/kitchen/transport/docker.rb', line 157 def windows_container? @options[:platform].to_s.include?("windows") end |