Class: Chef::Knife::AzureServerCreate

Inherits:
Chef::Knife show all
Includes:
AzureBase, Knife::WinrmBase
Defined in:
lib/chef/knife/azure_server_create.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from AzureBase

#connection, #find_file, included, #is_image_windows?, #locate_config_value, #msg_pair, #parse_publish_settings_file

Instance Attribute Details

#initial_sleep_delayObject

Returns the value of attribute initial_sleep_delay.



48
49
50
# File 'lib/chef/knife/azure_server_create.rb', line 48

def initial_sleep_delay
  @initial_sleep_delay
end

Instance Method Details

#bootstrap_common_params(bootstrap, server) ⇒ Object



333
334
335
336
337
338
339
340
341
342
# File 'lib/chef/knife/azure_server_create.rb', line 333

def bootstrap_common_params(bootstrap, server)

  bootstrap.config[:run_list] = config[:run_list]
  bootstrap.config[:prerelease] = config[:prerelease]
  bootstrap.config[:bootstrap_version] = locate_config_value(:bootstrap_version)
  bootstrap.config[:distro] = locate_config_value(:distro)
  bootstrap.config[:template_file] = locate_config_value(:template_file)
  load_cloud_attributes_in_hints(server)
  bootstrap
end

#bootstrap_for_node(server, fqdn, port) ⇒ Object



379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# File 'lib/chef/knife/azure_server_create.rb', line 379

def bootstrap_for_node(server,fqdn,port)
  bootstrap = Chef::Knife::Bootstrap.new
  bootstrap.name_args = [fqdn]
  bootstrap.config[:run_list] = config[:run_list]
  bootstrap.config[:ssh_user] = locate_config_value(:ssh_user)
  bootstrap.config[:ssh_password] = locate_config_value(:ssh_password)
  bootstrap.config[:ssh_port] = port
  bootstrap.config[:identity_file] = locate_config_value(:identity_file)
  bootstrap.config[:chef_node_name] = locate_config_value(:chef_node_name) || server.name
  bootstrap.config[:prerelease] = locate_config_value(:prerelease)
  bootstrap.config[:bootstrap_version] = locate_config_value(:bootstrap_version)
  bootstrap.config[:distro] = locate_config_value(:distro)
  bootstrap.config[:use_sudo] = true unless locate_config_value(:ssh_user) == 'root'
  bootstrap.config[:use_sudo_password] = true if bootstrap.config[:use_sudo]
  bootstrap.config[:template_file] = config[:template_file]
  bootstrap.config[:environment] = locate_config_value(:environment)
  # may be needed for vpc_mode
  bootstrap.config[:host_key_verify] = config[:host_key_verify]

  # Load cloud attributes.
  load_cloud_attributes_in_hints(server)

  bootstrap
end

#bootstrap_for_windows_node(server, fqdn, port) ⇒ Object



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
# File 'lib/chef/knife/azure_server_create.rb', line 345

def bootstrap_for_windows_node(server, fqdn, port)
  if locate_config_value(:bootstrap_protocol) == 'winrm'

      load_winrm_deps
      if not Chef::Platform.windows?
        require 'gssapi'
      end

      bootstrap = Chef::Knife::BootstrapWindowsWinrm.new

      bootstrap.config[:winrm_user] = locate_config_value(:winrm_user) || 'Administrator'
      bootstrap.config[:winrm_password] = locate_config_value(:winrm_password)
      bootstrap.config[:winrm_transport] = locate_config_value(:winrm_transport)

      bootstrap.config[:winrm_port] = port

  elsif locate_config_value(:bootstrap_protocol) == 'ssh'
      bootstrap = Chef::Knife::BootstrapWindowsSsh.new
      bootstrap.config[:ssh_user] = locate_config_value(:ssh_user)
      bootstrap.config[:ssh_password] = locate_config_value(:ssh_password)
      bootstrap.config[:ssh_port] = port
      bootstrap.config[:identity_file] = locate_config_value(:identity_file)
      bootstrap.config[:host_key_verify] = locate_config_value(:host_key_verify)
  else
      ui.error("Unsupported Bootstrapping Protocol. Supported : winrm, ssh")
      exit 1
  end
  bootstrap.name_args = [fqdn]
  bootstrap.config[:chef_node_name] = config[:chef_node_name] || server.name
  bootstrap.config[:encrypted_data_bag_secret] = config[:encrypted_data_bag_secret]
  bootstrap.config[:encrypted_data_bag_secret_file] = config[:encrypted_data_bag_secret_file]
  bootstrap_common_params(bootstrap, server)
