cheese-chef-provisioning-azurerm 1.0.7 → 1.0.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0f501881562076e732f468745379839590324a30
4
- data.tar.gz: 9f2cd2a3ed2fc68ff0d0940b40e6a1f4262b662d
3
+ metadata.gz: 0bd53a70d8bfd0e4caf6fcc087dc766c4ebd8b09
4
+ data.tar.gz: 3bb6edada005df509ef2708873a5a5b0ede8617c
5
5
  SHA512:
6
- metadata.gz: 4e15a311e699d333fe281817b7bfd941c16bbea6e3f9234842a5a5821d4a1da41c1fdd34d2af4adb448544a61fa166ccf27c6f45cdab2e856a8257fe1d72b52d
7
- data.tar.gz: bfeaa69e93a5a26eb9fd9386a52abe03996832c3755a04acf818d00cdec6f27afe31570d486286e8b6011a34af1a5069b6df890d247a35b4a1a9083efb828357
6
+ metadata.gz: 220b6f59eaeb8ea3b7a4f8101017aa29f0687950f4d761464071ad628161cd034273a5832b5315810b59049ae056d534f4ce15bfdf77cf3c076a36d2c7b7ceee
7
+ data.tar.gz: 51b8efd3b60eaea97451900000f45a137ac238c80038d1460992cebe528a209bc4cecedae4d2cff3c5be26a5eab28b81aeb89ce1cffbb249a14e2dc75b5fa164
@@ -0,0 +1,168 @@
1
+ require 'chef/provisioning/azurerm/azure_provider'
2
+
3
+ class Chef
4
+ class Provider
5
+ class AzureLoadBalancer < Chef::Provisioning::AzureRM::AzureProvider
6
+ provides :azure_load_balancer
7
+
8
+ def whyrun_supported?
9
+ true
10
+ end
11
+
12
+ action :create do
13
+ create_or_update_load_balancer
14
+
15
+ # load_balancer_exists = does_load_balancer_exist
16
+
17
+ # if load_balancer_exists
18
+ # converge_by("update load balancer #{new_resource.name}") do
19
+ # create_or_update_load_balancer
20
+ # end
21
+ # else
22
+ # converge_by("create load balancer #{new_resource.name}") do
23
+ # create_or_update_load_balancer
24
+ # end
25
+ # end
26
+ end
27
+
28
+ action :destroy do
29
+ converge_by("destroy load_balancer: #{new_resource.name}") do
30
+ if does_load_balancer_exist
31
+ destroy_load_balancer
32
+ else
33
+ action_handler.report_progress "load balancer #{new_resource.name} was not found."
34
+ end
35
+ end
36
+ end
37
+
38
+ def does_load_balancer_exist
39
+ try_azure_operation('listing load balancers', true) do
40
+ network_management_client.load_balancers.get(new_resource.resource_group, new_resource.name)
41
+ end
42
+ end
43
+
44
+ def destroy_load_balancer
45
+ action_handler.report_progress 'Destroying Load Balancer...'
46
+ try_azure_operation('destroying load balancer') do
47
+ network_management_client.load_balancers.delete(new_resource.resource_group, new_resource.name)
48
+ end
49
+ end
50
+ # #
51
+
52
+ def lb_update(lb)
53
+ network_management_client.load_balancers.create_or_update(new_resource.resource_group, new_resource.name, lb)
54
+ end
55
+
56
+ def create_or_update_load_balancer
57
+ tags = new_resource.load_balancer_options[:tags]
58
+ subinfo = network_management_client.subnets.get(new_resource.resource_group, new_resource.virtual_network, new_resource.subnet_name)
59
+
60
+ backend_pool = []
61
+
62
+ new_resource.virtual_machine.each do |ivm|
63
+ count = backend_pool.count
64
+ backend_pool[count] = Azure::ARM::Network::Models::BackendAddressPool.new
65
+ backend_pool[count].name = ivm
66
+ end
67
+
68
+ frontend_ipconf = Azure::ARM::Network::Models::FrontendIPConfiguration.new.tap do |feip|
69
+ new_resource.load_balancer_options[:frontend].each do |fendip|
70
+ feip.name = fendip[:name]
71
+ feip.subnet = subinfo
72
+ feip.private_ipallocation_method = fendip[:private_ipallocation_method]
73
+ end
74
+ end
75
+
76
+ probe = Azure::ARM::Network::Models::Probe.new.tap do |prb|
77
+ new_resource.load_balancer_options[:probes].each do |prbes|
78
+ prb.name = prbes[:name]
79
+ prb.port = prbes[:port]
80
+ prb.protocol = prbes[:protocol]
81
+ if prbes[:protocol].casecmp('http')
82
+ prb.request_path = prbes[:request_path]
83
+ end
84
+ prb.interval_in_seconds = prbes[:interval_in_seconds]
85
+ prb.number_of_probes = prbes[:number_of_probes]
86
+ end
87
+ end
88
+
89
+ lb = Azure::ARM::Network::Models::LoadBalancer.new.tap do |lbinfo|
90
+ lbinfo.location = new_resource.location
91
+ lbinfo.tags = tags
92
+ lbinfo.frontend_ipconfigurations = [frontend_ipconf]
93
+ lbinfo.backend_address_pools = backend_pool
94
+ lbinfo.probes = [probe]
95
+ end
96
+
97
+ lb_update(lb) # inital build
98
+
99
+ mylb = network_management_client.load_balancers.get(new_resource.resource_group, new_resource.name)
100
+
101
+ new_resource.virtual_machine.each do |ivm|
102
+ vmnic = network_management_client.network_interfaces.get(new_resource.resource_group, ivm)
103
+ mylb.backend_address_pools.each do |pool|
104
+ next unless pool.name == ivm
105
+ vmnic.ip_configurations.first.load_balancer_backend_address_pools = [pool] # mylb.backend_address_pools
106
+ network_management_client.network_interfaces.create_or_update(new_resource.resource_group, ivm, vmnic)
107
+ end
108
+ end
109
+
110
+ frontend_ipconf_sub = MsRestAzure::SubResource.new.tap do |subresource|
111
+ subresource.id = network_management_client.load_balancers.get(new_resource.resource_group, new_resource.name).frontend_ipconfigurations.first.id
112
+ end
113
+
114
+ backend_ipconf_sub = MsRestAzure::SubResource.new.tap do |subresource|
115
+ subresource.id = mylb.backend_address_pools.first.id
116
+ end
117
+
118
+ probe_sub = MsRestAzure::SubResource.new.tap do |subresource|
119
+ subresource.id = network_management_client.load_balancers.get(new_resource.resource_group, new_resource.name).probes.first.id
120
+ end
121
+
122
+ unless new_resource.load_balancer_options[:inboundnat].nil?
123
+ inboudnat = Azure::ARM::Network::Models::InboundNatRule.new.tap do |inat|
124
+ new_resource.load_balancer_options[:inboundnat].each do |ibnat|
125
+ inat.name = ibnat[:name]
126
+ inat.frontend_port = ibnat[:frontend_port]
127
+ inat.backend_port = ibnat[:backend_port]
128
+ inat.protocol = ibnat[:protocol]
129
+ inat.enable_floating_ip = ibnat[:enable_floating_ip]
130
+ inat.idle_timeout_in_minutes = ibnat[:idle_timeout_in_minutes]
131
+ inat.frontend_ipconfiguration = frontend_ipconf_sub
132
+ end
133
+ end
134
+ end
135
+
136
+ lbrules = Azure::ARM::Network::Models::LoadBalancingRule.new.tap do |lbrs|
137
+ new_resource.load_balancer_options[:lbr].each do |lbr|
138
+ lbrs.name = lbr[:name]
139
+ lbrs.backend_address_pool = backend_ipconf_sub
140
+ lbrs.protocol = lbr[:protocol]
141
+ lbrs.backend_port = lbr[:backend_port]
142
+ lbrs.frontend_port = lbr[:frontend_port]
143
+ lbrs.idle_timeout_in_minutes = lbr[:idle_timeout_in_minutes]
144
+ lbrs.enable_floating_ip = lbr[:enable_floating_ip]
145
+ lbrs.load_distribution = lbr[:load_distribution]
146
+ lbrs.frontend_ipconfiguration = frontend_ipconf_sub
147
+ lbrs.backend_address_pool = backend_ipconf_sub
148
+ lbrs.probe = probe_sub
149
+ end
150
+ end
151
+
152
+ lb = Azure::ARM::Network::Models::LoadBalancer.new.tap do |lbinfo|
153
+ lbinfo.location = new_resource.location
154
+ lbinfo.tags = tags
155
+ lbinfo.frontend_ipconfigurations = [frontend_ipconf]
156
+ lbinfo.backend_address_pools = backend_pool
157
+ unless new_resource.load_balancer_options[:inboundnat].nil?
158
+ lbinfo.inbound_nat_rules = [inboudnat]
159
+ end
160
+ lbinfo.probes = [probe]
161
+ lbinfo.load_balancing_rules = [lbrules]
162
+ end
163
+
164
+ lb_update(lb) # update additions build
165
+ end
166
+ end # class AzureLoadBalancer
167
+ end # class Provider
168
+ end # class Chef
@@ -10,7 +10,7 @@ require 'azure_mgmt_network'
10
10
  Chef::Log.info("chef-provisioning-azurerm #{Chef::Provisioning::AzureRM::VERSION}")
