Class: Kitchen::Driver::Azurerm

Inherits:
Base
  • Object
show all
Defined in:
lib/kitchen/driver/azurerm.rb

Overview

Azurerm

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#resource_management_clientObject

Returns the value of attribute resource_management_client.



19
20
21
# File 'lib/kitchen/driver/azurerm.rb', line 19

def resource_management_client
  @resource_management_client
end

Instance Method Details

#azure_resource_group_nameObject



298
299
300
301
302
# File 'lib/kitchen/driver/azurerm.rb', line 298

def azure_resource_group_name
  formatted_time = Time.now.utc.strftime "%Y%m%dT%H%M%S"
  return "#{config[:azure_resource_group_prefix]}#{config[:azure_resource_group_name]}-#{formatted_time}#{config[:azure_resource_group_suffix]}" unless config[:explicit_resource_group_name]
  config[:explicit_resource_group_name]
end

#create(state) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
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
275
276
277
278
279
280
281
# File 'lib/kitchen/driver/azurerm.rb', line 165

def create(state)
  state = validate_state(state)
  deployment_parameters = {
    location: config[:location],
    vmSize: config[:machine_size],
    storageAccountType: config[:storage_account_type],
    bootDiagnosticsEnabled: config[:boot_diagnostics_enabled],
    newStorageAccountName: "storage#{state[:uuid]}",
    adminUsername: state[:username],
    adminPassword: state[:password] || "P2ssw0rd",
    dnsNameForPublicIP: "kitchen-#{state[:uuid]}",
    vmName: state[:vm_name],
    systemAssignedIdentity: config[:system_assigned_identity],
    userAssignedIdentities: config[:user_assigned_identities],
  }

  if config[:subscription_id].to_s == ""
    raise "A subscription_id config value was not detected and kitchen-azurerm cannot continue. Please check your .kitchen.yml configuration. Exiting."
  end

  if config[:custom_data].to_s != ""
    deployment_parameters["customData"] = prepared_custom_data
  end
  # When deploying in a shared storage account, we needs to add
  # a unique suffix to support multiple kitchen instances
  if config[:existing_storage_account_blob_url].to_s != ""
    deployment_parameters["osDiskNameSuffix"] = "-#{state[:azure_resource_group_name]}"
  end
  if config[:existing_storage_account_blob_url].to_s != ""
    deployment_parameters["existingStorageAccountBlobURL"] = config[:existing_storage_account_blob_url]
  end
  if config[:existing_storage_account_container].to_s != ""
    deployment_parameters["existingStorageAccountBlobContainer"] = config[:existing_storage_account_container]
  end
  if config[:os_disk_size_gb].to_s != ""
    deployment_parameters["osDiskSizeGb"] = config[:os_disk_size_gb]
  end

  # The three deployment modes
  #  a) Private Image: Managed VM Image (by id)
  #  b) Private Image: Using a VHD URL (note: we must use existing_storage_account_blob_url due to azure limitations)
  #  c) Public Image: Using a marketplace image (urn)
  if config[:image_id].to_s != ""
    deployment_parameters["imageId"] = config[:image_id]
  elsif config[:image_url].to_s != ""
    deployment_parameters["imageUrl"] = config[:image_url]
    deployment_parameters["osType"] = config[:os_type]
  else
    image_publisher, image_offer, image_sku, image_version = config[:image_urn].split(":", 4)
    deployment_parameters["imagePublisher"] = image_publisher
    deployment_parameters["imageOffer"] = image_offer
    deployment_parameters["imageSku"] = image_sku
    deployment_parameters["imageVersion"] = image_version
  end

  options = Kitchen::Driver::Credentials.new.azure_options_for_subscription(config[:subscription_id], config[:azure_environment])

  debug "Azure environment: #{config[:azure_environment]}"
  @resource_management_client = ::Azure::Resources::Profiles::Latest::Mgmt::Client.new(options)

  # Create Resource Group
  resource_group = ::Azure::Resources::Profiles::Latest::Mgmt::Models::ResourceGroup.new
  resource_group.location = config[:location]
  resource_group.tags = config[:resource_group_tags]
  begin
    info "Creating Resource Group: #{state[:azure_resource_group_name]}"
    resource_management_client.resource_groups.create_or_update(state[:azure_resource_group_name], resource_group)
  rescue ::MsRestAzure::AzureOperationError => operation_error
    error operation_error.body
    raise operation_error
  end

  # Execute deployment steps
  begin
    if File.file?(config[:pre_deployment_template])
      pre_deployment_name = "pre-deploy-#{state[:uuid]}"
      info "Creating deployment: #{pre_deployment_name}"
      resource_management_client.deployments.begin_create_or_update_async(state[:azure_resource_group_name], pre_deployment_name, pre_deployment(config[:pre_deployment_template], config[:pre_deployment_parameters])).value!
      follow_deployment_until_end_state(state[:azure_resource_group_name], pre_deployment_name)
    end
    deployment_name = "deploy-#{state[:uuid]}"
    info "Creating deployment: #{deployment_name}"
    resource_management_client.deployments.begin_create_or_update_async(state[:azure_resource_group_name], deployment_name, deployment(deployment_parameters)).value!
    follow_deployment_until_end_state(state[:azure_resource_group_name], deployment_name)
    if File.file?(config[:post_deployment_template])
      post_deployment_name = "post-deploy-#{state[:uuid]}"
      info "Creating deployment: #{post_deployment_name}"
      resource_management_client.deployments.begin_create_or_update_async(state[:azure_resource_group_name], post_deployment_name, post_deployment(config[:post_deployment_template], config[:post_deployment_parameters])).value!
      follow_deployment_until_end_state(state[:azure_resource_group_name], post_deployment_name)
    end
  rescue ::MsRestAzure::AzureOperationError => operation_error
    rest_error = operation_error.body["error"]
    deployment_active = rest_error["code"] == "DeploymentActive"
    if deployment_active
      info "Deployment for resource group #{state[:azure_resource_group_name]} is ongoing."
      info "If you need to change the deployment template you'll need to rerun `kitchen create` for this instance."
    else
      info rest_error
      raise operation_error
    end
  end

  network_management_client = ::Azure::Network::Profiles::Latest::Mgmt::Client.new(options)

  if config[:vnet_id] == "" || config[:public_ip]
    # Retrieve the public IP from the resource group:
    result = network_management_client.public_ipaddresses.get(state[:azure_resource_group_name], "publicip")
    info "IP Address is: #{result.ip_address} [#{result.dns_settings.fqdn}]"
    state[:hostname] = result.ip_address
  else
    # Retrieve the internal IP from the resource group:
    network_interfaces = ::Azure::Network::Profiles::Latest::Mgmt::NetworkInterfaces.new(network_management_client)
    result = network_interfaces.get(state[:azure_resource_group_name], "nic")
    info "IP Address is: #{result.ip_configurations[0].private_ipaddress}"
    state[:hostname] = result.ip_configurations[0].private_ipaddress
  end
