Class: Azure::Role

Inherits:
Object
  • Object
show all
Includes:
AzureUtility
Defined in:
lib/azure/service_management/role.rb

Constant Summary collapse

TCP_ENDPOINTS_MAPPING =
{ '3389' => 'Remote Desktop',
'5986' => 'PowerShell',
'22' => 'SSH',
'21' => 'FTP',
'25' => 'SMTP',
'53' => 'DNS',
'80' => 'HTTP',
'110' => 'POP3',
'143' => 'IMAP',
'389' => 'LDAP',
'443' => 'HTTPs',
'587' => 'SMTPS',
'995' => 'POP3S',
'993' => 'IMAPS',
'1433' => 'MSSQL',
'3306' => 'MySQL'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from AzureUtility

#error_from_response_xml, #xml_content

Constructor Details

#initialize(connection) ⇒ Role

Returns a new instance of Role.



205
206
207
# File 'lib/azure/service_management/role.rb', line 205

def initialize(connection)
  @connection = connection
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



181
182
183
# File 'lib/azure/service_management/role.rb', line 181

def connection
  @connection
end

#deploynameObject

Returns the value of attribute deployname.



182
183
184
# File 'lib/azure/service_management/role.rb', line 182

def deployname
  @deployname
end

#hostedservicenameObject

Returns the value of attribute hostedservicename.



182
183
184
# File 'lib/azure/service_management/role.rb', line 182

def hostedservicename
  @hostedservicename
end

#hostnameObject

Returns the value of attribute hostname.



184
185
186
# File 'lib/azure/service_management/role.rb', line 184

def hostname
  @hostname
end

#ipaddressObject

Returns the value of attribute ipaddress.



181
182
183
# File 'lib/azure/service_management/role.rb', line 181

def ipaddress
  @ipaddress
end

#nameObject

Returns the value of attribute name.



181
182
183
# File 'lib/azure/service_management/role.rb', line 181

def name
  @name
end

#os_typeObject

Returns the value of attribute os_type.



185
186
187
# File 'lib/azure/service_management/role.rb', line 185

def os_type
  @os_type
end

#os_versionObject

Returns the value of attribute os_version.



185
186
187
# File 'lib/azure/service_management/role.rb', line 185

def os_version
  @os_version
end

#publicipaddressObject

Returns the value of attribute publicipaddress.



181
182
183
# File 'lib/azure/service_management/role.rb', line 181

def publicipaddress
  @publicipaddress
end

#role_xmlObject

Returns the value of attribute role_xml.



185
186
187
# File 'lib/azure/service_management/role.rb', line 185

def role_xml
  @role_xml
end

#sizeObject

Returns the value of attribute size.



181
182
183
# File 'lib/azure/service_management/role.rb', line 181

def size
  @size
end

#sshportObject

Returns the value of attribute sshport.



182
183
184
# File 'lib/azure/service_management/role.rb', line 182

def sshport
  @sshport
end

#statusObject

Returns the value of attribute status.



181
182
183
# File 'lib/azure/service_management/role.rb', line 181

def status
  @status
end

#tcpportsObject

Returns the value of attribute tcpports.



184
185
186
# File 'lib/azure/service_management/role.rb', line 184

def tcpports
  @tcpports
end

#thumbprintObject

Returns the value of attribute thumbprint.



182
183
184
# File 'lib/azure/service_management/role.rb', line 182

def thumbprint
  @thumbprint
end

#udpportsObject

Returns the value of attribute udpports.



184
185
186
# File 'lib/azure/service_management/role.rb', line 184

def udpports
  @udpports
end

#winrmportObject

Returns the value of attribute winrmport.



183
184
185
# File 'lib/azure/service_management/role.rb', line 183

def winrmport
  @winrmport
end

Instance Method Details

#add_endpoints_to_xml(xml, endpoints, params) ⇒ Object



279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/azure/service_management/role.rb', line 279

def add_endpoints_to_xml(xml, endpoints, params)
  existing_endpoints = find_deploy(params).input_endpoints

  endpoints.each do |ep|

    if existing_endpoints
      existing_endpoints.each do |eep|
        ep = eep if eep['LoadBalancedEndpointSetName'] && ep['LoadBalancedEndpointSetName'] && ( eep['LoadBalancedEndpointSetName'] == ep['LoadBalancedEndpointSetName'] )
      end
    end

    if ep['Port'] == params[:port] && ep['Protocol'].downcase == 'tcp'
      puts("Skipping tcp-endpoints: #{ep['LocalPort']} because this port is already in use by ssh/winrm endpoint in current VM.")
      next
    end

    xml.InputEndpoint {
      xml.LoadBalancedEndpointSetName ep['LoadBalancedEndpointSetName'] if ep['LoadBalancedEndpointSetName']
      xml.LocalPort ep['LocalPort']
      xml.Name ep['Name']
      xml.Port ep['Port']
      if ep['LoadBalancerProbe']
        xml.LoadBalancerProbe {
          xml.Path ep['LoadBalancerProbe']['Path'] if ep['LoadBalancerProbe']['Path']
          xml.Port ep['LoadBalancerProbe']['Port']
          xml.Protocol ep['LoadBalancerProbe']['Protocol']
          xml.IntervalInSeconds ep['LoadBalancerProbe']['IntervalInSeconds'] if ep['LoadBalancerProbe']['IntervalInSeconds']
          xml.TimeoutInSeconds ep['LoadBalancerProbe']['TimeoutInSeconds'] if ep['LoadBalancerProbe']['TimeoutInSeconds']
        }
      end
      xml.Protocol ep['Protocol']
      xml.EnableDirectServerReturn ep['EnableDirectServerReturn'] if ep['EnableDirectServerReturn']
      xml.LoadBalancerName ep['LoadBalancerName'] if ep['LoadBalancerName']
      xml.IdleTimeoutInMinutes ep['IdleTimeoutInMinutes'] if ep['IdleTimeoutInMinutes']
    }
  end
end

#create(params, roleXML) ⇒ Object



560
561
562
563
564
# File 'lib/azure/service_management/role.rb', line 560

def create(params, roleXML)
  servicecall = "hostedservices/#{params[:azure_dns_name]}/deployments" +
  "/#{params['deploy_name']}/roles"
  @connection.query_azure(servicecall, "post", roleXML.to_xml)
end

#fetch_thumbprintObject



317
318
319
320
# File 'lib/azure/service_management/role.rb', line 317

def fetch_thumbprint
  query_result = connection.query_azure("hostedservices/#{@hostedservicename}/deployments/#{@hostedservicename}/roles/#{@name}")
  query_result.at_css("DefaultWinRmCertificateThumbprint").nil? ? '' : query_result.at_css("DefaultWinRmCertificateThumbprint").text
end

#find_deploy(params) ⇒ Object



275
276
277
# File 'lib/azure/service_management/role.rb', line 275

def find_deploy(params)
  @connection.hosts.find(params[:azure_dns_name]).deploys[0] # TODO this relies on the 'production only' bug.
end

#parse(roleXML, hostedservicename, deployname) ⇒ Object



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
# File 'lib/azure/service_management/role.rb', line 209

def parse(roleXML, hostedservicename, deployname)
  @name = xml_content(roleXML, 'RoleName')
  @status = xml_content(roleXML, 'InstanceStatus')
  @size = xml_content(roleXML, 'InstanceSize')
  @ipaddress = xml_content(roleXML, 'IpAddress')
  @hostname = xml_content(roleXML, 'HostName')
  @hostedservicename = hostedservicename
  @deployname = deployname
  @thumbprint = fetch_thumbprint
  @tcpports = Array.new
  @udpports = Array.new

  endpoints = roleXML.css('InstanceEndpoint')
  @publicipaddress = xml_content(endpoints[0], 'Vip') if !endpoints.empty?
  endpoints.each do |endpoint|
    if xml_content(endpoint, 'Name').downcase == 'ssh'
      @sshport = xml_content(endpoint, 'PublicPort')
    elsif xml_content(endpoint, 'Name').downcase == 'winrm'
      @winrmport = xml_content(endpoint, 'PublicPort')
    else
      hash = Hash.new
      hash['Name'] = xml_content(endpoint, 'Name')
      hash['Vip'] = xml_content(endpoint, 'Vip')
      hash['PublicPort'] = xml_content(endpoint, 'PublicPort')
      hash['LocalPort'] = xml_content(endpoint, 'LocalPort')

      if xml_content(endpoint, 'Protocol') == 'tcp'
        @tcpports << hash
      else # == 'udp'
        @udpports << hash
      end
    end
  end
end

#parse_endpoint_from_params(protocol, azure_vm_name, endpoint_param_string) ⇒ Object

Expects endpoint_param_string to be in the form localport:publicport:lb_set_name:lb_probe_path Only localport is mandatory.



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/azure/service_management/role.rb', line 253

def parse_endpoint_from_params(protocol, azure_vm_name, endpoint_param_string)
  fields = endpoint_param_string.split(':').map(&:strip)
  hash = {}
  hash['LocalPort'] = fields[0]
  hash['Port'] = fields[1] || fields[0]
  hash['LoadBalancerName'] = fields[2] if fields[2] != 'EXTERNAL' # TODO: hackity hack.. Shouldn't use magic words.
  hash['LoadBalancedEndpointSetName'] = fields[3]
  hash['Protocol'] = protocol
  if TCP_ENDPOINTS_MAPPING.include?(hash['Port']) && protocol == 'TCP'
    hash['Name'] = TCP_ENDPOINTS_MAPPING[hash['Port']]
  else
    hash['Name'] = "#{protocol}Endpoint_chef_#{fields[0]}"
  end
  if fields[2]
    hash['LoadBalancerProbe'] = {}
    hash['LoadBalancerProbe']['Path'] = fields[4]
    hash['LoadBalancerProbe']['Port'] = fields[0]
    hash['LoadBalancerProbe']['Protocol'] = fields[4] ? 'HTTP' : protocol
  end
  hash
end

#parse_role_list_xml(roleListXML) ⇒ Object



244
245
246
247
248
249
# File 'lib/azure/service_management/role.rb', line 244

def parse_role_list_xml(roleListXML)
  @role_xml = roleListXML
  os_disk_xml = roleListXML.css('OSVirtualHardDisk')
  @os_type = xml_content(os_disk_xml, 'OS')
  @os_version = xml_content(os_disk_xml, 'SourceImageName')
end

#setup(params) ⇒ Object



322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
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
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
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
548
549
550
551
552
553
554
555
556
557
558
# File 'lib/azure/service_management/role.rb', line 322

def setup(params)
  azure_user_domain_name = params[:azure_user_domain_name] || params[:azure_domain_name]
  builder = Nokogiri::XML::Builder.new do |xml|
    xml.PersistentVMRole(
      'xmlns'=>'http://schemas.microsoft.com/windowsazure',
      'xmlns:i'=>'http://www.w3.org/2001/XMLSchema-instance'
    ) {
      xml.RoleName {xml.text params[:azure_vm_name]}
      xml.OsVersion('i:nil' => 'true')
      xml.RoleType 'PersistentVMRole'

      xml.ConfigurationSets {
        if params[:os_type] == 'Linux'
          xml.ConfigurationSet('i:type' => 'LinuxProvisioningConfigurationSet') {
            xml.ConfigurationSetType 'LinuxProvisioningConfiguration'
            xml.HostName params[:azure_vm_name]
            xml.UserName params[:ssh_user]
            unless params[:identity_file].nil?
              xml.DisableSshPasswordAuthentication 'true'
              xml.SSH {
                 xml.PublicKeys {
                   xml.PublicKey {
                     xml.Fingerprint params[:fingerprint].to_s.upcase
                     xml.Path '/home/' + params[:ssh_user] + '/.ssh/authorized_keys'
                   }
                 }
              }
            else
              xml.UserPassword params[:ssh_password]
              xml.DisableSshPasswordAuthentication 'false'
            end
          }
        elsif params[:os_type] == 'Windows'
          xml.ConfigurationSet('i:type' => 'WindowsProvisioningConfigurationSet') {
            xml.ConfigurationSetType 'WindowsProvisioningConfiguration'
            xml.ComputerName params[:azure_vm_name]
            xml.AdminPassword params[:admin_password]
            xml.ResetPasswordOnFirstLogon 'false'
            xml.EnableAutomaticUpdates 'false'
            if params[:azure_domain_name]
              xml.DomainJoin {
                xml.Credentials {
                  xml.Domain azure_user_domain_name
                 xml.Username params[:azure_domain_user]
                xml.Password params[:azure_domain_passwd]
               }
               xml.JoinDomain params[:azure_domain_name]
               xml.MachineObjectOU params[:azure_domain_ou_dn] if params[:azure_domain_ou_dn]
              }
            end
              if params[:bootstrap_proto].downcase == 'winrm'
                if params[:ssl_cert_fingerprint]
                  xml.StoredCertificateSettings {
                    xml.CertificateSetting {
                      xml.StoreLocation "LocalMachine"
                      xml.StoreName "My"
                      xml.Thumbprint params[:ssl_cert_fingerprint]
                    }
                  }
                end
                xml.WinRM {
                  xml.Listeners {
                   if params[:winrm_transport] == "ssl" || params[:ssl_cert_fingerprint]
                    xml.Listener {
                      xml.CertificateThumbprint params[:ssl_cert_fingerprint] if params[:ssl_cert_fingerprint]
                      xml.Protocol 'Https'
                    }
                    else
                    xml.Listener {
                      xml.Protocol 'Http'
                    }
                    end
                  }
                }
              end
            xml.AdminUsername params[:winrm_user]
            if params[:bootstrap_proto].downcase == 'winrm' && (params[:winrm_max_timeout] || params[:winrm_max_memoryPerShell])
              xml.AdditionalUnattendContent {
                xml.Passes {
                  xml.UnattendPass {
                    xml.PassName 'oobeSystem'
                    xml.Components {
                      xml.UnattendComponent {
                        xml.ComponentName 'Microsoft-Windows-Shell-Setup'
                        xml.ComponentSettings {
                          xml.ComponentSetting {
                            xml.SettingName 'AutoLogon'
                            xml.Content Base64.encode64(
                              Nokogiri::XML::Builder.new do |auto_logon_xml|
                                auto_logon_xml.AutoLogon {
                                  auto_logon_xml.Username params[:winrm_user]
                                  auto_logon_xml.Password {
                                    auto_logon_xml.Value params[:admin_password]
                                    auto_logon_xml.PlainText true
                                  }
                                  auto_logon_xml.LogonCount 1
                                  auto_logon_xml.Enabled true
                                }
                              end.to_xml(save_with: Nokogiri::XML::Node::SaveOptions::NO_DECLARATION)
                            ).strip
                          }
                          xml.ComponentSetting {
                            xml.SettingName 'FirstLogonCommands'
                            xml.Content Base64.encode64(
                              Nokogiri::XML::Builder.new do |first_logon_xml|
                                first_logon_xml.FirstLogonCommands {
                                  if params[:winrm_max_timeout]
                                    first_logon_xml.SynchronousCommand('wcm:action' => 'add') {
                                      first_logon_xml.Order 1
                                      first_logon_xml.CommandLine "cmd.exe /c winrm set winrm/config @{MaxTimeoutms=\"#{params[:winrm_max_timeout]}\"}"
                                      first_logon_xml.Description "Bump WinRM max timeout to #{params[:winrm_max_timeout]} milliseconds"
                                    }
                                  end

                                  if params[:winrm_max_memoryPerShell]
                                    first_logon_xml.SynchronousCommand('wcm:action' => 'add') {
                                      first_logon_xml.Order 2
                                      first_logon_xml.CommandLine "cmd.exe /c winrm set winrm/config/winrs @{MaxMemoryPerShellMB=\"#{params[:winrm_max_memoryPerShell]}\"}"
                                      first_logon_xml.Description "Bump WinRM max memory per shell to #{params[:winrm_max_memoryPerShell]} MB"
                                    }
                                  end
                                }
                              end.to_xml(save_with: Nokogiri::XML::Node::SaveOptions::NO_DECLARATION)
                            ).strip
                          }
                        }
                      }
                    }
                  }
                }
              }
            end
          }
        end

        xml.ConfigurationSet('i:type' => 'NetworkConfigurationSet') {
          xml.ConfigurationSetType 'NetworkConfiguration'
          xml.InputEndpoints {

            #1. bootstrap_proto = 'winrm' for windows => Set winrm port
            #2. bootstrap_proto = 'ssh' for windows and linux => Set ssh port
            #3. bootstrap_proto = 'cloud-api' for windows and linux => Set no port
            if params[:os_type] == 'Windows' and params[:bootstrap_proto].downcase == 'winrm'
              xml.InputEndpoint {
              if params[:winrm_transport] == "ssl"
                xml.LocalPort '5986'
              else
                xml.LocalPort '5985'
              end
              xml.Name 'WinRM'
              xml.Port params[:port]
              xml.Protocol 'TCP'
            }
            elsif(params[:bootstrap_proto].downcase == 'ssh')
              xml.InputEndpoint {
              xml.LocalPort '22'
              xml.Name 'SSH'
              xml.Port params[:port]
              xml.Protocol 'TCP'
            }
            end
            all_endpoints = Array.new

            if params[:tcp_endpoints]
              params[:tcp_endpoints].split(',').map(&:strip).each do |endpoint|
                all_endpoints << parse_endpoint_from_params('TCP', params[:azure_vm_name], endpoint)
              end
            end
            if params[:udp_endpoints]
              params[:udp_endpoints].split(',').map(&:strip).each do |endpoint|
                all_endpoints << parse_endpoint_from_params('UDP', params[:azure_vm_name], endpoint)
              end
            end
            add_endpoints_to_xml(xml, all_endpoints, params) if all_endpoints.any?
          }
          if params[:azure_subnet_name]
            xml.SubnetNames {
              xml.SubnetName params[:azure_subnet_name]
            }
          end
        }
      }

      # Azure resource extension support
      if params[:bootstrap_proto] == 'cloud-api'
        xml.ResourceExtensionReferences {
          xml.ResourceExtensionReference {
            xml.ReferenceName params[:chef_extension]
            xml.Publisher params[:chef_extension_publisher]
            xml.Name params[:chef_extension]
            xml.Version params[:chef_extension_version]
            xml.ResourceExtensionParameterValues {
              if params[:chef_extension_public_param]
                xml.ResourceExtensionParameterValue {
                  xml.Key "PublicParams"
                  xml.Value Base64.encode64(params[:chef_extension_public_param].to_json)
                  xml.Type "Public"
                }
              end
              if params[:chef_extension_private_param]
                xml.ResourceExtensionParameterValue {
                  xml.Key "PrivateParams"
                  xml.Value Base64.encode64(params[:chef_extension_private_param].to_json)
                  xml.Type "Private"
                }
              end
            }
            xml.State "Enable"
          }
        }
      end

      if params[:azure_availability_set]
        xml.AvailabilitySetName params[:azure_availability_set]
      end

      xml.VMImageName params[:azure_source_image] if params[:is_vm_image]

      xml.Label Base64.encode64(params[:azure_vm_name]).strip

      #OSVirtualHardDisk not required in case azure_source_image is a VMImage
      unless(params[:is_vm_image])
        xml.OSVirtualHardDisk {
          disk_name = params[:azure_os_disk_name] || "disk_" + SecureRandom.uuid
          xml.DiskName disk_name
          domain_suffix = params[:azure_api_host_name] ? params[:azure_api_host_name].scan(/core.*/)[0] : ''
          xml.MediaLink 'http://' + params[:azure_storage_account] + '.blob.' + domain_suffix + '/vhds/' + disk_name + '.vhd'
          xml.SourceImageName params[:azure_source_image]
        }
      end

      xml.RoleSize params[:azure_vm_size]
      xml.ProvisionGuestAgent true if params[:bootstrap_proto] == 'cloud-api'
    }
  end
  builder.doc
end

#setup_extension(params) ⇒ Object



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
# File 'lib/azure/service_management/role.rb', line 566

def setup_extension(params)
  ## add Chef Extension config in role_xml retrieved from the server
  puts "Adding Chef Extension config in server role..."
  role_xml = update_role_xml_for_extension(params[:role_xml], params)

  ## role_xml can't be used for update as it has additional tags like
  ## role_name, osversion etc. which update API does not support, also the
  ## xml is the child of parent node 'Deployment' in XML, so instead of
  ## modifying the role_xml to fit for our requirements, we create
  ## new XML (with Chef Extension config and other pre-existing VM config)
  ## using the required values of the updated role_xml
  builder = Nokogiri::XML::Builder.new do |xml|
    xml.PersistentVMRole(
      'xmlns' => 'http://schemas.microsoft.com/windowsazure',
      'xmlns:i' => 'http://www.w3.org/2001/XMLSchema-instance'
    ) {
        xml.ConfigurationSets role_xml.at_css('ConfigurationSets').children if !role_xml.at_css('ConfigurationSets').nil?
        xml.ResourceExtensionReferences role_xml.at_css('ResourceExtensionReferences').children if !role_xml.at_css('ResourceExtensionReferences').nil?
        xml.AvailabilitySetName role_xml.at_css('AvailabilitySetName').children if !role_xml.at_css('AvailabilitySetName').nil?
        xml.DataVirtualHardDisks role_xml.at_css('DataVirtualHardDisks').children if !role_xml.at_css('DataVirtualHardDisks').nil?
        xml.OSVirtualHardDisk role_xml.at_css('OSVirtualHardDisk').children if !role_xml.at_css('OSVirtualHardDisk').nil?
        xml.RoleSize role_xml.at_css('RoleSize').children if !role_xml.at_css('RoleSize').nil?
        xml.ProvisionGuestAgent role_xml.at_css('ProvisionGuestAgent').children if !role_xml.at_css('ProvisionGuestAgent').nil?
    }
  end

  builder.doc.to_xml.gsub("&lt\;","<").gsub("&gt\;",">")
end

#update(name, params, roleXML) ⇒ Object



707
708
709
710
711
712
713
714
715
716
717
# File 'lib/azure/service_management/role.rb', line 707

def update(name, params, roleXML)
  puts "Updating server role..."
  servicecall = "hostedservices/#{params[:azure_dns_name]}" +
  "/deployments/#{params[:deploy_name]}/roles/#{name}"
  ret_val = @connection.query_azure(servicecall, 'put', roleXML, '', true, true, 'application/xml')
  error_code, error_message = error_from_response_xml(ret_val)
  if error_code.length > 0
    Chef::Log.debug(ret_val.to_s)
    raise 'Unable to update role:' + error_code + ' : ' + error_message
  end
end

#update_role_xml_for_extension(roleXML, params) ⇒ Object



595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
# File 'lib/azure/service_management/role.rb', line 595

def update_role_xml_for_extension(roleXML, params)
  ## check if 'ResourceExtensionReferences' node already exist in the XML,
  ## if no add it, else retrieve the object of the existing node
  add_resource_extension_references = roleXML.at_css('ResourceExtensionReferences').nil?

  if add_resource_extension_references
    resource_extension_references = Nokogiri::XML::Node.new('ResourceExtensionReferences', roleXML)
  else
    resource_extension_references = roleXML.css('ResourceExtensionReferences')
  end

  ## check if Azure Chef Extension is already installed on the given server,
  ## if no than install it, else raise error saying that the extension is
  ## already installed
  ext = nil
  if !add_resource_extension_references
    if !resource_extension_references.at_css('ReferenceName').nil?
      resource_extension_references.css('ReferenceName').each { |node| ext = node if node.content == params[:chef_extension] }
    end
  end

  add_resource_extension_reference = ext.nil?

  ## create Azure Chef Extension config and add it in the role_xml
  if add_resource_extension_reference
    resource_extension_reference = Nokogiri::XML::Node.new('ResourceExtensionReference', roleXML)

    reference_name = Nokogiri::XML::Node.new('ReferenceName', roleXML)
    reference_name.content = params[:chef_extension]
    resource_extension_reference.add_child(reference_name)

    publisher = Nokogiri::XML::Node.new('Publisher', roleXML)
    publisher.content = params[:chef_extension_publisher]
    resource_extension_reference.add_child(publisher)

    name = Nokogiri::XML::Node.new('Name', roleXML)
    name.content = params[:chef_extension]
    resource_extension_reference.add_child(name)

    version = Nokogiri::XML::Node.new('Version', roleXML)
    version.content = params[:chef_extension_version]
    resource_extension_reference.add_child(version)

    resource_extension_parameter_values = Nokogiri::XML::Node.new('ResourceExtensionParameterValues', roleXML)
    if params[:chef_extension_public_param]
      resource_extension_parameter_value = Nokogiri::XML::Node.new('ResourceExtensionParameterValue', roleXML)

      key = Nokogiri::XML::Node.new('Key', roleXML)
      key.content = 'PublicParams'
      resource_extension_parameter_value.add_child(key)

      value = Nokogiri::XML::Node.new('Value', roleXML)
      value.content = Base64.encode64(params[:chef_extension_public_param].to_json)
      resource_extension_parameter_value.add_child(value)

      type = Nokogiri::XML::Node.new('Type', roleXML)
      type.content = 'Public'
      resource_extension_parameter_value.add_child(type)

      resource_extension_parameter_values.add_child(resource_extension_parameter_value)
    end

    if params[:chef_extension_private_param]
      resource_extension_parameter_value = Nokogiri::XML::Node.new('ResourceExtensionParameterValue', roleXML)

      key = Nokogiri::XML::Node.new('Key', roleXML)
      key.content = 'PrivateParams'
      resource_extension_parameter_value.add_child(key)

      value = Nokogiri::XML::Node.new('Value', roleXML)
      value.content = Base64.encode64(params[:chef_extension_private_param].to_json)
      resource_extension_parameter_value.add_child(value)

      type = Nokogiri::XML::Node.new('Type', roleXML)
      type.content = 'Private'
      resource_extension_parameter_value.add_child(type)

      resource_extension_parameter_values.add_child(resource_extension_parameter_value)
    end

    resource_extension_reference.add_child(resource_extension_parameter_values)

    state = Nokogiri::XML::Node.new('State', roleXML)
    state.content = 'enable'
    resource_extension_reference.add_child(state)

    if add_resource_extension_references
      resource_extension_references.add_child(resource_extension_reference)
    else
      resource_extension_references.last.add_child(resource_extension_reference)
    end

    roleXML.add_child(resource_extension_references) if add_resource_extension_references

    add_provision_guest_agent = roleXML.at_css('ProvisionGuestAgent').nil?

    if add_provision_guest_agent
      provision_guest_agent = Nokogiri::XML::Node.new('ProvisionGuestAgent', roleXML)
      provision_guest_agent.content = true
    else
      provision_guest_agent = roleXML.css('ProvisionGuestAgent')
      provision_guest_agent.first.content = true
    end

    roleXML.add_child(provision_guest_agent) if add_provision_guest_agent
  else ## raise error as Chef Extension is already installed on the server
    raise "Chef Extension is already installed on the server #{params[:azure_vm_name]}."
  end

  roleXML
end