11
11
  Chef::Log.info("chef-provisioning #{Chef::Provisioning::VERSION}")
12
12
 
13
- resources = %w(resource_group resource_template storage_account virtual_network network_interface public_ip_address data_disk)
13
+ resources = %w(resource_group resource_template storage_account virtual_network network_interface public_ip_address data_disk load_balancer)
14
14
  resources.each do |r|
15
15
  require "chef/resource/azure_#{r}"
16
16
  require "chef/provider/azure_#{r}"
@@ -35,7 +35,7 @@ class Chef
35
35
 
36
36
  def network_management_client
37
37
  credentials = Credentials.new.azure_credentials_for_subscription(new_resource.subscription_id)
38
- client = Azure::ARM::Network::NetworkResourceProviderClient.new(credentials)
38
+ client = Azure::ARM::Network::NetworkManagementClient.new(credentials)
39
39
  client.subscription_id = new_resource.subscription_id
40
40
  client
41
41
  end
@@ -1,17 +1,17 @@
1
- require "chef/mixin/shell_out"
2
- require "chef/provisioning/driver"
3
- require "chef/provisioning/convergence_strategy/install_cached"
4
- require "chef/provisioning/convergence_strategy/install_sh"
5
- require "chef/provisioning/convergence_strategy/install_msi"
6
- require "chef/provisioning/convergence_strategy/no_converge"
7
- require "chef/provisioning/transport/ssh"
8
- require "chef/provisioning/transport/winrm"
9
- require "chef/provisioning/machine/windows_machine"
10
- require "chef/provisioning/machine/unix_machine"
11
- require "chef/provisioning/machine_spec"
1
+ require 'chef/mixin/shell_out'
2
+ require 'chef/provisioning/driver'
3
+ require 'chef/provisioning/convergence_strategy/install_cached'
4
+ require 'chef/provisioning/convergence_strategy/install_sh'
5
+ require 'chef/provisioning/convergence_strategy/install_msi'
6
+ require 'chef/provisioning/convergence_strategy/no_converge'
7
+ require 'chef/provisioning/transport/ssh'
8
+ require 'chef/provisioning/transport/winrm'
9
+ require 'chef/provisioning/machine/windows_machine'
10
+ require 'chef/provisioning/machine/unix_machine'
11
+ require 'chef/provisioning/machine_spec'
12
12
  require 'chef/provisioning/azurerm/azure_provider'
