Class: Chef::Knife::VagrantServerCreate
Instance Method Summary
collapse
#colored_vagrant_state, included, #locate_config_value, #msg_pair, #vagrant_exec, #vagrant_instance_list, #vagrant_instance_state, #vagrant_version, #vagrant_version_cmp, #write_insecure_key
Instance Method Details
#bootstrap_node(server, ssh_host) ⇒ Object
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
|
# File 'lib/chef/knife/vagrant_server_create.rb', line 281
def bootstrap_node(server,ssh_host)
bootstrap = Chef::Knife::Bootstrap.new
bootstrap.name_args = [ssh_host]
bootstrap.config[:ssh_user] = config[:ssh_user]
bootstrap.config[:ssh_port] = config[:ssh_port]
bootstrap.config[:ssh_gateway] = config[:ssh_gateway]
bootstrap.config[:chef_node_name] = locate_config_value(:chef_node_name) || server.ip
msg_pair("Vagrant version", vagrant_version)
if config[:identity_file]
bootstrap.config[:identity_file] = config[:identity_file]
elsif vagrant_version_cmp('1.7') < 0
write_insecure_key
bootstrap.config[:identity_file] = File.join(locate_config_value(:vagrant_dir), 'insecure_key')
else
path = File.join(locate_config_value(:vagrant_dir), server.name, '.vagrant/')
Find.find(path) do |f|
if File.basename(f) == 'private_key'
bootstrap.config[:identity_file] = f
end
end
end
bootstrap.config[:distro] = locate_config_value(:distro) || "chef-full"
bootstrap.config[:use_sudo] = true unless config[:ssh_user] == 'root'
bootstrap.config[:host_key_verify] = config[:host_key_verify]
bootstrap.config[:run_list] = config[:run_list]
bootstrap.config[:prerelease] = config[:prerelease]
bootstrap.config[:bootstrap_version] = locate_config_value(:bootstrap_version)
bootstrap.config[:distro] = locate_config_value(:distro)
bootstrap.config[:template_file] = locate_config_value(:template_file)
bootstrap.config[:environment] = locate_config_value(:environment)
bootstrap.config[:prerelease] = config[:prerelease]
bootstrap.config[:bootstrap_version] = locate_config_value(:bootstrap_version)
bootstrap.config[:first_boot_attributes] = locate_config_value(:json_attributes) || {}
bootstrap.config[:encrypted_data_bag_secret] = locate_config_value(:encrypted_data_bag_secret)
bootstrap.config[:encrypted_data_bag_secret_file] = locate_config_value(:encrypted_data_bag_secret_file)
bootstrap.config[:secret] = locate_config_value(:secret)
bootstrap.config[:secret_file] = locate_config_value(:secret_file)
Chef::Config[:knife][:hints] ||= {}
Chef::Config[:knife][:hints]["vagrant"] ||= {}
msg_pair("SSH User", bootstrap.config[:ssh_user])
msg_pair("SSH identity file", bootstrap.config[:identity_file])
bootstrap
end
|
#build_port_forwards(ports) ⇒ Object
211
212
213
|
# File 'lib/chef/knife/vagrant_server_create.rb', line 211
def build_port_forwards(ports)
ports.collect { |k, v| "config.vm.network :forwarded_port, host: #{k}, guest: #{v}, host_ip: '127.0.0.1'" }.join("\n")
end
|
#build_shares(share_folders) ⇒ Object
223
224
225
226
227
228
|
# File 'lib/chef/knife/vagrant_server_create.rb', line 223
def build_shares(share_folders)
share_folders.collect do |share|
host, guest = share.chomp.split "::"
"config.vm.synced_folder '#{host}', '#{guest}'"
end.join("\n")
end
|
#build_vb_customize(customize) ⇒ Object
215
216
217
|
# File 'lib/chef/knife/vagrant_server_create.rb', line 215
def build_vb_customize(customize)
customize.split(/::/).collect { |k| "vb.customize [ #{k} ]" }.join("\n") if customize
end
|
#build_vmx_customize(customize) ⇒ Object
219
220
221
|
# File 'lib/chef/knife/vagrant_server_create.rb', line 219
def build_vmx_customize(customize)
customize.split(/::/).collect { |k| "v.vmx[#{k.split('=')[0]}] = #{k.split('=')[1]}" }.join("\n") if customize
end
|
#create_server_def ⇒ Object
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
|
# File 'lib/chef/knife/vagrant_server_create.rb', line 356
def create_server_def
server_def = {
:box => locate_config_value(:box),
:box_url => locate_config_value(:box_url),
:memsize => locate_config_value(:memsize),
:share_folders => config[:share_folders],
:port_forward => config[:port_forward],
:use_cachier => config[:use_cachier],
:vb_customize => locate_config_value(:vb_customize),
:vmx_customize => locate_config_value(:vmx_customize),
:vagrant_config => locate_config_value(:vagrant_config)
}
server_def[:ip_address] = config[:ip_address] || find_available_ip
collision = vagrant_instance_list.detect { |i| i[:ip_address] == server_def[:ip_address] }
if collision
ui.error("IP address #{server_def[:ip_address]} already in use by instance #{collision[:name]}")
exit 1
end
server_def[:name] = locate_config_value(:chef_node_name) || server_def[:ip_address]
OpenStruct.new(server_def)
end
|
#find_available_ip ⇒ Object
342
343
344
345
346
347
348
349
350
351
352
353
354
|
# File 'lib/chef/knife/vagrant_server_create.rb', line 342
def find_available_ip
subnet = locate_config_value('subnet')
IPAddr.new(subnet).to_range().each { |ip|
mask = IPAddr::IN4MASK ^ ip.instance_variable_get("@mask_addr")
unless [0, 1, mask].include? (ip & mask) or vagrant_instance_list.detect { |i| i[:ip_address] == ip.to_s }
return ip.to_s
end
}
ui.error("No unused IP address available in subnet #{subnet}")
exit 1
end
|
#run ⇒ Object
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
|
# File 'lib/chef/knife/vagrant_server_create.rb', line 174
def run
$stdout.sync = true
validate!
@server = create_server_def
msg_pair("Instance name", @server.name)
msg_pair("Instance IP", @server.ip_address)
msg_pair("Box", @server.box || @server.box_url)
if vagrant_instance_list.detect { |i| i[:name] == @server.name }
ui.error("Instance #{@server.name} already exists")
exit 1
end
print "\n#{ui.color("Launching instance", :magenta)}\n"
write_vagrantfile
cmd = "up --provider #{locate_config_value(:provider)}"
vagrant_exec(@server.name, cmd)
print "\n#{ui.color("Waiting for sshd", :magenta)}"
wait_for_sshd(@server.ip_address)
print "\n#{ui.color("Bootstraping instance", :magenta)}\n"
bootstrap_node(@server,@server.ip_address).run
puts "\n"
msg_pair("Instance Name", @server.name)
msg_pair("Box", @server.box || @server.box_url)
msg_pair("IP Address", @server.ip_address)
msg_pair("Environment", locate_config_value(:environment) || '_default')
msg_pair("Run List", (config[:run_list] || []).join(', '))
msg_pair("JSON Attributes", config[:json_attributes]) unless !config[:json_attributes] || config[:json_attributes].empty?
end
|
#tcp_test_ssh(hostname, ssh_port) ⇒ Object
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
|
# File 'lib/chef/knife/vagrant_server_create.rb', line 420
def tcp_test_ssh(hostname, ssh_port)
tcp_socket = TCPSocket.new(hostname, ssh_port)
readable = IO.select([tcp_socket], nil, nil, 5)
if readable
Chef::Log.debug("sshd accepting connections on #{hostname}, banner is #{tcp_socket.gets}")
yield
true
else
false
end
rescue SocketError, Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Errno::ENETUNREACH, IOError
sleep 2
false
rescue Errno::EPERM, Errno::ETIMEDOUT
false
rescue Errno::ECONNRESET
sleep 2
false
ensure
tcp_socket && tcp_socket.close
end
|
#tunnel_test_ssh(hostname, &block) ⇒ Object
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
|
# File 'lib/chef/knife/vagrant_server_create.rb', line 397
def tunnel_test_ssh(hostname, &block)
gw_host, gw_user = config[:ssh_gateway].split('@').reverse
gw_host, gw_port = gw_host.split(':')
gateway = Net::SSH::Gateway.new(gw_host, gw_user, :port => gw_port || 22)
status = false
gateway.open(hostname, config[:ssh_port]) do |local_tunnel_port|
status = tcp_test_ssh('localhost', local_tunnel_port, &block)
end
status
rescue SocketError, Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Errno::ENETUNREACH, IOError
sleep 2
false
rescue Errno::EPERM, Errno::ETIMEDOUT
false
end
|
#validate! ⇒ Object
335
336
337
338
339
340
|
# File 'lib/chef/knife/vagrant_server_create.rb', line 335
def validate!
unless locate_config_value(:box) || locate_config_value(:box_url)
ui.error("You need to either specify --box or --box-url")
exit 1
end
end
|
#wait_for_direct_sshd(hostname, ssh_port) ⇒ Object
413
414
415
416
417
418
|
# File 'lib/chef/knife/vagrant_server_create.rb', line 413
def wait_for_direct_sshd(hostname, ssh_port)
print(".") until tcp_test_ssh(hostname, ssh_port) {
sleep @initial_sleep_delay ||= 2
puts("done")
}
end
|
#wait_for_sshd(hostname) ⇒ Object
385
386
387
|
# File 'lib/chef/knife/vagrant_server_create.rb', line 385
def wait_for_sshd(hostname)
config[:ssh_gateway] ? wait_for_tunnelled_sshd(hostname) : wait_for_direct_sshd(hostname, config[:ssh_port])
end
|
#wait_for_tunnelled_sshd(hostname) ⇒ Object
389
390
391
392
393
394
395
|
# File 'lib/chef/knife/vagrant_server_create.rb', line 389
def wait_for_tunnelled_sshd(hostname)
print(".")
print(".") until tunnel_test_ssh(hostname) {
sleep @initial_sleep_delay ||= 2
puts("done")
}
end
|
#write_vagrantfile ⇒ Object
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
|
# File 'lib/chef/knife/vagrant_server_create.rb', line 230
def write_vagrantfile
additions = []
if @server.use_cachier
additions << 'config.cache.auto_detect = true' end
if @server.vagrant_config
additions << @server.vagrant_config.split(/::/)
end
unless config[:identity_file].nil?
additions << 'config.ssh.private_key_path = "' + config[:identity_file] + '"'
end
file = <<-EOF
Vagrant.configure("2") do |config|
config.vm.box = "#{@server.box}"
config.vm.box_url = "#{@server.box_url}"
config.vm.hostname = "#{@server.name}"
config.vm.network :private_network, ip: "#{@server.ip_address}"
#{build_port_forwards(@server.port_forward)}
#{build_shares(@server.share_folders)}
config.vm.provider :virtualbox do |vb|
vb.customize [ "modifyvm", :id, "--memory", #{@server.memsize} ]
#{build_vb_customize(@server.vb_customize)}
end
config.vm.provider :vmware_fusion do |v|
v.vmx["memsize"] = "#{@server.memsize}"
#{build_vmx_customize(@server.vmx_customize)}
end
config.vm.provider :vmware_workstation do |v|
v.vmx["memsize"] = "#{@server.memsize}"
#{build_vmx_customize(@server.vmx_customize)}
end
#{additions.join("\n")}
end
EOF
file
instance_dir = File.join(locate_config_value(:vagrant_dir), @server.name)
instance_file = File.join(instance_dir, 'Vagrantfile')
ui.msg("Creating #{instance_file}")
FileUtils.mkdir_p(instance_dir)
File.open(instance_file, 'w') { |f| f.write(file) }
end
|