Class: Chef::Knife::RackspaceServerCreate
- Inherits:
-
Chef::Knife
- Object
- Chef::Knife
- Chef::Knife::RackspaceServerCreate
- Includes:
- RackspaceBase, WinrmBase
- Defined in:
- lib/chef/knife/rackspace_server_create.rb
Instance Attribute Summary collapse
-
#initial_sleep_delay ⇒ Object
Returns the value of attribute initial_sleep_delay.
Instance Method Summary collapse
- #bootstrap_common_params(bootstrap, server) ⇒ Object
- #bootstrap_for_node(server, bootstrap_ip_address) ⇒ Object
- #bootstrap_for_windows_node(server, bootstrap_ip_address) ⇒ Object
- #encode_file(file) ⇒ Object
- #files ⇒ Object
- #load_winrm_deps ⇒ Object
- #parse_file_argument(arg) ⇒ Object
- #run ⇒ Object
- #setup_rackconnect_network!(server) ⇒ Object
- #tcp_test_ssh(server, bootstrap_ip) ⇒ Object
- #tcp_test_winrm(hostname, port) ⇒ Object
- #user_data ⇒ Object
Methods included from RackspaceBase
#auth_endpoint, #connection, #connection_params, included, #ip_address, #locate_config_value, #msg_pair, #public_dns_name, #region_warning_for_v1
Methods inherited from Chef::Knife
Instance Attribute Details
#initial_sleep_delay ⇒ Object
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
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 |
# File 'lib/chef/knife/rackspace_server_create.rb', line 555 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[: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] bootstrap.config[:secret] = locate_config_value(:secret) bootstrap.config[:secret_file] = locate_config_value(:secret_file) || "" Chef::Config[:knife][:hints] ||= {} Chef::Config[:knife][:hints]["rackspace"] ||= {} bootstrap end |
#bootstrap_for_node(server, bootstrap_ip_address) ⇒ Object
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 |
# File 'lib/chef/knife/rackspace_server_create.rb', line 538 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] = config[:ssh_port] || Chef::Config[:knife][:ssh_port] bootstrap.config[:identity_file] = config[:identity_file] bootstrap.config[:host_key_verify] = config[:host_key_verify] bootstrap.config[:bootstrap_vault_file] = config[:bootstrap_vault_file] if config[:bootstrap_vault_file] bootstrap.config[:bootstrap_vault_json] = config[:bootstrap_vault_json] if config[:bootstrap_vault_json] bootstrap.config[:bootstrap_vault_item] = config[:bootstrap_vault_item] if config[:bootstrap_vault_item] # 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.config[:distro] = locate_config_value(:distro) || 'chef-full' bootstrap_common_params(bootstrap, server) end |
#bootstrap_for_windows_node(server, bootstrap_ip_address) ⇒ Object
578 579 580 581 582 583 584 585 586 587 |
# File 'lib/chef/knife/rackspace_server_create.rb', line 578 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.config[:distro] = locate_config_value(:distro) || 'windows-chef-client-msi' bootstrap_common_params(bootstrap, server) end |
#encode_file(file) ⇒ Object
316 317 318 319 320 321 322 323 324 325 |
# File 'lib/chef/knife/rackspace_server_create.rb', line 316 def encode_file(file) begin filename = File.(file) content = File.read(filename) rescue Errno::ENOENT => e ui.error "Unable to read source file - #{filename}" exit 1 end Base64.encode64(content) end |
#files ⇒ Object
327 328 329 330 331 332 333 334 335 336 337 338 339 340 |
# File 'lib/chef/knife/rackspace_server_create.rb', line 327 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_deps ⇒ Object
274 275 276 277 278 279 280 |
# File 'lib/chef/knife/rackspace_server_create.rb', line 274 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
307 308 309 310 311 312 313 314 |
# File 'lib/chef/knife/rackspace_server_create.rb', line 307 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 |
#run ⇒ Object
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 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 |
# File 'lib/chef/knife/rackspace_server_create.rb', line 365 def run $stdout.sync = true # Maybe deprecate this option at some point config[:bootstrap_network] = 'private' if config[:private_network] 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 rackconnect_wait = Chef::Config[:knife][:rackconnect_wait] || config[:rackconnect_wait] rackconnect3 = Chef::Config[:knife][:rackconnect_v3_network_id] || config[:rackconnect_v3_network_id] rackspace_servicelevel_wait = Chef::Config[:knife][:rackspace_servicelevel_wait] || config[:rackspace_servicelevel_wait] node_name = get_node_name(config[:chef_node_name] || config[:server_name]) networks = get_networks(Chef::Config[:knife][:rackspace_networks], rackconnect3) 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], :user_data => user_data, :config_drive => locate_config_value(:rackspace_config_drive) || false, :personality => files, :key_name => Chef::Config[:knife][:rackspace_ssh_keypair] ) 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("ConfigDrive", server.config_drive) msg_pair("UserData", Chef::Config[:knife][:rackspace_user_data]) msg_pair("RackConnect Wait", rackconnect_wait ? 'yes' : 'no') msg_pair("RackConnect V3", rackconnect3 ? 'yes' : 'no') msg_pair("ServiceLevel Wait", rackspace_servicelevel_wait ? 'yes' : 'no') msg_pair("SSH Key", Chef::Config[:knife][:rackspace_ssh_keypair]) # wait for it to be ready to do stuff begin server.wait_for(Integer(locate_config_value(:server_create_timeout))) { print "."; Chef::Log.debug("#{progress}%") if rackconnect_wait and rackspace_servicelevel_wait Chef::Log.debug("rackconnect_automation_status: #{metadata.all['rackconnect_automation_status']}") Chef::Log.debug("rax_service_level_automation: #{metadata.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: #{metadata.all['rackconnect_automation_status']}") ready? and .all['rackconnect_automation_status'] == 'DEPLOYED' elsif rackspace_servicelevel_wait Chef::Log.debug("rax_service_level_automation: #{metadata.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.) print "\n#{ui.color("Waiting server", :magenta)}" puts("\n") if Chef::Config[:knife][:rackconnect_v3_network_id] print "\n#{ui.color("Setting up RackconnectV3 network and IPs", :magenta)}" setup_rackconnect_network!(server) while server.ipv4_address == "" server.reload sleep 5 end end if networks && Chef::Config[:knife][:rackspace_networks] msg_pair("Networks", Chef::Config[:knife][:rackspace_networks].sort.join(', ')) end msg_pair("Public DNS Name", public_dns_name(server)) msg_pair("Public IP Address", ip_address(server, 'public')) msg_pair("Private IP Address", ip_address(server, 'private')) msg_pair("Password", server.password) msg_pair("Metadata", server..all) bootstrap_ip_address = ip_address(server, config[:bootstrap_network]) 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)}" tcp_test_ssh(server, bootstrap_ip_address) 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) if server.image msg_pair("Boot Image ID", server.boot_image_id) if server.boot_image_id msg_pair("Metadata", server.) msg_pair("Public DNS Name", public_dns_name(server)) msg_pair("Public IP Address", ip_address(server, 'public')) msg_pair("Private IP Address", ip_address(server, 'private')) msg_pair("Password", server.password) msg_pair("Environment", config[:environment] || '_default') msg_pair("Run List", config[:run_list].join(', ')) end |
#setup_rackconnect_network!(server) ⇒ Object
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 |
# File 'lib/chef/knife/rackspace_server_create.rb', line 504 def setup_rackconnect_network!(server) auth_token = connection.authenticate tenant_id = connection.endpoint_uri.path.split("/").last region = connection.region uri = URI("https://#{region}.rackconnect.api.rackspacecloud.com/v3/#{tenant_id}/public_ips") Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http| begin req = Net::HTTP::Post.new(uri.request_uri) req['X-Auth-Token'] = auth_token req['Content-Type'] = 'application/json' req.body = JSON.dump("cloud_server" => { "id" => server.id }) http.use_ssl = true http.request req rescue StandardError => e puts "HTTP Request failed (#{e.message})" end end end |
#tcp_test_ssh(server, bootstrap_ip) ⇒ Object
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
# File 'lib/chef/knife/rackspace_server_create.rb', line 282 def tcp_test_ssh(server, bootstrap_ip) return true unless config[:tcp_test_ssh] != nil limit = Chef::Config[:knife][:retry_ssh_limit].to_i count = 0 begin Net::SSH.start(bootstrap_ip, 'root', :password => server.password ) do |ssh| Chef::Log.debug("sshd accepting connections on #{bootstrap_ip}") break end rescue count += 1 if count <= limit print '.' sleep config[:retry_ssh_every].to_i tcp_test_ssh(server, bootstrap_ip) else ui.error "Unable to SSH into #{bootstrap_ip}" exit 1 end end end |
#tcp_test_winrm(hostname, port) ⇒ Object
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 |
# File 'lib/chef/knife/rackspace_server_create.rb', line 344 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 |
#user_data ⇒ Object
524 525 526 527 528 529 530 531 532 533 534 535 536 |
# File 'lib/chef/knife/rackspace_server_create.rb', line 524 def user_data file = Chef::Config[:knife][:rackspace_user_data] return unless file begin filename = File.(file) content = File.read(filename) rescue Errno::ENOENT => e ui.error "Unable to read source file - #{filename}" exit 1 end content end |