Class: ForemanAzureRm::AzureRmCompute

Inherits:
Object
  • Object
show all
Defined in:
app/models/foreman_azure_rm/azure_rm_compute.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(azure_vm: ComputeModels::VirtualMachine.new, sdk: nil, resource_group: azure_vm.resource_group, nics: [], volumes: [], script_command: nil, script_uris: nil, nvidia_gpu_extension: false, tags: []) ⇒ AzureRmCompute

Returns a new instance of AzureRmCompute.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/models/foreman_azure_rm/azure_rm_compute.rb', line 14

def initialize(azure_vm: ComputeModels::VirtualMachine.new,
               sdk: nil,
               resource_group: azure_vm.resource_group,
               nics: [],
               volumes: [],
               script_command: nil,
               script_uris: nil,
               nvidia_gpu_extension: false,
               tags: [])

  @azure_vm = azure_vm
  @sdk = sdk
  @resource_group ||= resource_group
  @nics ||= nics
  @volumes ||= volumes
  @script_command ||= script_command
  @script_uris ||= script_uris
  @nvidia_gpu_extension ||= nvidia_gpu_extension
  @tags ||= tags
  @azure_vm.hardware_profile ||= ComputeModels::HardwareProfile.new
  @azure_vm.os_profile ||= ComputeModels::OSProfile.new
  @azure_vm.os_profile.linux_configuration ||= ComputeModels::LinuxConfiguration.new
  @azure_vm.os_profile.linux_configuration.ssh ||= ComputeModels::SshConfiguration.new
  @azure_vm.os_profile.linux_configuration.ssh.public_keys ||= [ComputeModels::SshPublicKey.new]
  @azure_vm.storage_profile ||= ComputeModels::StorageProfile.new
  @azure_vm.storage_profile.os_disk ||= ComputeModels::OSDisk.new
  @azure_vm.storage_profile.os_disk.managed_disk ||= ComputeModels::ManagedDiskParameters.new
end

Instance Attribute Details

#azure_vmObject

Returns the value of attribute azure_vm.



4
5
6
# File 'app/models/foreman_azure_rm/azure_rm_compute.rb', line 4

def azure_vm
  @azure_vm
end

#nicsObject

Returns the value of attribute nics.



6
7
8
# File 'app/models/foreman_azure_rm/azure_rm_compute.rb', line 6

def nics
  @nics
end

#nvidia_gpu_extensionObject

Returns the value of attribute nvidia_gpu_extension.



8
9
10
# File 'app/models/foreman_azure_rm/azure_rm_compute.rb', line 8

def nvidia_gpu_extension
  @nvidia_gpu_extension
end

#resource_groupObject

Returns the value of attribute resource_group.



5
6
7
# File 'app/models/foreman_azure_rm/azure_rm_compute.rb', line 5

def resource_group
  @resource_group
end

#script_commandObject

Returns the value of attribute script_command.



7
8
9
# File 'app/models/foreman_azure_rm/azure_rm_compute.rb', line 7

def script_command
  @script_command
end

#script_urisObject

Returns the value of attribute script_uris.



7
8
9
# File 'app/models/foreman_azure_rm/azure_rm_compute.rb', line 7

def script_uris
  @script_uris
end

#sdkObject

Returns the value of attribute sdk.



3
4
5
# File 'app/models/foreman_azure_rm/azure_rm_compute.rb', line 3

def sdk
  @sdk
end

#tagsObject

Returns the value of attribute tags.



10
11
12
# File 'app/models/foreman_azure_rm/azure_rm_compute.rb', line 10

def tags
  @tags
end

#volumesObject

Returns the value of attribute volumes.



9
10
11
# File 'app/models/foreman_azure_rm/azure_rm_compute.rb', line 9

def volumes
  @volumes
end

Instance Method Details

#data_disksObject



131
132
133
# File 'app/models/foreman_azure_rm/azure_rm_compute.rb', line 131

def data_disks
  @data_disks ||= @azure_vm.storage_profile.data_disks || []
end

#idObject



43
44
45
# File 'app/models/foreman_azure_rm/azure_rm_compute.rb', line 43

