knife-azure 1.9.0 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/lib/azure/azure_interface.rb +2 -3
  3. data/lib/azure/custom_errors.rb +1 -1
  4. data/lib/azure/helpers.rb +1 -1
  5. data/lib/azure/resource_management/ARM_deployment_template.rb +157 -162
  6. data/lib/azure/resource_management/ARM_interface.rb +72 -73
  7. data/lib/azure/resource_management/vnet_config.rb +11 -10
  8. data/lib/azure/resource_management/windows_credentials.rb +19 -19
  9. data/lib/azure/service_management/ASM_interface.rb +6 -5
  10. data/lib/azure/service_management/ag.rb +11 -11
  11. data/lib/azure/service_management/certificate.rb +7 -5
  12. data/lib/azure/service_management/connection.rb +10 -10
  13. data/lib/azure/service_management/deploy.rb +12 -14
  14. data/lib/azure/service_management/disk.rb +4 -2
  15. data/lib/azure/service_management/host.rb +7 -4
  16. data/lib/azure/service_management/image.rb +4 -4
  17. data/lib/azure/service_management/loadbalancer.rb +2 -2
  18. data/lib/azure/service_management/rest.rb +9 -8
  19. data/lib/azure/service_management/role.rb +67 -70
  20. data/lib/azure/service_management/storageaccount.rb +5 -3
  21. data/lib/azure/service_management/utility.rb +1 -1
  22. data/lib/azure/service_management/vnet.rb +1 -1
  23. data/lib/chef/knife/azure_ag_create.rb +13 -13
  24. data/lib/chef/knife/azure_ag_list.rb +1 -1
  25. data/lib/chef/knife/azure_base.rb +49 -66
  26. data/lib/chef/knife/azure_image_list.rb +6 -6
  27. data/lib/chef/knife/azure_internal-lb_create.rb +14 -14
  28. data/lib/chef/knife/azure_internal-lb_list.rb +1 -1
  29. data/lib/chef/knife/azure_server_create.rb +233 -268
  30. data/lib/chef/knife/azure_server_delete.rb +31 -31
  31. data/lib/chef/knife/azure_server_list.rb +1 -1
  32. data/lib/chef/knife/azure_server_show.rb +1 -1
  33. data/lib/chef/knife/azure_vnet_create.rb +15 -19
  34. data/lib/chef/knife/azure_vnet_list.rb +1 -1
  35. data/lib/chef/knife/azurerm_base.rb +39 -28
  36. data/lib/chef/knife/azurerm_server_create.rb +112 -177
  37. data/lib/chef/knife/azurerm_server_delete.rb +13 -13
  38. data/lib/chef/knife/azurerm_server_list.rb +1 -1
  39. data/lib/chef/knife/azurerm_server_show.rb +1 -1
  40. data/lib/chef/knife/bootstrap/bootstrapper.rb +34 -238
  41. data/lib/chef/knife/bootstrap/common_bootstrap_options.rb +77 -76
  42. data/lib/chef/knife/bootstrap_azure.rb +56 -33
  43. data/lib/chef/knife/bootstrap_azurerm.rb +46 -29
  44. data/lib/knife-azure/version.rb +18 -1
  45. metadata +28 -16
  46. data/lib/chef/knife/bootstrap/bootstrap_options.rb +0 -105
@@ -1,5 +1,5 @@
1
1
  #
2
- # Copyright:: Copyright 2018 Chef Software, Inc.
2
+ # Copyright:: Copyright 2010-2019, Chef Software Inc.
3
3
  # License:: Apache License, Version 2.0
4
4
  #
5
5
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -86,15 +86,14 @@ module Azure
86
86
  end
87
87
  end
88
88
 
89
- def list_images
90
- end
89
+ def list_images; end
91
90
 
92
91
  def list_servers(resource_group_name = nil)
93
- if resource_group_name.nil?
94
- servers = compute_management_client.virtual_machines.list_all
95
- else
96
- servers = compute_management_client.virtual_machines.list(resource_group_name)
97
- end
92
+ servers = if resource_group_name.nil?
93
+ compute_management_client.virtual_machines.list_all
94
+ else
95
+ compute_management_client.virtual_machines.list(resource_group_name)
96
+ end
98
97
 
