Module: Kitchen::Docker::Helpers::CliHelper

Includes:
Configurable, Logging, ShellOut
Included in:
Container, ContainerHelper, ImageHelper, Kitchen::Driver::Docker
Defined in:
lib/kitchen/docker/helpers/cli_helper.rb

Overview

rubocop:disable Metrics/ModuleLength, Style/Documentation

Instance Method Summary collapse

Instance Method Details

#build_copy_command(local_file, remote_file, opts = {}) ⇒ Object

rubocop:enable Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/AbcSize



119
120
121
122
123
124
# File 'lib/kitchen/docker/helpers/cli_helper.rb', line 119

def build_copy_command(local_file, remote_file, opts = {})
  cmd = 'cp'
  cmd << ' -a' if opts[:archive]
  cmd << " #{local_file} #{remote_file}"
  cmd
end

#build_env_variable_args(vars) ⇒ Object

Raises:

  • (ActionFailed)


133
134
135
136
137
138
139
140
141
142
# File 'lib/kitchen/docker/helpers/cli_helper.rb', line 133

def build_env_variable_args(vars)
  raise ActionFailed, 'Environment variables are not of a Hash type' unless vars.is_a?(Hash)

  args = ''
  vars.each do |k, v|
    args << " -e #{k.to_s.strip}=\"#{v.to_s.strip}\""
  end

  args
end

#build_exec_command(state, command) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/AbcSize



103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/kitchen/docker/helpers/cli_helper.rb', line 103

def build_exec_command(state, command)
  cmd = 'exec'
  cmd << ' -d' if config[:detach]
  cmd << build_env_variable_args(config[:env_variables]) if config[:env_variables]
  cmd << ' --privileged' if config[:privileged]
  cmd << ' -t' if config[:tty]
  cmd << ' -i' if config[:interactive]
  cmd << " -u #{config[:username]}" if config[:username]
  cmd << " -w #{config[:working_dir]}" if config[:working_dir]
  cmd << " #{state[:container_id]}"
  cmd << " #{command}"
  logger.debug("build_exec_command: #{cmd}")
  cmd
end

#build_powershell_command(args) ⇒ Object



126
127
128
129
130
131
# File 'lib/kitchen/docker/helpers/cli_helper.rb', line 126

def build_powershell_command(args)
  cmd = 'powershell -ExecutionPolicy Bypass -NoLogo '
  cmd << args
  logger.debug("build_powershell_command: #{cmd}")
  cmd
end

#build_run_command(image_id, transport_port = nil) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/MethodLength, Metrics/AbcSize



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/kitchen/docker/helpers/cli_helper.rb', line 65

def build_run_command(image_id, transport_port = nil)
  cmd = 'run -d'
  cmd << ' -i' if config[:interactive]
  cmd << ' -t' if config[:tty]
  cmd << build_env_variable_args(config[:env_variables]) if config[:env_variables]
  cmd << " -p #{transport_port}" unless transport_port.nil?
  Array(config[:forward]).each { |port| cmd << " -p #{port}" }
  Array(config[:dns]).each { |dns| cmd << " --dns #{dns}" }
  Array(config[:add_host]).each { |host, ip| cmd << " --add-host=#{host}:#{ip}" }
  Array(config[:volume]).each { |volume| cmd << " -v #{volume}" }
  Array(config[:volumes_from]).each { |container| cmd << " --volumes-from #{container}" }
  Array(config[:links]).each { |link| cmd << " --link #{link}" }
  Array(config[:devices]).each { |device| cmd << " --device #{device}" }
  Array(config[:mount]).each {|mount| cmd << " --mount #{mount}"}
  Array(config[:tmpfs]).each {|tmpfs| cmd << " --tmpfs #{tmpfs}"}
  cmd << " --name #{config[:instance_name]}" if config[:instance_name]
  cmd << ' -P' if config[:publish_all]
  cmd << " -h #{config[:hostname]}" if config[:hostname]
  cmd << " -m #{config[:memory]}" if config[:memory]
  cmd << " -c #{config[:cpu]}" if config[:cpu]
  cmd << " --gpus #{config[:gpus]}" if config[:gpus]
  cmd << " -e http_proxy=#{config[:http_proxy]}" if config[:http_proxy]
  cmd << " -e https_proxy=#{config[:https_proxy]}" if config[:https_proxy]
  cmd << ' --privileged' if config[:privileged]
  cmd << " --isolation #{config[:isolation]}" if config[:isolation]
  Array(config[:cap_add]).each { |cap| cmd << " --cap-add=#{cap}"} if config[:cap_add]
  Array(config[:cap_drop]).each { |cap| cmd << " --cap-drop=#{cap}"} if config[:cap_drop]
  Array(config[:security_opt]).each { |opt| cmd << " --security-opt=#{opt}"} if config[:security_opt]
  cmd << " --platform=#{config[:docker_platform]}" if config[:docker_platform]
  extra_run_options = config_to_options(config[:run_options])
  cmd << " #{extra_run_options}" unless extra_run_options.empty?
  cmd << " #{image_id} #{config[:run_command]}"
  logger.debug("build_run_command: #{cmd}")
  cmd
