Module: Chef::Knife::AzureBase

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(includer) ⇒ Object

:nodoc: Would prefer to do this in a rational way, but can’t be done b/c of Mixlib::CLI’s design :(



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/chef/knife/helpers/azure_base.rb', line 27

def self.included(includer)
  includer.class_eval do
    deps do
      require "readline"
      require "chef/json_compat"
      require_relative "../../../azure/service_management/ASM_interface"
    end

    option :azure_subscription_id,
      short: "-S ID",
      long: "--azure-subscription-id ID",
      description: "Your Azure subscription ID"

    option :azure_mgmt_cert,
      short: "-p FILENAME",
      long: "--azure-mgmt-cert FILENAME",
      description: "Your Azure PEM file name"

    option :azure_api_host_name,
      short: "-H HOSTNAME",
      long: "--azure-api-host-name HOSTNAME",
      description: "Your Azure host name"

    option :verify_ssl_cert,
      long: "--verify-ssl-cert",
      description: "Verify SSL Certificates for communication over HTTPS",
      boolean: true,
      default: false

    option :azure_publish_settings_file,
      long: "--azure-publish-settings-file FILENAME",
      description: "Your Azure Publish Settings File"
  end
end

Instance Method Details

#fetch_chef_client_logs(fetch_process_start_time, fetch_process_wait_timeout) ⇒ 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
# File 'lib/chef/knife/helpers/azure_base.rb', line 338

def fetch_chef_client_logs(fetch_process_start_time, fetch_process_wait_timeout)
  ## fetch server details ##
  role = fetch_role
  if !role.nil?
    ## fetch Chef Extension details deployed on the server ##
    ext = fetch_extension(role)
    if !ext.nil?
      ## fetch substatus field which contains the chef-client run logs ##
      substatus = fetch_substatus(ext)
      if !substatus.nil?
        ## chef-client run logs becomes available ##
        name = substatus.at_css("Name").text
        status = substatus.at_css("Status").text
        message = substatus.at_css("Message").text

        ## printing the logs ##
        puts "\n\n******** Please find the chef-client run details below ********\n\n"
        print "----> chef-client run status: "
        case status
        when "Success"
          ## chef-client run succeeded ##
          color = :green
        when "Error"
          ## chef-client run failed ##
          color = :red
        when "Transitioning"
          ## chef-client run did not complete within maximum timeout of 30 minutes ##
          ## fetch whatever logs available under the chef-client.log file ##
          color = :yellow
        end
        puts "#{ui.color(status, color, :bold)}"
        puts "----> chef-client run logs: "
        puts "\n#{message}\n" ## message field of substatus contains the chef-client run logs ##
      else
        ## unavailability of the substatus field indicates that chef-client run is not completed yet on the server ##
        fetch_process_wait_time = ((Time.now - fetch_process_start_time) / 60).round
        if fetch_process_wait_time <= fetch_process_wait_timeout ## wait for maximum 30 minutes until chef-client run logs becomes available ##
          print "#{ui.color(".", :bold)}"
          sleep 30
          fetch_chef_client_logs(fetch_process_start_time, fetch_process_wait_timeout)
        else
          ## wait time exceeded maximum threshold set for the wait timeout ##
          ui.error "\nchef-client run logs could not be fetched since fetch process exceeded wait timeout of #{fetch_process_wait_timeout} minutes.\n"
        end
      end
    else
      ## Chef Extension could not be found ##
      ui.error("Unable to find Chef extension under role #{config[:azure_vm_name] || @name_args[0]}.")
    end
  else
    ## server could not be found ##
    ui.error("chef-client run logs could not be fetched since role #{config[:azure_vm_name] || @name_args[0]} could not be found.")
  end
end

#fetch_deploymentObject



293
294
295
296
297
298
# File 'lib/chef/knife/helpers/azure_base.rb', line 293

def fetch_deployment
  deployment_name = service.deployment_name(config[:azure_dns_name])
  deployment = service.deployment("hostedservices/#{config[:azure_dns_name]}/deployments/#{deployment_name}")

  deployment
end

#fetch_extension(role) ⇒ Object



314
315
316
317
318
319
320
321
322
323
324
# File 'lib/chef/knife/helpers/azure_base.rb', line 314

def fetch_extension(role)
  ext_list_xml = role.css("ResourceExtensionStatusList ResourceExtensionStatus")
  return nil if ext_list_xml.nil?

  ext_list_xml.each do |ext|
    if ext.at_css("HandlerName").text == "Chef.Bootstrap.WindowsAzure.LinuxChefClient" || ext.at_css("HandlerName").text == "Chef.Bootstrap.WindowsAzure.ChefClient"
      return ext
    end
  end
  nil
end

#fetch_roleObject



300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/chef/knife/helpers/azure_base.rb', line 300

def fetch_role
  deployment = fetch_deployment

  if deployment.at_css("Deployment Name") != nil
    role_list_xml = deployment.css("RoleInstanceList RoleInstance")
    role_list_xml.each do |role|
      if role.at_css("RoleName").text == (config[:azure_vm_name] || @name_args[0])
        return role
      end
    end
  end
  nil
end

#fetch_substatus(extension) ⇒ Object



326
327
328
329
330
331
332
333
334
335
336
# File 'lib/chef/knife/helpers/azure_base.rb', line 326

def fetch_substatus(extension)
  return nil if extension.at_css("ExtensionSettingStatus SubStatusList SubStatus").nil?

  substatus_list_xml = extension.css("ExtensionSettingStatus SubStatusList SubStatus")
  substatus_list_xml.each do |substatus|
    if substatus.at_css("Name").text == "Chef Client run logs"
      return substatus
    end
  end
  nil
end

#find_file(name) ⇒ Object



277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/chef/knife/helpers/azure_base.rb', line 277

def find_file(name)
  name = ::File.expand_path(name)
  config_dir = Chef::Knife.chef_config_dir
  if File.exist? name
    file = name
  elsif config_dir && File.exist?(File.join(config_dir, name))
    file = File.join(config_dir, name)
  elsif File.exist?(File.join(ENV["HOME"], ".chef", name))
    file = File.join(ENV["HOME"], ".chef", name)
  else
    ui.error("Unable to find file - " + name)
    exit 1
  end
  file
end

#get_azure_profile_file_pathObject



233
234
235
# File 'lib/chef/knife/helpers/azure_base.rb', line 233

def get_azure_profile_file_path
  "~/.azure/azureProfile.json"
end

#get_default_subscription(azure_profile) ⇒ Object



257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/chef/knife/helpers/azure_base.rb', line 257

def get_default_subscription(azure_profile)
  first_subscription_as_default = nil
  azure_profile["subscriptions"].each do |subscription|
    if subscription["isDefault"]
      Chef::Log.info("Default subscription \'#{subscription["name"]}\'' selected.")
      return subscription
    end

    first_subscription_as_default ||= subscription
  end

  if first_subscription_as_default
    Chef::Log.info("First subscription \'#{subscription["name"]}\' selected as default.")
  else
    Chef::Log.info("No subscriptions found.")
    exit 1
  end
  first_subscription_as_default
end

#is_image_windows?Boolean

Returns:

  • (Boolean)


62
63
64
65
66
67
68
69
70
71
# File 'lib/chef/knife/helpers/azure_base.rb', line 62

def is_image_windows?
  images = service.list_images
  target_image = images.select { |i| i.name == config[:azure_source_image] }
  if target_image[0].nil?
    ui.error('Invalid image. Use the command "knife azure image list" to verify the image name')
    exit 1
  else
    target_image[0].os == "Windows"
  end
end

#msg_pair(label, value, color = :cyan) ⇒ Object



86
87
88
89
90
# File 'lib/chef/knife/helpers/azure_base.rb', line 86

def msg_pair(label, value, color = :cyan)
  if value && !value.to_s.empty?
    puts "#{ui.color(label, color)}: #{value}"
  end
end

#msg_server_summary(server) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/chef/knife/helpers/azure_base.rb', line 92

def msg_server_summary(server)
  puts "\n"
  msg_pair("DNS Name", server.hostedservicename + ".cloudapp.net")
  msg_pair("VM Name", server.name)
  msg_pair("Size", server.size)
  msg_pair("Azure Source Image", config[:azure_source_image])
  msg_pair("Azure Service Location", config[:azure_service_location])
  msg_pair("Public Ip Address", server.publicipaddress)
  msg_pair("Private Ip Address", server.ipaddress)
  msg_pair("SSH Port", server.sshport) unless server.sshport.nil?
  msg_pair("WinRM Port", server.winrmport) unless server.winrmport.nil?
  msg_pair("TCP Ports", server.tcpports) unless server.tcpports.nil? || server.tcpports.empty?
  msg_pair("UDP Ports", server.udpports) unless server.udpports.nil? || server.udpports.empty?
  msg_pair("Environment", config[:environment] || "_default")
  msg_pair("Runlist", config[:run_list]) unless config[:run_list].empty?
  puts "\n"
end

#parse_azure_profile(filename, errors) ⇒ Object



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/chef/knife/helpers/azure_base.rb', line 237

def parse_azure_profile(filename, errors)
  require "openssl"
  require "uri"
  errors = [] if errors.nil?
  azure_profile = File.read(File.expand_path(filename))
  azure_profile = JSON.parse(azure_profile)
  default_subscription = get_default_subscription(azure_profile)
  if default_subscription.key?("id") && default_subscription.key?("managementCertificate") && default_subscription.key?("managementEndpointUrl")

    config[:azure_subscription_id] = default_subscription["id"]
    mgmt_key = OpenSSL::PKey::RSA.new(default_subscription["managementCertificate"]["key"]).to_pem
    mgmt_cert = OpenSSL::X509::Certificate.new(default_subscription["managementCertificate"]["cert"]).to_pem
    config[:azure_mgmt_cert] = mgmt_key + mgmt_cert
    config[:azure_api_host_name] = URI(default_subscription["managementEndpointUrl"]).host
  else
    errors << "Check if values set for 'id', 'managementCertificate', 'managementEndpointUrl' in -> #{filename} for 'defaultSubscription'. \n  OR "
  end
  errors
end

#parse_publish_settings_file(filename) ⇒ Object



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
# File 'lib/chef/knife/helpers/azure_base.rb', line 206

def parse_publish_settings_file(filename)
  require "nokogiri"
  require "base64"
  require "openssl"
  require "uri"
  begin
    doc = Nokogiri::XML(File.open(find_file(filename)))
    profile = doc.at_css("PublishProfile")
    subscription = profile.at_css("Subscription")
    # check given PublishSettings XML file format.Currently PublishSettings file have two different XML format
    if profile.attribute("SchemaVersion").nil?
      management_cert = OpenSSL::PKCS12.new(Base64.decode64(profile.attribute("ManagementCertificate").value))
      config[:azure_api_host_name] = URI(profile.attribute("Url").value).host
    elsif profile.attribute("SchemaVersion").value == "2.0"
      management_cert = OpenSSL::PKCS12.new(Base64.decode64(subscription.attribute("ManagementCertificate").value))
      config[:azure_api_host_name] = URI(subscription.attribute("ServiceManagementUrl").value).host
    else
      ui.error("Publish settings file Schema not supported - " + filename)
    end
    config[:azure_mgmt_cert] = management_cert.certificate.to_pem + management_cert.key.to_pem
    config[:azure_subscription_id] = doc.at_css("Subscription").attribute("Id").value
  rescue
    ui.error("Incorrect publish settings file - " + filename)
    exit 1
  end
end

#pretty_key(key) ⇒ Object



110
111
112
# File 'lib/chef/knife/helpers/azure_base.rb', line 110

def pretty_key(key)
  key.to_s.tr("_", " ").gsub(/\w+/) { |w| w =~ /(ssh)|(aws)/i ? w.upcase : w.capitalize }
end

#serviceObject



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/chef/knife/helpers/azure_base.rb', line 73

def service
  @service ||= begin
                service = Azure::ServiceManagement::ASMInterface.new(
                  azure_subscription_id: config[:azure_subscription_id],
                  azure_mgmt_cert: config[:azure_mgmt_cert],
                  azure_api_host_name: config[:azure_api_host_name],
                  verify_ssl_cert: config[:verify_ssl_cert]
                )
              end
  @service.ui = ui
  @service
end

#validate!(keys) ⇒ Object

validates keys



176
177
178
179
180
181
182
183
184
# File 'lib/chef/knife/helpers/azure_base.rb', line 176

def validate!(keys)
  errors = []
  keys.each do |k|
    if config[k].nil?
      errors << "You did not provide a valid '#{pretty_key(k)}' value. Please set knife[:#{k}] in your knife.rb or pass as an option."
    end
  end
  exit 1 if errors.each { |e| ui.error(e) }.any?
end

#validate_asm_keys!(*keys) ⇒ Object

validate ASM mandatory keys



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/chef/knife/helpers/azure_base.rb', line 187

def validate_asm_keys!(*keys)
  mandatory_keys = %i{azure_subscription_id azure_mgmt_cert azure_api_host_name}
  keys.concat(mandatory_keys)

  unless config[:azure_mgmt_cert].nil?
    config[:azure_mgmt_cert] = File.read find_file(config[:azure_mgmt_cert])
  end

  if !config[:azure_publish_settings_file].nil?
    parse_publish_settings_file(config[:azure_publish_settings_file])
  elsif config[:azure_subscription_id].nil? && config[:azure_mgmt_cert].nil? && config[:azure_api_host_name].nil?
    azureprofile_file = get_azure_profile_file_path
    if File.exist?(File.expand_path(azureprofile_file))
      errors = parse_azure_profile(azureprofile_file, errors)
    end
  end
  validate!(keys)
end

#validate_params!Object

validate command pre-requisites (cli options) (config.length <= 6 && config.length >= 72)



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
# File 'lib/chef/knife/helpers/azure_base.rb', line 116

def validate_params!
  if config[:connection_password] && !config[:connection_password].length.between?(6, 72)
    ui.error("The supplied connection password must be 6-72 characters long and meet password complexity requirements")
    exit 1
  end

  if config[:azure_connect_to_existing_dns] && config[:azure_vm_name].nil?
    ui.error("Specify the VM name using --azure-vm-name option, since you are connecting to existing dns")
    exit 1
  end

  unless !!config[:azure_service_location] ^ !!config[:azure_affinity_group]
    ui.error("Specify either --azure-service-location or --azure-affinity-group")
    exit 1
  end

  unless service.valid_image?(config[:azure_source_image])
    ui.error("Image '#{config[:azure_source_image]}' is invalid")
    exit 1
  end

  # Validate join domain requirements.
  if config[:azure_domain_name] || config[:azure_domain_user]
    if config[:azure_domain_user].nil? || config[:azure_domain_passwd].nil?
      ui.error("Must specify both --azure-domain-user and --azure-domain-passwd.")
      exit 1
    end
  end

  if config[:winrm_ssl] && config[:thumbprint].nil? && config[:winrm_no_verify_cert].nil?
    ui.error("The SSL transport was specified without the --thumbprint option. Specify a thumbprint, or alternatively set the --winrm-no-verify-cert option to skip verification.")
    exit 1
  end

  if config[:extended_logs] && config[:connection_protocol] != "cloud-api"
    ui.error("--extended-logs option only works with --bootstrap-protocol cloud-api")
    exit 1
  end

  if config[:connection_protocol] == "cloud-api" && config[:azure_vm_name].nil? && config[:azure_dns_name].nil?
    ui.error("Specifying the DNS name using --azure-dns-name or VM name using --azure-vm-name option is required with --bootstrap-protocol cloud-api")
    exit 1
  end

  if config[:daemon]
    unless is_image_windows?
      raise ArgumentError, "The daemon option is only supported for Windows nodes."
    end

    unless  config[:connection_protocol] == "cloud-api"
      raise ArgumentError, "The --daemon option requires the use of --bootstrap-protocol cloud-api"
    end

    unless %w{none service task}.include?(config[:daemon].downcase)
      raise ArgumentError, "Invalid value for --daemon option. Valid values are 'none', 'service' and 'task'."
    end
  end
end