Module: FogExtensions::AzureRM::Compute
- Extended by:
- ActiveSupport::Concern
- Defined in:
- app/models/concerns/fog_extensions/azurerm/compute.rb
Instance Method Summary collapse
- #create_managed_virtual_machine(vm_hash, async = false) ⇒ Object
- #create_vm_extension(vm) ⇒ Object
- #define_managed_storage_profile(vm_name, vhd_path, publisher, offer, sku, version, os_disk_caching, platform, os_disk_size, premium_os_disk, data_disks = nil) ⇒ Object
- #get_public_ip(ip_rg, ip_name) ⇒ Object
- #get_vm_nic(nic_rg, nic_name) ⇒ Object
- #initialize(options) ⇒ Object
- #list_all_vms ⇒ Object
- #list_available_sizes(location) ⇒ Object
Instance Method Details
#create_managed_virtual_machine(vm_hash, async = false) ⇒ Object
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 236 237 238 239 240 241 242 243 |
# File 'app/models/concerns/fog_extensions/azurerm/compute.rb', line 176 def create_managed_virtual_machine(vm_hash, async = false) msg = "Creating Virtual Machine #{vm_hash[:name]} in Resource Group #{vm_hash[:resource_group]}." Fog::Logger.debug msg virtual_machine = Azure::ARM::Compute::Models::VirtualMachine.new unless vm_hash[:availability_set_id].nil? sub_resource = MsRestAzure::SubResource.new sub_resource.id = vm_hash[:availability_set_id] virtual_machine.availability_set = sub_resource end # If image UUID begins with / it is a custom managed image # Otherwise it is a marketplace URN unless vm_hash[:vhd_path].start_with?('/') urn = vm_hash[:vhd_path].split(':') vm_hash[:publisher] = urn[0] vm_hash[:offer] = urn[1] vm_hash[:sku] = urn[2] vm_hash[:version] = urn[3] vm_hash[:vhd_path] = nil end string_data = vm_hash[:custom_data] string_data = WHITE_SPACE if string_data.nil? encoded_data = Base64.strict_encode64(string_data) virtual_machine.hardware_profile = define_hardware_profile(vm_hash[:vm_size]) virtual_machine.storage_profile = define_managed_storage_profile(vm_hash[:name], vm_hash[:vhd_path], vm_hash[:publisher], vm_hash[:offer], vm_hash[:sku], vm_hash[:version], vm_hash[:os_disk_caching], vm_hash[:platform], vm_hash[:os_disk_size], vm_hash[:premium_os_disk], vm_hash[:data_disks]) virtual_machine.os_profile = if vm_hash[:platform].casecmp(WINDOWS).zero? define_windows_os_profile(vm_hash[:name], vm_hash[:username], vm_hash[:password], vm_hash[:provision_vm_agent], vm_hash[:enable_automatic_updates], encoded_data) else define_linux_os_profile(vm_hash[:name], vm_hash[:username], vm_hash[:password], vm_hash[:disable_password_authentication], vm_hash[:ssh_key_path], vm_hash[:ssh_key_data], encoded_data) end virtual_machine.network_profile = define_network_profile(vm_hash[:network_interface_card_ids]) virtual_machine.location = vm_hash[:location] begin response = if async @compute_mgmt_client.virtual_machines.create_or_update_async(vm_hash[:resource_group], vm_hash[:name], virtual_machine) else @compute_mgmt_client.virtual_machines.create_or_update(vm_hash[:resource_group], vm_hash[:name], virtual_machine) end rescue MsRestAzure::AzureOperationError => e raise_azure_exception(e, msg) end Fog::Logger.debug "Virtual Machine #{vm_hash[:name]} Created Successfully." unless async response end |
#create_vm_extension(vm) ⇒ Object
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 172 173 174 |
# File 'app/models/concerns/fog_extensions/azurerm/compute.rb', line 140 def create_vm_extension(vm) if vm[:script_command].present? && vm[:script_uris].present? extension = Azure::ARM::Compute::Models::VirtualMachineExtension.new extension.publisher = 'Microsoft.Azure.Extensions' extension.virtual_machine_extension_type = 'CustomScript' extension.type_handler_version = '2.0' extension.auto_upgrade_minor_version = true extension.location = vm['location'].gsub(/\s+/, '').downcase extension.settings = { 'commandToExecute' => vm[:script_command], 'fileUris' => vm[:script_uris].split(',') } @compute_mgmt_client.virtual_machine_extensions.create_or_update(vm['resource_group'], vm['name'], 'ForemanCustomScript', extension) end if vm[:platform] == 'Windows' if vm[:puppet_master].present? extension = Azure::ARM::Compute::Models::VirtualMachineExtension.new extension.publisher = 'PuppetLabs' extension.virtual_machine_extension_type = 'PuppetEnterpriseAgent' extension.type_handler_version = '3.8' extension.auto_upgrade_minor_version = true extension.location = vm['location'].gsub(/\s+/, '').downcase extension.settings = { 'puppet_master_service' => vm[:puppet_master] } @compute_mgmt_client.virtual_machine_extensions.create_or_update(vm['resource_group'], vm['name'], 'InstallPuppet', extension) end end end |
#define_managed_storage_profile(vm_name, vhd_path, publisher, offer, sku, version, os_disk_caching, platform, os_disk_size, premium_os_disk, data_disks = nil) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'app/models/concerns/fog_extensions/azurerm/compute.rb', line 54 def define_managed_storage_profile(vm_name, vhd_path, publisher, offer, sku, version, os_disk_caching, platform, os_disk_size, premium_os_disk, data_disks = nil) storage_profile = Azure::ARM::Compute::Models::StorageProfile.new os_disk = Azure::ARM::Compute::Models::OSDisk.new managed_disk_params = Azure::ARM::Compute::Models::ManagedDiskParameters.new # Create OS disk os_disk.name = "#{vm_name}-osdisk" os_disk.os_type = if platform == 'Windows' Azure::ARM::Compute::Models::OperatingSystemTypes::Windows else Azure::ARM::Compute::Models::OperatingSystemTypes::Linux end os_disk.create_option = Azure::ARM::Compute::Models::DiskCreateOptionTypes::FromImage os_disk.caching = if os_disk_caching.present? case os_disk_caching when 'None' Azure::ARM::Compute::Models::CachingTypes::None when 'ReadOnly' Azure::ARM::Compute::Models::CachingTypes::ReadOnly when 'ReadWrite' Azure::ARM::Compute::Models::CachingTypes::ReadWrite else # ARM best practices stipulate RW caching on the OS disk Azure::ARM::Compute::Models::CachingTypes::ReadWrite end end os_disk.disk_size_gb = os_disk_size managed_disk_params.storage_account_type = if premium_os_disk == 'true' Azure::ARM::Compute::Models::StorageAccountTypes::PremiumLRS else Azure::ARM::Compute::Models::StorageAccountTypes::StandardLRS end os_disk.managed_disk = managed_disk_params storage_profile.os_disk = os_disk # Create data disks unless data_disks.nil? disks = [] disk_count = 0 data_disks.each do |_, attrs| managed_data_disk = Azure::ARM::Compute::Models::ManagedDiskParameters.new managed_data_disk.storage_account_type = if attrs[:account_type] == 'true' Azure::ARM::Compute::Models::StorageAccountTypes::PremiumLRS else Azure::ARM::Compute::Models::StorageAccountTypes::StandardLRS end disk = Azure::ARM::Compute::Models::DataDisk.new disk.name = "#{vm_name}-disk#{disk_count}" disk.caching = if attrs[:data_disk_caching].present? case attrs[:data_disk_caching] when 'None' Azure::ARM::Compute::Models::CachingTypes::None when 'ReadOnly' Azure::ARM::Compute::Models::CachingTypes::ReadOnly when 'ReadWrite' Azure::ARM::Compute::Models::CachingTypes::ReadWrite else # ARM best practices stipulate no caching on data disks by default Azure::ARM::Compute::Models::CachingTypes::None end end disk.disk_size_gb = attrs[:disk_size_gb] disk.create_option = Azure::ARM::Compute::Models::DiskCreateOption::Empty disk.lun = disk_count + 1 disk.managed_disk = managed_data_disk disk_count += 1 disks << disk end storage_profile.data_disks = disks end if vhd_path.nil? # We are using a marketplace image storage_profile.image_reference = image_reference(publisher, offer, sku, version) else # We are using a custom managed image image_ref = Azure::ARM::Compute::Models::ImageReference.new image_ref.id = vhd_path storage_profile.image_reference = image_ref end storage_profile end |
#get_public_ip(ip_rg, ip_name) ⇒ Object
50 51 52 |
# File 'app/models/concerns/fog_extensions/azurerm/compute.rb', line 50 def get_public_ip(ip_rg, ip_name) @network_client.public_ipaddresses.get(ip_rg, ip_name) end |
#get_vm_nic(nic_rg, nic_name) ⇒ Object
46 47 48 |
# File 'app/models/concerns/fog_extensions/azurerm/compute.rb', line 46 def get_vm_nic(nic_rg, nic_name) @network_client.network_interfaces.get(nic_rg, nic_name) end |
#initialize(options) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'app/models/concerns/fog_extensions/azurerm/compute.rb', line 6 def initialize() begin require 'azure_mgmt_compute' require 'azure_mgmt_storage' require 'azure_mgmt_network' require 'azure/storage' rescue LoadError => e retry if require('rubygems') raise e. end [:environment] = 'AzureCloud' if [:environment].nil? telemetry = "fog-azure-rm/#{Fog::AzureRM::VERSION}" credentials = Fog::Credentials::AzureRM.get_credentials([:tenant_id], [:client_id], [:client_secret], [:environment]) @compute_mgmt_client = ::Azure::ARM::Compute::ComputeManagementClient.new(credentials, resource_manager_endpoint_url([:environment])) @compute_mgmt_client.subscription_id = [:subscription_id] @compute_mgmt_client.add_user_agent_information(telemetry) @storage_mgmt_client = ::Azure::ARM::Storage::StorageManagementClient.new(credentials, resource_manager_endpoint_url([:environment])) @storage_mgmt_client.subscription_id = [:subscription_id] @storage_mgmt_client.add_user_agent_information(telemetry) # noinspection RubyArgCount @storage_service = Fog::Storage::AzureRM.new(tenant_id: [:tenant_id], client_id: [:client_id], client_secret: [:client_secret], subscription_id: [:subscription_id], environment: [:environment]) @network_client = ::Azure::ARM::Network::NetworkManagementClient.new(credentials, resource_manager_endpoint_url([:environment])) @network_client.subscription_id = [:subscription_id] @network_client.add_user_agent_information(telemetry) end |
#list_all_vms ⇒ Object
42 43 44 |
# File 'app/models/concerns/fog_extensions/azurerm/compute.rb', line 42 def list_all_vms @compute_mgmt_client.virtual_machines.list_all end |
#list_available_sizes(location) ⇒ Object
34 35 36 37 38 39 40 |
# File 'app/models/concerns/fog_extensions/azurerm/compute.rb', line 34 def list_available_sizes(location) sizes = [] @compute_mgmt_client.virtual_machine_sizes.list(location).value().each do |vmsize| sizes << vmsize.name end sizes end |