end

#config_to_options(config) ⇒ String

Convert the config input for ‘:build_options` or `:run_options` in to a command line string for use with Docker.

rubocop:disable Metrics/CyclomaticComplexity

Parameters:

  • config (nil, String, Array, Hash)

    Config data to convert.

Returns:

  • (String)

Since:

  • 2.5.0



167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/kitchen/docker/helpers/cli_helper.rb', line 167

def config_to_options(config)
  case config
  when nil
    ''
  when String
    config
  when Array
    config.map { |c| config_to_options(c) }.join(' ')
  when Hash
    config.map { |k, v| Array(v).map { |c| "--#{k}=#{Shellwords.escape(c)}" }.join(' ') }.join(' ')
  end
end

#dev_nullObject



144
145
146
147
148
149
150
151
# File 'lib/kitchen/docker/helpers/cli_helper.rb', line 144

def dev_null
  case RbConfig::CONFIG['host_os']
  when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
    'NUL'
  else
    '/dev/null'
  end
end

#docker_command(cmd, options = {}) ⇒ Object

rubocop:disable Metrics/AbcSize



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/kitchen/docker/helpers/cli_helper.rb', line 29

def docker_command(cmd, options={})
  docker = config[:binary].dup
  docker << " -H #{config[:socket]}" if config[:socket]
  docker << ' --tls' if config[:tls]
  docker << ' --tlsverify' if config[:tls_verify]
  docker << " --tlscacert=#{config[:tls_cacert]}" if config[:tls_cacert]
  docker << " --tlscert=#{config[:tls_cert]}" if config[:tls_cert]
  docker << " --tlskey=#{config[:tls_key]}" if config[:tls_key]
  logger.debug("docker_command: #{docker} #{cmd} shell_opts: #{docker_shell_opts(options)}")
  run_command("#{docker} #{cmd}", docker_shell_opts(options))
end

#docker_shell_opts(options = {}) ⇒ Object



153
154
155
156
157
158
# File 'lib/kitchen/docker/helpers/cli_helper.rb', line 153

def docker_shell_opts(options = {})
  options[:live_stream] = nil if options[:suppress_output]
  options.delete(:suppress_output)

  options
end

#run_command(cmd, options = {}) ⇒ Object

Copied from kitchen because we need stderr rubocop:disable Metrics/MethodLength, Metrics/AbcSize



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/kitchen/docker/helpers/cli_helper.rb', line 44

def run_command(cmd, options = {})
  if options.fetch(:use_sudo, false)
    cmd = "#{options.fetch(:sudo_command, "sudo -E")} #{cmd}"
  end
  subject = "[#{options.fetch(:log_subject, "local")} command]"

  debug("#{subject} BEGIN (#{cmd})")
  sh = Mixlib::ShellOut.new(cmd, shell_opts(options))
  sh.run_command
  debug("#{subject} END #{Util.duration(sh.execution_time)}")
  sh.error!
  sh.stdout + sh.stderr
rescue Mixlib::ShellOut::ShellCommandFailed => ex
  raise ShellCommandFailed, ex.message
rescue Exception => error # rubocop:disable Lint/RescueException
  error.extend(Kitchen::Error)
  raise
end