13
13
  require 'chef/provisioning/azurerm/credentials'
14
- require "chef/provisioning/azurerm/version"
14
+ require 'chef/provisioning/azurerm/version'
15
15
  require 'chef/provisioning/azurerm/azure_resource'
16
16
 
17
17
  require 'azure_mgmt_resources'
@@ -38,28 +38,28 @@ class Chef
38
38
  end
39
39
 
40
40
  def self.canonicalize_url(driver_url, config)
41
- scheme, account_id = driver_url.split(":", 2)
41
+ scheme, account_id = driver_url.split(':', 2)
42
42
  if account_id.nil? || account_id.empty?
43
43
  subscription = Credentials.new
44
44
  subscription.azure_credentials_for_subscription(subscription_id)
45
- if !subscription
45
+ unless subscription
46
46
  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."
47
47
  end
48
48
  config = Cheffish::MergedConfig.new({ azure_subscriptions: subscription }, config)
49
49
  end
50
50
  if subscription
51
- [ "#{scheme}:#{subscription[:subscription_id]}", config ]
51
+ ["#{scheme}:#{subscription[:subscription_id]}", config]
52
52
  else
53
- [ driver_url, config]
53
+ [driver_url, config]
54
54
  end
55
55
  end
56
56
 
57
57
  def initialize(driver_url, config)
58
58
  super
59
- scheme, subscription_id = driver_url.split(":", 2)
59
+ scheme, subscription_id = driver_url.split(':', 2)
60
60
  self.subscription_id = subscription_id
61
61
  @subscription = Credentials.new.azure_credentials_for_subscription(subscription_id)
62
- if !subscription
62
+ unless subscription
63
63
  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."
64
64
  end
65
65
  Chef::Config.chef_provisioning ||= {}
@@ -69,9 +69,7 @@ class Chef
69
69
  attr_accessor :subscription_id
70
70
 
71
71
  def deep_symbolize_keys(hash_like)
72
- if hash_like.nil? || hash_like.empty?
73
- return {}
74
- end
72
+ return {} if hash_like.nil? || hash_like.empty?
75
73
  r = {}
76
74
  hash_like.each do |key, value|
77
75
  value = deep_symbolize_keys(value) if value.respond_to?(:values)
@@ -100,14 +98,14 @@ class Chef
100
98
  osProfile = bootstrap_options[:osProfile]
101
99
  network_security_group_name = bootstrap_options[:network_security_group_name]
102
100
  tags = bootstrap_options[:tags]
103
- raise "location not provided, where the virtual machine should be created" unless location
104
- raise "resource_group_name not provided, where the virtual machine should be created" unless resource_group_name
105
- raise "osProfile properties not provided in bootstrap_options" unless osProfile
101
+ raise 'location not provided, where the virtual machine should be created' unless location
102
+ raise 'resource_group_name not provided, where the virtual machine should be created' unless resource_group_name
103
+ raise 'osProfile properties not provided in bootstrap_options' unless osProfile
106
104
  computerName = osProfile[:computerName]
107
105
  adminPassword = osProfile[:adminPassword]
108
106
  adminUsername = osProfile[:adminUsername]
109
107
  linuxConfiguration = osProfile[:linuxConfiguration]