end

#create_server_defObject



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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
# File 'lib/chef/knife/azure_server_create.rb', line 420

def create_server_def
  server_def = {
    :azure_storage_account => locate_config_value(:azure_storage_account),
    :azure_dns_name => locate_config_value(:azure_dns_name),
    :azure_vm_name => locate_config_value(:azure_vm_name),
    :azure_service_location => locate_config_value(:azure_service_location),
    :azure_os_disk_name => locate_config_value(:azure_os_disk_name),
    :azure_source_image => locate_config_value(:azure_source_image),
    :azure_vm_size => locate_config_value(:azure_vm_size),
    :tcp_endpoints => locate_config_value(:tcp_endpoints),
    :udp_endpoints => locate_config_value(:udp_endpoints),
    :bootstrap_proto => locate_config_value(:bootstrap_protocol),
    :azure_connect_to_existing_dns => locate_config_value(:azure_connect_to_existing_dns)
  }
  # If user is connecting a new VM to an existing dns, then
  # the VM needs to have a unique public port. Logic below takes care of this.
  if !is_image_windows? or locate_config_value(:bootstrap_protocol) == 'ssh'
    port = locate_config_value(:ssh_port) || '22'
    if locate_config_value(:azure_connect_to_existing_dns) && (port == '22')
       port = Random.rand(64000) + 1000
    end
  else
    port = locate_config_value(:winrm_port) || '5985'
    if locate_config_value(:azure_connect_to_existing_dns) && (port == '5985')
        port = Random.rand(64000) + 1000
    end
  end
  server_def[:port] = port

  if is_image_windows?
    server_def[:os_type] = 'Windows'
    if not locate_config_value(:winrm_password) or not locate_config_value(:bootstrap_protocol)
      ui.error("WinRM Password and Bootstrapping Protocol are compulsory parameters")
    end
    server_def[:admin_password] = locate_config_value(:winrm_password)
    server_def[:bootstrap_proto] = locate_config_value(:bootstrap_protocol)
  else
    server_def[:os_type] = 'Linux'
    server_def[:bootstrap_proto] = 'ssh'
    if not locate_config_value(:ssh_user)
      ui.error("SSH User is compulsory parameter")
      exit 1
    end
    unless locate_config_value(:ssh_password) or locate_config_value(:identity_file)
        ui.error("Specify either SSH Key or SSH Password")
        exit 1
    end

    server_def[:ssh_user] = locate_config_value(:ssh_user)
    server_def[:ssh_password] = locate_config_value(:ssh_password)
    server_def[:identity_file] = locate_config_value(:identity_file)
    server_def[:identity_file_passphrase] = locate_config_value(:identity_file_passphrase)
  end
  server_def
end

#load_cloud_attributes_in_hints(server) ⇒ Object



318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/chef/knife/azure_server_create.rb', line 318

def load_cloud_attributes_in_hints(server)
  # Modify global configuration state to ensure hint gets set by knife-bootstrap
  # Query azure and load necessary attributes.
  cloud_attributes = {}
  cloud_attributes["public_ip"] = server.publicipaddress
  cloud_attributes["vm_name"] = server.name
  cloud_attributes["public_fqdn"] = server.hostedservicename.to_s + ".cloudapp.net"
  cloud_attributes["public_ssh_port"] = server.sshport if server.sshport
  cloud_attributes["public_winrm_port"] = server.winrmport if server.winrmport

  Chef::Config[:knife][:hints] ||= {}
  Chef::Config[:knife][:hints]["azure"] ||= cloud_attributes

end

#load_winrm_depsObject



39
40
41
42
43
44
# File 'lib/chef/knife/azure_server_create.rb', line 39

def load_winrm_deps
  require 'winrm'
  require 'em-winrm'
  require 'chef/knife/winrm'
  require 'chef/knife/bootstrap_windows_winrm'
end

#random_string(len = 10) ⇒ Object



194
195
196
# File 'lib/chef/knife/azure_server_create.rb', line 194

def random_string(len=10)
  (0...len).map{65.+(rand(25)).chr}.join
