Class: Chef::Knife::Bootstrap

Inherits:
Chef::Knife show all
Includes:
DataBagSecretOptions
Defined in:
lib/chef/knife/bootstrap.rb,
lib/chef/knife/bootstrap/client_builder.rb,
lib/chef/knife/bootstrap/chef_vault_handler.rb

Defined Under Namespace

Classes: ChefVaultHandler, ClientBuilder

Instance Attribute Summary collapse

Attributes inherited from Chef::Knife

#name_args, #ui

Instance Method Summary collapse

Methods included from DataBagSecretOptions

#encryption_secret_provided?, #encryption_secret_provided_ignore_encrypt_flag?, included, #read_secret, #validate_secrets

Methods included from EncryptedDataBagItem::CheckEncrypted

#encrypted?

Methods inherited from Chef::Knife

#api_key, #apply_computed_config, category, chef_config_dir, common_name, #config_file_settings, config_loader, #configure_chef, #create_object, #delete_object, dependency_loaders, deps, #format_rest_error, guess_category, #humanize_exception, #humanize_http_exception, inherited, list_commands, load_commands, load_config, load_deps, #merge_configs, msg, #noauth_rest, #parse_options, reset_config_loader!, reset_subcommands!, #rest, run, #run_with_pretty_exceptions, #server_url, #show_usage, snake_case_name, subcommand_category, subcommand_class_from, subcommand_loader, subcommands, subcommands_by_category, ui, unnamed?, use_separate_defaults?, #username

Methods included from Mixin::ConvertToClassName

#constantize, #convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string, #snake_case_basename

Methods included from Mixin::PathSanity

#enforce_path_sanity

Constructor Details

#initialize(argv = []) ⇒ Bootstrap

Returns a new instance of Bootstrap.



221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/chef/knife/bootstrap.rb', line 221

def initialize(argv=[])
  super
  @client_builder = Chef::Knife::Bootstrap::ClientBuilder.new(
    chef_config: Chef::Config,
    knife_config: config,
    ui: ui,
  )
  @chef_vault_handler = Chef::Knife::Bootstrap::ChefVaultHandler.new(
    knife_config: config,
    ui: ui
  )
end

Instance Attribute Details

#chef_vault_handlerObject

Returns the value of attribute chef_vault_handler.



31
32
33
# File 'lib/chef/knife/bootstrap.rb', line 31

def chef_vault_handler
  @chef_vault_handler
end

#client_builderObject

Returns the value of attribute client_builder.



30
31
32
# File 'lib/chef/knife/bootstrap.rb', line 30

def client_builder
  @client_builder
end

Instance Method Details

#bootstrap_contextObject



294
295
296
297
298
299
300
301
# File 'lib/chef/knife/bootstrap.rb', line 294

def bootstrap_context
  @bootstrap_context ||= Knife::Core::BootstrapContext.new(
    config,
    config[:run_list],
    Chef::Config,
    secret
  )
end

#bootstrap_templateObject



251
252
253
254
255
256
# File 'lib/chef/knife/bootstrap.rb', line 251

def bootstrap_template
  # The order here is important. We want to check if we have the new Chef 12 option is set first.
  # Knife cloud plugins unfortunately all set a default option for the :distro so it should be at
  # the end.
  config[:bootstrap_template] || config[:template_file] || config[:distro] || default_bootstrap_template
end

#default_bootstrap_templateString

The default bootstrap template to use to bootstrap a server This is a public API hook which knife plugins use or inherit and override.

Returns:

  • (String)

    Default bootstrap template



238
239
240
# File 'lib/chef/knife/bootstrap.rb', line 238

def default_bootstrap_template
  "chef-full"
end

#find_templateObject



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

def find_template
  template = bootstrap_template

  # Use the template directly if it's a path to an actual file
  if File.exists?(template)
    Chef::Log.debug("Using the specified bootstrap template: #{File.dirname(template)}")
    return template
  end

  # Otherwise search the template directories until we find the right one
  bootstrap_files = []
  bootstrap_files << File.join(File.dirname(__FILE__), 'bootstrap/templates', "#{template}.erb")
  bootstrap_files << File.join(Knife.chef_config_dir, "bootstrap", "#{template}.erb") if Chef::Knife.chef_config_dir
  bootstrap_files << File.join(ENV['HOME'], '.chef', 'bootstrap', "#{template}.erb") if ENV['HOME']
  bootstrap_files << Gem.find_files(File.join("chef","knife","bootstrap","#{template}.erb"))
  bootstrap_files.flatten!

  template_file = Array(bootstrap_files).find do |bootstrap_template|
    Chef::Log.debug("Looking for bootstrap template in #{File.dirname(bootstrap_template)}")
    File.exists?(bootstrap_template)
  end

  unless template_file
    ui.info("Can not find bootstrap definition for #{template}")
    raise Errno::ENOENT
  end

  Chef::Log.debug("Found bootstrap template in #{File.dirname(template_file)}")

  template_file