110
- raise "adminUsername under osProfile properties not provided in bootstrap_options" unless adminUsername
108
+ raise 'adminUsername under osProfile properties not provided in bootstrap_options' unless adminUsername
111
109
 
112
110
  Chef::Log.debug "Azure machine_options: #{machine_options.inspect}"
113
111
  action_handler.report_progress "Creating #{machine_spec.name} in #{location} with supplied parameters..."
@@ -129,7 +127,7 @@ class Chef
129
127
  end
130
128
  if os_disk_exist
131
129
  action_handler.report_progress "OS disk with the name #{vm_name}_os_disk already exist."
132
- action_handler.report_progress "checking attached status..."
130
+ action_handler.report_progress 'checking attached status...'
133
131
  if os_disk_exist.owner_id.nil?
134
132
  action_handler.report_progress "OS disk #{vm_name} not attached to any VM. Using this..."
135
133
  os_disk = Azure::ARM::Compute::Models::OSDisk.new.tap do |os_disk|
@@ -165,7 +163,7 @@ class Chef
165
163
  end
166
164
  if nic_exist
167
165
  action_handler.report_progress "network interface with the name #{vm_name} already exist."
168
- action_handler.report_progress "checking attached status..."
166
+ action_handler.report_progress 'checking attached status...'
169
167
  if nic_exist.virtual_machine.nil?
170
168
  action_handler.report_progress "network interface #{vm_name} not attached to any VM. Using this..."
171
169
  nic = nic_exist
@@ -192,48 +190,54 @@ class Chef
192
190
  end
193
191
  ]
194
192
  end
195
- )
193
+ )
196
194
  end
197
195
 
198
196
  vm_create_params = Azure::ARM::Compute::Models::VirtualMachine.new.tap do |vm|
199
197
  vm.location = location
200
198
  vm.availability_set = availabilitySet if availability_set && availability_set_id
201
199
  vm.tags = tags if tags
202
- vm.os_profile = Azure::ARM::Compute::Models::OSProfile.new.tap do |os_profile|
203
- os_profile.computer_name = computerName if computerName
204
- os_profile.admin_username = adminUsername
205
- os_profile.admin_password = adminPassword if adminPassword
206
- os_profile.linux_configuration = Azure::ARM::Compute::Models::LinuxConfiguration.new.tap do |linux|
207
- linux.disable_password_authentication = linuxConfiguration[:disablePasswordAuthentication].nil? ? true : linuxConfiguration[:disablePasswordAuthentication]
208
- if ssh = linuxConfiguration[:ssh]
209
- linux.ssh = Azure::ARM::Compute::Models::SshConfiguration.new.tap do |ssh_config|
210
- if publicKeys = ssh[:publicKeys]
211
- public_keys = []
212
- publicKeys.each do |publicKey|
213
- sshPublicKey = Azure::ARM::Compute::Models::SshPublicKey.new.tap do |pub_key|
214
- pub_key.path = publicKey[:path] || "/home/#{adminUsername}/.ssh/authorized_keys"
215
- if publicKey[:keyData]
216
- pub_key.key_data = publicKey[:keyData]
217
- else
218
- raise "public key keyData not provided"
200
+ unless os_disk_exist
201
+ vm.os_profile = Azure::ARM::Compute::Models::OSProfile.new.tap do |os_profile|
202
+ os_profile.computer_name = computerName if computerName
203
+ os_profile.admin_username = adminUsername
204
+ os_profile.admin_password = adminPassword if adminPassword
205
+ if linuxConfiguration
206
+ os_profile.linux_configuration = Azure::ARM::Compute::Models::LinuxConfiguration.new.tap do |linux|
207
+ linux.disable_password_authentication = linuxConfiguration[:disablePasswordAuthentication].nil? ? true : linuxConfiguration[:disablePasswordAuthentication]
208
+ if ssh = linuxConfiguration[:ssh]
209
+ linux.ssh = Azure::ARM::Compute::Models::SshConfiguration.new.tap do |ssh_config|
210
+ if publicKeys = ssh[:publicKeys]
211
+ public_keys = []
212
+ publicKeys.each do |publicKey|
213
+ sshPublicKey = Azure::ARM::Compute::Models::SshPublicKey.new.tap do |pub_key|
214
+ pub_key.path = publicKey[:path] || "/home/#{adminUsername}/.ssh/authorized_keys"
215
+ if publicKey[:keyData]
216
+ pub_key.key_data = publicKey[:keyData]
217
+ else
218
+ raise 'public key keyData not provided'
219
+ end
220
+ end
221
+ public_keys << sshPublicKey
219
222
  end
223
+ ssh_config.public_keys = public_keys
220
224
  end
221
- public_keys << sshPublicKey
222
225
  end
223
- ssh_config.public_keys = public_keys
224
226
  end
225
227
  end
226
228
  end
227
- end if linuxConfiguration
228
- end unless os_disk_exist
229
+ end
230
+ end
229
231
 
230
232
  vm.storage_profile = Azure::ARM::Compute::Models::StorageProfile.new.tap do |store_profile|