99
98
  cols = ["VM Name", "Resource Group Name", "Location", "Provisioning State", "OS Type"]
100
99
  rows = []
@@ -152,14 +151,14 @@ module Azure
152
151
  network_interface_name = server.network_profile.network_interfaces[0].id.split("/")[-1]
153
152
  network_interface_data = network_resource_client.network_interfaces.get(resource_group, network_interface_name)
154
153
  public_ip_id_data = network_interface_data.ip_configurations[0].public_ipaddress
155
- unless public_ip_id_data.nil?
154
+ if public_ip_id_data.nil?
155
+ public_ip_data = nil
156
+ else
156
157
  public_ip_name = public_ip_id_data.id.split("/")[-1]
157
158
  public_ip_data = network_resource_client.public_ipaddresses.get(resource_group, public_ip_name)
158
- else
159
- public_ip_data = nil
160
159
  end
161
160
 
162
- details = Array.new
161
+ details = []
163
162
  details << ui.color("Server Name", :bold, :cyan)
164
163
  details << server.name
165
164
 
@@ -188,18 +187,18 @@ module Azure
188
187
  details << server.storage_profile.os_disk.os_type
189
188
 
190
189
  details << ui.color("Public IP address", :bold, :cyan)
191
- unless public_ip_data.nil?
192
- details << public_ip_data.ip_address
193
- else
194
- details << " -- "
195
- end
190
+ details << if public_ip_data.nil?
191
+ " -- "
192
+ else
193
+ public_ip_data.ip_address
194
+ end
196
195
 
197
196
  details << ui.color("FQDN", :bold, :cyan)
198
- unless public_ip_data.nil? || public_ip_data.dns_settings.nil?
199
- details << public_ip_data.dns_settings.fqdn
200
- else
201
- details << " -- "
202
- end
197
+ details << if public_ip_data.nil? || public_ip_data.dns_settings.nil?
198
+ " -- "
199
+ else
200
+ public_ip_data.dns_settings.fqdn
201
+ end
203
202
 
204
203
  puts ui.list(details, :columns_across, 2)
205
204
  end
@@ -243,11 +242,11 @@ module Azure
243
242
 
244
243
  def platform(image_reference)
245
244
  @platform ||= begin
246
- if image_reference =~ /WindowsServer.*/
247
- platform = "Windows"
248
- else
249
- platform = "Linux"
250
- end
245
+ platform = if image_reference =~ /WindowsServer.*/
246
+ "Windows"
247
+ else
248
+ "Linux"
249
+ end
251
250
  platform
252
251
  end
253
252
  end
@@ -279,7 +278,18 @@ module Azure
279
278
  ## fetch substatus field which contains the chef-client run logs ##
280
279
  substatus = fetch_substatus(resource_group_name, virtual_machine_name, chef_extension_name)
281
280
 
282
- unless substatus.nil?
281
+ if substatus.nil?
282
+ ## unavailability of the substatus field indicates that chef-client run is not completed yet on the server ##
283
+ fetch_process_wait_time = ((Time.now - fetch_process_start_time) / 60).round
284
+ if fetch_process_wait_time <= fetch_process_wait_timeout
285
+ print ui.color(".", :bold).to_s
286
+ sleep 30
287
+ fetch_chef_client_logs(resource_group_name, virtual_machine_name, chef_extension_name, fetch_process_start_time, fetch_process_wait_timeout)
288
+ else
289
+ ## wait time exceeded 30 minutes timeout ##
290
+ ui.error "\nchef-client run logs could not be fetched since fetch process exceeded wait timeout of #{fetch_process_wait_timeout} minutes.\n"
291
+ end
292
+ else
283
293
  ## chef-client run logs becomes available ##
284
294
  status = parse_substatus_code(substatus.code, 2)
285
295
  message = substatus.message
@@ -287,31 +297,20 @@ module Azure
287
297
  puts "\n\n******** Please find the chef-client run details below ********\n\n"
288
298
  print "----> chef-client run status: "
289
299
  case status
