Class: Chef::Knife::AzureServerCreate
- Inherits:
-
Chef::Knife
- Object
- Chef::Knife
- Chef::Knife::AzureServerCreate
- Includes:
- AzureBase, Knife::WinrmBase
- Defined in:
- lib/chef/knife/azure_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, fqdn, port) ⇒ Object
- #bootstrap_for_windows_node(server, fqdn, port) ⇒ Object
- #cleanup_and_exit(remove_hosted_service_on_failure, remove_storage_service_on_failure) ⇒ Object
- #create_server_def ⇒ Object
- #get_role_server ⇒ Object
- #get_virtual_machine_status ⇒ Object
- #load_cloud_attributes_in_hints(server) ⇒ Object
- #load_winrm_deps ⇒ Object
- #random_string(len = 10) ⇒ Object
- #run ⇒ Object
- #strip_non_ascii(string) ⇒ Object
- #tcp_test_ssh(fqdn, sshport) ⇒ Object
- #tcp_test_winrm(ip_addr, port) ⇒ Object
- #validate! ⇒ Object
- #wait_for_virtual_machine_state(vm_status_goal, total_wait_time_in_minutes, retry_interval_in_seconds) ⇒ Object
- #wait_until_virtual_machine_ready(retry_interval_in_seconds = 30) ⇒ Object
Methods included from AzureBase
#connection, #find_file, included, #is_image_windows?, #locate_config_value, #msg_pair, #msg_server_summary, #parse_publish_settings_file
Instance Attribute Details
#initial_sleep_delay ⇒ Object
Returns the value of attribute initial_sleep_delay.
50 51 52 |
# File 'lib/chef/knife/azure_server_create.rb', line 50 def initial_sleep_delay @initial_sleep_delay end |
Instance Method Details
#bootstrap_common_params(bootstrap, server) ⇒ Object
452 453 454 455 456 457 458 459 460 461 462 |
# File 'lib/chef/knife/azure_server_create.rb', line 452 def bootstrap_common_params(bootstrap, server) bootstrap.config[:run_list] = config[:run_list] bootstrap.config[:prerelease] = config[:prerelease] bootstrap.config[:first_boot_attributes] = locate_config_value(:json_attributes) || {} 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) load_cloud_attributes_in_hints(server) bootstrap end |
#bootstrap_for_node(server, fqdn, port) ⇒ Object
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 |
# File 'lib/chef/knife/azure_server_create.rb', line 499 def bootstrap_for_node(server,fqdn,port) bootstrap = Chef::Knife::Bootstrap.new bootstrap.name_args = [fqdn] bootstrap.config[:ssh_user] = locate_config_value(:ssh_user) bootstrap.config[:ssh_password] = locate_config_value(:ssh_password) bootstrap.config[:ssh_port] = port bootstrap.config[:identity_file] = locate_config_value(:identity_file) bootstrap.config[:chef_node_name] = locate_config_value(:chef_node_name) || server.name bootstrap.config[:use_sudo] = true unless locate_config_value(:ssh_user) == 'root' bootstrap.config[:use_sudo_password] = true if bootstrap.config[:use_sudo] bootstrap.config[:environment] = locate_config_value(:environment) # may be needed for vpc_mode bootstrap.config[:host_key_verify] = config[:host_key_verify] bootstrap.config[:secret] = locate_config_value(:secret) bootstrap.config[:secret_file] = locate_config_value(:secret_file) bootstrap_common_params(bootstrap, server) end |
#bootstrap_for_windows_node(server, fqdn, port) ⇒ Object
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 |
# File 'lib/chef/knife/azure_server_create.rb', line 465 def bootstrap_for_windows_node(server, fqdn, port) if locate_config_value(:bootstrap_protocol) == 'winrm' load_winrm_deps if not Chef::Platform.windows? require 'gssapi' end bootstrap = Chef::Knife::BootstrapWindowsWinrm.new bootstrap.config[:winrm_user] = locate_config_value(:winrm_user) || 'Administrator' bootstrap.config[:winrm_password] = locate_config_value(:winrm_password) bootstrap.config[:winrm_transport] = locate_config_value(:winrm_transport) bootstrap.config[:winrm_port] = port elsif locate_config_value(:bootstrap_protocol) == 'ssh' bootstrap = Chef::Knife::BootstrapWindowsSsh.new bootstrap.config[:ssh_user] = locate_config_value(:ssh_user) bootstrap.config[:ssh_password] = locate_config_value(:ssh_password) bootstrap.config[:ssh_port] = port bootstrap.config[:identity_file] = locate_config_value(:identity_file) bootstrap.config[:host_key_verify] = locate_config_value(:host_key_verify) else ui.error("Unsupported Bootstrapping Protocol. Supported : winrm, ssh") exit 1 end bootstrap.name_args = [fqdn] bootstrap.config[:chef_node_name] = config[:chef_node_name] || server.name 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_common_params(bootstrap, server) end |
#cleanup_and_exit(remove_hosted_service_on_failure, remove_storage_service_on_failure) ⇒ Object
608 609 610 611 612 613 614 615 616 617 618 619 620 621 |
# File 'lib/chef/knife/azure_server_create.rb', line 608 def cleanup_and_exit(remove_hosted_service_on_failure, remove_storage_service_on_failure) ui.warn("Cleaning up resources...") if remove_hosted_service_on_failure ret_val = connection.hosts.delete(remove_hosted_service_on_failure) ret_val.content.empty? ? ui.warn("Deleted created DNS: #{remove_hosted_service_on_failure}.") : ui.warn("Deletion failed for created DNS:#{remove_hosted_service_on_failure}. " + ret_val.text) end if remove_storage_service_on_failure ret_val = connection.storageaccounts.delete(remove_storage_service_on_failure) ret_val.content.empty? ? ui.warn("Deleted created Storage Account: #{remove_storage_service_on_failure}.") : ui.warn("Deletion failed for created Storage Account: #{remove_storage_service_on_failure}. " + ret_val.text) end exit 1 end |
#create_server_def ⇒ Object
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 |
# File 'lib/chef/knife/azure_server_create.rb', line 539 def create_server_def server_def = { :azure_storage_account => locate_config_value(:azure_storage_account), :azure_dns_name => locate_config_value(:azure_dns_name), :azure_vm_name => locate_config_value(:azure_vm_name), :azure_service_location => locate_config_value(:azure_service_location), :azure_os_disk_name => locate_config_value(:azure_os_disk_name), :azure_source_image => locate_config_value(:azure_source_image), :azure_vm_size => locate_config_value(:azure_vm_size), :tcp_endpoints => locate_config_value(:tcp_endpoints), :udp_endpoints => locate_config_value(:udp_endpoints), :bootstrap_proto => locate_config_value(:bootstrap_protocol), :azure_connect_to_existing_dns => locate_config_value(:azure_connect_to_existing_dns), :winrm_user => locate_config_value(:winrm_user), :azure_availability_set => locate_config_value(:azure_availability_set), :azure_affinity_group => locate_config_value(:azure_affinity_group), :azure_network_name => locate_config_value(:azure_network_name), :azure_subnet_name => locate_config_value(:azure_subnet_name) } # If user is connecting a new VM to an existing dns, then # the VM needs to have a unique public port. Logic below takes care of this. if !is_image_windows? or locate_config_value(:bootstrap_protocol) == 'ssh' port = locate_config_value(:ssh_port) || '22' if locate_config_value(:azure_connect_to_existing_dns) && (port == '22') port = Random.rand(64000) + 1000 end else port = locate_config_value(:winrm_port) || '5985' if locate_config_value(:azure_connect_to_existing_dns) && (port == '5985') port = Random.rand(64000) + 1000 end end server_def[:port] = port if is_image_windows? server_def[:os_type] = 'Windows' if not locate_config_value(:winrm_password) or not locate_config_value(:bootstrap_protocol) ui.error("WinRM Password and Bootstrapping Protocol are compulsory parameters") exit 1 end # We can specify the AdminUsername after API version 2013-03-01. However, in this API version, # the AdminUsername is a required parameter. # Also, the user name cannot be Administrator, Admin, Admin1 etc, for enhanced security (provided by Azure) if locate_config_value(:winrm_user).nil? || locate_config_value(:winrm_user).downcase =~ /admin*/ ui.error("WinRM User is compulsory parameter and it cannot be named 'admin*'") exit end server_def[:admin_password] = locate_config_value(:winrm_password) server_def[:bootstrap_proto] = locate_config_value(:bootstrap_protocol) else server_def[:os_type] = 'Linux' server_def[:bootstrap_proto] = 'ssh' if not locate_config_value(:ssh_user) ui.error("SSH User is compulsory parameter") exit 1 end unless locate_config_value(:ssh_password) or locate_config_value(:identity_file) ui.error("Specify either SSH Key or SSH Password") exit 1 end server_def[:ssh_user] = locate_config_value(:ssh_user) server_def[:ssh_password] = locate_config_value(:ssh_password) server_def[:identity_file] = locate_config_value(:identity_file) server_def[:identity_file_passphrase] = locate_config_value(:identity_file_passphrase) end server_def end |
#get_role_server ⇒ Object
283 284 285 286 |
# File 'lib/chef/knife/azure_server_create.rb', line 283 def get_role_server() deploy = connection.deploys.queryDeploy(locate_config_value(:azure_dns_name)) deploy.find_role(locate_config_value(:azure_vm_name)) end |
#get_virtual_machine_status ⇒ Object
268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/chef/knife/azure_server_create.rb', line 268 def get_virtual_machine_status() role = get_role_server() unless role.nil? Chef::Log.debug("Role status is #{role.status.to_s}") if "ReadyRole".eql? role.status.to_s return :vm_status_ready elsif "Provisioning".eql? role.status.to_s return :vm_status_provisioning else return :vm_status_not_detected end end return :vm_status_not_detected end |
#load_cloud_attributes_in_hints(server) ⇒ Object
437 438 439 440 441 442 443 444 445 446 447 448 449 450 |
# File 'lib/chef/knife/azure_server_create.rb', line 437 def load_cloud_attributes_in_hints(server) # Modify global configuration state to ensure hint gets set by knife-bootstrap # Query azure and load necessary attributes. cloud_attributes = {} cloud_attributes["public_ip"] = server.publicipaddress cloud_attributes["vm_name"] = server.name cloud_attributes["public_fqdn"] = server.hostedservicename.to_s + ".cloudapp.net" cloud_attributes["public_ssh_port"] = server.sshport if server.sshport cloud_attributes["public_winrm_port"] = server.winrmport if server.winrmport Chef::Config[:knife][:hints] ||= {} Chef::Config[:knife][:hints]["azure"] ||= cloud_attributes end |
#load_winrm_deps ⇒ Object
41 42 43 44 45 46 |
# File 'lib/chef/knife/azure_server_create.rb', line 41 def load_winrm_deps require 'winrm' require 'em-winrm' require 'chef/knife/winrm' require 'chef/knife/bootstrap_windows_winrm' end |
#random_string(len = 10) ⇒ Object
220 221 222 |
# File 'lib/chef/knife/azure_server_create.rb', line 220 def random_string(len=10) (0...len).map{65.+(rand(25)).chr}.join end |
#run ⇒ Object
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 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 |
# File 'lib/chef/knife/azure_server_create.rb', line 337 def run $stdout.sync = true storage = nil Chef::Log.info("validating...") validate! Chef::Log.info("creating...") config[:azure_dns_name] = get_dns_name(locate_config_value(:azure_dns_name)) if not locate_config_value(:azure_vm_name) config[:azure_vm_name] = locate_config_value(:azure_dns_name) end remove_hosted_service_on_failure = locate_config_value(:azure_dns_name) if connection.hosts.exists?(locate_config_value(:azure_dns_name)) remove_hosted_service_on_failure = nil end #If Storage Account is not specified, check if the geographic location has one to re-use if not locate_config_value(:azure_storage_account) storage_accts = connection.storageaccounts.all storage = storage_accts.find { |storage_acct| storage_acct.location.to_s == locate_config_value(:azure_service_location) } if not storage config[:azure_storage_account] = [strip_non_ascii(locate_config_value(:azure_vm_name)), random_string].join.downcase remove_storage_service_on_failure = config[:azure_storage_account] else remove_storage_service_on_failure = nil config[:azure_storage_account] = storage.name.to_s end else if connection.storageaccounts.exists?(locate_config_value(:azure_storage_account)) remove_storage_service_on_failure = nil else remove_storage_service_on_failure = locate_config_value(:azure_storage_account) end end begin connection.deploys.create(create_server_def) wait_until_virtual_machine_ready() server = get_role_server() fqdn = server.publicipaddress rescue Exception => e Chef::Log.error("Failed to create the server -- exception being rescued: #{e.to_s}") = "#{e.class}: #{e}\n#{e.backtrace.join("\n")}" Chef::Log.debug("#{}") cleanup_and_exit(remove_hosted_service_on_failure, remove_storage_service_on_failure) end msg_server_summary(server) if is_image_windows? # Set distro to windows-chef-client-msi config[:distro] = "windows-chef-client-msi" if (config[:distro].nil? || config[:distro] == "chef-full") if locate_config_value(:bootstrap_protocol) == 'ssh' port = server.sshport print "#{ui.color("Waiting for sshd on #{fqdn}:#{port}", :magenta)}" print(".") until tcp_test_ssh(fqdn,port) { sleep @initial_sleep_delay ||= 10 puts("done") } elsif locate_config_value(:bootstrap_protocol) == 'winrm' port = server.winrmport print "#{ui.color("Waiting for winrm on #{fqdn}:#{port}", :magenta)}" print(".") until tcp_test_winrm(fqdn,port) { sleep @initial_sleep_delay ||= 10 puts("done") } end puts("\n") bootstrap_for_windows_node(server,fqdn, port).run else unless server && server.publicipaddress && server.sshport Chef::Log.fatal("server not created") exit 1 end port = server.sshport print "#{ui.color("Waiting for sshd on #{fqdn}:#{port}", :magenta)}" print(".") until tcp_test_ssh(fqdn,port) { sleep @initial_sleep_delay ||= 10 puts("done") } puts("\n") bootstrap_for_node(server,fqdn,port).run end msg_server_summary(server) end |
#strip_non_ascii(string) ⇒ Object
216 217 218 |
# File 'lib/chef/knife/azure_server_create.rb', line 216 def strip_non_ascii(string) string.gsub(/[^0-9a-z ]/i, '') end |
#tcp_test_ssh(fqdn, sshport) ⇒ Object
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 |
# File 'lib/chef/knife/azure_server_create.rb', line 310 def tcp_test_ssh(fqdn, sshport) tcp_socket = TCPSocket.new(fqdn, sshport) readable = IO.select([tcp_socket], nil, nil, 5) if readable Chef::Log.debug("sshd accepting connections on #{fqdn}, banner is #{tcp_socket.gets}") yield true else false end 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 ensure tcp_socket && tcp_socket.close end |
#tcp_test_winrm(ip_addr, port) ⇒ Object
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
# File 'lib/chef/knife/azure_server_create.rb', line 288 def tcp_test_winrm(ip_addr, port) hostname = ip_addr socket = 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 |
#validate! ⇒ Object
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 |
# File 'lib/chef/knife/azure_server_create.rb', line 518 def validate! super([ :azure_subscription_id, :azure_mgmt_cert, :azure_api_host_name, :azure_source_image, :azure_vm_size, ]) if locate_config_value(:azure_connect_to_existing_dns) && locate_config_value(:azure_vm_name).nil? ui.error("Specify the VM name using --azure-vm-name option, since you are connecting to existing dns") exit 1 end if locate_config_value(:azure_service_location) && locate_config_value(:azure_affinity_group) ui.error("Cannot specify both --azure_service_location and --azure_affinity_group, use one or the other.") exit 1 elsif locate_config_value(:azure_service_location).nil? && locate_config_value(:azure_affinity_group).nil? ui.error("Must specify either --azure_service_location or --azure_affinity_group.") exit 1 end end |
#wait_for_virtual_machine_state(vm_status_goal, total_wait_time_in_minutes, retry_interval_in_seconds) ⇒ Object
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 |
# File 'lib/chef/knife/azure_server_create.rb', line 239 def wait_for_virtual_machine_state(vm_status_goal, total_wait_time_in_minutes, retry_interval_in_seconds) vm_status_ordering = {:vm_status_not_detected => 0, :vm_status_provisioning => 1, :vm_status_ready => 2} vm_status_description = {:vm_status_not_detected => 'any', :vm_status_provisioning => 'provisioning', :vm_status_ready => 'ready'} print ui.color("Waiting for virtual machine to reach status '#{vm_status_description[vm_status_goal]}'", :magenta) total_wait_time_in_seconds = total_wait_time_in_minutes * 60 max_polling_attempts = total_wait_time_in_seconds / retry_interval_in_seconds polling_attempts = 0 wait_start_time = Time.now begin vm_status = get_virtual_machine_status() vm_ready = vm_status_ordering[vm_status] >= vm_status_ordering[vm_status_goal] print '.' sleep retry_interval_in_seconds if !vm_ready polling_attempts += 1 end until vm_ready || polling_attempts >= max_polling_attempts if ! vm_ready raise Chef::Exceptions::CommandTimeout, "Virtual machine state '#{vm_status_description[vm_status_goal]}' not reached after #{total_wait_time_in_minutes} minutes." end elapsed_time_in_minutes = ((Time.now - wait_start_time) / 60).round(2) print ui.color("vm state '#{vm_status_description[vm_status_goal]}' reached after #{elapsed_time_in_minutes} minutes.\n", :cyan) vm_status end |
#wait_until_virtual_machine_ready(retry_interval_in_seconds = 30) ⇒ Object
224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/chef/knife/azure_server_create.rb', line 224 def wait_until_virtual_machine_ready(retry_interval_in_seconds = 30) vm_status = nil puts begin vm_status = wait_for_virtual_machine_state(:vm_status_provisioning, 5, retry_interval_in_seconds) if vm_status != :vm_status_ready wait_for_virtual_machine_state(:vm_status_ready, 15, retry_interval_in_seconds) end rescue Exception => e Chef::Log.error("#{e.to_s}") raise 'Verify connectivity to Azure and subscription resource limit compliance (e.g. maximum CPU core limits) and try again.' end end |