Class: Kitchen::Verifier::Serverspec

Inherits:
Base
  • Object
show all
Defined in:
lib/kitchen/verifier/serverspec.rb

Overview

Serverspec verifier for Kitchen.

Instance Method Summary collapse

Instance Method Details

#bundler_cmdObject



300
301
302
# File 'lib/kitchen/verifier/serverspec.rb', line 300

def bundler_cmd
  config[:bundler_path] ? "#{config[:bundler_path]}/bundle" : '$(which bundle)'
end

#bundler_local_cmdObject



304
305
306
# File 'lib/kitchen/verifier/serverspec.rb', line 304

def bundler_local_cmd
  config[:bundler_path] ? "#{config[:bundler_path]}/bundle" : 'bundle'
end

#call(state) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/kitchen/verifier/serverspec.rb', line 51

def call(state)
  info("[#{name}] Verify on instance=#{instance} with state=#{state}")
  sleep_if_set
  merge_state_to_env(state)
  if config[:remote_exec]
    instance.transport.connection(state) do |conn|
      conn.execute(install_command)
      conn.execute(serverspec_commands)
    end
  else
    config[:default_path] = Dir.pwd if config[:default_path] == '/tmp/kitchen'
    install_command
    serverspec_commands
  end
  debug("[#{name}] Verify completed.")
end

#colorObject



336
337
338
# File 'lib/kitchen/verifier/serverspec.rb', line 336

def color
  config[:color] ? '-c' : nil
end

#custom_serverspec_commandObject



295
296
297
298
# File 'lib/kitchen/verifier/serverspec.rb', line 295

def custom_serverspec_command
  return config[:custom_serverspec_command] if config[:custom_serverspec_command]
  config[:serverspec_command]
end

#env_varsObject



269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/kitchen/verifier/serverspec.rb', line 269

def env_vars
  return nil if config[:env_vars].none?
  cmd = nil
  if !config[:remote_exec]
    config[:env_vars].map do |k, v|
      info("Environment variable #{k} value #{v}")
      ENV[k.to_s] = v.to_s
    end
  else
    cmd = config[:env_vars].map { |k, v| "#{k}=#{v}" }.join(' ')
    debug(cmd)
  end
  cmd
end

#fi_test_serverspec_installedObject



250
251
252
# File 'lib/kitchen/verifier/serverspec.rb', line 250

def fi_test_serverspec_installed
  config[:test_serverspec_installed] ? 'fi' : nil
end

#gem_proxy_parmObject



332
333
334
# File 'lib/kitchen/verifier/serverspec.rb', line 332

def gem_proxy_parm
  http_proxy ? "--http-proxy #{http_proxy}" : nil
end

#http_proxyObject



324
325
326
# File 'lib/kitchen/verifier/serverspec.rb', line 324

def http_proxy
  config[:http_proxy]
end

#https_proxyObject



328
329
330
# File 'lib/kitchen/verifier/serverspec.rb', line 328

def https_proxy
  config[:https_proxy]
end

#install_bundlerObject

private



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/kitchen/verifier/serverspec.rb', line 150

def install_bundler
  if config[:remote_exec]
    "      if [ $(\#{sudo('gem')} list bundler -i) == 'false' ]; then\n        \#{sudo_env('gem')} install \#{gem_proxy_parm} --no-ri --no-rdoc bundler\n      fi\n    INSTALL\n  else\n    begin\n      require 'bundler'\n    rescue LoadError\n      shellout `gem install --no-ri --no-rdoc  bundler`\n    end\n  end\nend\n"

#install_commandObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/kitchen/verifier/serverspec.rb', line 108

def install_command
  info('Installing with custom install command') if config[:custom_install_command]
  return config[:custom_install_command] if config[:custom_install_command]
  if config[:remote_exec]
    info('Installing ruby, bundler and serverspec remotely on server')
    "      \#{config[:additional_install_command]}\n      if [ ! $(which ruby) ]; then\n        echo '-----> Installing ruby, will try to determine platform os'\n        if [ -f /etc/centos-release ] || [ -f /etc/redhat-release ] || [ -f /etc/oracle-release ]; then\n          \#{sudo_env('yum')} -y install ruby\n        else\n          if [ -f /etc/system-release ] || [ grep -q 'Amazon Linux' /etc/system-release ]; then\n            \#{sudo_env('yum')} -y install ruby\n          else\n            \#{sudo_env('apt-get')} -y install ruby\n          fi\n        fi\n      fi\n      \#{install_bundler}\n      if [ -d \#{config[:default_path]} ]; then\n        \#{install_serverspec}\n        \#{install_runner}\n      else\n        echo \"ERROR: Default path '\#{config[:default_path]}' does not exist\"\n        exit 1\n      fi\n    INSTALL\n  else\n    info('Installing bundler and serverspec locally on workstation')\n    if config[:additional_install_command]\n      c = config[:additional_install_command]\n      shellout c\n    end\n    install_bundler\n    install_serverspec\n    install_runner\n  end\nend\n"

