Class: Chef::Knife::AzureServerCreate

Inherits:
Bootstrap
  • Object
show all
Includes:
AzureBase, Bootstrap::Bootstrapper, Bootstrap::CommonBootstrapOptions
Defined in:
lib/chef/knife/azure_server_create.rb

Constant Summary collapse

SUPPORTED_CONNECTION_PROTOCOLS =
%w{ssh winrm cloud-api}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Bootstrap::Bootstrapper

#create_node_and_client_pem, #default_hint_options, #get_chef_extension_name, #get_chef_extension_private_params, #get_chef_extension_public_params, #get_chef_extension_publisher, #get_chef_extension_version, #load_correct_secret, #ohai_hints

Methods included from Bootstrap::CommonBootstrapOptions

included

Methods included from AzureBase

#fetch_chef_client_logs, #fetch_deployment, #fetch_extension, #fetch_role, #fetch_substatus, #find_file, #get_azure_profile_file_path, #get_default_subscription, included, #is_image_windows?, #locate_config_value, #msg_pair, #msg_server_summary, #parse_azure_profile, #parse_publish_settings_file, #pretty_key, #service, #validate!, #validate_asm_keys!, #validate_params!

Instance Attribute Details

#initial_sleep_delayObject

Returns the value of attribute initial_sleep_delay.



47
48
49
# File 'lib/chef/knife/azure_server_create.rb', line 47

def initial_sleep_delay
  @initial_sleep_delay
end

Instance Method Details

#create_server_defObject



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
# File 'lib/chef/knife/azure_server_create.rb', line 385

def create_server_def
  server_def = {
    azure_storage_account: locate_config_value(:azure_storage_account),
    azure_api_host_name: locate_config_value(:azure_api_host_name),
    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),
    connection_protocol: locate_config_value(:connection_protocol),
    azure_connect_to_existing_dns: locate_config_value(:azure_connect_to_existing_dns),
    connection_user: locate_config_value(:connection_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),
    ssl_cert_fingerprint: locate_config_value(:thumbprint),
    cert_path: locate_config_value(:cert_path),
    cert_password: locate_config_value(:cert_passphrase),
    winrm_ssl: locate_config_value(:winrm_ssl),
    winrm_max_timeout: locate_config_value(:winrm_max_timeout).to_i * 60 * 1000, # converting minutes to milliseconds
    winrm_max_memory_per_shell: locate_config_value(:winrm_max_memory_per_shell),
  }

  if locate_config_value(:connection_protocol) == "cloud-api"
    server_def[:chef_extension] = get_chef_extension_name
    server_def[:chef_extension_publisher] = get_chef_extension_publisher
    server_def[:chef_extension_version] = get_chef_extension_version
    server_def[:chef_extension_public_param] = get_chef_extension_public_params
    server_def[:chef_extension_private_param] = get_chef_extension_private_params
  else
    if is_image_windows?
      # 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(:connection_user).nil? || locate_config_value(:connection_user).downcase =~ /admin*/
        ui.error("Connection User is compulsory parameter and it cannot be named 'admin*'")
        exit 1
      # take cares of when user name contains domain
      # azure add role api doesn't support '\\' in user name
      elsif locate_config_value(:connection_user).split('\\').length.eql?(2)
        server_def[:connection_user] = locate_config_value(:connection_user).split('\\')[1]
      end
    else
      unless locate_config_value(:connection_user)
        ui.error("Connection User is compulsory parameter")
        exit 1
      end
      unless locate_config_value(:connection_password) || locate_config_value(:ssh_identity_file)
        ui.error("Specify either SSH Key or SSH Password")
        exit 1
      end
    end
  end

  if is_image_windows?
    server_def[:os_type] = "Windows"
    server_def[:admin_password] = locate_config_value(:connection_password)
    server_def[:connection_protocol] = locate_config_value(:connection_protocol) || "winrm"
  else
    server_def[:os_type] = "Linux"
    server_def[:connection_protocol] = locate_config_value(:connection_protocol).nil? || locate_config_value(:connection_protocol) == "winrm" ? "ssh" : locate_config_value(:connection_protocol)
    server_def[:connection_user] = locate_config_value(:connection_user)
    server_def[:connection_password] = locate_config_value(:connection_password)
    server_def[:ssh_identity_file] = locate_config_value(:ssh_identity_file)
    server_def[:identity_file_passphrase] = locate_config_value(:identity_file_passphrase)
  end

  azure_connect_to_existing_dns = locate_config_value(:azure_connect_to_existing_dns)
  if is_image_windows? && server_def[:connection_protocol] == "winrm"
    port = locate_config_value(:connection_port) || "5985"
    port = locate_config_value(:connection_port) || Random.rand(64000) + 1000 if azure_connect_to_existing_dns
  elsif server_def[:connection_protocol] == "ssh"
    port = locate_config_value(:connection_port) || "22"
    port = locate_config_value(:connection_port) || Random.rand(64000) + 1000 if azure_connect_to_existing_dns
  end

  server_def[:port] = port

  server_def[:is_vm_image] = service.vm_image?(locate_config_value(:azure_source_image))
  server_def[:azure_domain_name] = locate_config_value(:azure_domain_name) if locate_config_value(:azure_domain_name)

  if locate_config_value(:azure_domain_user)
    # extract domain name since it should be part of username
    case locate_config_value(:azure_domain_user)
    when /(\S+)\\(.+)/ # format - fully-qualified-DNS-domain\username
      server_def[:azure_domain_name] = $1 if locate_config_value(:azure_domain_name).nil?
      server_def[:azure_user_domain_name] = $1
      server_def[:azure_domain_user] = $2
    when /(.+)@(\S+)/ # format - user@fully-qualified-DNS-domain
      server_def[:azure_domain_name] = $2 if locate_config_value(:azure_domain_name).nil?
      server_def[:azure_user_domain_name] = $2
      server_def[:azure_domain_user] = $1
    else
      if locate_config_value(:azure_domain_name).nil?
        ui.error('--azure-domain-name should be specified if --azure-domain-user is not in one of the following formats: fully-qualified-DNS-domain\username, user@fully-qualified-DNS-domain')
        exit 1
      end
      server_def[:azure_domain_user] = locate_config_value(:azure_domain_user)
    end
  end
  server_def[:azure_domain_passwd] = locate_config_value(:azure_domain_passwd)
  server_def[:azure_domain_ou_dn] = locate_config_value(:azure_domain_ou_dn)

  server_def