231
- store_profile.image_reference = Azure::ARM::Compute::Models::ImageReference.new.tap do |ref|
232
- ref.publisher = bootstrap_options[:storageProfile][:imageReference][:publisher]
233
- ref.offer = bootstrap_options[:storageProfile][:imageReference][:offer]
234
- ref.sku = bootstrap_options[:storageProfile][:imageReference][:sku]
235
- ref.version = bootstrap_options[:storageProfile][:imageReference][:version]
236
- end unless os_disk_exist
233
+ unless os_disk_exist
234
+ store_profile.image_reference = Azure::ARM::Compute::Models::ImageReference.new.tap do |ref|
235
+ ref.publisher = bootstrap_options[:storageProfile][:imageReference][:publisher]
236
+ ref.offer = bootstrap_options[:storageProfile][:imageReference][:offer]
237
+ ref.sku = bootstrap_options[:storageProfile][:imageReference][:sku]
238
+ ref.version = bootstrap_options[:storageProfile][:imageReference][:version]
239
+ end
240
+ end
237
241
  store_profile.os_disk = os_disk
238
242
  end
239
243
 
@@ -254,32 +258,32 @@ class Chef
254
258
  vm = azure_vm_service.virtual_machines.create_or_update(resource_group_name, machine_spec.name, vm_create_params)
255
259
 
256
260
  machine_spec.reference = {
257
- "driver_version" => Chef::Provisioning::AzureRM::VERSION,
258
- "allocated_at" => Time.now.utc.to_s,
259
- "host_node" => action_handler.host_node,
260
- "location" => location,
261
- "resource_group_name" => resource_group_name,
262
- "adminUsername" => adminUsername
261
+ 'driver_version' => Chef::Provisioning::AzureRM::VERSION,
262
+ 'allocated_at' => Time.now.utc.to_s,
263
+ 'host_node' => action_handler.host_node,
264
+ 'location' => location,
265
+ 'resource_group_name' => resource_group_name,
266
+ 'adminUsername' => adminUsername
263
267
  }
264
268
  machine_spec.driver_url = driver_url
265
269
  machine_spec.reference['key_name'] = bootstrap_options[:key_name] if bootstrap_options[:key_name]
266
270
  machine_spec.reference['transport_address_location'] = machine_options[:transport_address_location] if machine_options[:transport_address_location]
267
271
  machine_spec.reference['vm_name'] = machine_spec.name
268
- case vm.storage_profile.os_disk.os_type.downcase
269
- when 'windows'
270
- machine_spec.reference['is_windows'] = true
271
- else
272
- machine_spec.reference['is_windows'] = false
273
- end
272
+ machine_spec.reference['is_windows'] = case vm.storage_profile.os_disk.os_type.downcase
273
+ when 'windows'
274
+ true
275
+ else
276
+ false
277
+ end
274
278
  action_handler.report_progress "Created #{machine_spec.name} in #{location}..."
275
279
  end
276
280
 
277
281
  def connect_to_machine(name, chef_server = nil)
278
- if name.is_a?(MachineSpec)
279
- machine_spec = name
280
- else
281
- machine_spec = Chef::Provisioning::ChefMachineSpec.get(name, chef_server)
282
- end
282
+ machine_spec = if name.is_a?(MachineSpec)
283
+ name
284
+ else
285
+ Chef::Provisioning::ChefMachineSpec.get(name, chef_server)
286
+ end
283
287
 
284
288
  machine_for(machine_spec, machine_spec.reference)
285
289
  end
@@ -299,7 +303,7 @@ class Chef
299
303
  when 'stopped'
300
304
  action_handler.report_progress "#{machine_spec.name} is stopped. Driver can't start it, please contact admin"
301
305
  raise "#{machine_spec.name} is stopped"
302
- #TODO: start the VM
306
+ # TODO: start the VM
303
307
  when nil
304
308
  raise "Could not find power state of #{machine_spec.name}"
305
309
  else
@@ -311,10 +315,10 @@ class Chef
311
315
  end
312
316
 
313
317
  # (see Chef::Provisioning::Driver#destroy_machine)
314
- def destroy_machine(action_handler, machine_spec, machine_options)
318
+ def destroy_machine(action_handler, machine_spec, _machine_options)
315
319
  vm = vm_for(machine_spec)
316
320
  vm_name = machine_spec.name
317
- resource_group_name = machine_spec.reference["resource_group_name"]
321
+ resource_group_name = machine_spec.reference['resource_group_name']
318
322
  # Check if we need to proceed
319
323
  return if vm.nil? || vm_name.nil? || resource_group_name.nil?
320
324
  # Skip if we don't actually need to do anything
@@ -330,6 +334,131 @@ class Chef
330
334
  action_handler.report_progress "Destroyed VM #{machine_spec.name}"
331
335
  end
332
336
 
