class Chef::Knife::Configure

Attributes

admin_client_key[R]
admin_client_name[R]
chef_repo[R]
chef_server[R]
new_client_key[R]
new_client_name[R]
validation_client_name[R]
validation_key[R]

Public Instance Methods

ask_user_for_config() click to toggle source
# File lib/chef/knife/configure.rb, line 116
def ask_user_for_config
  server_name = guess_servername
  @chef_server = config[:chef_server_url] || ask_question("Please enter the chef server URL: ", :default => "https://#{server_name}/organizations/myorg")
  if config[:initial]
    @new_client_name        = config[:node_name] || ask_question("Please enter a name for the new user: ", :default => Etc.getlogin)
    @admin_client_name      = config[:admin_client_name] || ask_question("Please enter the existing admin name: ", :default => "admin")
    @admin_client_key       = config[:admin_client_key] || ask_question("Please enter the location of the existing admin's private key: ", :default => "/etc/chef-server/admin.pem")
    @admin_client_key       = File.expand_path(@admin_client_key)
  else
    @new_client_name        = config[:node_name] || ask_question("Please enter an existing username or clientname for the API: ", :default => Etc.getlogin)
  end

  @new_client_key = config[:client_key] || File.join(chef_config_path, "#{@new_client_name}.pem")
  @new_client_key = File.expand_path(@new_client_key)
end
chef_config_path() click to toggle source
# File lib/chef/knife/configure.rb, line 138
def chef_config_path
  Chef::Util::PathHelper.home(".chef")
end
configure_chef() click to toggle source
Calls superclass method
# File lib/chef/knife/configure.rb, line 63
def configure_chef
  # We are just faking out the system so that you can do this without a key specified
  Chef::Config[:node_name] = "woot"
  super
  Chef::Config[:node_name] = nil
end
guess_servername() click to toggle source
# File lib/chef/knife/configure.rb, line 132
def guess_servername
  o = Ohai::System.new
  o.all_plugins(%w{ os hostname fqdn })
  o[:fqdn] || o[:machinename] || o[:hostname] || "localhost"
end
run() click to toggle source
# File lib/chef/knife/configure.rb, line 70
      def run
        FileUtils.mkdir_p(chef_config_path)
        config_file = File.join(chef_config_path, "credentials")

        ask_user_for_config

        config_file = File.expand_path(config_file)
        if File.exist?(config_file)
          confirm("Overwrite #{config_file}?")
        end
        ::File.open(config_file, "w") do |f|
          f.puts <<-EOH
[default]
client_name     = '#{new_client_name}'
client_key      = '#{new_client_key}'
chef_server_url = '#{chef_server}'
EOH
        end

        if config[:initial]
          ui.msg("Creating initial API user...")
          Chef::Config[:chef_server_url] = chef_server
          Chef::Config[:node_name] = admin_client_name
          Chef::Config[:client_key] = admin_client_key
          user_create = Chef::Knife::UserCreate.new
          user_create.name_args = [ new_client_name ]
          user_create.config[:user_password] = config[:user_password] ||
            ui.ask("Please enter a password for the new user: ") { |q| q.echo = false }
          user_create.config[:admin] = true
          user_create.config[:file] = new_client_key
          user_create.config[:yes] = true
          user_create.config[:disable_editing] = true
          user_create.run
        else
          ui.msg("*****")
          ui.msg("")
          ui.msg("You must place your client key in:")
          ui.msg("  #{new_client_key}")
          ui.msg("Before running commands with Knife")
          ui.msg("")
          ui.msg("*****")
        end

        ui.msg("Configuration file written to #{config[:config_file]}")
      end