end

#get_extension_statusObject



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
# File 'lib/chef/knife/azure_server_create.rb', line 342

def get_extension_status
  deployment_name = service.deployment_name(locate_config_value(:azure_dns_name))
  deployment = service.deployment("hostedservices/#{locate_config_value(:azure_dns_name)}/deployments/#{deployment_name}")
  extension_status = {}

  if deployment.at_css("Deployment Name") != nil
    role_list_xml = deployment.css("RoleInstanceList RoleInstance")
    role_list_xml.each do |role|
      if role.at_css("RoleName").text == locate_config_value(:azure_vm_name)
        lnx_waagent_fail_msg = "Failed to deserialize the status reported by the Guest Agent"
        waagent_status_msg = role.at_css("GuestAgentStatus FormattedMessage Message").text
        if role.at_css("GuestAgentStatus Status").text == "Ready"
          extn_status = role.at_css("ResourceExtensionStatusList Status").text
          Chef::Log.debug("Resource extension status is #{extn_status}")
          if extn_status == "Installing"
            extension_status[:status] = :extension_installing
            extension_status[:message] = role.at_css("ResourceExtensionStatusList FormattedMessage Message").text
          elsif extn_status == "NotReady"
            extension_status[:status] = :extension_provisioning
            extension_status[:message] = role.at_css("ResourceExtensionStatusList FormattedMessage Message").text
          elsif extn_status == "Ready"
            extension_status[:status] = :extension_ready
            extension_status[:message] = role.at_css("ResourceExtensionStatusList FormattedMessage Message").text
          else
            extension_status[:status] = :extension_status_not_detected
          end
        # This fix is for linux waagent issue: api unable to deserialize the waagent status.
        elsif (role.at_css("GuestAgentStatus Status").text == "NotReady") && (waagent_status_msg == lnx_waagent_fail_msg)
          extension_status[:status] = :extension_ready
        else
          extension_status[:status] = :wagent_provisioning
          extension_status[:message] = role.at_css("GuestAgentStatus Message").text
        end
      else
        extension_status[:status] = :extension_status_not_detected
      end
    end
  else
    extension_status[:status] = :extension_status_not_detected
  end
  extension_status
