Module: ForemanAzureRm::VMExtensions::ManagedVM

Extended by:
ActiveSupport::Concern
Included in:
AzureRm
Defined in:
app/models/concerns/foreman_azure_rm/vm_extensions/managed_vm.rb

Instance Method Summary collapse

Instance Method Details

#create_managed_virtual_machine(vm_hash) ⇒ Object



237
238
239
240
241
242
243
244
# File 'app/models/concerns/foreman_azure_rm/vm_extensions/managed_vm.rb', line 237

def create_managed_virtual_machine(vm_hash)
  vm_params = initialize_vm(vm_hash)
  vm_params.plan = marketplace_image_plan(vm_hash[:image_id])
  vm_params.network_profile = define_network_profile(vm_hash[:network_interface_card_ids])
  vm_params.storage_profile.image_reference = define_image(vm_hash[:resource_group], vm_hash[:image_id])
  vm_params.storage_profile.data_disks = define_data_disks(vm_hash[:name], vm_hash[:data_disks])
  sdk.create_or_update_vm(vm_hash[:resource_group], vm_hash[:name], vm_params)
end

#create_nics(region, args = {}) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'app/models/concerns/foreman_azure_rm/vm_extensions/managed_vm.rb', line 123

def create_nics(region, args = {})
  nics               = []
  args[:interfaces_attributes].each do |nic, attrs|
    private_ip = ActiveRecord::Type::Boolean.new.deserialize(attrs[:private_ip])
    priv_ip_alloc       = if private_ip
                            NetworkModels::IPAllocationMethod::Static
                          else
                            NetworkModels::IPAllocationMethod::Dynamic
                          end
    pub_ip_alloc        = case attrs[:public_ip]
                          when 'Static'
                            NetworkModels::IPAllocationMethod::Static
                          when 'Dynamic'
                            NetworkModels::IPAllocationMethod::Dynamic
                          when 'None'
                            nil
                          else
                              raise RuntimeError, "Public IP value must be either 'Dynamic', 'Static' or 'None'"
                          end
    if pub_ip_alloc.present?
      public_ip_params = NetworkModels::PublicIPAddress.new.tap do |ip|
        ip.location = region
        ip.public_ipallocation_method = pub_ip_alloc
      end

      pip = sdk.create_or_update_pip(args[:resource_group],
                                     "#{args[:vm_name]}-pip#{nic}",
                                     public_ip_params)
    end
    new_nic = sdk.create_or_update_nic(
      args[:resource_group],
      "#{args[:vm_name]}-nic#{nic}",
      NetworkModels::NetworkInterface.new.tap do |interface|
        interface.location = region
        interface.ip_configurations = [
          NetworkModels::NetworkInterfaceIPConfiguration.new.tap do |nic_conf|
            nic_conf.name = "#{args[:vm_name]}-nic#{nic}"
            nic_conf.private_ipallocation_method = priv_ip_alloc
            nic_conf.private_ipaddress = attrs[:ip] if priv_ip_alloc == "Static"
            nic_conf.subnet = subnets.select{ |subnet| subnet.id == attrs[:network] }.first
            nic_conf.public_ipaddress = pip
          end
        ]
      end
    )
    nics << new_nic
  end
  nics
end

#create_vm_extension(region, args = {}) ⇒ Object



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
# File 'app/models/concerns/foreman_azure_rm/vm_extensions/managed_vm.rb', line 246

def create_vm_extension(region, args = {})
  if args[:script_command].present? || args[:script_uris].present?
    args[:script_uris] ||=  args[:script_uris].to_s
    extension = ComputeModels::VirtualMachineExtension.new

    case args[:platform]
    # https://docs.microsoft.com/en-us/azure/virtual-machines/extensions/custom-script-linux
    when 'Linux'
      extension.publisher = 'Microsoft.Azure.Extensions'
      extension.virtual_machine_extension_type = 'CustomScript'
      extension.type_handler_version = '2.0'
    # https://docs.microsoft.com/en-us/azure/virtual-machines/extensions/custom-script-windows
    when 'Windows'
      extension.publisher = 'Microsoft.Compute'
      extension.virtual_machine_extension_type = 'CustomScriptExtension'
      extension.type_handler_version = '1.10'
    else
      raise RuntimeError, "Unsupported platform #{args[:platform]}"
    end

    extension.auto_upgrade_minor_version = true
    extension.location = region
    extension.settings = {
          'commandToExecute' => args[:script_command],
          'fileUris'         => args[:script_uris].split(',')
    }

    extension_for_log = "#{extension.publisher}/#{extension.virtual_machine_extension_type}/#{extension.type_handler_version}"
    Foreman::Logging.logger('app').info "Azure RM machine #{args[:name]}: creating #{extension_for_log} extension"
    sdk.create_or_update_vm_extensions(args[:resource_group],
                                       args[:vm_name],
                                       'ForemanCustomScript',
                                       extension)
  end
