Class: Chef::Knife::RackspaceServerCreate

Inherits:
Chef::Knife
  • Object
show all
Includes:
RackspaceBase, WinrmBase
Defined in:
lib/chef/knife/rackspace_server_create.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RackspaceBase

#auth_endpoint, #connection, #connection_params, included, #locate_config_value, #msg_pair, #private_ip, #public_dns_name, #public_ip, #region_warning_for_v1

Methods inherited from Chef::Knife

#get_networks, #get_node_name

Instance Attribute Details

#initial_sleep_delayObject

Returns the value of attribute initial_sleep_delay.



42
43
44
# File 'lib/chef/knife/rackspace_server_create.rb', line 42

def initial_sleep_delay
  @initial_sleep_delay
end

Instance Method Details

#bootstrap_common_params(bootstrap, server) ⇒ Object



434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
# File 'lib/chef/knife/rackspace_server_create.rb', line 434

def bootstrap_common_params(bootstrap, server)
  bootstrap.config[:environment] = config[:environment]
  bootstrap.config[:run_list] = config[:run_list]
  if version_one?
    bootstrap.config[:chef_node_name] = config[:chef_node_name] || server.id
  else
    bootstrap.config[:chef_node_name] = config[:chef_node_name] || server.name
  end
  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[:first_boot_attributes] = config[:first_boot_attributes]
  bootstrap.config[:bootstrap_proxy] = locate_config_value(:bootstrap_proxy)
  bootstrap.config[:encrypted_data_bag_secret] = config[:encrypted_data_bag_secret]
  bootstrap.config[:encrypted_data_bag_secret_file] = config[:encrypted_data_bag_secret_file]
  Chef::Config[:knife][:hints] ||= {}
  Chef::Config[:knife][:hints]["rackspace"] ||= {}
  bootstrap
end

#bootstrap_for_node(server, bootstrap_ip_address) ⇒ Object



421
422
423
424
425
426
427
428
429
430
431
432
# File 'lib/chef/knife/rackspace_server_create.rb', line 421

def bootstrap_for_node(server, bootstrap_ip_address)
  bootstrap = Chef::Knife::Bootstrap.new
  bootstrap.name_args = [bootstrap_ip_address]
  bootstrap.config[:ssh_user] = config[:ssh_user] || "root"
  bootstrap.config[:ssh_password] = server.password
  bootstrap.config[:ssh_port] = Chef::Config[:knife][:ssh_port] || config[:ssh_port]
  bootstrap.config[:identity_file] = config[:identity_file]
  bootstrap.config[:host_key_verify] = config[:host_key_verify]
  # bootstrap will run as root...sudo (by default) also messes up Ohai on CentOS boxes
  bootstrap.config[:use_sudo] = true unless config[:ssh_user] == 'root'
  bootstrap_common_params(bootstrap, server)
end

#bootstrap_for_windows_node(server, bootstrap_ip_address) ⇒ Object



455
456
457
458
459
460
461
462
463
# File 'lib/chef/knife/rackspace_server_create.rb', line 455

def bootstrap_for_windows_node(server, bootstrap_ip_address)
  bootstrap = Chef::Knife::BootstrapWindowsWinrm.new
  bootstrap.name_args = [bootstrap_ip_address]
  bootstrap.config[:winrm_user] = locate_config_value(:winrm_user) || 'Administrator'
  bootstrap.config[:winrm_password] = locate_config_value(:winrm_password) || server.password
  bootstrap.config[:winrm_transport] = locate_config_value(:winrm_transport)
  bootstrap.config[:winrm_port] = locate_config_value(:winrm_port)
  bootstrap_common_params(bootstrap, server)
end

#encode_file(file) ⇒ Object



246
247
248
249
250
251
252
253
254
255
# File 'lib/chef/knife/rackspace_server_create.rb', line 246

def encode_file(file)
  begin
    filename = File.expand_path(file)
    content = File.read(filename)
  rescue Errno::ENOENT => e
    ui.error "Unable to read source file - #{filename}"
    exit 1
  end
  Base64.encode64(content)
