forked from ranand19/circuit-oneops-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
monkey_patch.rb
346 lines (290 loc) · 10.2 KB
/
monkey_patch.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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
100
101
102
103
104
105
106
107
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
require 'fileutils'
=begin
Address hashie warning spam.
https://github.com/berkshelf/berkshelf/pull/1668
=end
require "hashie"
require "hashie/logger"
Hashie.logger = Logger.new(nil)
=begin
Expose WORKORDER environment variable for kitchen/verifier/busser
=end
require "kitchen"
require "kitchen/version"
module Kitchen
class Instance
def converge_action
if !verifier[:transport].eql?("local")
banner "Converging #{to_str}..."
elapsed = action(:converge) do |state|
if legacy_ssh_base_driver?
legacy_ssh_base_converge(state)
else
provisioner.call(state)
end
end
info("Finished converging #{to_str} #{Util.duration(elapsed.real)}.")
else
banner "Skipping converging step"
end
self
end
def legacy_ssh_base_setup(state)
warn("Running legacy setup for '#{driver.name}' Driver")
# TODO: Document upgrade path and provide link
# warn("Driver authors: please read http://example.com for more details.")
if !verifier[:transport].eql?("local")
driver.setup(state)
else
banner "Skipping #{driver.name} setup step"
end
end
end
end
module Kitchen
module Driver
class Proxy < Kitchen::Driver::SSHBase
def reset_instance(state)
if config[:transport] && config[:transport].eql?("local")
info("Transport mode #{config[:transport]} so no-ops")
else
if cmd = config[:reset_command]
info("Resetting instance state with command: #{cmd}")
ssh(build_ssh_args(state), cmd)
end
end
end
end
end
end
require "kitchen/verifier/busser"
module Kitchen
module Verifier
class Busser
def busser_env
root = config[:sandbox] ? config[:sandbox] : config[:root_path]
busser_root = config[:busser_root] ? config[:busser_root] : root
gem_home = config[:gem_home] ? config[:gem_home] : remote_path_join(root, "gems")
gem_path = config[:gem_path] ? config[:gem_path] : nil
gem_cache = config[:gem_cache] ? config[:gem_cache] : remote_path_join(root, "cache")
array_env_var = [
shell_env_var("BUSSER_ROOT", busser_root),
shell_env_var("GEM_HOME", gem_home),
shell_env_var("GEM_CACHE", gem_cache),
shell_env_var("WORKORDER", ENV['WORKORDER'])
]
array_env_var.push(shell_env_var("GEM_PATH", gem_path)) if gem_path
array_env_var.join("\n")
.tap { |str| str.insert(0, reload_ps1_path) if windows_os? }
end
def run_command
return if local_suite_files.empty?
if config[:transport] && config[:transport].eql?("local")
config[:sandbox] = sandbox_path
end
if config[:sudo] && config[:sudo].eql?("false")
cmd = config[:busser_bin].dup
.tap { |str| str.insert(0, "& ") if powershell_shell? }
else
cmd = sudo(config[:busser_bin]).dup
.tap { |str| str.insert(0, "& ") if powershell_shell? }
end
prefix_command(<<-CMD).chomp
#{busser_env} ; #{cmd} test #{plugins.join(" ").gsub!("busser-", "")}
CMD
end
end
end
end
=begin
Monkey patch ridley/chef/cookbook/metadata to safeguard
in scenario where name is not lowercase in metadata.
=end
require "ridley"
module Ridley::Chef
class Cookbook
class Metadata
def name(arg = nil)
arg = arg.nil? ? nil : arg.downcase
set_or_return(
:name,
arg,
:kind_of => [ String ]
)
end
end
end
end
=begin
Ensure that while copying sandbox it will have
the correct structure with lowercase.
=end
module Kitchen
module Provisioner
module Chef
class CommonSandbox
def cp_this_cookbook
info("Preparing current project directory as a cookbook")
debug("Using metadata.rb from #{metadata_rb}")
cb_name = MetadataChopper.extract(metadata_rb).first || raise(UserError,
"The metadata.rb does not define the 'name' key." \
" Please add: `name '<cookbook_name>'` to metadata.rb and retry")
cb_name.downcase!
cb_path = File.join(tmpbooks_dir, cb_name)
glob = Util.list_directory(config[:kitchen_root])
FileUtils.mkdir_p(cb_path)
FileUtils.cp_r(glob, cb_path)
end
end
end
end
end
=begin
Modify existing chef_solo to run extra command before
actual execution of chef_solo.
=end
require "kitchen/provisioner/chef_base"
module Kitchen
module Provisioner
class ChefSolo < ChefBase
def prepare_script
info("Preparing script")
if config[:script]
debug("Using script from #{config[:script]}")
FileUtils.cp_r(config[:script], sandbox_path)
else
prepare_stubbed_script
end
FileUtils.chmod(0755,
File.join(sandbox_path, File.basename(config[:script])))
end
# Create a minimal, no-op script in the sandbox path.
#
# @api private
def prepare_stubbed_script
base = powershell_shell? ? "bootstrap.ps1" : "bootstrap.sh"
config[:script] = File.join(sandbox_path, base)
info("#{File.basename(config[:script])} not found " \
"so Kitchen will run a stubbed script. Is this intended?")
File.open(config[:script], "wb") do |file|
if powershell_shell?
file.write(%{Write-Host "NO BOOTSTRAP SCRIPT PRESENT`n"\n})
else
file.write(%{#!/bin/sh\necho "NO BOOTSTRAP SCRIPT PRESENT"\n})
end
end
end
def run_command
config[:log_level] = "info" if !modern? && config[:log_level] = "auto"
cmd = sudo(config[:chef_solo_path]).dup
.tap { |str| str.insert(0, "& ") if powershell_shell? }
## begin of bootstrap
# added shell provision to run before converge
script = remote_path_join(
config[:root_path], File.basename(config[:script])
)
code = powershell_shell? ? %{& "#{script}"} : sudo(script)
## end of bootstrap
chef_cmd("#{code} ; #{cmd}")
end
def create_sandbox
super
prepare_config_rb
prepare_script
end
end
end
end
require 'kitchen/verifier/serverspec'
# Monkey-patching kitchen-verifier-serverspec to use shellout instead of
# kernel.system, to correctly handle failures, i.e. re-translate exit code.
# This is fixed in the newer versions of the kitchen-verifier-serverspec gem,
# but we cannot use them due to different gem dependencies and a gem conflict:
# kitchen-verifier-serverspec (>0.4.0) depends on net-ssh ~> 3.0
# and at the same time chef-11.18.12 depends on net-ssh ~> 2.6
module Kitchen
module Verifier
# Serverspec verifier for Kitchen.
class Serverspec < Kitchen::Verifier::Base
def serverspec_commands
if config[:remote_exec]
if custom_serverspec_command
<<-INSTALL
#{custom_serverspec_command}
INSTALL
else
<<-INSTALL
#{config[:additional_serverspec_command]}
mkdir -p #{config[:default_path]}
cd #{config[:default_path]}
RSPEC_CMD=#{rspec_bash_cmd}
echo "---> RSPEC_CMD variable is: ${RSPEC_CMD}"
#{rspec_commands}
#{remove_default_path}
INSTALL
end
elsif custom_serverspec_command
shellout custom_serverspec_command
else
if config[:additional_serverspec_command]
c = config[:additional_serverspec_command]
shellout c
end
c = rspec_commands
shellout c
end
end
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
# if using a driver that uses transport expose those too
%w[username password ssh_key port].each do |key|
next if instance.transport[key.to_sym].nil?
value = instance.transport[key.to_sym].to_s
ENV['KITCHEN_' + key.to_s.upcase] = value
info("Transport Environment variable #{'KITCHEN_' + key.to_s.upcase} value #{value}")
end
config[:shellout_opts].merge!(env_state)
end
end
end
end
require 'kitchen/transport/rsync'
module Kitchen
module Transport
class Rsync < Ssh
def create_new_connection(options, &block)
if @connection
logger.debug("[SSH] shutting previous connection #{@connection}")
@connection.close
end
@connection_options = options
@connection = self.class::Connection.new(options, &block)
end
class Connection < Ssh::Connection
def upload(locals, remote)
remote = remote.sub(/^([A-z]):\//, '/cygdrive/\1/')
Array(locals).each do |local|
full_remote = File.join(remote, File.basename(local))
recursive = File.directory?(local)
execute("mkdir -p #{full_remote}") if recursive
time = Benchmark.realtime do
ssh_command = [login_command.command, login_command.arguments[0..-2]].flatten.join(' ')
sync_command = "rsync -e '#{ssh_command}' -a#{@logger.debug? ? 'v' : ''}z #{local} #{@session.options[:user]}@#{@session.host}:#{remote}"
@logger.debug("[RSYNC] Running rsync command: #{sync_command}")
system(sync_command)
end
logger.debug("[RSYNC] Time taken to upload #{local} to #{self}:#{full_remote}: %.2f sec" % time)
end
end
end
end
end
end