def id
  @azure_vm.id
end

#identityObject



145
146
147
# File 'app/models/foreman_azure_rm/azure_rm_compute.rb', line 145

def identity
  @azure_vm.name
end

#identity=(setuuid) ⇒ Object



149
150
151
# File 'app/models/foreman_azure_rm/azure_rm_compute.rb', line 149

def identity=(setuuid)
  @azure_vm.name = setuuid
end

#image_uuidObject Also known as: image_id



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'app/models/foreman_azure_rm/azure_rm_compute.rb', line 196

def image_uuid
  image = @azure_vm.storage_profile.image_reference
  return nil unless image
  if image.id.nil?
    return "marketplace://#{image.publisher}:#{image.offer}:#{image.sku}:#{image.version}"
  else
    image_rg = image.id.split('/')[4]
    image_name = image.id.split('/')[-1]
    if sdk.list_custom_images.find { |custom_img| custom_img.name == image_name }
      return "custom://#{image_name}"
    elsif sdk.fetch_gallery_image_id(image_rg, image_name)
      return "gallery://#{image_name}"
    end
  end
end

#interfacesObject



120
121
122
# File 'app/models/foreman_azure_rm/azure_rm_compute.rb', line 120

def interfaces
  nics
end

#interfaces_attributes=(attrs) ⇒ Object



124
125
# File 'app/models/foreman_azure_rm/azure_rm_compute.rb', line 124

def interfaces_attributes=(attrs)
end

#ip_addressesObject



127
128
129
# File 'app/models/foreman_azure_rm/azure_rm_compute.rb', line 127

def ip_addresses
  []
end

#network_interface_card_idsObject



85
86
87
88
89
# File 'app/models/foreman_azure_rm/azure_rm_compute.rb', line 85

def network_interface_card_ids
  return nil unless @azure_vm.network_profile
  nics = @azure_vm.network_profile.network_interfaces
  nics.map(&:id)
end

#os_disk_cachingObject



192
193
194
# File 'app/models/foreman_azure_rm/azure_rm_compute.rb', line 192

def os_disk_caching
  @azure_vm.storage_profile.os_disk.caching
end

#os_disk_size_gbObject



188
189
190
# File 'app/models/foreman_azure_rm/azure_rm_compute.rb', line 188

def os_disk_size_gb
  @azure_vm.storage_profile.os_disk.disk_size_gb
end

#passwordObject



172
173
174
# File 'app/models/foreman_azure_rm/azure_rm_compute.rb', line 172

def password
  @azure_vm.os_profile.admin_password
end

#persisted?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'app/models/foreman_azure_rm/azure_rm_compute.rb', line 47

def persisted?
  !!identity && !!id
end

#platformObject



164
165
166
# File 'app/models/foreman_azure_rm/azure_rm_compute.rb', line 164

def platform
  @azure_vm.storage_profile.os_disk.os_type
end

#premium_os_diskObject



184
185
186
# File 'app/models/foreman_azure_rm/azure_rm_compute.rb', line 184

def premium_os_disk
  @azure_vm.storage_profile.os_disk.managed_disk.
end

#private_ip_addressObject



109
110
111
112
113
114
115
116
117
118
# File 'app/models/foreman_azure_rm/azure_rm_compute.rb', line 109

def private_ip_address
  interfaces.each do |nic|
    nic.ip_configurations.each do |configuration|
      next unless configuration.primary
      if configuration.private_ipaddress.present?
        return private_ip_address = configuration.private_ipaddress
      end
    end
  end
end

#provisioning_ip_addressObject



91
92
93
# File 'app/models/foreman_azure_rm/azure_rm_compute.rb', line 91

def provisioning_ip_address
  public_ip_address || private_ip_address
end

#public_ip_addressObject



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'app/models/foreman_azure_rm/azure_rm_compute.rb', line 95

def public_ip_address
  interfaces.each do |nic|
    nic.ip_configurations.each do |configuration|
      next unless configuration.primary
      return nil if configuration.public_ipaddress.blank?
      ip_id     = configuration.public_ipaddress.id
      ip_rg     = ip_id.split('/')[4]
      ip_name   = ip_id.split('/')[-1]
      public_ip = sdk.public_ip(ip_rg, ip_name)
      return public_ip.ip_address
    end
  end