end

#create_vm_nvidia_gpu_extension(region, args = {}) ⇒ Object



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
# File 'app/models/concerns/foreman_azure_rm/vm_extensions/managed_vm.rb', line 282

def create_vm_nvidia_gpu_extension(region, args = {})
  extension = ComputeModels::VirtualMachineExtension.new
  extension.publisher = 'Microsoft.HpcCompute'
  extension.type_handler_version = '1.3'
  extension.auto_upgrade_minor_version = true
  extension.location = region

  case args[:platform]
  # https://docs.microsoft.com/fr-fr/azure/virtual-machines/extensions/hpccompute-gpu-linux
  when 'Linux'
    extension_name = 'NvidiaGpuDriverLinux'
  # https://docs.microsoft.com/fr-fr/azure/virtual-machines/extensions/hpccompute-gpu-windows
  when 'Windows'
    extension_name = 'NvidiaGpuDriverWindows'
  else
    raise RuntimeError, "Unsupported platform #{args[:platform]}"
  end
  extension.virtual_machine_extension_type = extension_name

  extension_for_log = "#{extension.publisher}/#{extension.virtual_machine_extension_type}/#{extension.type_handler_version}"
  Foreman::Logging.logger('app').info "Azure RM machine #{args[:name]}: creating #{extension_for_log} extension"
  sdk.create_or_update_vm_extensions(args[:resource_group],
                                     args[:vm_name],
                                     extension_name,
                                     extension)
end

#define_data_disks(vm_name, data_disks) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/models/concerns/foreman_azure_rm/vm_extensions/managed_vm.rb', line 43

def define_data_disks(vm_name, data_disks)
  unless data_disks.nil?
    disks = []
    disk_count = 0
    data_disks.each do |disk_num, attrs|
      managed_data_disk = ComputeModels::ManagedDiskParameters.new
      disk = ComputeModels::DataDisk.new
      disk.name = "#{vm_name}-data-disk#{disk_count}"
      disk.caching  = disk_caching(attrs[:data_disk_caching])
      disk.disk_size_gb = attrs[:disk_size_gb]
      disk.create_option = ComputeModels::DiskCreateOption::Empty
      disk.lun = disk_count + 1
      disk.managed_disk = managed_data_disk
      disk_count += 1
      disks << disk
    end
    disks
  end
end

#define_image(rg_name, image) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'app/models/concerns/foreman_azure_rm/vm_extensions/managed_vm.rb', line 87

def define_image(rg_name, image)
  image_type, image_id = image.split('://')
  case image_type
  when 'marketplace'
    urn = image_id.split(':')
    publisher = urn[0]
    offer     = urn[1]
    sku       = urn[2]
    version   = urn[3]
    image_reference = marketplace_image_reference(publisher, offer, sku, version)
  when 'custom'
    custom_image = sdk.get_custom_image(rg_name, image_id)
    image_reference = ComputeModels::ImageReference.new
    image_reference.id = custom_image.id
  when 'gallery'
    image_reference = ComputeModels::ImageReference.new
    image_reference.id = sdk.fetch_gallery_image_id(rg_name, image_id)
  else
    image_reference = nil
  end
  image_reference
end

#define_managed_storage_profile(vm_name, os_disk_caching, platform, premium_os_disk, os_disk_size_gb) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/models/concerns/foreman_azure_rm/vm_extensions/managed_vm.rb', line 7

def define_managed_storage_profile(vm_name, os_disk_caching, platform, premium_os_disk, os_disk_size_gb)
  storage_profile = ComputeModels::StorageProfile.new
  os_disk = ComputeModels::OSDisk.new
  managed_disk_params = ComputeModels::ManagedDiskParameters.new

  # Create OS disk
  os_disk.name = "#{vm_name}-osdisk"
  os_disk.os_type = platform
  os_disk.disk_size_gb = os_disk_size_gb
  os_disk.create_option = ComputeModels::DiskCreateOptionTypes::FromImage
  os_disk.caching = disk_caching(os_disk_caching)
  managed_disk_params. = if premium_os_disk == 'true'
                                               ComputeModels::StorageAccountTypes::PremiumLRS
                                             else
                                               ComputeModels::StorageAccountTypes::StandardLRS
                                             end
  os_disk.managed_disk = managed_disk_params
  storage_profile.os_disk = os_disk
  storage_profile
end

#define_network_profile(network_interface_card_ids) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
# File 'app/models/concerns/foreman_azure_rm/vm_extensions/managed_vm.rb', line 110