end

#custom_data_script_windowsObject



549
550
551
552
553
554
555
# File 'lib/kitchen/driver/azurerm.rb', line 549

def custom_data_script_windows
  <<-EOH
  #{enable_winrm_powershell_script}
  #{format_data_disks_powershell_script}
  logoff
  EOH
end

#custom_linux_configuration(public_key) ⇒ Object



557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
# File 'lib/kitchen/driver/azurerm.rb', line 557

def custom_linux_configuration(public_key)
  <<-EOH
  {
    "disablePasswordAuthentication": "true",
    "ssh": {
      "publicKeys": [
        {
          "path": "[concat('/home/',parameters('adminUsername'),'/.ssh/authorized_keys')]",
          "keyData": "#{public_key}"
        }
      ]
    }
  }
  EOH
end

#data_disks_for_vm_jsonObject



304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/kitchen/driver/azurerm.rb', line 304

def data_disks_for_vm_json
  return nil if config[:data_disks].nil?
  disks = []

  if config[:use_managed_disks]
    config[:data_disks].each do |data_disk|
      disks << { name: "datadisk#{data_disk[:lun]}", lun: data_disk[:lun], diskSizeGB: data_disk[:disk_size_gb], createOption: "Empty" }
    end
    debug "Additional disks being added to configuration: #{disks.inspect}"
  else
    warn 'Data disks are only supported when used with the "use_managed_disks" option. No additional disks were added to the configuration.'
  end
  disks.to_json
