Class: Chef::Knife::Bootstrap

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

Instance Attribute Summary

Attributes inherited from Chef::Knife

#name_args, #ui

Instance Method Summary collapse

Methods inherited from Chef::Knife

#api_key, category, common_name, #configure_chef, #create_object, #delete_object, deps, #format_rest_error, guess_category, #highlight_config_error, #humanize_exception, #humanize_http_exception, inherited, #initialize, list_commands, load_commands, load_deps, msg, #noauth_rest, #parse_options, #read_config_file, 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?, #username

Methods included from Mixin::ConvertToClassName

#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

This class inherits a constructor from Chef::Knife

Instance Method Details

#find_template(template = nil) ⇒ Object



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

def find_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(Knife.chef_config_dir, "bootstrap", "#{config[:distro]}.erb") if Knife.chef_config_dir
    bootstrap_files << File.join(ENV['HOME'], '.chef', 'bootstrap', "#{config[:distro]}.erb") if ENV['HOME']
    bootstrap_files << Gem.find_files(File.join("chef","knife","bootstrap","#{config[:distro]}.erb"))
    bootstrap_files.flatten!
  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
    ui.info("Can not find bootstrap definition for #{config[:distro]}")
    raise Errno::ENOENT
  end

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

  template
end

#knife_sshObject



200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/chef/knife/bootstrap.rb', line 200

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] = Chef::Config[:knife][:ssh_port] || config[:ssh_port]
  ssh.config[:ssh_gateway] = Chef::Config[:knife][:ssh_gateway] || config[:ssh_gateway]
  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



215
216
217
218
219
220
# File 'lib/chef/knife/bootstrap.rb', line 215

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

#read_templateObject



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

def read_template
  IO.read(@template_file).chomp
end

#render_template(template = nil) ⇒ Object



159
160
161
162
# File 'lib/chef/knife/bootstrap.rb', line 159

def render_template(template=nil)
  context = Knife::Core::BootstrapContext.new(config, config[:run_list], Chef::Config)
  Erubis::Eruby.new(template).evaluate(context)
end

#runObject



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/chef/knife/bootstrap.rb', line 168

def run
  validate_name_args!
  @template_file = find_template(config[:bootstrap_template])
  @node_name = Array(@name_args).first
  # back compat--templates may use this setting:
  config[:server_name] = @node_name
  
  $stdout.sync = true

  ui.info("Bootstrapping Chef on #{ui.color(@node_name, :bold)}")

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

#server_nameObject



196
197
198
# File 'lib/chef/knife/bootstrap.rb', line 196

def server_name
  Array(@name_args).first
end

#ssh_commandObject



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

def ssh_command
  command = render_template(read_template)

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

  command
end

#validate_name_args!Object



189
190
191
192
193
194
# File 'lib/chef/knife/bootstrap.rb', line 189

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