end

#ready?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'app/models/foreman_azure_rm/azure_rm_compute.rb', line 56

def ready?
  vm_status == 'running'
end

#reloadObject



60
61
# File 'app/models/foreman_azure_rm/azure_rm_compute.rb', line 60

def reload
end

#ssh_key_dataObject



176
177
178
179
180
181
182
# File 'app/models/foreman_azure_rm/azure_rm_compute.rb', line 176

def ssh_key_data
  # since you can only give one additional
  # sshkey through foreman's UI
  sshkey = @azure_vm.os_profile.linux_configuration.ssh.public_keys[1]
  return unless sshkey.present?
  sshkey.key_data
end

#startObject



67
68
69
70
# File 'app/models/foreman_azure_rm/azure_rm_compute.rb', line 67

def start
  sdk.start_vm(@azure_vm.resource_group, name)
  true
end

#stateObject



63
64
65
# File 'app/models/foreman_azure_rm/azure_rm_compute.rb', line 63

def state
  vm_status
end

#stopObject



72
73
74
75
# File 'app/models/foreman_azure_rm/azure_rm_compute.rb', line 72

def stop
  sdk.stop_vm(@azure_vm.resource_group, name)
  true
end

#to_sObject



77
78
79
# File 'app/models/foreman_azure_rm/azure_rm_compute.rb', line 77

def to_s
  name
end

#usernameObject



168
169
170
# File 'app/models/foreman_azure_rm/azure_rm_compute.rb', line 168

def username
  @azure_vm.os_profile.admin_username
end

#vm_descriptionObject



153
154
155
# File 'app/models/foreman_azure_rm/azure_rm_compute.rb', line 153

def vm_description
    _("%{vm_size} VM Size") % {:vm_size => vm_size}
end

#vm_extensionObject



214
215
216
217
218
219
220
221
222
223
224
# File 'app/models/foreman_azure_rm/azure_rm_compute.rb', line 214

def vm_extension
  return nil unless @azure_vm.resources
  @vm_extension ||= begin
    @azure_vm.resources.each do |ext|
      ext_name = ext.id.split('/')[-1]
      next unless ext_name == 'ForemanCustomScript'
      return sdk.get_vm_extension(@azure_vm.resource_group, name, ext_name)
    end
    nil
  end
end

#vm_nvidia_gpu_extensionObject



226
227
228
229
230
231
232
233
234
235
236
# File 'app/models/foreman_azure_rm/azure_rm_compute.rb', line 226

def vm_nvidia_gpu_extension
  return nil unless @azure_vm.resources
  @vm_nvidia_gpu_extension ||= begin
    @azure_vm.resources.each do |ext|
      ext_name = ext.id.split('/')[-1]
      next unless ['NvidiaGpuDriverLinux', 'NvidiaGpuDriverWindows'].include? ext_name
      return sdk.get_vm_extension(@azure_vm.resource_group, name, ext_name)
    end
    nil
  end
end

#vm_sizeObject

Following properties are for AzureRm These are not part of Foreman’s interface



160
161
162
# File 'app/models/foreman_azure_rm/azure_rm_compute.rb', line 160

def vm_size
  @azure_vm.hardware_profile.vm_size
end

#vm_statusObject



81
82
83
# File 'app/models/foreman_azure_rm/azure_rm_compute.rb', line 81

def vm_status
  sdk.check_vm_status(@azure_vm.resource_group, name)
end

#volumes_attributes=(attrs) ⇒ Object



142
143
# File 'app/models/foreman_azure_rm/azure_rm_compute.rb', line 142

def volumes_attributes=(attrs)
end

#wait_for(_timeout = 0, _interval = 0, &block) ⇒ Object



51
52
53
54
# File 'app/models/foreman_azure_rm/azure_rm_compute.rb', line 51

def wait_for(_timeout = 0, _interval = 0, &block)
  instance_eval(&block)
  return true
end