end

#deployment(parameters) ⇒ Object



376
377
378
379
380
381
382
383
384
385
# File 'lib/kitchen/driver/azurerm.rb', line 376

def deployment(parameters)
  template = template_for_transport_name
  deployment = ::Azure::Resources::Profiles::Latest::Mgmt::Models::Deployment.new
  deployment.properties = ::Azure::Resources::Profiles::Latest::Mgmt::Models::DeploymentProperties.new
  deployment.properties.mode = ::Azure::Resources::Profiles::Latest::Mgmt::Models::DeploymentMode::Incremental
  deployment.properties.template = JSON.parse(template)
  deployment.properties.parameters = parameters_in_values_format(parameters)
  debug(JSON.pretty_generate(deployment.properties.template))
  deployment
end

#deployment_state(resource_group, deployment_name) ⇒ Object



465
466
467
468
# File 'lib/kitchen/driver/azurerm.rb', line 465

def deployment_state(resource_group, deployment_name)
  deployments = resource_management_client.deployments.get(resource_group, deployment_name)
  deployments.properties.provisioning_state
end

#destroy(state) ⇒ Object



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
503
504
# File 'lib/kitchen/driver/azurerm.rb', line 470

def destroy(state)
  return if state[:server_id].nil?
  options = Kitchen::Driver::Credentials.new.azure_options_for_subscription(state[:subscription_id], state[:azure_environment])
  @resource_management_client = ::Azure::Resources::Profiles::Latest::Mgmt::Client.new(options)
  if config[:destroy_resource_group_contents] == true
    info "Destroying individual resources within the Resource Group."
    empty_deployment_name = "empty-deploy-#{state[:uuid]}"
    begin
      info "Creating deployment: #{empty_deployment_name}"
      resource_management_client.deployments.begin_create_or_update_async(state[:azure_resource_group_name], empty_deployment_name, empty_deployment).value!
      follow_deployment_until_end_state(state[:azure_resource_group_name], empty_deployment_name)
    rescue ::MsRestAzure::AzureOperationError => operation_error
      error operation_error.body
      raise operation_error
    end
  end
  if config[:destroy_explicit_resource_group] == false && !config[:explicit_resource_group_name].nil?
    warn 'The "destroy_explicit_resource_group" setting value is set to "false". The resource group will not be deleted.'
    warn 'Remember to manually destroy resources, or set "destroy_resource_group_contents: true" to save costs!' unless config[:destroy_resource_group_contents] == true
    return
  end
  info "Azure environment: #{state[:azure_environment]}"
  begin
    info "Destroying Resource Group: #{state[:azure_resource_group_name]}"
    resource_management_client.resource_groups.begin_delete(state[:azure_resource_group_name])
    info "Destroy operation accepted and will continue in the background."
  rescue ::MsRestAzure::AzureOperationError => operation_error
    error operation_error.body
    raise operation_error
  end
  state.delete(:server_id)
  state.delete(:hostname)
  state.delete(:username)
  state.delete(:password)
end

#empty_deploymentObject



398
399
400
401
402
403
404
405
406
# File 'lib/kitchen/driver/azurerm.rb', line 398