337
+ # ####
338
+ def allocate_load_balancer(action_handler, lb_spec, lb_options, machine_specs)
339
+ tags = lb_options[:tags]
340
+ resource_group = lb_options[:resource_group]
341
+ location = lb_options[:location]
342
+ virtual_network = lb_options[:virtual_network]
343
+ subnet_name = lb_options[:subnet_name]
344
+
345
+ subinfo = azure_net_service.subnets.get(lb_options.resource_group, lb_options.virtual_network, lb_options.subnet_name)
346
+
347
+ backend_pool = []
348
+
349
+ count = backend_pool.count
350
+ backend_pool[0] = Azure::ARM::Network::Models::BackendAddressPool.new
351
+ backend_pool[0].name = lb_spec.name
352
+
353
+
354
+ frontend_ipconf = Azure::ARM::Network::Models::FrontendIPConfiguration.new.tap do |feip|
355
+ lb_options[:frontendIPConfigurations].each do |fendip|
356
+ feip.name = fendip[:name]
357
+ feip.subnet = subinfo
358
+ feip.private_ipallocation_method = fendip[:private_ipallocation_method]
359
+ end
360
+ end
361
+
362
+ probe = Azure::ARM::Network::Models::Probe.new.tap do |prb|
363
+ lb_options[:probes].each do |prbes|
364
+ prb.name = prbes[:name]
365
+ prb.port = prbes[:port]
366
+ prb.protocol = prbes[:protocol]
367
+ if prbes[:protocol].to_s == "http"
368
+ prb.request_path = prbes[:request_path]
369
+ else
370
+ prb.request_path = nil
371
+ end
372
+ prb.interval_in_seconds = prbes[:interval_in_seconds]
373
+ prb.number_of_probes = prbes[:number_of_probes]
374
+ end
375
+ end
376
+
377
+ lb = Azure::ARM::Network::Models::LoadBalancer.new.tap do |lbinfo|
378
+ lbinfo.location = location
379
+ lbinfo.tags = tags
380
+ lbinfo.frontend_ipconfigurations = [frontend_ipconf]
381
+ lbinfo.backend_address_pools = backend_pool
382
+ lbinfo.probes = [probe]
383
+ end
384
+
385
+ lb_update(resource_group, lb_spec.name, lb) # inital build
386
+
387
+ mylb = azure_net_service.load_balancers.get(resource_group, lb_spec.name)
388
+
389
+ machine_specs.each do |ivm|
390
+ vmnic = azure_net_service.network_interfaces.get(resource_group, ivm.name)
391
+ mylb.backend_address_pools.each do |pool|
392
+ vmnic.ip_configurations.first.load_balancer_backend_address_pools = [pool] # mylb.backend_address_pools
393
+ azure_net_service.network_interfaces.create_or_update(resource_group, ivm.name, vmnic)
394
+ end
395
+ end
396
+
397
+ frontend_ipconf_sub = MsRestAzure::SubResource.new.tap do |subresource|
398
+ subresource.id = azure_net_service.load_balancers.get(resource_group, lb_spec.name).frontend_ipconfigurations.first.id
399
+ end
400
+
401
+ backend_ipconf_sub = MsRestAzure::SubResource.new.tap do |subresource|
402
+ subresource.id = mylb.backend_address_pools.first.id
403
+ end
404
+
405
+ probe_sub = MsRestAzure::SubResource.new.tap do |subresource|
406
+ subresource.id = azure_net_service.load_balancers.get(resource_group, lb_spec.name).probes.first.id
407
+ end
408
+
409
+ unless lb_options[:inboundNatRules].nil?
410
+ inboudnat = Azure::ARM::Network::Models::InboundNatRule.new.tap do |inat|
411
+ lb_options[:inboundNatRules].each do |ibnat|
412
+ inat.name = ibnat[:name]
413
+ inat.frontend_port = ibnat[:frontend_port]
414
+ inat.backend_port = ibnat[:backend_port]
415
+ inat.protocol = ibnat[:protocol]
416
+ inat.enable_floating_ip = ibnat[:enable_floating_ip]
417
+ inat.idle_timeout_in_minutes = ibnat[:idle_timeout_in_minutes]
418
+ inat.frontend_ipconfiguration = frontend_ipconf_sub
419
+ end
420
+ end
421
+ end
422
+
423
+ lbrules = Azure::ARM::Network::Models::LoadBalancingRule.new.tap do |lbrs|
424
+ lb_options[:loadBalancingRules].each do |lbr|
425
+ lbrs.name = lbr[:name]
426
+ lbrs.backend_address_pool = backend_ipconf_sub
427
+ lbrs.protocol = lbr[:protocol]
428
+ lbrs.backend_port = lbr[:backend_port]
429
+ lbrs.frontend_port = lbr[:frontend_port]
430
+ lbrs.idle_timeout_in_minutes = lbr[:idle_timeout_in_minutes]
431
+ lbrs.enable_floating_ip = lbr[:enable_floating_ip]
432
+ lbrs.load_distribution = lbr[:load_distribution]
433
+ lbrs.frontend_ipconfiguration = frontend_ipconf_sub
434
+ lbrs.backend_address_pool = backend_ipconf_sub
435
+ lbrs.probe = probe_sub
436
+ end
437
+ end
438
+
439
+ lb = Azure::ARM::Network::Models::LoadBalancer.new.tap do |lbinfo|
440
+ lbinfo.location = location
441
+ lbinfo.tags = tags
442
+ lbinfo.frontend_ipconfigurations = [frontend_ipconf]
443
+ lbinfo.backend_address_pools = backend_pool
444
+ unless lb_options[:inboundNatRules].nil?
445
+ lbinfo.inbound_nat_rules = [inboudnat]
446
+ end
447
+ lbinfo.probes = [probe]
448
+ lbinfo.load_balancing_rules = [lbrules]
449
+ end
450
+ lb_update(resource_group, lb_spec.name, lb) # update additions build
451
+ end
452
+
453
+ def lb_update(resource_group, name, lb)
454
+ azure_net_service.load_balancers.create_or_update(resource_group, name, lb)
455
+ end
456
+
457
+ def destroy_load_balancer(action_handler, lb_spec, lb_options)
458
+ raise "#{lb_spec.name} hasn't recived a resource group. Unable to destroy." unless lb_options.resource_group
459
+ azure_net_service.load_balancers.delete(lb_options.resource_group, lb_spec.name)
460
+ end
461
+
333
462
  private
