Class: Chef::Knife::Configure

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

Instance Attribute Summary collapse

Attributes inherited from Chef::Knife

#name_args

Instance Method Summary collapse

Methods inherited from Chef::Knife

#ask_question, build_sub_class, #bulk_delete, #confirm, #create_object, #delete_object, #edit_data, #edit_object, #file_exists_and_is_readable?, find_command, #format_for_display, #format_list_for_display, list_commands, load_commands, #load_from_file, #output, #pretty_print, #rest, #stdin, #stdout

Methods included from Mixin::ConvertToClassName

#convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string, #snake_case_basename

Instance Attribute Details

#admin_client_keyObject (readonly)

Returns the value of attribute admin_client_key.



24
25
26
# File 'lib/chef/knife/configure.rb', line 24

def admin_client_key
  @admin_client_key
end

#admin_client_nameObject (readonly)

Returns the value of attribute admin_client_name.



24
25
26
# File 'lib/chef/knife/configure.rb', line 24

def admin_client_name
  @admin_client_name
end

#chef_repoObject (readonly)

Returns the value of attribute chef_repo.



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

def chef_repo
  @chef_repo
end

#chef_serverObject (readonly)

Returns the value of attribute chef_server.



24
25
26
# File 'lib/chef/knife/configure.rb', line 24

def chef_server
  @chef_server
end

#new_client_keyObject (readonly)

Returns the value of attribute new_client_key.



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

def new_client_key
  @new_client_key
end

#new_client_nameObject (readonly)

Returns the value of attribute new_client_name.



24
25
26
# File 'lib/chef/knife/configure.rb', line 24

def new_client_name
  @new_client_name
end

#validation_client_nameObject (readonly)

Returns the value of attribute validation_client_name.



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

def validation_client_name
  @validation_client_name
end

#validation_keyObject (readonly)

Returns the value of attribute validation_key.



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

def validation_key
  @validation_key
end

Instance Method Details

#ask_user_for_configObject



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/chef/knife/configure.rb', line 113

def ask_user_for_config
  @chef_server            = config[:chef_server_url] || ask_question("Your chef server URL? ", :default => 'http://localhost:4000')
  @new_client_name        = config[:node_name] || ask_question("Select a user name for your new client: ", :default => Etc.getlogin)
  @admin_client_name      = config[:admin_client_name] || ask_question("Your existing admin client user name? ", :default => 'chef-webui')
  @admin_client_key       = config[:admin_client_key] || ask_question("The location of your existing admin key? ", :default => '/etc/chef/webui.pem')
  @validation_client_name = config[:validation_client_name] || ask_question("Your validation client user name? ", :default => 'chef-validator')
  @validation_key         = config[:validation_key] || ask_question("The location of your validation key? ", :default => '/etc/chef/validation.pem')
  @chef_repo              = config[:repository] || ask_question("Path to a chef repository (or leave blank)? ")

  @new_client_key = config[:client_key] || File.join(chef_config_path, "#{@new_client_name}.pem")
end

#ask_user_for_config_pathObject



106
107
108
109
110
111
# File 'lib/chef/knife/configure.rb', line 106

def ask_user_for_config_path
  config[:config_file] ||= ask_question("Where should I put the config file? ")
  if File.exists?(config[:config_file])
    confirm("Overwrite #{config[:config_file]}")
  end
end

#chef_config_pathObject



129
130
131
# File 'lib/chef/knife/configure.rb', line 129

def chef_config_path
  File.dirname(config_file)
end

#config_fileObject



125
126
127
# File 'lib/chef/knife/configure.rb', line 125

def config_file
  config[:config_file]
end

#configure_chefObject



40
41
42
43
44
45
# File 'lib/chef/knife/configure.rb', line 40

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

#runObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
# File 'lib/chef/knife/configure.rb', line 47

def run
  ask_user_for_config_path

  Mixlib::Log::Formatter.show_time = false
  Chef::Log.init(STDOUT)
  Chef::Log.level(:info)

  FileUtils.mkdir_p(chef_config_path)

  ask_user_for_config

  ::File.open(config[:config_file], "w") do |f|
    f.puts <<-EOH
log_level                :info
log_location             STDOUT
node_name                '#{new_client_name}'
client_key               '#{new_client_key}'
validation_client_name   '#{validation_client_name}'
validation_key           '#{validation_key}'
chef_server_url          '#{chef_server}'
cache_type               'BasicFile'
cache_options( :path => '#{File.join(chef_config_path, "checksums")}' )
EOH
    unless chef_repo.empty?
      f.puts "cookbook_path [ '#{chef_repo}/cookbooks', '#{chef_repo}/site-cookbooks' ]"
    end
  end

  if config[:initial]
    Chef::Log.warn("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
    client_create = Chef::Knife::ClientCreate.new
    client_create.name_args = [ new_client_name ]
    client_create.config[:admin] = true
    client_create.config[:file] = new_client_key
    client_create.config[:yes] = true
    client_create.config[:no_editor] = true
    client_create.run
  else
    Chef::Log.warn("*****")
    Chef::Log.warn("")
    Chef::Log.warn("You must place your client key in:")
    Chef::Log.warn("  #{new_client_key}")
    Chef::Log.warn("Before running commands with Knife!")
    Chef::Log.warn("")
    Chef::Log.warn("*****")
    Chef::Log.warn("")
    Chef::Log.warn("You must place your validation key in:")
    Chef::Log.warn("  #{validation_key}")
    Chef::Log.warn("Before generating instance data with Knife!")
    Chef::Log.warn("")
    Chef::Log.warn("*****")
  end

  Chef::Log.warn("Configuration file written to #{config[:config_file]}")
end