#install_gemfileObject



216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/kitchen/verifier/serverspec.rb', line 216

def install_gemfile
  if config[:gemfile]
    "    \#{read_gemfile}\n    INSTALL\n  else\n    <<-INSTALL\n    \#{sudo('rm')} -f \#{config[:default_path]}/Gemfile\n    \#{sudo('echo')} \"source 'https://rubygems.org'\" >> \#{config[:default_path]}/Gemfile\n    \#{sudo('echo')} \"gem 'net-ssh','~> 2.9'\"  >> \#{config[:default_path]}/Gemfile\n    \#{sudo('echo')} \"gem 'serverspec'\" >> \#{config[:default_path]}/Gemfile\n    INSTALL\n  end\nend\n"

#install_runnerObject



166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/kitchen/verifier/serverspec.rb', line 166

def install_runner
  if config[:require_runner]
    if config[:remote_exec]
      "        if [ ! -f \#{config[:default_path]}/\#{runner_filename} ]; then\n          \#{sudo_env('curl')} -o \#{config[:default_path]}/\#{runner_filename} \#{config[:runner_url]}\n        fi\n      INSTALL\n    else\n      raise ActionFailed, 'Serverspec Runners only for remote execution'\n    end\n  end\nend\n"

#install_serverspecObject

rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/kitchen/verifier/serverspec.rb', line 181

def install_serverspec
  if config[:remote_exec]
    "        \#{test_serverspec_installed}\n        \#{install_gemfile}\n        BUNDLE_CMD=\#{bundler_cmd}\n        echo $BUNDLE_CMD\n        \#{sudo_env('')} $BUNDLE_CMD install --gemfile=\#{config[:default_path]}/Gemfile\n      \#{fi_test_serverspec_installed}\n    INSTALL\n  else\n    if config[:test_serverspec_installed]\n      begin\n        require 'serverspec'\n        return\n      rescue LoadError\n        info('serverspec not installed installing ...')\n      end\n    end\n    unless config[:gemfile]\n      gemfile = \"\#{config[:default_path]}/Gemfile\"\n      File.open(gemfile, 'w') do |f|\n        f.write(\"source 'https://rubygems.org'\\ngem 'net-ssh','~> 2.9.4'\\ngem 'serverspec'\")\n      end\n    end\n    gemfile = config[:gemfile] if config[:gemfile]\n    begin\n      shellout \"\#{bundler_local_cmd} install --gemfile=\#{gemfile}\"\n    rescue\n      raise ActionFailed, 'Serverspec install failed'\n    end\n    nil\n  end\nend\n"

#merge_state_to_env(state) ⇒ Object



362
363
364
365
366
367
368
369
370
371
372
373
# File 'lib/kitchen/verifier/serverspec.rb', line 362

def merge_state_to_env(state)
  env_state = { :environment => {} }
  env_state[:environment]['KITCHEN_INSTANCE'] = instance.name
  env_state[:environment]['KITCHEN_PLATFORM'] = instance.platform.name
  env_state[:environment]['KITCHEN_SUITE'] = instance.suite.name
  state.each_pair do |key, value|
    env_state[:environment]['KITCHEN_' + key.to_s.upcase] = value.to_s
    ENV['KITCHEN_' + key.to_s.upcase] = value.to_s
    info("Environment variable #{'KITCHEN_' + key.to_s.upcase} value #{value}")
  end
  config[:shellout_opts].merge!(env_state)
end

#read_gemfileObject



231
232
233
234
235
236
237
238
239
# File 'lib/kitchen/verifier/serverspec.rb', line 231

def read_gemfile
  data = "#{sudo('rm')} -f #{config[:default_path]}/Gemfile\n"
  f = File.open(config[:gemfile], 'r')
  f.each_line do |line|
    data = "#{data}#{sudo('echo')} \"#{line}\" >> #{config[:default_path]}/Gemfile\n"
  end
  f.close
  data
end

#remove_default_pathObject



241
242
243
244
# File 'lib/kitchen/verifier/serverspec.rb', line 241

def remove_default_path
  info('Removing default path') if config[:remove_default_path]
  config[:remove_default_path] ? "rm -rf #{config[:default_path]}" : nil
end

#rspec_bash_cmdObject



308
309
310
# File 'lib/kitchen/verifier/serverspec.rb', line 308

def rspec_bash_cmd
  config[:rspec_path] ? "#{config[:rspec_path]}/rspec" : '$(which rspec)'
end

#rspec_cmdObject



