Class: Chef::Knife::Bootstrap

Inherits:
Chef::Knife show all
Defined in:
lib/chef/knife/bootstrap.rb

Direct Known Subclasses

WindowsBootstrap

Constant Summary

Constants inherited from Chef::Knife

DEFAULT_SUBCOMMAND_FILES

Instance Attribute Summary

Attributes inherited from Chef::Knife

#name_args

Instance Method Summary collapse

Methods inherited from Chef::Knife

#ask_question, #bulk_delete, category, #configure_chef, #confirm, #create_object, #delete_object, #edit_data, #edit_object, #file_exists_and_is_readable?, #format_for_display, #format_list_for_display, guess_category, inherited, #initialize, list_commands, load_commands, #load_from_file, #msg, msg, #output, #parse_options, #pretty_print, #rest, run, #show_usage, snake_case_name, #stdin, #stdout, subcommand_category, subcommand_class_from, subcommands, subcommands_by_category, unnamed?

Methods included from Mixin::ConvertToClassName

#convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string, #snake_case_basename

Constructor Details

This class inherits a constructor from Chef::Knife

Instance Method Details

#hObject



83
84
85
# File 'lib/chef/knife/bootstrap.rb', line 83

def h
  @highline ||= HighLine.new
end

#knife_sshObject



150
151
152
153
154
155
156
157
158
159
# File 'lib/chef/knife/bootstrap.rb', line 150

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

#knife_ssh_with_password_authObject



161
162
163
164
165
166
# File 'lib/chef/knife/bootstrap.rb', line 161

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

#load_template(template = nil) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/chef/knife/bootstrap.rb', line 87

def load_template(template=nil)
  # Are we bootstrapping using an already shipped template?
  if config[:template_file]
    bootstrap_files = config[:template_file]
  else
    bootstrap_files = []
    bootstrap_files << File.join(File.dirname(__FILE__), 'bootstrap', "#{config[:distro]}.erb")
    bootstrap_files << File.join(Dir.pwd, ".chef", "bootstrap", "#{config[:distro]}.erb")
    bootstrap_files << File.join(ENV['HOME'], '.chef', 'bootstrap', "#{config[:distro]}.erb")
  end

  template = 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
    Chef::Log.info("Can not find bootstrap definition for #{config[:distro]}")
    raise Errno::ENOENT
  end

  Chef::Log.debug("Found bootstrap template in #{File.dirname(template)}")
  
  IO.read(template).chomp
end

#render_template(template = nil) ⇒ Object



113
114
115
116
117
118
# File 'lib/chef/knife/bootstrap.rb', line 113

def render_template(template=nil)
  context = {}
  context[:run_list] = config[:run_list]
  context[:config] = config
  Erubis::Eruby.new(template).evaluate(context)
end

#runObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/chef/knife/bootstrap.rb', line 120

def run 
  require 'highline'

  validate_name_args!

  $stdout.sync = true

  Chef::Log.info("Bootstrapping Chef on #{h.color(config[:server_name], :bold)}")

  begin
    knife_ssh.run
  rescue Net::SSH::AuthenticationFailed
    unless config[:ssh_password]
      puts "Failed to authenticate #{config[:ssh_user]} - trying password auth"
      knife_ssh_with_password_auth.run
    end
  end
end

#server_nameObject



146
147
148
# File 'lib/chef/knife/bootstrap.rb', line 146

def server_name
  Array(@name_args).first
end

#ssh_commandObject



168
169
170
171
172
173
174
175
176
# File 'lib/chef/knife/bootstrap.rb', line 168

def ssh_command
  command = render_template(load_template(config[:bootstrap_template]))

  if config[:use_sudo]
    command = "sudo #{command}"
  end

  command
end

#validate_name_args!Object



139
140
141
142
143
144
# File 'lib/chef/knife/bootstrap.rb', line 139

def validate_name_args!
  if Array(@name_args).first.nil?
    Chef::Log.error("Must pass an FQDN or ip to bootstrap")
    exit 1
  end
end