# class Chef::Knife::Bootstrap

### Public Instance Methods

find_template(template=nil) click to toggle source

    # File lib/chef/knife/bootstrap.rb, line 157
    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_ssh() click to toggle source

    # File lib/chef/knife/bootstrap.rb, line 227
    def knife_ssh
      ssh = Chef::Knife::Ssh.new
      ssh.ui = ui
      ssh.name_args = [ server_name, ssh_command ]
      ssh.config[:ssh_user] = Chef::Config[:knife][: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[:forward_agent] = Chef::Config[:knife][:forward_agent] || config[:forward_agent]
      ssh.config[:identity_file] = Chef::Config[:knife][:identity_file] || config[:identity_file]
      ssh.config[:manual] = true
      ssh.config[:host_key_verify] = Chef::Config[:knife][:host_key_verify] || config[:host_key_verify]
      ssh.config[:on_error] = :raise
      ssh
    end

knife_ssh_with_password_auth() click to toggle source

    # File lib/chef/knife/bootstrap.rb, line 243
    def knife_ssh_with_password_auth
      ssh = knife_ssh
      ssh.config[:identity_file] = nil
      ssh.config[:ssh_password] = ssh.get_password
      ssh
    end

read_template() click to toggle source

    # File lib/chef/knife/bootstrap.rb, line 190
    def read_template
      IO.read(@template_file).chomp
    end

render_template(template=nil) click to toggle source

    # File lib/chef/knife/bootstrap.rb, line 185
    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/bootstrap.rb, line 194
    def run
      validate_name_args!
      warn_chef_config_secret_key
      @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("Connecting to #{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_name() click to toggle source

    # File lib/chef/knife/bootstrap.rb, line 223
    def server_name
      Array(@name_args).first
    end

ssh_command() click to toggle source

    # File lib/chef/knife/bootstrap.rb, line 250
    def ssh_command
      command = render_template(read_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!() click to toggle source

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

warn_chef_config_secret_key() click to toggle source

    # File lib/chef/knife/bootstrap.rb, line 260
          def warn_chef_config_secret_key
            unless Chef::Config[:encrypted_data_bag_secret].nil?
              ui.warn "* " * 40
              ui.warn("Specifying the encrypted data bag secret key using an 'encrypted_data_bag_secret'
    entry in 'knife.rb' is deprecated. Please see CHEF-4011 for more details. You
    can supress this warning and still distribute the secret key to all bootstrapped
    machines by adding the following to your 'knife.rb' file:

      knife[:secret_file] = "/path/to/your/secret"

    If you would like to selectively distribute a secret key during bootstrap
    please use the '--secret' or '--secret-file' options of this command instead.

    #{ui.color('IMPORTANT:', :red, :bold)} In a future version of Chef, this
    behavior will be removed and any 'encrypted_data_bag_secret' entries in
    'knife.rb' will be ignored completely.
    ")
              ui.warn "* " * 40
            end
          end