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) ⇒ AzureRmCompute

Returns a new instance of AzureRmCompute.



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

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

  @azure_vm = azure_vm
  @sdk = sdk
  @resource_group ||= resource_group
  @nics ||= nics
  @volumes ||= volumes
  @script_command ||= script_command
  @script_uris ||= script_uris
  @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

#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

#volumesObject

Returns the value of attribute volumes.



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

def volumes
  @volumes
end

Instance Method Details

#data_disksObject



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

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

#idObject



37
38
39
# File 'app/models/foreman_azure_rm/azure_rm_compute.rb', line 37

def id
  @azure_vm.id
end

#identityObject



139
140
141
# File 'app/models/foreman_azure_rm/azure_rm_compute.rb', line 139

def identity
  @azure_vm.name
end

#identity=(setuuid) ⇒ Object



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

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

#image_uuidObject Also known as: image_id



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'app/models/foreman_azure_rm/azure_rm_compute.rb', line 190

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



114
115
116
# File 'app/models/foreman_azure_rm/azure_rm_compute.rb', line 114

def interfaces
  nics
end

#interfaces_attributes=(attrs) ⇒ Object



118
119
# File 'app/models/foreman_azure_rm/azure_rm_compute.rb', line 118

def interfaces_attributes=(attrs)
end

#ip_addressesObject



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

def ip_addresses
  []
end

#network_interface_card_idsObject



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

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



186
187
188
# File 'app/models/foreman_azure_rm/azure_rm_compute.rb', line 186

def os_disk_caching
  @azure_vm.storage_profile.os_disk.caching
end

#os_disk_size_gbObject



182
183
184
# File 'app/models/foreman_azure_rm/azure_rm_compute.rb', line 182

def os_disk_size_gb
  @azure_vm.storage_profile.os_disk.disk_size_gb
end

#passwordObject



166
167
168
# File 'app/models/foreman_azure_rm/azure_rm_compute.rb', line 166

def password
  @azure_vm.os_profile.admin_password
end

#persisted?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'app/models/foreman_azure_rm/azure_rm_compute.rb', line 41

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

#platformObject



158
159
160
# File 'app/models/foreman_azure_rm/azure_rm_compute.rb', line 158

def platform
  @azure_vm.storage_profile.os_disk.os_type
end

#premium_os_diskObject



178
179
180
# File 'app/models/foreman_azure_rm/azure_rm_compute.rb', line 178

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

#private_ip_addressObject



103
104
105
106
107
108
109
110
111
112
# File 'app/models/foreman_azure_rm/azure_rm_compute.rb', line 103

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



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

def provisioning_ip_address
  public_ip_address || private_ip_address
end

#public_ip_addressObject



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/models/foreman_azure_rm/azure_rm_compute.rb', line 89

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)


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

def ready?
  vm_status == 'running'
end

#reloadObject



54
55
# File 'app/models/foreman_azure_rm/azure_rm_compute.rb', line 54

def reload
end

#ssh_key_dataObject



170
171
172
173
174
175
176
# File 'app/models/foreman_azure_rm/azure_rm_compute.rb', line 170

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



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

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

#stateObject



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

def state
  vm_status
end

#stopObject



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

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

#to_sObject



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

def to_s
  name
end

#usernameObject



162
163
164
# File 'app/models/foreman_azure_rm/azure_rm_compute.rb', line 162

def username
  @azure_vm.os_profile.admin_username
end

#vm_descriptionObject



147
148
149
# File 'app/models/foreman_azure_rm/azure_rm_compute.rb', line 147

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

#vm_extensionObject



208
209
210
211
212
213
214
# File 'app/models/foreman_azure_rm/azure_rm_compute.rb', line 208

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

#vm_sizeObject

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



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

def vm_size
  @azure_vm.hardware_profile.vm_size
end

#vm_statusObject



75
76
77
# File 'app/models/foreman_azure_rm/azure_rm_compute.rb', line 75

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

#volumes_attributes=(attrs) ⇒ Object



136
137
# File 'app/models/foreman_azure_rm/azure_rm_compute.rb', line 136

def volumes_attributes=(attrs)
end

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



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

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