Class: ForemanAzureRM::AzureRM
- Inherits:
-
ComputeResource
- Object
- ComputeResource
- ForemanAzureRM::AzureRM
- Defined in:
- app/models/foreman_azure_rm/azure_rm.rb
Defined Under Namespace
Classes: VMContainer
Class Method Summary collapse
Instance Method Summary collapse
- #capabilities ⇒ Object
- #create_nics(args = {}) ⇒ Object
-
#create_vm(args = {}) ⇒ Object
Preferred behavior is to utilize Fog but Fog Azure RM does not currently support creating managed VMs.
- #destroy_vm(uuid) ⇒ Object
- #find_vm_by_uuid(uuid) ⇒ Object
- #host_interfaces_attrs(host) ⇒ Object
- #locations ⇒ Object
- #networks ⇒ Object
- #new_interface(attr = {}) ⇒ Object
- #new_volume(attr = {}) ⇒ Object
- #provided_attributes ⇒ Object
- #provider_friendly_name ⇒ Object
- #resource_groups ⇒ Object
- #storage_accts(location = nil) ⇒ Object
- #subnets(location) ⇒ Object
- #test_connection(options = {}) ⇒ Object
- #to_label ⇒ Object
- #virtual_networks(location = nil) ⇒ Object
- #vm_sizes(location) ⇒ Object
- #vms ⇒ Object
Class Method Details
.model_name ⇒ Object
26 27 28 |
# File 'app/models/foreman_azure_rm/azure_rm.rb', line 26 def self.model_name ComputeResource.model_name end |
Instance Method Details
#capabilities ⇒ Object
34 35 36 |
# File 'app/models/foreman_azure_rm/azure_rm.rb', line 34 def capabilities [:image] end |
#create_nics(args = {}) ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 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 |
# File 'app/models/foreman_azure_rm/azure_rm.rb', line 150 def create_nics(args = {}) nics = [] formatted_location = args[:location].gsub(/\s+/, '').downcase args[:interfaces_attributes].each do |nic, attrs| attrs[:pubip_alloc] = attrs[:bridge] attrs[:privip_alloc] = (attrs[:name] == 'false') ? false : true pip_alloc = case attrs[:pubip_alloc] when 'Static' Fog::ARM::Network::Models::IPAllocationMethod::Static when 'Dynamic' Fog::ARM::Network::Models::IPAllocationMethod::Dynamic when 'None' nil end priv_ip_alloc = if attrs[:priv_ip_alloc] Fog::ARM::Network::Models::IPAllocationMethod::Static else Fog::ARM::Network::Models::IPAllocationMethod::Dynamic end if pip_alloc.present? pip = azure_network_service.public_ips.create( name: "#{args[:vm_name]}-pip#{nic}", resource_group: args[:resource_group], location: formatted_location, public_ip_allocation_method: pip_alloc ) end new_nic = azure_network_service.network_interfaces.create( name: "#{args[:vm_name]}-nic#{nic}", resource_group: args[:resource_group], location: formatted_location, subnet_id: attrs[:network], public_ip_address_id: pip.present? ? pip.id : nil, ip_configuration_name: 'ForemanIPConfiguration', private_ip_allocation_method: priv_ip_alloc ) nics << new_nic end nics end |
#create_vm(args = {}) ⇒ Object
Preferred behavior is to utilize Fog but Fog Azure RM does not currently support creating managed VMs
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 |
# File 'app/models/foreman_azure_rm/azure_rm.rb', line 193 def create_vm(args = {}) args[:vm_name] = args[:name].split('.')[0] nics = create_nics(args) if args[:ssh_key_data].present? disable_password_auth = true ssh_key_path = "/home/#{args[:username]}/.ssh/authorized_keys" else disable_password_auth = false ssh_key_path = nil end vm = client.create_managed_virtual_machine( name: args[:vm_name], location: args[:location], resource_group: args[:resource_group], vm_size: args[:vm_size], username: args[:username], password: args[:password], ssh_key_data: args[:ssh_key_data], ssh_key_path: ssh_key_path, disable_password_authentication: disable_password_auth, network_interface_card_ids: nics.map(&:id), platform: args[:platform], vhd_path: args[:image_id], os_disk_caching: args[:os_disk_caching], data_disks: args[:volumes_attributes], os_disk_size: args[:os_disk_size], premium_os_disk: args[:premium_os_disk], ) vm_hash = Fog::Compute::AzureRM::Server.parse(vm) vm_hash[:password] = args[:password] vm_hash[:platform] = args[:platform] vm_hash[:puppet_master] = args[:puppet_master] vm_hash[:script_command] = args[:script_command] vm_hash[:script_uris] = args[:script_uris] client.create_vm_extension(vm_hash) client.servers.new vm_hash # fog-azure-rm raises all ARM errors as RuntimeError rescue Fog::Errors::Error, RuntimeError => e Foreman::Logging.exception('Unhandled Azure RM error', e) destroy_vm vm.id if vm raise e end |
#destroy_vm(uuid) ⇒ Object
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 |
# File 'app/models/foreman_azure_rm/azure_rm.rb', line 236 def destroy_vm(uuid) vm = find_vm_by_uuid(uuid) raw_model = client.get_virtual_machine(vm.resource_group, vm.name) os_disk_name = raw_model.storage_profile.os_disk.name data_disks = raw_model.storage_profile.data_disks nic_ids = vm.network_interface_card_ids # In ARM things must be deleted in order vm.destroy nic_ids.each do |id| nic = azure_network_service.network_interfaces.get(id.split('/')[4], id.split('/')[-1]) ip_id = nic.public_ip_address_id nic.destroy if ip_id.present? azure_network_service.public_ips.get(ip_id.split('/')[4], ip_id.split('/')[-1]).destroy end end client.managed_disks.get(vm.resource_group, os_disk_name).destroy data_disks.each do |disk| client.managed_disks.get(vm.resource_group, disk.name).destroy end rescue ActiveRecord::RecordNotFound # If the VM does not exist, we don't really care. true end |
#find_vm_by_uuid(uuid) ⇒ Object
141 142 143 144 145 146 147 148 |
# File 'app/models/foreman_azure_rm/azure_rm.rb', line 141 def find_vm_by_uuid(uuid) # TODO: Find a better way to handle this than loading and sorting through # all VMs, which also requires that names be globally unique, instead of # unique within a resource group vm = vms.all.find { |vm| vm.name == uuid } raise ActiveRecord::RecordNotFound unless vm.present? vm end |
#host_interfaces_attrs(host) ⇒ Object
79 80 81 82 83 |
# File 'app/models/foreman_azure_rm/azure_rm.rb', line 79 def host_interfaces_attrs(host) host.interfaces.select(&:physical?).each.with_index.reduce({}) do |hash, (nic, index)| hash.merge(index.to_s => nic.compute_attributes.merge(ip: nic.ip, ip6: nic.ip6)) end end |
#locations ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'app/models/foreman_azure_rm/azure_rm.rb', line 38 def locations [ 'Central US', 'South Central US', 'North Central US', 'West Central US', 'East US', 'East US 2', 'West US', 'West US 2' ] end |
#networks ⇒ Object
111 112 113 114 115 116 117 |
# File 'app/models/foreman_azure_rm/azure_rm.rb', line 111 def networks subnets = [] virtual_networks.each do |vnet| subnets << subnets(vnet.id) end subnets end |
#new_interface(attr = {}) ⇒ Object
119 120 121 |
# File 'app/models/foreman_azure_rm/azure_rm.rb', line 119 def new_interface(attr = {}) azure_network_service.network_interfaces.new(attr) end |
#new_volume(attr = {}) ⇒ Object
123 124 125 |
# File 'app/models/foreman_azure_rm/azure_rm.rb', line 123 def new_volume(attr = {}) client.managed_disks.new(attr) end |
#provided_attributes ⇒ Object
75 76 77 |
# File 'app/models/foreman_azure_rm/azure_rm.rb', line 75 def provided_attributes super.merge({ :ip => :provisioning_ip_address }) end |
#provider_friendly_name ⇒ Object
30 31 32 |
# File 'app/models/foreman_azure_rm/azure_rm.rb', line 30 def provider_friendly_name 'Azure Resource Manager' end |
#resource_groups ⇒ Object
51 52 53 54 55 56 57 58 |
# File 'app/models/foreman_azure_rm/azure_rm.rb', line 51 def resource_groups rgs = rg_client.list_resource_groups rg_names = [] rgs.each do |rg| rg_names << rg.name end rg_names end |
#storage_accts(location = nil) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'app/models/foreman_azure_rm/azure_rm.rb', line 60 def storage_accts(location = nil) stripped_location = location.gsub(/\s+/, '').downcase acct_names = [] if location.nil? storage_client.list_storage_accounts.each do |acct| acct_names << acct.name end else (storage_client.list_storage_accounts.select { |acct| acct.location == stripped_location }).each do |acct| acct_names << acct.name end end acct_names end |
#subnets(location) ⇒ Object
94 95 96 97 98 99 100 101 102 |
# File 'app/models/foreman_azure_rm/azure_rm.rb', line 94 def subnets(location) vnets = virtual_networks(location) subnets = [] vnets.each do |vnet| subnets.concat(azure_network_service.subnets(resource_group: vnet.resource_group, virtual_network_name: vnet.name).all) end subnets end |
#test_connection(options = {}) ⇒ Object
104 105 106 107 108 109 |
# File 'app/models/foreman_azure_rm/azure_rm.rb', line 104 def test_connection( = {}) rg_client.resource_groups.each do |rg| puts "#{rg.name}" end super() end |
#to_label ⇒ Object
22 23 24 |
# File 'app/models/foreman_azure_rm/azure_rm.rb', line 22 def to_label "#{name} (#{provider_friendly_name})" end |
#virtual_networks(location = nil) ⇒ Object
85 86 87 88 89 90 91 92 |
# File 'app/models/foreman_azure_rm/azure_rm.rb', line 85 def virtual_networks(location = nil) if location.nil? azure_network_service.virtual_networks else stripped_location = location.gsub(/\s+/, '').downcase azure_network_service.virtual_networks.select { |vnet| vnet.location == stripped_location } end end |
#vm_sizes(location) ⇒ Object
137 138 139 |
# File 'app/models/foreman_azure_rm/azure_rm.rb', line 137 def vm_sizes(location) client.list_available_sizes(location) end |
#vms ⇒ Object
127 128 129 130 131 132 133 134 135 |
# File 'app/models/foreman_azure_rm/azure_rm.rb', line 127 def vms container = VMContainer.new rg_client.resource_groups.each do |rg| client.servers(resource_group: rg.name).each do |vm| container.virtualmachines << vm end end container end |