Class: Chef::Knife::Bootstrap
Defined Under Namespace
Classes: ChefVaultHandler, ClientBuilder
Instance Attribute Summary collapse
Attributes inherited from Chef::Knife
#name_args, #ui
Instance Method Summary
collapse
#encryption_secret_provided?, #encryption_secret_provided_ignore_encrypt_flag?, included, #read_secret, #validate_secrets
#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
#constantize, #convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string, #snake_case_basename
#enforce_path_sanity
Constructor Details
#initialize(argv = []) ⇒ Bootstrap
Returns a new instance of Bootstrap.
222
223
224
225
226
227
228
229
230
231
232
233
|
# File 'lib/chef/knife/bootstrap.rb', line 222
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_handler ⇒ Object
Returns the value of attribute chef_vault_handler.
32
33
34
|
# File 'lib/chef/knife/bootstrap.rb', line 32
def chef_vault_handler
@chef_vault_handler
end
|
#client_builder ⇒ Object
Returns the value of attribute client_builder.
31
32
33
|
# File 'lib/chef/knife/bootstrap.rb', line 31
def client_builder
@client_builder
end
|
Instance Method Details
#bootstrap_context ⇒ Object
295
296
297
298
299
300
301
302
|
# File 'lib/chef/knife/bootstrap.rb', line 295
def bootstrap_context
@bootstrap_context ||= Knife::Core::BootstrapContext.new(
config,
config[:run_list],
Chef::Config,
secret
)
end
|
#bootstrap_template ⇒ Object
252
253
254
255
256
257
|
# File 'lib/chef/knife/bootstrap.rb', line 252
def bootstrap_template
config[:bootstrap_template] || config[:template_file] || config[:distro] || default_bootstrap_template
end
|
#default_bootstrap_template ⇒ String
The default bootstrap template to use to bootstrap a server This is a public API hook which knife plugins use or inherit and override.
239
240
241
|
# File 'lib/chef/knife/bootstrap.rb', line 239
def default_bootstrap_template
"chef-full"
end
|
#find_template ⇒ Object
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
|
# File 'lib/chef/knife/bootstrap.rb', line 259
def find_template
template = bootstrap_template
if File.exists?(template)
Chef::Log.debug("Using the specified bootstrap template: #{File.dirname(template)}")
return template
end
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
Chef::Util::PathHelper.home('.chef', 'bootstrap', "#{template}.erb") {|p| bootstrap_files << p}
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_ssh ⇒ Object
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
|
# File 'lib/chef/knife/bootstrap.rb', line 354
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_auth ⇒ Object
370
371
372
373
374
375
|
# File 'lib/chef/knife/bootstrap.rb', line 370
def knife_ssh_with_password_auth
ssh = knife_ssh
ssh.config[:identity_file] = nil
ssh.config[:ssh_password] = ssh.get_password
ssh
end
|
#render_template ⇒ Object
304
305
306
307
308
|
# File 'lib/chef/knife/bootstrap.rb', line 304
def render_template
template_file = find_template
template = IO.read(template_file).chomp
Erubis::Eruby.new(template).evaluate(bootstrap_context)
end
|
#run ⇒ Object
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
342
|
# File 'lib/chef/knife/bootstrap.rb', line 310
def run
validate_name_args!
$stdout.sync = true
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
|
#secret ⇒ Object
291
292
293
|
# File 'lib/chef/knife/bootstrap.rb', line 291
def secret
@secret ||= encryption_secret_provided_ignore_encrypt_flag? ? read_secret : nil
end
|
#server_name ⇒ String
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.
248
249
250
|
# File 'lib/chef/knife/bootstrap.rb', line 248
def server_name
Array(@name_args).first
end
|
#ssh_command ⇒ Object
377
378
379
380
381
382
383
384
385
|
# File 'lib/chef/knife/bootstrap.rb', line 377
def ssh_command
command = render_template
if config[:use_sudo]
command = config[:use_sudo_password] ? "echo '#{config[:ssh_password]}' | sudo -SH #{command}" : "sudo -H #{command}"
end
command
end
|
#validate_name_args! ⇒ Object
344
345
346
347
348
349
350
351
352
|
# File 'lib/chef/knife/bootstrap.rb', line 344
def validate_name_args!
if server_name.nil?
ui.error("Must pass an FQDN or ip to bootstrap")
exit 1
elsif server_name == "windows"
ui.warn("Hostname containing 'windows' specified. Please install 'knife-windows' if you are attempting to bootstrap a Windows node via WinRM.")
end
end
|