265
266
267
# File 'lib/kitchen/verifier/serverspec.rb', line 265

def rspec_cmd
  config[:require_runner] ? "ruby #{config[:default_path]}/#{runner_filename}" : "#{rspec_path}rspec"
end

#rspec_commandsObject



254
255
256
257
258
259
260
261
262
263
# File 'lib/kitchen/verifier/serverspec.rb', line 254

def rspec_commands
  info('Running Serverspec')
  if config[:require_runner]
    "#{env_vars} #{sudo_env(rspec_cmd)} #{color} -f #{config[:format]} --default-path  #{config[:default_path]} #{rspec_path_option} #{config[:extra_flags]}"
  elsif config[:remote_exec]
    config[:patterns].map { |s| "#{env_vars} #{sudo_env('')} $RSPEC_CMD #{color} -f #{config[:format]} --default-path  #{config[:default_path]} #{config[:extra_flags]} -P #{s}" }.join(';')
  else
    config[:patterns].map { |s| "#{env_vars} #{sudo_env(rspec_cmd)} #{color} -f #{config[:format]} --default-path  #{config[:default_path]} #{config[:extra_flags]} -P #{s}" }.join(';')
  end
end

#rspec_pathObject



312
313
314
# File 'lib/kitchen/verifier/serverspec.rb', line 312

def rspec_path
  config[:rspec_path] ? "#{config[:rspec_path]}/" : nil
end

#rspec_path_optionObject



316
317
318
# File 'lib/kitchen/verifier/serverspec.rb', line 316

def rspec_path_option
  config[:rspec_path] ? "--rspec-path #{config[:rspec_path]}/" : nil
end

#run_commandObject

for legacy drivers.



69
70
71
72
# File 'lib/kitchen/verifier/serverspec.rb', line 69

def run_command
  sleep_if_set
  serverspec_commands
end

#runner_filenameObject



320
321
322
# File 'lib/kitchen/verifier/serverspec.rb', line 320

def runner_filename
  File.basename(config[:runner_url])
end

#serverspec_commandsObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/kitchen/verifier/serverspec.rb', line 79

def serverspec_commands
  if config[:remote_exec]
    if custom_serverspec_command
      "      \#{custom_serverspec_command}\n      INSTALL\n    else\n      <<-INSTALL\n      \#{config[:additional_serverspec_command]}\n      mkdir -p \#{config[:default_path]}\n      cd \#{config[:default_path]}\n      RSPEC_CMD=\#{rspec_bash_cmd}\n      echo $RSPEC_CMD\n      \#{rspec_commands}\n      \#{remove_default_path}\n      INSTALL\n    end\n  elsif custom_serverspec_command\n    shellout custom_serverspec_command\n  else\n    if config[:additional_serverspec_command]\n      c = config[:additional_serverspec_command]\n      shellout c\n    end\n    c = rspec_commands\n    shellout c\n  end\nend\n"

#setup_cmdObject



74
75
76
77
# File 'lib/kitchen/verifier/serverspec.rb', line 74

def setup_cmd
  sleep_if_set
  install_command
end

#shellout(command) ⇒ Object



350
351
352
353
354
355
356
357
358
359
360
# File 'lib/kitchen/verifier/serverspec.rb', line 350

def shellout(command)
  info("Running command: #{command}")
  cmd = Mixlib::ShellOut.new(command, config[:shellout_opts])
  cmd.live_stream = config[:live_stream]
  cmd.run_command
  begin
    cmd.error!
  rescue Mixlib::ShellOut::ShellCommandFailed
    raise ActionFailed, "Command #{command.inspect} failed for #{instance.to_str}"
  end
end

#sleep_if_setObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Sleep for a period of time, if a value is set in the config.



343
344
345
346
347
348
# File 'lib/kitchen/verifier/serverspec.rb', line 343

def sleep_if_set
  config[:sleep].to_i.times do
    print '.'
    sleep 1
  end
end

#sudo_env(pm) ⇒ Object



284
285
286
287
288
289
290
291
292
293
# File 'lib/kitchen/verifier/serverspec.rb', line 284

def sudo_env(pm)
  if config[:remote_exec]
    s = https_proxy ? "https_proxy=#{https_proxy}" : nil
    p = http_proxy ? "http_proxy=#{http_proxy}" : nil
    p || s ? "#{sudo('env')} #{p} #{s} #{pm}" : sudo(pm).to_s
  else
    # TODO: handle proxies

    pm
  end
end

#test_serverspec_installedObject



246
247
248
# File 'lib/kitchen/verifier/serverspec.rb', line 246

def test_serverspec_installed
  config[:test_serverspec_installed] ? "if [ $(#{sudo('gem')} list serverspec -i) == 'false' ]; then" : nil
end