def empty_deployment
  template = virtual_machine_deployment_template_file("empty.erb", nil)
  empty_deployment = ::Azure::Resources::Profiles::Latest::Mgmt::Models::Deployment.new
  empty_deployment.properties = ::Azure::Resources::Profiles::Latest::Mgmt::Models::DeploymentProperties.new
  empty_deployment.properties.mode = ::Azure::Resources::Profiles::Latest::Mgmt::Models::DeploymentMode::Complete
  empty_deployment.properties.template = JSON.parse(template)
  debug(JSON.pretty_generate(empty_deployment.properties.template))
  empty_deployment
end

#enable_winrm_powershell_scriptObject



506
507
508
509
510
511
512
513
514
515
516
517
# File 'lib/kitchen/driver/azurerm.rb', line 506

def enable_winrm_powershell_script
  config[:winrm_powershell_script] ||
    <<-PS1
  $cert = New-SelfSignedCertificate -DnsName $env:COMPUTERNAME -CertStoreLocation Cert:\\LocalMachine\\My
  $config = '@{CertificateThumbprint="' + $cert.Thumbprint + '"}'
  winrm create winrm/config/listener?Address=*+Transport=HTTPS $config
  winrm set winrm/config/service/auth '@{Basic="true";Kerberos="false";Negotiate="true";Certificate="false";CredSSP="true"}'
  New-NetFirewallRule -DisplayName "Windows Remote Management (HTTPS-In)" -Name "Windows Remote Management (HTTPS-In)" -Profile Any -LocalPort 5986 -Protocol TCP
  winrm set winrm/config/service '@{AllowUnencrypted="true"}'
  New-NetFirewallRule -DisplayName "Windows Remote Management (HTTP-In)" -Name "Windows Remote Management (HTTP-In)" -Profile Any -LocalPort 5985 -Protocol TCP
    PS1
end

#existing_state_value?(state, property) ⇒ Boolean

Returns:

  • (Boolean)


283
284
285
# File 'lib/kitchen/driver/azurerm.rb', line 283

def existing_state_value?(state, property)
  state.key?(property) && !state[property].nil?
end

#follow_deployment_until_end_state(resource_group, deployment_name) ⇒ Object



428
429
430
431
432
433
434
435
436
437
438
439
# File 'lib/kitchen/driver/azurerm.rb', line 428

def follow_deployment_until_end_state(resource_group, deployment_name)
  end_provisioning_states = "Canceled,Failed,Deleted,Succeeded"
  end_provisioning_state_reached = false
  until end_provisioning_state_reached
    list_outstanding_deployment_operations(resource_group, deployment_name)
    sleep 10
    deployment_provisioning_state = deployment_state(resource_group, deployment_name)
    end_provisioning_state_reached = end_provisioning_states.split(",").include?(deployment_provisioning_state)
  end
  info "Resource Template deployment reached end state of '#{deployment_provisioning_state}'."
  show_failed_operations(resource_group, deployment_name) if deployment_provisioning_state == "Failed"
end

#format_data_disks_powershell_scriptObject



519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
# File 'lib/kitchen/driver/azurerm.rb', line 519

def format_data_disks_powershell_script
  return unless config[:format_data_disks]
  info "Data disks will be initialized and formatted NTFS automatically." unless config[:data_disks].nil?
  config[:format_data_disks_powershell_script] ||
    <<-PS1
  Write-Host "Initializing and formatting raw disks"
  $disks = Get-Disk | where partitionstyle -eq 'raw'
  $letters = New-Object System.Collections.ArrayList
  $letters.AddRange( ('F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z') )
  Function AvailableVolumes() {
  $currentDrives = get-volume
  ForEach ($v in $currentDrives) {
    if ($letters -contains $v.DriveLetter.ToString()) {
  Write-Host "Drive letter $($v.DriveLetter) is taken, moving to next letter"
  $letters.Remove($v.DriveLetter.ToString())
}
    }
  }
  ForEach ($d in $disks) {
    AvailableVolumes
    $driveLetter = $letters[0]
    Write-Host "Creating volume $($driveLetter)"
    $d | Initialize-Disk -PartitionStyle GPT -PassThru | New-Partition -DriveLetter $driveLetter  -UseMaximumSize
    # Prevent error ' Cannot perform the requested operation while the drive is read only'
    Start-Sleep 1
    Format-Volume -FileSystem NTFS -NewFileSystemLabel "datadisk" -DriveLetter $driveLetter -Confirm:$false
  }
    PS1