290
- when "succeeded"
291
- ## chef-client run succeeded ##
292
- color = :green
293
- when "failed"
294
- ## chef-client run failed ##
295
- color = :red
296
- when "transitioning"
297
- ## chef-client run did not complete within maximum timeout of 30 minutes ##
298
- ## fetch whatever logs available under the chef-client.log file ##
299
- color = :yellow
300
+ when "succeeded"
301
+ ## chef-client run succeeded ##
302
+ color = :green
303
+ when "failed"
304
+ ## chef-client run failed ##
305
+ color = :red
306
+ when "transitioning"
307
+ ## chef-client run did not complete within maximum timeout of 30 minutes ##
308
+ ## fetch whatever logs available under the chef-client.log file ##
309
+ color = :yellow
300
310
  end
301
- puts "#{ui.color(status, color, :bold)}"
311
+ puts ui.color(status, color, :bold).to_s
302
312
  puts "----> chef-client run logs: "
303
313
  puts "\n#{message}\n" ## message field of substatus contains the chef-client run logs ##
304
- else
305
- ## unavailability of the substatus field indicates that chef-client run is not completed yet on the server ##
306
- fetch_process_wait_time = ((Time.now - fetch_process_start_time) / 60).round
307
- if fetch_process_wait_time <= fetch_process_wait_timeout
308
- print "#{ui.color('.', :bold)}"
309
- sleep 30
310
- fetch_chef_client_logs(resource_group_name, virtual_machine_name, chef_extension_name, fetch_process_start_time, fetch_process_wait_timeout)
311
- else
312
- ## wait time exceeded 30 minutes timeout ##
313
- ui.error "\nchef-client run logs could not be fetched since fetch process exceeded wait timeout of #{fetch_process_wait_timeout} minutes.\n"
314
- end
315
314
  end
316
315
  end
317
316
 
@@ -341,11 +340,11 @@ module Azure
341
340
  params[:azure_vnet_subnet_name]
342
341
  )
343
342
  if params[:tcp_endpoints]
344
- if @platform == "Windows"
345
- params[:tcp_endpoints] = params[:tcp_endpoints] + ",3389"
346
- else
347
- params[:tcp_endpoints] = params[:tcp_endpoints] + ",22,16001"
348
- end
343
+ params[:tcp_endpoints] = if @platform == "Windows"
344
+ params[:tcp_endpoints] + ",3389"
345
+ else
346
+ params[:tcp_endpoints] + ",22,16001"
347
+ end
349
348
  random_no = rand(100..1000)
350
349
  params[:azure_sec_group_name] = params[:azure_vm_name] + "_sec_grp_" + random_no.to_s
351
350
  if security_group_exist?(params[:azure_resource_group_name], params[:azure_sec_group_name])
@@ -362,22 +361,21 @@ module Azure
362
361
  ui.log("Deployment name is: #{deployment.name}")
363
362
  ui.log("Deployment ID is: #{deployment.id}")
364
363
  deployment.properties.dependencies.each do |deploy|
365
- if deploy.resource_type == "Microsoft.Compute/virtualMachines"
366
- if params[:chef_extension_public_param][:extendedLogs] == "true"
367
- print "\n\nWaiting for the first chef-client run on virtual machine #{deploy.resource_name}"
368
- fetch_chef_client_logs(params[:azure_resource_group_name],
369
- deploy.resource_name,
370
- params[:chef_extension],
371
- Time.now
372
- )
373
- end
374
-
375
- ui.log("VM Details ...")
376
- ui.log("-------------------------------")
377
- ui.log("Virtual Machine name is: #{deploy.resource_name}")
378
- ui.log("Virtual Machine ID is: #{deploy.id}")
379
- show_server(deploy.resource_name, params[:azure_resource_group_name])
364
+ next unless deploy.resource_type == "Microsoft.Compute/virtualMachines"
365
+
366
+ if params[:chef_extension_public_param][:extendedLogs] == "true"
367
+ print "\n\nWaiting for the first chef-client run on virtual machine #{deploy.resource_name}"
368
+ fetch_chef_client_logs(params[:azure_resource_group_name],
369
+ deploy.resource_name,
370
+ params[:chef_extension],
371
+ Time.now)
380
372
  end
