Class: Chef::Knife::AzurermServerCreate

Inherits:
Chef::Knife show all
Includes:
AzurermBase, Bootstrap::Bootstrapper, Bootstrap::CommonBootstrapOptions
Defined in:
lib/chef/knife/azurerm_server_create.rb

Constant Summary

Constants included from Azure::ARM::ReadCred

Azure::ARM::ReadCred::CRED_TYPE_DOMAIN_CERTIFICATE, Azure::ARM::ReadCred::CRED_TYPE_DOMAIN_PASSWORD, Azure::ARM::ReadCred::CRED_TYPE_DOMAIN_VISIBLE_PASSWORD, Azure::ARM::ReadCred::CRED_TYPE_GENERIC

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Bootstrap::Bootstrapper

#bootstrap_common_params, #bootstrap_exec, #bootstrap_for_node, #bootstrap_for_windows_node, #create_node_and_client_pem, #default_bootstrap_template, #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_cloud_attributes_in_hints, #load_correct_secret, #load_winrm_deps, #ohai_hints, #tcp_test_ssh, #tcp_test_winrm

Methods included from Bootstrap::CommonBootstrapOptions

included

Methods included from AzurermBase

#authentication_details, #azure_authentication, #check_token_validity, #find_file, #get_azure_cli_version, included, #is_token_valid?, #locate_config_value, #msg_server_summary, #parse_publish_settings_file, #refresh_token, #service, #token_details_for_linux, #token_details_for_windows, #token_details_from_accessToken_file, #validate_arm_keys!, #validate_azure_login, #validate_params!

Methods included from Azure::ARM::WindowsCredentials

#latest_credential_target, #target_name, #token_details_from_WCM

Instance Attribute Details

#initial_sleep_delayObject

Returns the value of attribute initial_sleep_delay.



35
36
37
# File 'lib/chef/knife/azurerm_server_create.rb', line 35

def initial_sleep_delay
  @initial_sleep_delay
end

Instance Method Details

#create_server_defObject



214
215
216
217
218
219
220
221
222
223
224
225
226
227
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
265
266
267
268
269
270
271
272
273
274
# File 'lib/chef/knife/azurerm_server_create.rb', line 214

def create_server_def
  server_def = {
    :azure_resource_group_name => locate_config_value(:azure_resource_group_name),
    :azure_storage_account => locate_config_value(:azure_storage_account),
    :azure_storage_account_type => locate_config_value(:azure_storage_account_type),
    :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_os_disk_caching => locate_config_value(:azure_os_disk_caching),
    :azure_os_disk_create_option => locate_config_value(:azure_os_disk_create_option),
    :azure_vm_size => locate_config_value(:azure_vm_size),
    :azure_image_reference_publisher => locate_config_value(:azure_image_reference_publisher),
    :azure_image_reference_offer => locate_config_value(:azure_image_reference_offer),
    :azure_image_reference_sku => locate_config_value(:azure_image_reference_sku),
    :azure_image_reference_version => locate_config_value(:azure_image_reference_version),
    :winrm_user => locate_config_value(:winrm_user),
    :azure_availability_set => locate_config_value(:azure_availability_set),
    :azure_vnet_name => locate_config_value(:azure_vnet_name),
    :azure_vnet_subnet_name => locate_config_value(:azure_vnet_subnet_name),
    :ssl_cert_fingerprint => locate_config_value(:thumbprint),
    :cert_path => locate_config_value(:cert_path),
    :cert_password => locate_config_value(:cert_passphrase),
    :vnet_subnet_address_prefix => locate_config_value(:vnet_subnet_address_prefix),
    :server_count => locate_config_value(:server_count)
  }

  server_def[:tcp_endpoints] = locate_config_value(:tcp_endpoints) if locate_config_value(:tcp_endpoints)

  # We assign azure_vm_name to chef_node_name If node name is nill because storage account name is combination of hash value and node name.
  config[:chef_node_name] ||= locate_config_value(:azure_vm_name)
  
  server_def[:azure_storage_account] = locate_config_value(:azure_vm_name) if server_def[:azure_storage_account].nil?
  server_def[:azure_storage_account] = server_def[:azure_storage_account].gsub(/[!@#$%^&*()_-]/,'')

  server_def[:azure_os_disk_name] = locate_config_value(:azure_vm_name) if server_def[:azure_os_disk_name].nil?
  server_def[:azure_os_disk_name] = server_def[:azure_os_disk_name].gsub(/[!@#$%^&*()_-]/,'')

  server_def[:azure_vnet_name] = locate_config_value(:azure_vm_name) if server_def[:azure_vnet_name].nil?
  server_def[:azure_vnet_subnet_name] = locate_config_value(:azure_vm_name) if locate_config_value(:azure_vnet_subnet_name).nil?

  server_def[:chef_extension] = get_chef_extension_name
  server_def[:chef_extension_publisher] = get_chef_extension_publisher
  server_def[:chef_extension_version] = locate_config_value(:azure_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
  server_def[:auto_upgrade_minor_version] = false

  if is_image_windows?
    server_def[:admin_password] = locate_config_value(:winrm_password)
  else
    server_def[:ssh_user] = locate_config_value(:ssh_user)
    server_def[:ssh_password] = locate_config_value(:ssh_password)
    server_def[:disablePasswordAuthentication] = "false"
    if locate_config_value(:ssh_public_key)
      server_def[:disablePasswordAuthentication] = "true"
      server_def[:ssh_key] = File.read(locate_config_value(:ssh_public_key))
    end
  end

  server_def
end

#format_ohai_hints(ohai_hints) ⇒ Object



284
285
286
287
# File 'lib/chef/knife/azurerm_server_create.rb', line 284

def format_ohai_hints(ohai_hints)
  ohai_hints = ohai_hints.split(',').each { |hint| hint.strip! }
  ohai_hints.join(',')
end

#is_supported_ohai_hint?(hint) ⇒ Boolean

Returns:

  • (Boolean)


289
290
291
# File 'lib/chef/knife/azurerm_server_create.rb', line 289

def is_supported_ohai_hint?(hint)
  supported_ohai_hints.any? { |supported_ohai_hint| hint.eql? supported_ohai_hint }
end

#runObject



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/chef/knife/azurerm_server_create.rb', line 193

def run
  $stdout.sync = true
  # check azure cli version due to azure changed `azure` to `az` in azure-cli2.0
  get_azure_cli_version
  validate_arm_keys!(
    :azure_resource_group_name,
    :azure_vm_name,
    :azure_service_location
  )

  begin
    validate_params!
    set_default_image_reference!
    ssh_override_winrm if !is_image_windows?
    vm_details = service.create_server(create_server_def)
  rescue => error
    service.common_arm_rescue_block(error)
    exit
  end
end

#supported_ohai_hintsObject



276
277
278
279
280
281
282
# File 'lib/chef/knife/azurerm_server_create.rb', line 276

def supported_ohai_hints
  [
    'vm_name',
    'public_fqdn',
    'platform'
  ]
end

#validate_ohai_hintsObject



293
294
295
296
297
298
299
300
# File 'lib/chef/knife/azurerm_server_create.rb', line 293

def validate_ohai_hints
  hint_values = locate_config_value(:ohai_hints).split(',')
  hint_values.each do |hint|
    if ! is_supported_ohai_hint?(hint)
      raise ArgumentError, "Ohai Hint name #{hint} passed is not supported. Please run the command help to see the list of supported values."
    end
  end
end