end

#list_outstanding_deployment_operations(resource_group, deployment_name) ⇒ Object



449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
# File 'lib/kitchen/driver/azurerm.rb', line 449

def list_outstanding_deployment_operations(resource_group, deployment_name)
  end_operation_states = "Failed,Succeeded"
  deployment_operations = resource_management_client.deployment_operations.list(resource_group, deployment_name)
  deployment_operations.each do |val|
    resource_provisioning_state = val.properties.provisioning_state
    unless val.properties.target_resource.nil?
      resource_name = val.properties.target_resource.resource_name
      resource_type = val.properties.target_resource.resource_type
    end
    end_operation_state_reached = end_operation_states.split(",").include?(resource_provisioning_state)
    unless end_operation_state_reached
      info "Resource #{resource_type} '#{resource_name}' provisioning status is #{resource_provisioning_state}"
    end
  end
end

#parameters_in_values_format(parameters_in) ⇒ Object



421
422
423
424
425
426
# File 'lib/kitchen/driver/azurerm.rb', line 421

def parameters_in_values_format(parameters_in)
  parameters = parameters_in.map do |key, value|
    { key.to_sym => { "value" => value } }
  end
  parameters.reduce(:merge!)
end

#post_deployment(post_deployment_template_filename, post_deployment_parameters) ⇒ Object



387
388
389
390
391
392
393
394
395
396
# File 'lib/kitchen/driver/azurerm.rb', line 387

def post_deployment(post_deployment_template_filename, post_deployment_parameters)
  post_deployment_template = ::File.read(post_deployment_template_filename)
  post_deployment = ::Azure::Resources::Profiles::Latest::Mgmt::Models::Deployment.new
  post_deployment.properties = ::Azure::Resources::Profiles::Latest::Mgmt::Models::DeploymentProperties.new
  post_deployment.properties.mode = ::Azure::Resources::Profiles::Latest::Mgmt::Models::DeploymentMode::Incremental
  post_deployment.properties.template = JSON.parse(post_deployment_template)
  post_deployment.properties.parameters = parameters_in_values_format(post_deployment_parameters)
  debug(post_deployment.properties.template)
  post_deployment
end

#pre_deployment(pre_deployment_template_filename, pre_deployment_parameters) ⇒ Object



365
366
367
368
369
370
371
372
373
374
# File 'lib/kitchen/driver/azurerm.rb', line 365

def pre_deployment(pre_deployment_template_filename, pre_deployment_parameters)
  pre_deployment_template = ::File.read(pre_deployment_template_filename)
  pre_deployment = ::Azure::Resources::Profiles::Latest::Mgmt::Models::Deployment.new
  pre_deployment.properties = ::Azure::Resources::Profiles::Latest::Mgmt::Models::DeploymentProperties.new
  pre_deployment.properties.mode = ::Azure::Resources::Profiles::Latest::Mgmt::Models::DeploymentMode::Incremental
  pre_deployment.properties.template = JSON.parse(pre_deployment_template)
  pre_deployment.properties.parameters = parameters_in_values_format(pre_deployment_parameters)
  debug(pre_deployment.properties.template)
  pre_deployment
end

#prepared_custom_dataObject



620
621
622
623
624
625
626
627
628
629
630
# File 'lib/kitchen/driver/azurerm.rb', line 620

def prepared_custom_data
  # If user_data is a file reference, lets read it as such
  return nil if config[:custom_data].nil?
  @custom_data ||= begin
    if File.file?(config[:custom_data])
      Base64.strict_encode64(File.read(config[:custom_data]))
    else
      Base64.strict_encode64(config[:custom_data])
    end
  end