373
+
374
+ ui.log("VM Details ...")
375
+ ui.log("-------------------------------")
376
+ ui.log("Virtual Machine name is: #{deploy.resource_name}")
377
+ ui.log("Virtual Machine ID is: #{deploy.id}")
378
+ show_server(deploy.resource_name, params[:azure_resource_group_name])
381
379
  end
382
380
  end
383
381
  end
@@ -398,7 +396,7 @@ module Azure
398
396
  end
399
397
 
400
398
  def create_resource_group(params = {})
401
- resource_group = ResourceGroup.new()
399
+ resource_group = ResourceGroup.new
402
400
  resource_group.name = params[:azure_resource_group_name]
403
401
  resource_group.location = params[:azure_service_location]
404
402
 
@@ -414,7 +412,7 @@ module Azure
414
412
 
415
413
  def create_virtual_machine_using_template(params)
416
414
  template = create_deployment_template(params)
417
- parameters = create_deployment_parameters(params, @platform)
415
+ parameters = create_deployment_parameters(params)
418
416
 
419
417
  deploy_prop = DeploymentProperties.new
420
418
  deploy_prop.template = template
@@ -466,7 +464,8 @@ module Azure
466
464
  ext_version = compute_management_client.virtual_machine_extension_images.list_versions(
467
465
  params[:azure_service_location],
468
466
  params[:chef_extension_publisher],
469
- params[:chef_extension]).last.name
467
+ params[:chef_extension]
468
+ ).last.name
470
469
  ext_version_split_values = ext_version.split(".")
471
470
  ext_version = ext_version_split_values[0] + "." + ext_version_split_values[1]
472
471
  ext_version
@@ -1,7 +1,6 @@
1
1
  #
2
2
  # Author:: Aliasgar Batterywala (aliasgar.batterywala@clogeny.com)
3
- #
4
- # Copyright:: Copyright 2016-2018 Chef Software, Inc.
3
+ # Copyright:: Copyright 2010-2019, Chef Software Inc.
5
4
  # License:: Apache License, Version 2.0
6
5
  #
7
6
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -60,8 +59,8 @@ module Azure::ARM
60
59
  {
61
60
  "name" => subnet_name,
62
61
  "properties" => {
63
- "addressPrefix" => subnet_prefix
64
- }
62
+ "addressPrefix" => subnet_prefix,
63
+ },
65
64
  }
66
65
  end
67
66
 
@@ -138,8 +137,8 @@ module Azure::ARM
138
137
  else ## subnets exist in vnet, calculate new address_prefix for the new subnet based on the space taken by these existing subnets under the given address space of the virtual network ##
139
138
  vnet_network_address = IPAddress(vnet_address_prefix)
140
139
  subnets = sort_subnets_by_cidr_prefix(subnets)
141
- available_networks_pool = Array.new
142
- used_networks_pool = Array.new
140
+ available_networks_pool = []
141
+ used_networks_pool = []
143
142
  subnets.each do |subnet|
144
143
  ## in case the larger network is not divided into smaller subnets but
145
144
  ## divided into only 1 largest subnet of the complete network size ##
@@ -160,7 +159,8 @@ module Azure::ARM
160
159
 
161
160
  ## sort both the network pools before trimming the available_networks_pool ##
162
161
  available_networks_pool, used_networks_pool = sort_pools(
163
- available_networks_pool, used_networks_pool)
162
+ available_networks_pool, used_networks_pool
163
+ )
164
164
 
165
165
  ## trim the available_networks_pool based on the networks already
166
166
  ## allocated to the existing subnets ##
@@ -172,7 +172,8 @@ module Azure::ARM
172
172
 
173
173
  ## sort both the network pools after trimming the available_networks_pool ##
174
174
  available_networks_pool, used_networks_pool = sort_pools(
175
- available_networks_pool, used_networks_pool)
175
+ available_networks_pool, used_networks_pool
176
+ )
176
177
  end
177
178
 
178
179
  ## space available in the vnet_address_prefix network for the new subnet ##
@@ -225,7 +226,7 @@ module Azure::ARM
225
226
  vnet_config[:virtualNetworkName] = vnet_name