end

#get_virtual_machine_statusObject



325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'lib/chef/knife/azure_server_create.rb', line 325

def get_virtual_machine_status
  @server = service.get_role_server(locate_config_value(:azure_dns_name), locate_config_value(:azure_vm_name))
  if @server.nil?
    :vm_status_not_detected
  else
    Chef::Log.debug("Role status is #{@server.status}")
    case @server.status.to_s
    when "ReadyRole"
      :vm_status_ready
    when "Provisioning"
      :vm_status_provisioning
    else
      :vm_status_not_detected
    end
  end
end

#plugin_create_instance!Object



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/chef/knife/azure_server_create.rb', line 189

def plugin_create_instance!
  Chef::Log.info("Creating...")
  set_defaults
  server_def = create_server_def
  vm_details = service.create_server(server_def)

  wait_until_virtual_machine_ready

  config[:connection_port] = server_def[:port]
  config[:connection_protocol] = server_def[:connection_protocol]
  config[:chef_node_name] = locate_config_value(:chef_node_name) || server_name
rescue => error
  ui.error("Something went wrong. Please use -VV option for more details.")
  Chef::Log.debug(error.backtrace.join("\n").to_s)
  exit 1
end

#plugin_finalizeObject



218
219
220
221
222
223
224
# File 'lib/chef/knife/azure_server_create.rb', line 218

def plugin_finalize
  if locate_config_value(:connection_protocol) == "cloud-api" && locate_config_value(:extended_logs)
    print "\nWaiting for the first chef-client run"
    fetch_chef_client_logs(Time.now, 30)
  end
  msg_server_summary(@server)
end

#plugin_setup!Object

run() would be executing from parent class Chef::Knife::Bootstrap, defined in core. Required methods have been overridden here run() execution begins ####



147
# File 'lib/chef/knife/azure_server_create.rb', line 147

def plugin_setup!; end

#plugin_validate_options!Object



183
184
185
186
187
# File 'lib/chef/knife/azure_server_create.rb', line 183

def plugin_validate_options!
  Chef::Log.info("Validating...")
  validate_asm_keys!(:azure_source_image)
  validate_params!
end

#server_nameObject Also known as: host_descriptor



206
207
208
209
210
211
212
213
214
# File 'lib/chef/knife/azure_server_create.rb', line 206

def server_name
  @server_name ||= if @server.nil?
                     nil
                   elsif !@server.hostedservicename.nil?
                     @server.hostedservicename + ".cloudapp.net"
                   else
                     @server.ipaddress
                   end
end

#validate_name_args!Object



149
# File 'lib/chef/knife/azure_server_create.rb', line 149

def validate_name_args!; end

#validate_protocol!TrueClass

Note:

we are overriding this method here to consider “cloud-api” as valid protocol

Ensure a valid protocol is provided for target host connection

The method call will cause the program to exit(1) if:

* Conflicting protocols are given via the target URI and the --protocol option
* The protocol is not a supported protocol

Returns:

  • (TrueClass)

    If options are valid.



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/chef/knife/azure_server_create.rb', line 160

def validate_protocol!
  from_cli = config[:connection_protocol]
  if from_cli && connection_protocol != from_cli
    # Hanging indent to align with the ERROR: prefix
    ui.error <<~EOM
      The URL '#{host_descriptor}' indicates protocol is '#{connection_protocol}'
      while the --protocol flag specifies '#{from_cli}'.  Please include
      only one or the other.
    EOM
    exit 1
  end

  unless SUPPORTED_CONNECTION_PROTOCOLS.include?(connection_protocol)
    ui.error <<~EOM
      Unsupported protocol '#{connection_protocol}'.

      Supported protocols are: #{SUPPORTED_CONNECTION_PROTOCOLS.join(" ")}
    EOM
    exit 1
  end
  true