334
463
 
335
464
  def machine_for(machine_spec, machine_options, vm = nil)
@@ -340,7 +469,7 @@ class Chef
340
469
  convergence_strategy = convergence_strategy_for(machine_spec, machine_options)
341
470
 
342
471
  if machine_spec.reference['is_windows']
343
- raise "Sorry, No windows convergance support yet."
472
+ raise 'Sorry, No windows convergance support yet.'
344
473
  else
345
474
  Chef::Provisioning::Machine::UnixMachine.new(machine_spec, transport, convergence_strategy)
346
475
  end
@@ -361,7 +490,7 @@ class Chef
361
490
  end
362
491
 
363
492
  def default_ssh_username
364
- "ubuntu"
493
+ 'ubuntu'
365
494
  end
366
495
 
367
496
  def vm_for(machine_spec)
@@ -381,7 +510,7 @@ class Chef
381
510
 
382
511
  def transport_for(machine_spec, machine_options, vm)
383
512
  if machine_spec.reference['is_windows']
384
- raise "Sorry, No windows convergance support yet."
513
+ raise 'Sorry, No windows convergance support yet.'
385
514
  else
386
515
  create_ssh_transport(machine_spec, machine_options, vm)
387
516
  end
@@ -391,12 +520,12 @@ class Chef
391
520
  ssh_options = ssh_options_for(machine_spec, machine_options, vm)
392
521
  username = machine_spec.reference['adminUsername'] || default_ssh_username
393
522
  options = {}
394
- if machine_spec.reference[:sudo] || (!machine_spec.reference.has_key?(:sudo) && username != 'root')
523
+ if machine_spec.reference[:sudo] || (!machine_spec.reference.key?(:sudo) && username != 'root')
395
524
  options[:prefix] = 'sudo '
396
525
  end
397
- #Enable pty by default
526
+ # Enable pty by default
398
527
  options[:ssh_pty_enable] = true
399
- if machine_spec.reference.has_key?('ssh_gateway')
528
+ if machine_spec.reference.key?('ssh_gateway')
400
529
  options[:ssh_gateway] = machine_spec.reference['ssh_gateway']
401
530
  elsif machine_options[:ssh_gateway]
402
531
  options[:ssh_gateway] = machine_options[:ssh_gateway]
@@ -414,12 +543,12 @@ class Chef
414
543
 
415
544
  def ssh_options_for(machine_spec, machine_options, vm)
416
545
  result = {
417
- :auth_methods => [ 'publickey' ],
418
- :keys_only => true
546
+ auth_methods: ['publickey'],
547
+ keys_only: true
419
548
  }.merge(machine_options[:ssh_options] || {})
420
- unless result.has_key?(:key_data)
549
+ unless result.key?(:key_data)
421
550
  result[:keys_only] = true
422
- result[:key_data] = [ private_key_for(machine_spec, machine_options, vm) ]
551
+ result[:key_data] = [private_key_for(machine_spec, machine_options, vm)]
423
552
  end
424
553
  result
425
554
  end
@@ -437,7 +566,7 @@ class Chef
437
566
  request_url = azure_net_service.base_url
438
567
  options = {
439
568
  middlewares: [[MsRest::RetryPolicyMiddleware, times: 3, retry: 0.02], [:cookie_jar]],
440
- query_params: {'api-version' => api_version,'$expand' => nil},
569
+ query_params: { 'api-version' => api_version, '$expand' => nil },
441
570
  headers: request_headers,
442
571
  base_url: request_url
443
572
  }
@@ -458,7 +587,7 @@ class Chef
458
587
  end
459
588
  end
460
589
 
461
- def private_key_for(machine_spec, machine_options, vm)
590
+ def private_key_for(machine_spec, machine_options, _vm)
462
591
  if machine_spec.reference['key_name']
463
592
  key = get_private_key(machine_spec.reference['key_name'])
464
593
  unless key
