Class: Chef::Knife::Configure

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

Constant Summary

Constants inherited from Chef::Knife

CHEF_ORGANIZATION_MANAGEMENT, OFFICIAL_PLUGINS, OPSCODE_HOSTED_CHEF_ACCESS_CONTROL

Instance Attribute Summary collapse

Attributes inherited from Chef::Knife

#name_args, #ui

Instance Method Summary collapse

Methods inherited from Chef::Knife

#api_key, #apply_computed_config, category, chef_config_dir, #cli_keys, common_name, #config_file_settings, config_loader, #config_source, #create_object, #delete_object, dependency_loaders, deps, #format_rest_error, guess_category, #humanize_exception, #humanize_http_exception, inherited, #initialize, list_commands, load_commands, load_config, load_deps, #maybe_setup_fips, #merge_configs, msg, #noauth_rest, #parse_options, reset_config_loader!, reset_subcommands!, #rest, run, #run_with_pretty_exceptions, #server_url, #show_usage, snake_case_name, subcommand_category, subcommand_class_from, subcommand_files, subcommand_loader, subcommands, subcommands_by_category, #test_mandatory_field, ui, unnamed?, use_separate_defaults?, #username

Methods included from Mixin::ConvertToClassName

#constantize, #convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string, #normalize_snake_case_name, #snake_case_basename

Constructor Details

This class inherits a constructor from Chef::Knife

Instance Attribute Details

#admin_client_keyObject (readonly)

Returns the value of attribute admin_client_key.



26
27
28
# File 'lib/chef/knife/configure.rb', line 26

def admin_client_key
  @admin_client_key
end

#admin_client_nameObject (readonly)

Returns the value of attribute admin_client_name.



26
27
28
# File 'lib/chef/knife/configure.rb', line 26

def admin_client_name
  @admin_client_name
end

#chef_repoObject (readonly)

Returns the value of attribute chef_repo.



27
28
29
# File 'lib/chef/knife/configure.rb', line 27

def chef_repo
  @chef_repo
end

#chef_serverObject (readonly)

Returns the value of attribute chef_server.



26
27
28
# File 'lib/chef/knife/configure.rb', line 26

def chef_server
  @chef_server
end

#new_client_keyObject (readonly)

Returns the value of attribute new_client_key.



27
28
29
# File 'lib/chef/knife/configure.rb', line 27

def new_client_key
  @new_client_key
end

#new_client_nameObject (readonly)

Returns the value of attribute new_client_name.



26
27
28
# File 'lib/chef/knife/configure.rb', line 26

def new_client_name
  @new_client_name
end

#validation_client_nameObject (readonly)

Returns the value of attribute validation_client_name.



27
28
29
# File 'lib/chef/knife/configure.rb', line 27

def validation_client_name
  @validation_client_name
end

#validation_keyObject (readonly)

Returns the value of attribute validation_key.



27
28
29
# File 'lib/chef/knife/configure.rb', line 27

def validation_key
  @validation_key
end

Instance Method Details

#ask_user_for_configObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/chef/knife/configure.rb', line 114

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: "#{Chef::Dist::SERVER_CONF_DIR}/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_pathString

Returns the path to the user’s .chef directory.

Returns:

  • (String)

    the path to the user’s .chef directory



138
139
140
# File 'lib/chef/knife/configure.rb', line 138

def chef_config_path
  @chef_config_path ||= Chef::Util::PathHelper.home(".chef")
end

#config_file_pathString

Returns the full path to the config file (credential file).

Returns:

  • (String)

    the full path to the config file (credential file)



143
144
145
# File 'lib/chef/knife/configure.rb', line 143

def config_file_path
  @config_file_path ||= ::File.expand_path(::File.join(chef_config_path, "credentials"))
end

#configure_chefObject



64
65
66
67
68
69
# File 'lib/chef/knife/configure.rb', line 64

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_servernameString

Returns our best guess at what the servername should be using Ohai data and falling back to localhost.

Returns:

  • (String)

    our best guess at what the servername should be using Ohai data and falling back to localhost



131
132
133
134
135
# File 'lib/chef/knife/configure.rb', line 131

def guess_servername
  o = Ohai::System.new
  o.all_plugins(%w{ os hostname fqdn })
  o[:fqdn] || o[:machinename] || o[:hostname] || "localhost"
end

#runObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
112
# File 'lib/chef/knife/configure.rb', line 71

def run
  FileUtils.mkdir_p(chef_config_path)

  ask_user_for_config

  confirm("Overwrite #{config_file_path}") if ::File.exist?(config_file_path)

  ::File.open(config_file_path, "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("Knife configuration file written to #{config_file_path}")
end