end

#runObject



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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/chef/knife/azure_server_create.rb', line 247

def run
  $stdout.sync = true
  storage = nil

  Chef::Log.info("validating...")
  validate!

  Chef::Log.info("creating...")

  if not locate_config_value(:azure_vm_name)
    config[:azure_vm_name] = locate_config_value(:azure_dns_name)
  end

  #If Storage Account is not specified, check if the geographic location has one to re-use
  if not locate_config_value(:azure_storage_account)
    storage_accts = connection.storageaccounts.all
    storage = storage_accts.find { |storage_acct| storage_acct.location.to_s == locate_config_value(:azure_service_location) }
    if not storage
      config[:azure_storage_account] = [strip_non_ascii(locate_config_value(:azure_vm_name)), random_string].join.downcase
    else
      config[:azure_storage_account] = storage.name.to_s
    end
  end

  server = connection.deploys.create(create_server_def)
  fqdn = server.publicipaddress

  puts("\n")
  if is_image_windows?
    if locate_config_value(:bootstrap_protocol) == 'ssh'
      port = server.sshport
      print "\n#{ui.color("Waiting for sshd on #{fqdn}:#{port}", :magenta)}"

      print(".") until tcp_test_ssh(fqdn,port) {
        sleep @initial_sleep_delay ||= 10
        puts("done")
     }

    elsif locate_config_value(:bootstrap_protocol) == 'winrm'
      port = server.winrmport

      print "\n#{ui.color("Waiting for winrm on #{fqdn}:#{port}", :magenta)}"

      print(".") until tcp_test_winrm(fqdn,port) {
        sleep @initial_sleep_delay ||= 10
        puts("done")
     }

    end
    sleep 15
    bootstrap_for_windows_node(server,fqdn, port).run
  else
    unless server && server.publicipaddress && server.sshport
      Chef::Log.fatal("server not created")
    exit 1
  end

  port = server.sshport

  print "\n#{ui.color("Waiting for sshd on #{fqdn}:#{port}", :magenta)}"

  print(".") until tcp_test_ssh(fqdn,port) {
    sleep @initial_sleep_delay ||= 10
    puts("done")
  }

  sleep 15
    bootstrap_for_node(server,fqdn,port).run
  end
end

#strip_non_ascii(string) ⇒ Object



190
191
192
# File 'lib/chef/knife/azure_server_create.rb', line 190

def strip_non_ascii(string)
  string.gsub(/[^0-9a-z ]/i, '')
end

#tcp_test_ssh(fqdn, sshport) ⇒ Object



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
# File 'lib/chef/knife/azure_server_create.rb', line 220

def tcp_test_ssh(fqdn, sshport)
  tcp_socket = TCPSocket.new(fqdn, sshport)
  readable = IO.select([tcp_socket], nil, nil, 5)
  if readable
    Chef::Log.debug("sshd accepting connections on #{fqdn}, banner is #{tcp_socket.gets}")
    yield
    true
  else
    false
  end
rescue SocketError
  sleep 2
  false
rescue Errno::ETIMEDOUT
  false
rescue Errno::EPERM
  false
rescue Errno::ECONNREFUSED
  sleep 2
  false
rescue Errno::EHOSTUNREACH
  sleep 2
  false
ensure
  tcp_socket && tcp_socket.close
end

#tcp_test_winrm(ip_addr, port) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/chef/knife/azure_server_create.rb', line 198

def tcp_test_winrm(ip_addr, port)
	    hostname = ip_addr
  socket = TCPSocket.new(hostname, port)
	    return true
rescue SocketError
  sleep 2
  false
rescue Errno::ETIMEDOUT
  false
rescue Errno::EPERM
  false
rescue Errno::ECONNREFUSED
  sleep 2
  false
rescue Errno::EHOSTUNREACH
  sleep 2
  false
rescue Errno::ENETUNREACH
  sleep 2
  false
end

#validate!Object



404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
# File 'lib/chef/knife/azure_server_create.rb', line 404

def validate!
  super([
        :azure_subscription_id,
        :azure_mgmt_cert,
        :azure_api_host_name,
        :azure_dns_name,
        :azure_service_location,
        :azure_source_image,
        :azure_vm_size,
  ])
  if locate_config_value(:azure_connect_to_existing_dns) && locate_config_value(: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
end