@@ -470,7 +599,7 @@ class Chef
470
599
  elsif machine_options[:bootstrap_options] && machine_options[:bootstrap_options][:key_name]
471
600
  get_private_key(machine_options[:bootstrap_options][:key_name])
472
601
  else
473
- # TODO make a way to suggest other keys to try ...
602
+ # TODO: make a way to suggest other keys to try ...
474
603
  raise "No key found to connect to #{machine_spec.name} (#{machine_spec.reference.inspect})!"
475
604
  end
476
605
  end
@@ -483,12 +612,12 @@ class Chef
483
612
  )
484
613
 
485
614
  # Defaults
486
- if !machine_spec.reference
615
+ unless machine_spec.reference
487
616
  return Chef::Provisioning::ConvergenceStrategy::NoConverge.new(convergence_options, config)
488
617
  end
489
618
 
490
619
  if machine_spec.reference['is_windows']
491
- raise "Sorry, No windows convergance support yet."
620
+ raise 'Sorry, No windows convergance support yet.'
492
621
  elsif machine_options[:cached_installer] == true
493
622
  Chef::Provisioning::ConvergenceStrategy::InstallCached.new(convergence_options, config)
494
623
  else
@@ -529,11 +658,11 @@ class Chef
529
658
  action_handler.report_progress "waiting for #{machine_spec.name} (#{driver_url}) to be connectable (transport up and running) ..."
530
659
  max_wait_time = Chef::Config.chef_provisioning[:machine_max_wait_time] || 120
531
660
  Retryable.retryable(
532
- :tries => (max_wait_time/sleep_time).to_i,
533
- :sleep => sleep_time,
534
- :matching => /did not become connectable within/
535
- ) do |retries, exception|
536
- action_handler.report_progress "been waiting #{sleep_time*retries}/#{max_wait_time} -- sleeping #{sleep_time} seconds for #{machine_spec.name} (#{driver_url}) to become connectable ..."
661
+ tries: (max_wait_time / sleep_time).to_i,
662
+ sleep: sleep_time,
663
+ matching: /did not become connectable within/
664
+ ) do |retries, _exception|
665
+ action_handler.report_progress "been waiting #{sleep_time * retries}/#{max_wait_time} -- sleeping #{sleep_time} seconds for #{machine_spec.name} (#{driver_url}) to become connectable ..."
537
666
  unless transport.available?
538
667
  raise "VM #{machine_spec.name} (#{driver_url}) did not become connectable within #{max_wait_time} seconds"
539
668
  end
@@ -1,7 +1,7 @@
1
1
  class Chef
2
2
  module Provisioning
3
3
  module AzureRM
4
- VERSION = '1.0.7'.freeze
4
+ VERSION = '1.0.8'.freeze
5
5
  end
6
6
  end
7
7
  end
@@ -0,0 +1,18 @@
1
+ require 'chef/provisioning/azurerm/azure_resource'
2
+
3
+ class Chef
4
+ class Resource
5
+ class AzureLoadBalancer < Chef::Provisioning::AzureRM::AzureResource
6
+ resource_name :azure_load_balancer
7
+ actions :create, :destroy, :nothing
8
+ default_action :create
9
+ attribute :name, kind_of: String, name_attribute: true
10
+ attribute :resource_group, kind_of: String
11
+ attribute :location, kind_of: String, default: 'westus'
12
+ attribute :load_balancer_options, kind_of: Hash
13
+ attribute :virtual_network, kind_of: String
14
+ attribute :subnet_name, kind_of: String
15
+ attribute :virtual_machine, kind_of: Array
16
+ end
17
+ end
18
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cheese-chef-provisioning-azurerm
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.7
4
+ version: 1.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stuart Preston
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2017-04-21 00:00:00.000000000 Z
12
+ date: 2017-09-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: chef
@@ -133,6 +133,7 @@ files:
133
133
  - LICENSE.txt
134
134
  - README.md
135
135
  - lib/chef/provider/azure_data_disk.rb
136
+ - lib/chef/provider/azure_load_balancer.rb
136
137
  - lib/chef/provider/azure_network_interface.rb
137
138
  - lib/chef/provider/azure_public_ip_address.rb
138
139
  - lib/chef/provider/azure_resource_group.rb
@@ -148,6 +149,7 @@ files:
148
149
  - lib/chef/provisioning/azurerm/version.rb
149
150
  - lib/chef/provisioning/driver_init/azurerm.rb
150
151
  - lib/chef/resource/azure_data_disk.rb
152
+ - lib/chef/resource/azure_load_balancer.rb
151
153
  - lib/chef/resource/azure_network_interface.rb
152
154
  - lib/chef/resource/azure_public_ip_address.rb
153
155
  - lib/chef/resource/azure_resource_group.rb
@@ -174,7 +176,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
174
176
  version: '0'
175
177
  requirements: []
176
178
  rubyforge_project:
177
- rubygems_version: 2.6.8
179
+ rubygems_version: 2.4.5.1
178
180
  signing_key:
179
181
  specification_version: 4
180
182
  summary: Chef Provisioner for the Azure Resource Management (ARM) REST API.