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_pathObject



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

def bundler_path
  config[:bundler_path] ? "#{config[:bundler_path]}/" : nil
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



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

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

#custom_serverspec_commandObject



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

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

#env_varsObject



272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/kitchen/verifier/serverspec.rb', line 272

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



255
256
257
# File 'lib/kitchen/verifier/serverspec.rb', line 255

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

#gem_proxy_parmObject



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

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

#http_proxyObject



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

def http_proxy
  config[:http_proxy]
end

#https_proxyObject



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

def https_proxy
  config[:https_proxy]
end

#install_bundlerObject

private



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/kitchen/verifier/serverspec.rb', line 156

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      system `gem install --no-ri --no-rdoc  bundler`\n    end\n  end\nend\n"

#install_commandObject



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
147
148
149
150
151
152
# File 'lib/kitchen/verifier/serverspec.rb', line 113

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      info(\"Running command: \#{c}\")\n      system c\n    end\n    install_bundler\n    install_serverspec\n    install_runner\n  end\nend\n"

#install_gemfileObject



221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/kitchen/verifier/serverspec.rb', line 221

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



172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/kitchen/verifier/serverspec.rb', line 172

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



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
215
216
217
218
219
# File 'lib/kitchen/verifier/serverspec.rb', line 187

def install_serverspec
  bundler_cmd = "#{bundler_path}bundler"
  if config[:remote_exec]
    "        \#{test_serverspec_installed}\n        \#{install_gemfile}\n        \#{sudo_env(bundler_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      system \"\#{bundler_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



357
358
359
360
361
362
363
364
365
366
# File 'lib/kitchen/verifier/serverspec.rb', line 357

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
  end
  config[:shellout_opts].merge!(env_state)
end

#read_gemfileObject



236
237
238
239
240
241
242
243
244
# File 'lib/kitchen/verifier/serverspec.rb', line 236

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



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

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_cmdObject



268
269
270
# File 'lib/kitchen/verifier/serverspec.rb', line 268

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

#rspec_commandsObject



259
260
261
262
263
264
265
266
# File 'lib/kitchen/verifier/serverspec.rb', line 259

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]}"
  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('\n')
  end
end

#rspec_pathObject



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

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

#rspec_path_optionObject



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

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



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

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
107
108
109
110
111
# 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      if [ -d \#{config[:default_path]} ]; then\n        cd \#{config[:default_path]}\n        \#{rspec_commands}\n        \#{remove_default_path}\n      else\n        echo \"ERROR: Default path '\#{config[:default_path]}' does not exist\"\n        exit 1\n      fi\n      INSTALL\n    end\n  elsif custom_serverspec_command\n    info(\"Running command: \#{custom_serverspec_command}\")\n    system custom_serverspec_command\n  else\n    if config[:additional_serverspec_command]\n      c = config[:additional_serverspec_command]\n      info(\"Running command: \#{c}\")\n      system c\n    end\n    c = rspec_commands\n    info(\"Running command: \#{c}\")\n    system 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



345
346
347
348
349
350
351
352
353
354
355
# File 'lib/kitchen/verifier/serverspec.rb', line 345

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, "Action #verify 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.



338
339
340
341
342
343
# File 'lib/kitchen/verifier/serverspec.rb', line 338

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

#sudo_env(pm) ⇒ Object



287
288
289
290
291
292
293
294
295
296
# File 'lib/kitchen/verifier/serverspec.rb', line 287

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



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

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