end

#knife_sshObject



353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# File 'lib/chef/knife/bootstrap.rb', line 353

def knife_ssh
  ssh = Chef::Knife::Ssh.new
  ssh.ui = ui
  ssh.name_args = [ server_name, ssh_command ]
  ssh.config[:ssh_user] = config[:ssh_user]
  ssh.config[:ssh_password] = config[:ssh_password]
  ssh.config[:ssh_port] = config[:ssh_port]
  ssh.config[:ssh_gateway] = config[:ssh_gateway]
  ssh.config[:forward_agent] = config[:forward_agent]
  ssh.config[:identity_file] = config[:identity_file]
  ssh.config[:manual] = true
  ssh.config[:host_key_verify] = config[:host_key_verify]
  ssh.config[:on_error] = :raise
  ssh
end

#knife_ssh_with_password_authObject



369
370
371
372
373
374
# File 'lib/chef/knife/bootstrap.rb', line 369

def knife_ssh_with_password_auth
  ssh = knife_ssh
  ssh.config[:identity_file] = nil
  ssh.config[:ssh_password] = ssh.get_password
  ssh
end

#render_templateObject



303
304
305
306
307
# File 'lib/chef/knife/bootstrap.rb', line 303

def render_template
  template_file = find_template
  template = IO.read(template_file).chomp
  Erubis::Eruby.new(template).evaluate(bootstrap_context)
end

#runObject



309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/chef/knife/bootstrap.rb', line 309

def run
  validate_name_args!

  $stdout.sync = true

  # chef-vault integration must use the new client-side hawtness, otherwise to use the
  # new client-side hawtness, just delete your validation key.
  if chef_vault_handler.doing_chef_vault? || 
      (Chef::Config[:validation_key] && !File.exist?(File.expand_path(Chef::Config[:validation_key])))
    client_builder.run

    chef_vault_handler.run(node_name: config[:chef_node_name])

    bootstrap_context.client_pem = client_builder.client_path
  else
    ui.info("Doing old-style registration with the validation key at #{Chef::Config[:validation_key]}...")
    ui.info("Delete your validation key in order to use your user credentials instead")
    ui.info("")
  end

  ui.info("Connecting to #{ui.color(server_name, :bold)}")

  begin
    knife_ssh.run
  rescue Net::SSH::AuthenticationFailed
    if config[:ssh_password]
      raise
    else
      ui.info("Failed to authenticate #{config[:ssh_user]} - trying password auth")
      knife_ssh_with_password_auth.run
    end
  end
end

#secretObject



290
291
292
# File 'lib/chef/knife/bootstrap.rb', line 290

def secret
  @secret ||= encryption_secret_provided_ignore_encrypt_flag? ? read_secret : nil
end

#server_nameString

The server_name is the DNS or IP we are going to connect to, it is not necessarily the node name, the fqdn, or the hostname of the server. This is a public API hook which knife plugins use or inherit and override.

Returns:

  • (String)

    The DNS or IP that bootstrap will connect to



247
248
249
# File 'lib/chef/knife/bootstrap.rb', line 247

def server_name
  Array(@name_args).first
end

#ssh_commandObject



376
377
378
379
380
381
382
383
384
# File 'lib/chef/knife/bootstrap.rb', line 376

def ssh_command
  command = render_template

  if config[:use_sudo]
    command = config[:use_sudo_password] ? "echo '#{config[:ssh_password]}' | sudo -S #{command}" : "sudo #{command}"
  end

  command
end

#validate_name_args!Object



343
344
345
346
347
348
349
350
351
# File 'lib/chef/knife/bootstrap.rb', line 343

def validate_name_args!
  if server_name.nil?
    ui.error("Must pass an FQDN or ip to bootstrap")
    exit 1
  elsif server_name == "windows"
    # catches "knife bootstrap windows" when that command is not installed
    ui.warn("Hostname containing 'windows' specified. Please install 'knife-windows' if you are attempting to bootstrap a Windows node via WinRM.")
  end
end