end

#wait_for_resource_extension_state(extension_status_goal, total_wait_time_in_minutes, retry_interval_in_seconds) ⇒ Object



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
# File 'lib/chef/knife/azure_server_create.rb', line 295

def wait_for_resource_extension_state(extension_status_goal, total_wait_time_in_minutes, retry_interval_in_seconds)
  extension_status_ordering = { extension_status_not_detected: 0, wagent_provisioning: 1, extension_installing: 2, extension_provisioning: 3, extension_ready: 4 }

  status_description = { extension_status_not_detected: "any", wagent_provisioning: "wagent provisioning", extension_installing: "installing", extension_provisioning: "provisioning", extension_ready: "ready" }

  print ui.color("\nWaiting for Resource Extension to reach status '#{status_description[extension_status_goal]}'\n", :magenta)

  max_polling_attempts = (total_wait_time_in_minutes * 60) / retry_interval_in_seconds
  polling_attempts = 0

  wait_start_time = Time.now

  begin
    extension_status = get_extension_status
    extension_ready = extension_status_ordering[extension_status[:status]] >= extension_status_ordering[extension_status_goal]
    print "."
    sleep retry_interval_in_seconds unless extension_ready
    polling_attempts += 1
  end until extension_ready || polling_attempts >= max_polling_attempts

  unless extension_ready
    raise Chef::Exceptions::CommandTimeout, "Resource extension state '#{status_description[extension_status_goal]}' not reached after #{total_wait_time_in_minutes} minutes. #{extension_status[:message]}"
  end

  elapsed_time_in_minutes = ((Time.now - wait_start_time) / 60).round(2)
  print ui.color("\nResource extension state '#{status_description[extension_status_goal]}' reached after #{elapsed_time_in_minutes} minutes.\n", :cyan)

  extension_status[:status]
end

#wait_for_virtual_machine_state(vm_status_goal, total_wait_time_in_minutes, retry_interval_in_seconds) ⇒ Object



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
# File 'lib/chef/knife/azure_server_create.rb', line 266

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("\nWaiting for virtual machine to reach status '#{vm_status_description[vm_status_goal]}'\n", :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 unless vm_ready
    polling_attempts += 1
  end until vm_ready || polling_attempts >= max_polling_attempts

  unless 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("\nvm 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

run() execution ends ####



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
# File 'lib/chef/knife/azure_server_create.rb', line 228

def wait_until_virtual_machine_ready(retry_interval_in_seconds = 30)
  vm_status = nil
  begin
    azure_vm_startup_timeout = locate_config_value(:azure_vm_startup_timeout).to_i
    azure_vm_ready_timeout = locate_config_value(:azure_vm_ready_timeout).to_i
    vm_status = wait_for_virtual_machine_state(:vm_status_provisioning, azure_vm_startup_timeout, retry_interval_in_seconds)
    if vm_status != :vm_status_ready
      begin
        wait_for_virtual_machine_state(:vm_status_ready, azure_vm_ready_timeout, retry_interval_in_seconds)
      rescue Chef::Exceptions::CommandTimeout => e
        ui.warn("\n#{e.message}")
        ui.warn("Ignoring failure to reach 'ready' with bootstrap.")
      end
    end

    msg_server_summary(@server)

    if locate_config_value(:connection_protocol) == "cloud-api"
      extension_status = wait_for_resource_extension_state(:wagent_provisioning, 5, retry_interval_in_seconds)

      if extension_status != :extension_installing
        extension_status = wait_for_resource_extension_state(:extension_installing, 5, retry_interval_in_seconds)
      end

      if extension_status != :extension_provisioning
        extension_status = wait_for_resource_extension_state(:extension_provisioning, 10, retry_interval_in_seconds)
      end

      if extension_status != :extension_ready
        wait_for_resource_extension_state(:extension_ready, 5, retry_interval_in_seconds)
      end
    end
  rescue Exception => e
    Chef::Log.error("#{e}")
    raise "Verify connectivity to Azure and subscription resource limit compliance (e.g. maximum CPU core limits) and try again."
  end
end