end

#public_key_for_deployment(private_key_filename) ⇒ Object



342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# File 'lib/kitchen/driver/azurerm.rb', line 342

def public_key_for_deployment(private_key_filename)
  if File.file?(private_key_filename) == false
    k = SSHKey.generate

    ::FileUtils.mkdir_p(File.dirname(private_key_filename))

    private_key_file = File.new(private_key_filename, "w")
    private_key_file.syswrite(k.private_key)
    private_key_file.chmod(0600)
    private_key_file.close

    public_key_file = File.new("#{private_key_filename}.pub", "w")
    public_key_file.syswrite(k.ssh_public_key)
    public_key_file.chmod(0600)
    public_key_file.close

    output = k.ssh_public_key
  else
    output = File.read("#{private_key_filename}.pub")
  end
  output.strip
end

#resource_manager_endpoint_url(azure_environment) ⇒ Object



607
608
609
610
611
612
613
614
615
616
617
618
# File 'lib/kitchen/driver/azurerm.rb', line 607

def resource_manager_endpoint_url(azure_environment)
  case azure_environment.downcase
  when "azureusgovernment"
    MsRestAzure::AzureEnvironments::AzureUSGovernment.resource_manager_endpoint_url
  when "azurechina"
    MsRestAzure::AzureEnvironments::AzureChinaCloud.resource_manager_endpoint_url
  when "azuregermancloud"
    MsRestAzure::AzureEnvironments::AzureGermanCloud.resource_manager_endpoint_url
  when "azure"
    MsRestAzure::AzureEnvironments::AzureCloud.resource_manager_endpoint_url
  end
end

#show_failed_operations(resource_group, deployment_name) ⇒ Object



441
442
443
444
445
446
447
# File 'lib/kitchen/driver/azurerm.rb', line 441

def show_failed_operations(resource_group, deployment_name)
  failed_operations = resource_management_client.deployment_operations.list(resource_group, deployment_name)
  failed_operations.each do |val|
    resource_code = val.properties.status_code
    raise val.properties.status_message.inspect.to_s if resource_code != "OK"
  end
end

#template_for_transport_nameObject



319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'lib/kitchen/driver/azurerm.rb', line 319

def template_for_transport_name
  template = JSON.parse(virtual_machine_deployment_template)
  if instance.transport.name.casecmp("winrm") == 0
    if instance.platform.name.index("nano").nil?
      info "Adding WinRM configuration to provisioning profile."
      encoded_command = Base64.strict_encode64(custom_data_script_windows)
      template["resources"].select { |h| h["type"] == "Microsoft.Compute/virtualMachines" }.each do |resource|
        resource["properties"]["osProfile"]["customData"] = encoded_command
        resource["properties"]["osProfile"]["windowsConfiguration"] = windows_unattend_content
      end
    end
  end

  unless instance.transport[:ssh_key].nil?
    info "Adding public key from #{File.expand_path(instance.transport[:ssh_key])}.pub to the deployment."
    public_key = public_key_for_deployment(File.expand_path(instance.transport[:ssh_key]))
    template["resources"].select { |h| h["type"] == "Microsoft.Compute/virtualMachines" }.each do |resource|
      resource["properties"]["osProfile"]["linuxConfiguration"] = JSON.parse(custom_linux_configuration(public_key))
    end
  end
  template.to_json
end

#validate_state(state = {}) ⇒ Object



287
288
289
290
291
292
293
294
295
296
# File 'lib/kitchen/driver/azurerm.rb', line 287

def validate_state(state = {})
  state[:uuid] = SecureRandom.hex(8) unless existing_state_value?(state, :uuid)
  state[:server_id] = "vm#{state[:uuid]}" unless existing_state_value?(state, :server_id)
  state[:azure_resource_group_name] = azure_resource_group_name unless existing_state_value?(state, :azure_resource_group_name)
  %i{subscription_id username password vm_name azure_environment use_managed_disks}.each do |config_element|
    state[config_element] = config[config_element] unless existing_state_value?(state, config_element)
  end
  state.delete(:password) unless instance.transport[:ssh_key].nil?
  state