226
227
  if vnet ## handle resources in the existing virtual network ##
227
228
  vnet_config[:addressPrefixes] = vnet_address_spaces(vnet)
228
- vnet_config[:subnets] = Array.new
229
+ vnet_config[:subnets] = []
229
230
  subnets = subnets_list(resource_group_name, vnet_name)
230
231
  if subnets
231
232
  subnets.each do |subnet|
@@ -238,7 +239,7 @@ module Azure::ARM
238
239
  end
239
240
  else ## create config for new vnet ##
240
241
  vnet_config[:addressPrefixes] = [ "10.0.0.0/16" ]
241
- vnet_config[:subnets] = Array.new
242
+ vnet_config[:subnets] = []
242
243
  vnet_config[:subnets].push(
243
244
  subnet(vnet_subnet_name, "10.0.0.0/24")
244
245
  )
@@ -1,6 +1,6 @@
1
1
  #
2
2
  # Author:: Nimisha Sharad (nimisha.sharad@clogeny.com)
3
- # Copyright:: Copyright 2015-2018 Chef Software, Inc.
3
+ # Copyright:: Copyright 2010-2019, Chef Software Inc.
4
4
  # License:: Apache License, Version 2.0
5
5
  #
6
6
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -40,35 +40,35 @@ module Azure::ARM
40
40
  # Ref: https://msdn.microsoft.com/en-us/library/windows/desktop/ms724284(v=vs.85).aspx
41
41
  class FILETIME < FFI::Struct
42
42
  layout :dwLowDateTime, :DWORD,
43
- :dwHighDateTime, :DWORD
43
+ :dwHighDateTime, :DWORD
44
44
  end
45
45
 
46
46
  # Ref: https://msdn.microsoft.com/en-us/library/windows/desktop/aa374790(v=vs.85).aspx
47
47
  class CREDENTIAL_ATTRIBUTE < FFI::Struct
48
48
  layout :Keyword, :LPTSTR,
49
- :Flags, :DWORD,
50
- :ValueSize, :DWORD,
51
- :Value, :LPBYTE
49
+ :Flags, :DWORD,
50
+ :ValueSize, :DWORD,
51
+ :Value, :LPBYTE
52
52
  end
53
53
 
54
54
  # Ref: https://msdn.microsoft.com/en-us/library/windows/desktop/aa374788(v=vs.85).aspx
55
55
  class CREDENTIAL_OBJECT < FFI::Struct
56
56
  layout :Flags, :DWORD,
57
- :Type, :DWORD,
58
- :TargetName, :LPTSTR,
59
- :Comment, :LPTSTR,
60
- :LastWritten, FILETIME,
61
- :CredentialBlobSize, :DWORD,
62
- :CredentialBlob, :LPBYTE,
63
- :Persist, :DWORD,
64
- :AttributeCount, :DWORD,
65
- :Attributes, CREDENTIAL_ATTRIBUTE,
66
- :TargetAlias, :LPTSTR,
67
- :UserName, :LPTSTR
57
+ :Type, :DWORD,
58
+ :TargetName, :LPTSTR,
59
+ :Comment, :LPTSTR,
60
+ :LastWritten, FILETIME,
61
+ :CredentialBlobSize, :DWORD,
62
+ :CredentialBlob, :LPBYTE,
63
+ :Persist, :DWORD,
64
+ :AttributeCount, :DWORD,
65
+ :Attributes, CREDENTIAL_ATTRIBUTE,
66
+ :TargetAlias, :LPTSTR,
67
+ :UserName, :LPTSTR
68
68
  end
69
69
 
70
70
  # Ref: https://msdn.microsoft.com/en-us/library/windows/desktop/aa374804(v=vs.85).aspx
71
- safe_attach_function :CredReadW, [:LPCTSTR, :DWORD, :DWORD, :pointer], :BOOL
71
+ safe_attach_function :CredReadW, %i{LPCTSTR DWORD DWORD pointer}, :BOOL
72
72
  end
73
73
 
74
74
  module WindowsCredentials
@@ -100,7 +100,7 @@ module Azure::ARM
100
100
  credential[:tokentype] = tokentype[0].split(":")[1]