def define_network_profile(network_interface_card_ids)
  network_interface_cards = []
  network_interface_card_ids.each_with_index do |id, index|
    nic = ComputeModels::NetworkInterfaceReference.new
    nic.id = id
    nic.primary = true
    network_interface_cards << nic
  end
  network_profile = ComputeModels::NetworkProfile.new
  network_profile.network_interfaces = network_interface_cards
  network_profile
end

#disk_caching(disk_type_caching) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/models/concerns/foreman_azure_rm/vm_extensions/managed_vm.rb', line 28

def disk_caching(disk_type_caching)
  case disk_type_caching
  when 'None'
    ComputeModels::CachingTypes::None
  when 'ReadOnly'
    ComputeModels::CachingTypes::ReadOnly
  when 'ReadWrite'
    ComputeModels::CachingTypes::ReadWrite
  when nil, ""
    nil
  else
    raise RuntimeError, "Disk caching value must be either 'None', 'ReadOnly' or 'ReadWrite'."
  end
end

#initialize_vm(vm_hash) ⇒ Object



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
# File 'app/models/concerns/foreman_azure_rm/vm_extensions/managed_vm.rb', line 173

def initialize_vm(vm_hash)
  custom_data = vm_hash[:custom_data]
  msg = "Creating Virtual Machine #{vm_hash[:name]} in Resource Group #{vm_hash[:resource_group]}."
  logger.debug msg
  vm_create_params = ComputeModels::VirtualMachine.new.tap do |vm|
    vm.location = vm_hash[:location]
    vm.tags = {}
    unless vm_hash[:tags].nil?
      arr = vm_hash[:tags].split(',')
      arr.each do |item|
        kv = item.split('=')
        vm.tags[kv[0].strip] = kv[1].strip
      end            
    end
    unless vm_hash[:availability_set_id].nil?
      sub_resource = MsRestAzure::SubResource.new
      sub_resource.id = vm_hash[:availability_set_id]
      vm.availability_set = sub_resource
    end

    vm.os_profile = ComputeModels::OSProfile.new.tap do |os_profile|
      os_profile.computer_name  = vm_hash[:name]
      os_profile.admin_username = vm_hash[:username]
      os_profile.admin_password = vm_hash[:password]

      # Adding the ssh-key support for authentication
      if vm_hash[:platform] == 'Linux'
        os_profile.linux_configuration = ComputeModels::LinuxConfiguration.new.tap do |linux|
          linux.disable_password_authentication = vm_hash[:disable_password_authentication]
          linux.ssh = ComputeModels::SshConfiguration.new.tap do |ssh_config|
            ssh_config.public_keys = [
              ComputeModels::SshPublicKey.new.tap do |foreman_key|
                foreman_key.key_data = key_pair.public
                foreman_key.path = "/home/#{vm_hash[:username]}/.ssh/authorized_keys"
              end
            ]
            if vm_hash[:ssh_key_data].present?
              key_data = vm_hash[:ssh_key_data]
              pub_key = ComputeModels::SshPublicKey.new
              pub_key.key_data = key_data
              pub_key.path = "/home/#{vm_hash[:username]}/.ssh/authorized_keys"
              ssh_config.public_keys << pub_key
            end
          end
        end
      end
      # added custom_data here so that azure's vm gets this
      os_profile.custom_data    = Base64.strict_encode64(custom_data) unless vm_hash[:custom_data].nil?
    end
    vm.storage_profile = define_managed_storage_profile(
                                                          vm_hash[:name],
                                                          vm_hash[:os_disk_caching],
                                                          vm_hash[:platform],
                                                          vm_hash[:premium_os_disk],
                                                          vm_hash[:os_disk_size_gb]
                                                        )
    vm.hardware_profile = ComputeModels::HardwareProfile.new.tap do |hw_profile|
      hw_profile.vm_size = vm_hash[:vm_size]
    end
  end

  vm_create_params
end

#marketplace_image_plan(image) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/models/concerns/foreman_azure_rm/vm_extensions/managed_vm.rb', line 63

def marketplace_image_plan(image)
  image_type, image_id = image.split('://')
  return nil unless image_type == 'marketplace' && image_id.include?('byos')
  urn = image_id.split(':')
  publisher = urn[0]
  offer     = urn[1]
  sku       = urn[2]
  version   = urn[3]
  image_plan = ComputeModels::PurchasePlan.new
  image_plan.publisher = publisher.downcase
  image_plan.name = sku.downcase
  image_plan.product = offer.downcase
  image_plan
end

#marketplace_image_reference(publisher, offer, sku, version) ⇒ Object



78
79
80
81
82
83
84
85
# File 'app/models/concerns/foreman_azure_rm/vm_extensions/managed_vm.rb', line 78

def marketplace_image_reference(publisher, offer, sku, version)
  image_reference = ComputeModels::ImageReference.new
  image_reference.publisher = publisher
  image_reference.offer = offer
  image_reference.sku = sku
  image_reference.version = version
  image_reference
end