class Chef::Knife::Setup

Public Instance Methods

bootstrap_client() click to toggle source
# File lib/chef/knife/knife-setup.rb, line 167
def bootstrap_client
  begin
    knife_ssh(ssh_command).run
  rescue Net::SSH::AuthenticationFailed
    unless config[:ssh_password]
      puts "Failed to authenticate #{config[:ssh_user]} - trying password auth"
      knife_ssh_with_password_auth(ssh_command).run
    end
  end
end
get_node() click to toggle source
# File lib/chef/knife/knife-setup.rb, line 146
def get_node
  node = Chef::Node.load(server_name)
  if node.nil?
    ui.error("Could not find a node named #{@server_name}")
    exit 1
  end
  return node
end
knife_ssh(command) click to toggle source
# File lib/chef/knife/knife-setup.rb, line 207
def knife_ssh(command)
  ssh = Chef::Knife::Ssh.new
  ssh.ui = ui
  ssh.name_args = [ @node_name, 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[:identity_file] = config[:identity_file]
  ssh.config[:manual] = true
  ssh.config[:no_host_key_verify] = config[:no_host_key_verify]
  ssh.config[:on_error] = :raise
  ssh
end
knife_ssh_with_password_auth(command) click to toggle source
# File lib/chef/knife/knife-setup.rb, line 221
def knife_ssh_with_password_auth(command)
  ssh = knife_ssh(command)
  ssh.config[:identity_file] = nil
  ssh.config[:ssh_password] = ssh.get_password
  ssh
end
load_template(template=nil) click to toggle source
# File lib/chef/knife/knife-setup.rb, line 113
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(@@chef_config_dir, "bootstrap", "#{config[:distro]}.erb")
    bootstrap_files << File.join(ENV['HOME'], '.chef', 'bootstrap', "#{config[:distro]}.erb")
    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

  ui.info("Found bootstrap template in #{File.dirname(template)}")

  IO.read(template).chomp
end
post() click to toggle source
# File lib/chef/knife/knife-setup.rb, line 238
def post
  url = Chef::Config[:post_url]
  message = {
    :fqdn    => get_node.to_hash["fqdn"]
  }
  body = JSON.generate(message)
  uri = URI.parse(url)
  request = Net::HTTP::Post.new(uri.request_uri)
  http = Net::HTTP.new(uri.host, uri.port)
  request.add_field('Content-Type', 'application/json-rpc')
  request.body = body
  response = http.request(request)
  raise "HTTP Error: #{response.code} on #{url}" unless response.code == "200" || response.code == "204"
end
render_template(template=nil) click to toggle source
# File lib/chef/knife/knife-setup.rb, line 141
def render_template(template=nil)
  context = Knife::Core::BootstrapContext.new(config, config[:run_list], Chef::Config)
  Erubis::Eruby.new(template).evaluate(context)
end
run() click to toggle source
# File lib/chef/knife/knife-setup.rb, line 178
def run

  validate_name_args!
  @node_name = Array(@name_args).first
  # back compat--templates may use this setting:
  config[:server_name] = @node_name

  $stdout.sync = true

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

  bootstrap_client unless config[:nobootstrap]
  set_run_list(get_node, config[:run_list]) if config[:run_list]
  set_env(get_node, config[:environment]) unless config[:environment] == "_default"
  knife_ssh("chef-client").run if config[:norunchef]
  post if Chef::Config[:post_url]
end
server_name() click to toggle source
# File lib/chef/knife/knife-setup.rb, line 203
def server_name
  config[:chef_node_name] || Array(@name_args).first
end
set_env(node, env) click to toggle source
# File lib/chef/knife/knife-setup.rb, line 161
def set_env(node, env)
  ui.info("Setting environment to #{env} for #{node.name}")
  node.chef_environment(env)
  node.save
end
set_run_list(node, run_list) click to toggle source
# File lib/chef/knife/knife-setup.rb, line 155
def set_run_list(node, run_list)
  ui.info("Setting run_list to #{run_list.inspect} for #{node.name}")
  node.run_list.reset!(run_list)
  node.save
end
ssh_command() click to toggle source
# File lib/chef/knife/knife-setup.rb, line 228
def ssh_command
  command = render_template(load_template(config[:bootstrap_template]))

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

  command
end
validate_name_args!() click to toggle source
# File lib/chef/knife/knife-setup.rb, line 196
def validate_name_args!
  if Array(@name_args).first.nil?
    ui.error("Must pass an FQDN or ip to bootstrap")
    exit 1
  end
end