end

#filesObject



257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/chef/knife/rackspace_server_create.rb', line 257

def files
    return {} unless  Chef::Config[:knife][:file]

    files = []
    Chef::Config[:knife][:file].each do |arg|
      dest, src = parse_file_argument(arg)
      Chef::Log.debug("Inject file #{src} into #{dest}")
      files << {
        :path => dest,
        :contents => encode_file(src)
      }
  end
  files
end

#load_winrm_depsObject



203
204
205
206
207
208
209
# File 'lib/chef/knife/rackspace_server_create.rb', line 203

def load_winrm_deps
  require 'winrm'
  require 'em-winrm'
  require 'chef/knife/bootstrap_windows_winrm'
  require 'chef/knife/core/windows_bootstrap_context'
  require 'chef/knife/winrm'
end

#parse_file_argument(arg) ⇒ Object



237
238
239
240
241
242
243
244
# File 'lib/chef/knife/rackspace_server_create.rb', line 237

def parse_file_argument(arg)
  dest, src = arg.split('=')
  unless dest && src
    ui.error "Unable to process file arguments #{arg}. The --file option requires both the destination on the remote machine as well as the local source be supplied using the form DESTINATION-PATH=SOURCE-PATH"
    exit 1
  end
  [dest, src]
end

#runObject



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
347
348
349
350
351
352
353
354
355
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
# File 'lib/chef/knife/rackspace_server_create.rb', line 296

def run
  $stdout.sync = true

  unless Chef::Config[:knife][:image]
    ui.error("You have not provided a valid image value.  Please note the short option for this value recently changed from '-i' to '-I'.")
    exit 1
  end

  if locate_config_value(:bootstrap_protocol) == 'winrm'
    load_winrm_deps
  end

  node_name = get_node_name(config[:chef_node_name] || config[:server_name])
  networks = get_networks(Chef::Config[:knife][:rackspace_networks])

  rackconnect_wait = Chef::Config[:knife][:rackconnect_wait] || config[:rackconnect_wait]
  rackspace_servicelevel_wait = Chef::Config[:knife][:rackspace_servicelevel_wait] || config[:rackspace_servicelevel_wait]

  server = connection.servers.new(
    :name => node_name,
    :image_id => Chef::Config[:knife][:image],
    :flavor_id => locate_config_value(:flavor),
    :metadata => Chef::Config[:knife][:rackspace_metadata],
    :disk_config => Chef::Config[:knife][:rackspace_disk_config],
    :personality => files
  )

  if version_one?
    server.save
  else
    server.save(:networks => networks)
  end

  msg_pair("Instance ID", server.id)
  msg_pair("Host ID", server.host_id)
  msg_pair("Name", server.name)
  msg_pair("Flavor", server.flavor.name)
  msg_pair("Image", server.image.name)
  msg_pair("Metadata", server..all)
  msg_pair("RackConnect Wait", rackconnect_wait ? 'yes' : 'no')
  msg_pair("ServiceLevel Wait", rackspace_servicelevel_wait ? 'yes' : 'no')

  # wait for it to be ready to do stuff
  begin
    server.wait_for(1200) { 
      print "."; 
      Chef::Log.debug("#{progress}%")
      if rackconnect_wait and rackspace_servicelevel_wait
        Chef::Log.debug("rackconnect_automation_status: #{.all['rackconnect_automation_status']}")
        Chef::Log.debug("rax_service_level_automation: #{.all['rax_service_level_automation']}")
        ready? and .all['rackconnect_automation_status'] == 'DEPLOYED' and .all['rax_service_level_automation'] == 'Complete'
      elsif rackconnect_wait
        Chef::Log.debug("rackconnect_automation_status: #{.all['rackconnect_automation_status']}")
        ready? and .all['rackconnect_automation_status'] == 'DEPLOYED'
      elsif rackspace_servicelevel_wait
        Chef::Log.debug("rax_service_level_automation: #{.all['rax_service_level_automation']}")
        ready? and .all['rax_service_level_automation'] == 'Complete'
      else
        ready?
      end
    }
  rescue Fog::Errors::TimeoutError
    ui.error('Timeout waiting for the server to be created')
    msg_pair('Progress', "#{server.progress}%")
    msg_pair('rackconnect_automation_status', server..all['rackconnect_automation_status'])
    msg_pair('rax_service_level_automation', server..all['rax_service_level_automation'])
    Chef::Application.fatal! 'Server didn\'t finish on time'
  end
  msg_pair("Metadata", server.)
  if(networks && Chef::Config[:knife][:rackspace_networks])
    msg_pair("Networks", Chef::Config[:knife][:rackspace_networks].sort.join(', '))
  end

  print "\n#{ui.color("Waiting server", :magenta)}"

  server.wait_for(Integer(locate_config_value(:server_create_timeout))) { print "."; ready? }
  # wait for it to be ready to do stuff

  puts("\n")

  msg_pair("Public DNS Name", public_dns_name(server))
  msg_pair("Public IP Address", public_ip(server))
  msg_pair("Private IP Address", private_ip(server))
  msg_pair("Password", server.password)
  msg_pair("Metadata", server..all)

  #which IP address to bootstrap
  bootstrap_ip_address = public_ip(server)
  if config[:private_network]
    bootstrap_ip_address = private_ip(server)
  end
  Chef::Log.debug("Bootstrap IP Address #{bootstrap_ip_address}")
  if bootstrap_ip_address.nil?
    ui.error("No IP address available for bootstrapping.")
    exit 1
  end