101
101
  credential[:user] = user[0].split(":")[1]
102
102
  credential[:token] = access_token[0].split(":")[1]
103
- #Todo: refresh_token is not complete currently
103
+ # Todo: refresh_token is not complete currently
104
104
  # target_name method needs to be modified for that
105
105
  credential[:refresh_token] = refresh_token[0].split(":")[1]
106
106
  credential[:clientid] = clientid[0].split(":")[1]
@@ -118,7 +118,7 @@ module Azure::ARM
118
118
  exit
119
119
  end
120
120
 
121
- #Todo: For getting the complete refreshToken, both credentials (ending with --0-2 and --1-2) have to be read
121
+ # Todo: For getting the complete refreshToken, both credentials (ending with --0-2 and --1-2) have to be read
122
122
  def target_name
123
123
  # cmdkey command is used for accessing windows credential manager.
124
124
  # Multiple credentials get created in windows credential manager for a single Azure account in xplat-cli
@@ -1,5 +1,5 @@
1
1
  #
2
- # Copyright:: Copyright 2016-2018 Chef Software, Inc.
2
+ # Copyright:: Copyright 2010-2019, Chef Software Inc.
3
3
  # License:: Apache License, Version 2.0
4
4
  #
5
5
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -64,9 +64,10 @@ module Azure
64
64
  end
65
65
 
66
66
  def rdp_port(arr_ports)
67
- if !arr_ports
67
+ unless arr_ports
68
68
  return ""
69
69
  end
70
+
70
71
  if arr_ports.length > 0
71
72
  arr_ports.each do |port|
72
73
  if port["Name"] == "Remote Desktop"
@@ -78,7 +79,7 @@ module Azure
78
79
  end
79
80
 
80
81
  def find_server(params = {})
81
- server = connection.roles.find(params[:name], params = { :azure_dns_name => params[:azure_dns_name] })
82
+ server = connection.roles.find(params[:name], params = { azure_dns_name: params[:azure_dns_name] })
82
83
  end
83
84
 
84
85
  def delete_server(params = {})
@@ -117,7 +118,7 @@ module Azure
117
118
 
118
119
  puts ""
119
120
  if role
120
- details = Array.new
121
+ details = []
121
122
  details << ui.color("Role name", :bold, :cyan)
122
123
  details << role.name
123
124
  details << ui.color("Status", :bold, :cyan)
@@ -226,7 +227,7 @@ module Azure
226
227
  remove_hosted_service_on_failure = nil
227
228
  end
228
229
 
229
- #If Storage Account is not specified, check if the geographic location has one to re-use
230
+ # If Storage Account is not specified, check if the geographic location has one to re-use
230
231
  if not params[:azure_storage_account]
231
232
  storage_accts = connection.storageaccounts.all
232
233
  storage = storage_accts.find { |storage_acct| storage_acct.location.to_s == params[:azure_service_location] }
@@ -1,6 +1,6 @@
1
1
  #
2
2
  # Author:: Jeff Mendoza (jeffmendoza@live.com)
3
- # Copyright:: Copyright 2013-2018 Chef Software, Inc.
3
+ # Copyright:: Copyright 2010-2019, Chef Software Inc.
4
4
  # License:: Apache License, Version 2.0
5
5
  #
6
6
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -26,11 +26,11 @@ module Azure
26
26
  @ags ||= begin
27
27
  @ags = {}
28
28
  response = @connection.query_azure("affinitygroups",
29
- "get",
30
- "",
31
- "",
32
- true,
33
- false)
29
+ "get",
30
+ "",
31
+ "",
32
+ true,
33
+ false)
34
34
  response.css("AffinityGroup").each do |ag|
35
35
  item = AG.new(@connection).parse(ag)
36
36
  @ags[item.name] = item
@@ -89,11 +89,11 @@ module Azure
89
89
  end
90
90
  end
91
91
  @connection.query_azure("affinitygroups",
92
- "post",
93
- builder.to_xml,
94
- "",
95
- true,
96
- false)
92
+ "post",
93
+ builder.to_xml,
94
+ "",
95
+ true,
96
+ false)
97
97
  end
98
98
  end
99
99
  end