end

#virtual_machine_deployment_templateObject



592
593
594
595
596
597
598
599
# File 'lib/kitchen/driver/azurerm.rb', line 592

def virtual_machine_deployment_template
  if config[:vnet_id] == ""
    virtual_machine_deployment_template_file("public.erb", vm_tags: vm_tag_string(config[:vm_tags]), use_managed_disks: config[:use_managed_disks], image_url: config[:image_url], existing_storage_account_blob_url: config[:existing_storage_account_blob_url], image_id: config[:image_id], existing_storage_account_container: config[:existing_storage_account_container], custom_data: config[:custom_data], os_disk_size_gb: config[:os_disk_size_gb], data_disks_for_vm_json: data_disks_for_vm_json)
  else
    info "Using custom vnet: #{config[:vnet_id]}"
    virtual_machine_deployment_template_file("internal.erb", vnet_id: config[:vnet_id], subnet_id: config[:subnet_id], public_ip: config[:public_ip], vm_tags: vm_tag_string(config[:vm_tags]), use_managed_disks: config[:use_managed_disks], image_url: config[:image_url], existing_storage_account_blob_url: config[:existing_storage_account_blob_url], image_id: config[:image_id], existing_storage_account_container: config[:existing_storage_account_container], custom_data: config[:custom_data], os_disk_size_gb: config[:os_disk_size_gb], data_disks_for_vm_json: data_disks_for_vm_json)
  end
end

#virtual_machine_deployment_template_file(template_file, data = {}) ⇒ Object



601
602
603
604
605
# File 'lib/kitchen/driver/azurerm.rb', line 601

def virtual_machine_deployment_template_file(template_file, data = {})
  template = File.read(File.expand_path(File.join(__dir__, "../../../templates", template_file)))
  render_binding = OpenStruct.new(data)
  ERB.new(template, nil, "-").result(render_binding.instance_eval { binding })
end

#vm_tag_string(vm_tags_in) ⇒ Object



408
409
410
411
412
413
414
415
416
417
418
419
# File 'lib/kitchen/driver/azurerm.rb', line 408

def vm_tag_string(vm_tags_in)
  tag_string = ""
  unless vm_tags_in.empty?
    tag_array = vm_tags_in.map do |key, value|
      "\"#{key}\": \"#{value}\",\n"
    end
    # Strip punctuation from last item
    tag_array[-1] = tag_array[-1][0..-3]
    tag_string = tag_array.join
  end
  tag_string
end

#windows_unattend_contentObject



573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
# File 'lib/kitchen/driver/azurerm.rb', line 573

def windows_unattend_content
  {
    additionalUnattendContent: [
      {
        passName: "oobeSystem",
        componentName: "Microsoft-Windows-Shell-Setup",
        settingName: "FirstLogonCommands",
        content: '<FirstLogonCommands><SynchronousCommand><CommandLine>cmd /c "copy C:\\AzureData\\CustomData.bin C:\\Config.ps1"</CommandLine><Description>copy</Description><Order>1</Order></SynchronousCommand><SynchronousCommand><CommandLine>%windir%\\System32\\WindowsPowerShell\\v1.0\\powershell.exe -NoProfile -ExecutionPolicy Bypass -file C:\\Config.ps1</CommandLine><Description>script</Description><Order>2</Order></SynchronousCommand></FirstLogonCommands>',
      },
      {
        passName: "oobeSystem",
        componentName: "Microsoft-Windows-Shell-Setup",
        settingName: "AutoLogon",
        content: "[concat('<AutoLogon><Password><Value>', parameters('adminPassword'), '</Value></Password><Enabled>true</Enabled><LogonCount>1</LogonCount><Username>', parameters('adminUserName'), '</Username></AutoLogon>')]",
      }
    ],
  }
end