if locate_config_value(:bootstrap_protocol) == 'winrm'
  print "\n#{ui.color("Waiting for winrm", :magenta)}"
  print(".") until tcp_test_winrm(bootstrap_ip_address, locate_config_value(:winrm_port))
  bootstrap_for_windows_node(server, bootstrap_ip_address).run
else
  print "\n#{ui.color("Waiting for sshd", :magenta)}"
  print(".") until tcp_test_ssh(bootstrap_ip_address) {
    sleep @initial_sleep_delay ||= 10
    puts("done")
  }
  bootstrap_for_node(server, bootstrap_ip_address).run
end

  puts "\n"
  msg_pair("Instance ID", server.id)
  msg_pair("Host ID", server.host_id)
  msg_pair("Name", server.name)
  msg_pair("Flavor", server.flavor.name)
  msg_pair("Image", server.image.name)
  msg_pair("Metadata", server.)
  msg_pair("Public DNS Name", public_dns_name(server))
  msg_pair("Public IP Address", public_ip(server))
  msg_pair("Private IP Address", private_ip(server))
  msg_pair("Password", server.password)
  msg_pair("Environment", config[:environment] || '_default')
  msg_pair("Run List", config[:run_list].join(', '))
end

#tcp_test_ssh(hostname) ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/chef/knife/rackspace_server_create.rb', line 211

def tcp_test_ssh(hostname)
  ssh_port = Chef::Config[:knife][:ssh_port] || config[: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 Errno::ETIMEDOUT
  false
rescue Errno::EPERM
  false
rescue Errno::ECONNREFUSED
  sleep 2
  false
rescue Errno::EHOSTUNREACH
  sleep 2
  false
ensure
  tcp_socket && tcp_socket.close
end

#tcp_test_winrm(hostname, port) ⇒ Object



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/chef/knife/rackspace_server_create.rb', line 274

def tcp_test_winrm(hostname, port)
  TCPSocket.new(hostname, port)
  return true
rescue SocketError
  sleep 2
  false
rescue Errno::ETIMEDOUT
  false
rescue Errno::EPERM
  false
rescue Errno::ECONNREFUSED
  sleep 2
  false
rescue Errno::EHOSTUNREACH
  sleep 2
  false
rescue Errno::ENETUNREACH
  sleep 2
  false
end