Class: Chef::Provisioning::AzureRM::Driver
- Inherits:
-
Driver
- Object
- Driver
- Chef::Provisioning::AzureRM::Driver
- Defined in:
- lib/chef/provisioning/azurerm/driver.rb
Instance Attribute Summary collapse
-
#region ⇒ Object
readonly
Returns the value of attribute region.
-
#subscription ⇒ Object
readonly
Returns the value of attribute subscription.
-
#subscription_id ⇒ Object
Returns the value of attribute subscription_id.
Class Method Summary collapse
- .canonicalize_url(driver_url, config) ⇒ Object
-
.from_url(driver_url, config) ⇒ AzureDriver
Construct an AzureDriver object from a URL - used to parse existing URL data to hydrate a driver object.
Instance Method Summary collapse
-
#allocate_load_balancer(action_handler, lb_spec, lb_options, machine_specs) ⇒ Object
####.
-
#allocate_machine(action_handler, machine_spec, machine_options) ⇒ Object
Allocate a new machine with the Azure API and start it up, without blocking to wait for it.
- #connect_to_machine(name, chef_server = nil) ⇒ Object
- #deep_symbolize_keys(hash_like) ⇒ Object
- #destroy_load_balancer(action_handler, lb_spec, lb_options) ⇒ Object
- #destroy_machine(action_handler, machine_spec, _machine_options) ⇒ Object
-
#initialize(driver_url, config) ⇒ Driver
constructor
A new instance of Driver.
- #lb_update(resource_group, name, lb) ⇒ Object
- #ready_machine(action_handler, machine_spec, machine_options) ⇒ Object
Constructor Details
#initialize(driver_url, config) ⇒ Driver
Returns a new instance of Driver.
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/chef/provisioning/azurerm/driver.rb', line 57 def initialize(driver_url, config) super scheme, subscription_id = driver_url.split(':', 2) self.subscription_id = subscription_id @subscription = Credentials.new.azure_credentials_for_subscription(subscription_id) unless subscription raise "Driver #{driver_url} has a subscription ID, but the system has no credentials configured for it! If you have access to this subscription, you can use `azure account download` and `azure account import` in the Azure CLI to get the credentials, or set azure_subscriptions to [ { subscription_id: '...', management_credentials: ... }] in your Chef configuration." end Chef::Config.chef_provisioning ||= {} end |
Instance Attribute Details
#region ⇒ Object (readonly)
Returns the value of attribute region.
29 30 31 |
# File 'lib/chef/provisioning/azurerm/driver.rb', line 29 def region @region end |
#subscription ⇒ Object (readonly)
Returns the value of attribute subscription.
68 69 70 |
# File 'lib/chef/provisioning/azurerm/driver.rb', line 68 def subscription @subscription end |
#subscription_id ⇒ Object
Returns the value of attribute subscription_id.
69 70 71 |
# File 'lib/chef/provisioning/azurerm/driver.rb', line 69 def subscription_id @subscription_id end |
Class Method Details
.canonicalize_url(driver_url, config) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/chef/provisioning/azurerm/driver.rb', line 40 def self.canonicalize_url(driver_url, config) scheme, account_id = driver_url.split(':', 2) if account_id.nil? || account_id.empty? subscription = Credentials.new subscription.azure_credentials_for_subscription(subscription_id) unless subscription raise "Driver #{driver_url} did not specify a subscription ID, and no default subscription was found. Have you downloaded the Azure CLI and used `azure account download` and `azure account import` to set up Azure? Alternately, you can set azure_subscriptions to [ { subscription_id: '...', management_credentials: ... }] in your Chef configuration." end config = Cheffish::MergedConfig.new({ azure_subscriptions: subscription }, config) end if subscription ["#{scheme}:#{subscription[:subscription_id]}", config] else [driver_url, config] end end |
.from_url(driver_url, config) ⇒ AzureDriver
Construct an AzureDriver object from a URL - used to parse existing URL data to hydrate a driver object. URL scheme: azure:subscription_id
36 37 38 |
# File 'lib/chef/provisioning/azurerm/driver.rb', line 36 def self.from_url(driver_url, config) Driver.new(driver_url, config) end |
Instance Method Details
#allocate_load_balancer(action_handler, lb_spec, lb_options, machine_specs) ⇒ Object
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 |
# File 'lib/chef/provisioning/azurerm/driver.rb', line 338 def allocate_load_balancer(action_handler, lb_spec, , machine_specs) = [:tags] resource_group = [:resource_group] location = [:location] virtual_network = [:virtual_network] subnet_name = [:subnet_name] subinfo = azure_net_service.subnets.get(.resource_group, .virtual_network, .subnet_name) backend_pool = [] count = backend_pool.count backend_pool[0] = Azure::ARM::Network::Models::BackendAddressPool.new backend_pool[0].name = lb_spec.name frontend_ipconf = Azure::ARM::Network::Models::FrontendIPConfiguration.new.tap do |feip| [:frontendIPConfigurations].each do |fendip| feip.name = fendip[:name] feip.subnet = subinfo feip.private_ipallocation_method = fendip[:private_ipallocation_method] end end probe = Azure::ARM::Network::Models::Probe.new.tap do |prb| [:probes].each do |prbes| prb.name = prbes[:name] prb.port = prbes[:port] prb.protocol = prbes[:protocol] if prbes[:protocol].to_s == "http" prb.request_path = prbes[:request_path] else prb.request_path = nil end prb.interval_in_seconds = prbes[:interval_in_seconds] prb.number_of_probes = prbes[:number_of_probes] end end lb = Azure::ARM::Network::Models::LoadBalancer.new.tap do |lbinfo| lbinfo.location = location lbinfo. = lbinfo.frontend_ipconfigurations = [frontend_ipconf] lbinfo.backend_address_pools = backend_pool lbinfo.probes = [probe] end lb_update(resource_group, lb_spec.name, lb) # inital build mylb = azure_net_service.load_balancers.get(resource_group, lb_spec.name) machine_specs.each do |ivm| vmnic = azure_net_service.network_interfaces.get(resource_group, ivm.name) mylb.backend_address_pools.each do |pool| vmnic.ip_configurations.first.load_balancer_backend_address_pools = [pool] # mylb.backend_address_pools azure_net_service.network_interfaces.create_or_update(resource_group, ivm.name, vmnic) end end frontend_ipconf_sub = MsRestAzure::SubResource.new.tap do |subresource| subresource.id = azure_net_service.load_balancers.get(resource_group, lb_spec.name).frontend_ipconfigurations.first.id end backend_ipconf_sub = MsRestAzure::SubResource.new.tap do |subresource| subresource.id = mylb.backend_address_pools.first.id end probe_sub = MsRestAzure::SubResource.new.tap do |subresource| subresource.id = azure_net_service.load_balancers.get(resource_group, lb_spec.name).probes.first.id end unless [:inboundNatRules].nil? inboudnat = Azure::ARM::Network::Models::InboundNatRule.new.tap do |inat| [:inboundNatRules].each do |ibnat| inat.name = ibnat[:name] inat.frontend_port = ibnat[:frontend_port] inat.backend_port = ibnat[:backend_port] inat.protocol = ibnat[:protocol] inat.enable_floating_ip = ibnat[:enable_floating_ip] inat.idle_timeout_in_minutes = ibnat[:idle_timeout_in_minutes] inat.frontend_ipconfiguration = frontend_ipconf_sub end end end lbrules = Azure::ARM::Network::Models::LoadBalancingRule.new.tap do |lbrs| [:loadBalancingRules].each do |lbr| lbrs.name = lbr[:name] lbrs.backend_address_pool = backend_ipconf_sub lbrs.protocol = lbr[:protocol] lbrs.backend_port = lbr[:backend_port] lbrs.frontend_port = lbr[:frontend_port] lbrs.idle_timeout_in_minutes = lbr[:idle_timeout_in_minutes] lbrs.enable_floating_ip = lbr[:enable_floating_ip] lbrs.load_distribution = lbr[:load_distribution] lbrs.frontend_ipconfiguration = frontend_ipconf_sub lbrs.backend_address_pool = backend_ipconf_sub lbrs.probe = probe_sub end end lb = Azure::ARM::Network::Models::LoadBalancer.new.tap do |lbinfo| lbinfo.location = location lbinfo. = lbinfo.frontend_ipconfigurations = [frontend_ipconf] lbinfo.backend_address_pools = backend_pool unless [:inboundNatRules].nil? lbinfo.inbound_nat_rules = [inboudnat] end lbinfo.probes = [probe] lbinfo.load_balancing_rules = [lbrules] end lb_update(resource_group, lb_spec.name, lb) # update additions build end |
#allocate_machine(action_handler, machine_spec, machine_options) ⇒ Object
Allocate a new machine with the Azure API and start it up, without blocking to wait for it. Creates any needed resources to get a machine up and running.
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 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 172 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 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 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/chef/provisioning/azurerm/driver.rb', line 87 def allocate_machine(action_handler, machine_spec, ) existing_vm = vm_for(machine_spec) # We don't need to do anything if the existing VM is found return if existing_vm = ([:bootstrap_options] || {}).dup vm_name = machine_spec.name location = [:location] || [:location] resource_group_name = [:resource_group_name] availability_set = [:availability_set] osProfile = [:osProfile] network_security_group_name = [:network_security_group_name] = [:tags] raise 'location not provided, where the virtual machine should be created' unless location raise 'resource_group_name not provided, where the virtual machine should be created' unless resource_group_name raise 'osProfile properties not provided in bootstrap_options' unless osProfile computerName = osProfile[:computerName] adminPassword = osProfile[:adminPassword] adminUsername = osProfile[:adminUsername] linuxConfiguration = osProfile[:linuxConfiguration] raise 'adminUsername under osProfile properties not provided in bootstrap_options' unless adminUsername Chef::Log.debug "Azure machine_options: #{.inspect}" action_handler.report_progress "Creating #{machine_spec.name} in #{location} with supplied parameters..." if availability_set action_handler.report_progress "Looking for availability_set #{availability_set}." availability_set_id = azure_vm_service.availability_sets.get(resource_group_name, availability_set).id raise "availability_set #{availability_set} under resource group #{resource_group_name} was not found." unless availability_set_id availabilitySet = MsRestAzure::SubResource.new.tap do |subResource| subResource.id = availability_set_id end end action_handler.report_progress "Preparing OS disk for the VM #{vm_name}..." begin os_disk_exist = azure_vm_service.disks.get(resource_group_name, "#{vm_name}_os_disk") rescue MsRestAzure::AzureOperationError os_disk_exist = nil end if os_disk_exist action_handler.report_progress "OS disk with the name #{vm_name}_os_disk already exist." action_handler.report_progress 'checking attached status...' if os_disk_exist.owner_id.nil? action_handler.report_progress "OS disk #{vm_name} not attached to any VM. Using this..." os_disk = Azure::ARM::Compute::Models::OSDisk.new.tap do |os_disk| os_disk.create_option = 'attach' os_disk.os_type = machine_spec.reference['is_windows'] ? 'Windows' : 'Linux' os_disk.managed_disk = Azure::ARM::Compute::Models::ManagedDiskParameters.new.tap do |managedDisk| managedDisk.id = os_disk_exist.id end end else raise "OS disk #{vm_name} attached to a VM. Contact your cloud administrator or change VM name." end else os_disk = Azure::ARM::Compute::Models::OSDisk.new.tap do |os_disk| os_disk.name = "#{vm_name}_os_disk" os_disk.create_option = Azure::ARM::Compute::Models::DiskCreateOptionTypes::FromImage os_disk.managed_disk = Azure::ARM::Compute::Models::ManagedDiskParameters.new.tap do |managedDisk| managedDisk.storage_account_type = [:storageProfile][:storage_account_type] end end end begin subnet = azure_net_service.subnets.get(resource_group_name, [:virtual_network_name], [:subnet_name]) rescue MsRestAzure::AzureOperationError => e raise e. end action_handler.report_progress "Creating a network interface for the VM #{vm_name}" begin nic_exist = azure_net_service.network_interfaces.get(resource_group_name, vm_name) rescue MsRestAzure::AzureOperationError nic_exist = nil end if nic_exist action_handler.report_progress "network interface with the name #{vm_name} already exist." action_handler.report_progress 'checking attached status...' if nic_exist.virtual_machine.nil? action_handler.report_progress "network interface #{vm_name} not attached to any VM. Using this..." nic = nic_exist else raise "network interface #{vm_name} attached to a VM. Contact your cloud administrator or change VM name." end else begin network_security_group = azure_net_service.network_security_groups.get(resource_group_name, network_security_group_name) if network_security_group_name rescue MsRestAzure::AzureOperationError => e raise e. end nic = azure_net_service.network_interfaces.create_or_update( resource_group_name, vm_name, Azure::ARM::Network::Models::NetworkInterface.new.tap do |interface| interface.location = location interface.network_security_group = network_security_group if network_security_group_name interface.ip_configurations = [ Azure::ARM::Network::Models::NetworkInterfaceIPConfiguration.new.tap do |nic_conf| nic_conf.name = vm_name nic_conf.private_ipallocation_method = Azure::ARM::Network::Models::IPAllocationMethod::Dynamic nic_conf.subnet = subnet end ] end ) end vm_create_params = Azure::ARM::Compute::Models::VirtualMachine.new.tap do |vm| vm.location = location vm.availability_set = availabilitySet if availability_set && availability_set_id vm. = if unless os_disk_exist vm.os_profile = Azure::ARM::Compute::Models::OSProfile.new.tap do |os_profile| os_profile.computer_name = computerName if computerName os_profile.admin_username = adminUsername os_profile.admin_password = adminPassword if adminPassword if linuxConfiguration os_profile.linux_configuration = Azure::ARM::Compute::Models::LinuxConfiguration.new.tap do |linux| linux.disable_password_authentication = linuxConfiguration[:disablePasswordAuthentication].nil? ? true : linuxConfiguration[:disablePasswordAuthentication] if ssh = linuxConfiguration[:ssh] linux.ssh = Azure::ARM::Compute::Models::SshConfiguration.new.tap do |ssh_config| if publicKeys = ssh[:publicKeys] public_keys = [] publicKeys.each do |publicKey| sshPublicKey = Azure::ARM::Compute::Models::SshPublicKey.new.tap do |pub_key| pub_key.path = publicKey[:path] || "/home/#{adminUsername}/.ssh/authorized_keys" if publicKey[:keyData] pub_key.key_data = publicKey[:keyData] else raise 'public key keyData not provided' end end public_keys << sshPublicKey end ssh_config.public_keys = public_keys end end end end end end end vm.storage_profile = Azure::ARM::Compute::Models::StorageProfile.new.tap do |store_profile| unless os_disk_exist store_profile.image_reference = Azure::ARM::Compute::Models::ImageReference.new.tap do |ref| ref.publisher = [:storageProfile][:imageReference][:publisher] ref.offer = [:storageProfile][:imageReference][:offer] ref.sku = [:storageProfile][:imageReference][:sku] ref.version = [:storageProfile][:imageReference][:version] end end store_profile.os_disk = os_disk end vm.hardware_profile = Azure::ARM::Compute::Models::HardwareProfile.new.tap do |hardware| hardware.vm_size = [:hardwareProfile][:vmSize] end vm.network_profile = Azure::ARM::Compute::Models::NetworkProfile.new.tap do |net_profile| net_profile.network_interfaces = [ Azure::ARM::Compute::Models::NetworkInterfaceReference.new.tap do |ref| ref.id = nic.id ref.primary = true end ] end end vm = azure_vm_service.virtual_machines.create_or_update(resource_group_name, machine_spec.name, vm_create_params) machine_spec.reference = { 'driver_version' => Chef::Provisioning::AzureRM::VERSION, 'allocated_at' => Time.now.utc.to_s, 'host_node' => action_handler.host_node, 'location' => location, 'resource_group_name' => resource_group_name, 'adminUsername' => adminUsername } machine_spec.driver_url = driver_url machine_spec.reference['key_name'] = [:key_name] if [:key_name] machine_spec.reference['transport_address_location'] = [:transport_address_location] if [:transport_address_location] machine_spec.reference['vm_name'] = machine_spec.name machine_spec.reference['is_windows'] = case vm.storage_profile.os_disk.os_type.downcase when 'windows' true else false end action_handler.report_progress "Created #{machine_spec.name} in #{location}..." end |
#connect_to_machine(name, chef_server = nil) ⇒ Object
281 282 283 284 285 286 287 288 289 |
# File 'lib/chef/provisioning/azurerm/driver.rb', line 281 def connect_to_machine(name, chef_server = nil) machine_spec = if name.is_a?(MachineSpec) name else Chef::Provisioning::ChefMachineSpec.get(name, chef_server) end machine_for(machine_spec, machine_spec.reference) end |
#deep_symbolize_keys(hash_like) ⇒ Object
71 72 73 74 75 76 77 78 79 |
# File 'lib/chef/provisioning/azurerm/driver.rb', line 71 def deep_symbolize_keys(hash_like) return {} if hash_like.nil? || hash_like.empty? r = {} hash_like.each do |key, value| value = deep_symbolize_keys(value) if value.respond_to?(:values) r[key.to_sym] = value end r end |
#destroy_load_balancer(action_handler, lb_spec, lb_options) ⇒ Object
457 458 459 460 |
# File 'lib/chef/provisioning/azurerm/driver.rb', line 457 def destroy_load_balancer(action_handler, lb_spec, ) raise "#{lb_spec.name} hasn't recived a resource group. Unable to destroy." unless .resource_group azure_net_service.load_balancers.delete(.resource_group, lb_spec.name) end |
#destroy_machine(action_handler, machine_spec, _machine_options) ⇒ Object
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 |
# File 'lib/chef/provisioning/azurerm/driver.rb', line 318 def destroy_machine(action_handler, machine_spec, ) vm = vm_for(machine_spec) vm_name = machine_spec.name resource_group_name = machine_spec.reference['resource_group_name'] # Check if we need to proceed return if vm.nil? || vm_name.nil? || resource_group_name.nil? # Skip if we don't actually need to do anything return unless action_handler.should_perform_actions # TODO: action_handler.do |block| ? action_handler.report_progress "Destroying VM #{machine_spec.name}" azure_vm_service.virtual_machines.delete(resource_group_name, vm_name) action_handler.report_progress "Destroying OS disk for machine #{vm_name}" azure_vm_service.disks.delete(resource_group_name, "#{vm_name}_os_disk") action_handler.report_progress "Destroying network interface #{machine_spec.name}" azure_net_service.network_interfaces.delete(resource_group_name, vm_name) action_handler.report_progress "Destroyed VM #{machine_spec.name}" end |
#lb_update(resource_group, name, lb) ⇒ Object
453 454 455 |
# File 'lib/chef/provisioning/azurerm/driver.rb', line 453 def lb_update(resource_group, name, lb) azure_net_service.load_balancers.create_or_update(resource_group, name, lb) end |
#ready_machine(action_handler, machine_spec, machine_options) ⇒ Object
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
# File 'lib/chef/provisioning/azurerm/driver.rb', line 292 def ready_machine(action_handler, machine_spec, ) = deep_symbolize_keys() vm = vm_for(machine_spec) if vm.nil? raise "Machine #{machine_spec.name} does not have a VM associated with it, or the VM does not exist." end case machine_power_state(vm) when 'running' action_handler.report_progress "#{machine_spec.name} is ready" when 'stopped' action_handler.report_progress "#{machine_spec.name} is stopped. Driver can't start it, please contact admin" raise "#{machine_spec.name} is stopped" # TODO: start the VM when nil raise "Could not find power state of #{machine_spec.name}" else action_handler.report_progress "Waiting for #{machine_spec.name} to become ready..." wait_until_ready(action_handler, machine_spec) end wait_for_transport(action_handler, machine_spec, ) machine_for(machine_spec, , vm) end |