Class: Chef::Knife::Setup

Inherits:
ChefWebui::Knife show all
Defined in:
lib/chef/knife/setup.rb

Instance Method Summary collapse

Methods inherited from ChefWebui::Knife

common_options, #init_config_variable, #webui_session

Instance Method Details

#configure_chefObject



41
42
43
44
45
46
47
# File 'lib/chef/knife/setup.rb', line 41

def configure_chef
  super
  init_config_variable(:chef_repo_path, '.')
  init_config_variable(:validation_key)
  init_config_variable(:validation_client_name)
  init_config_variable(:api_server_url, 'https://api.opscode.com')
end

#runObject



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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/chef/knife/setup.rb', line 49

def run
  if name_args.length == 0
    organization = ask("Enter your organization:")
  else
    organization = name_args[0]
  end

  # Create the organization
  begin
    webui_session
    output "Ensuring organization #{organization} existence ..."
    webui_session.create_org(organization)
  rescue
  end

  # Get validator key
  if config[:regen_validator_key]
    output "Regenerating validation key for #{organization}-validator..."
    validation_key = webui_session.regenerate_validator_key(organization)
  elsif Chef::Config[:validation_client_name] == "#{organization}-validator" &&
        Chef::Config[:validation_key] && File.exist?(Chef::Config[:validation_key])
    output "Copying validation key from #{Chef::Config[:validation_key]} ..."
    validation_key = IO.read(Chef::Config[:validation_key])
  else
    ui.error <<-EOM
Validator key file for #{organization}-validator not found.
If you don't have a key, pass --regen-validator-key to regenerate it.
If you have a key, pass -U #{organization}-validator and (optionally) -K VALIDATOR_KEY_FILE.
EOM
    exit 1
  end

  # Get user key
  if config[:regen_user_key]
    output "Regenerating user key for #{Chef::Config[:username]} ..."
    user_key = webui_session.regenerate_user_key
  elsif Chef::Config[:node_name] == "#{Chef::Config[:username]}" &&
        Chef::Config[:client_key] && File.exist?(Chef::Config[:client_key])
    output "Copying user key from #{Chef::Config[:client_key]} ..."
    user_key = IO.read(Chef::Config[:client_key])
  else
    ui.error <<-EOM
User key file for #{Chef::Config[:username]} not found.
If you don't have a key, pass --regen-user-key to regenerate it.
If you have a key, pass -u #{Chef::Config[:username]} and -k YOUR_KEY_FILE.
EOM
    exit 1
  end

  # Write out client client.rb
  client_rb_file = "#{File.dirname(Chef::Config[:validation_key])}/client.rb"
#        if File.exist?(client_rb_file)
#          output "client.rb file #{client_rb_file} already exists.  Skipping ..."
#        else
    output "Writing #{client_rb_file} ..."
    write_file(client_rb_file, <<-EOM)
current_dir = File.dirname(__FILE__)
require 'socket'
log_level                :info
log_location             STDOUT
validation_key           "#{organization}-validator"
chef_server_url          "#{Chef::Config[:api_server_url]}/organizations/#{organization}"
client_key               "\#{current_dir}/\#{Socket.gethostname}.pem"
cache_type               'BasicFile'
cache_options( :path => "#{ENV['HOME']}/.chef/checksums" )
cookbook_path            [\"#{current_dir}/../cookbooks\"]
EOM
#        end

  # Write out client validation.pem
  if File.exist?(Chef::Config[:validation_key]) && !config[:regen_validator_key]
    output "Validation key #{Chef::Config[:validation_key]} already exists.  Skipping creation ..."
  else
    output "Writing #{Chef::Config[:validation_key]} ..."
    write_file(Chef::Config[:validation_key], validation_key)
  end

  if !Chef::Config[:chef_repo_path]
    ui.error("You must pass --chef-repo-path PATH (specify the place you want to run knife from)")
    exit 1
  end

  # Write out workstation knife.rb
  client_rb_file = "#{Chef::Config[:chef_repo_path]}/.chef/knife.rb"
#        if File.exist?(client_rb_file)
#          output "client.rb file #{client_rb_file} already exists.  Skipping ..."
#        else
    output "Writing #{client_rb_file} ..."
    write_file(client_rb_file, <<-EOM)
current_dir = File.dirname(__FILE__)
log_level                :info
log_location             STDOUT
node_name                "#{Chef::Config[:username]}"
client_key               "\#{current_dir}/#{Chef::Config[:username]}.pem"
validation_client_name   "#{organization}-validator"
validation_client_key    "\#{current_dir}/#{organization}-validator.pem"
chef_server_url          "https://api.opscode.com/organizations/#{organization}"
EOM
#        end

  # Write out workstation user.pem
  user_pem_file = "#{Chef::Config[:chef_repo_path]}/.chef/#{Chef::Config[:username]}.pem"
  if File.exist?(user_pem_file) && !config[:regen_user_key]
    output "User key #{user_pem_file} already exists.  Skipping creation ..."
  else
    output "Writing #{user_pem_file} ..."
    write_file(user_pem_file, user_key)
  end

  # Write out workstation validator.pem
  workstation_validator_file = "#{Chef::Config[:chef_repo_path]}/.chef/#{organization}-validator.pem"
  if File.exist?(workstation_validator_file) && !config[:regen_validator_key]
    output "Validation key #{workstation_validator_file} already exists.  Skipping creation ..."
  else
    output "Writing #{workstation_validator_file} ..."
    write_file(workstation_validator_file, validation_key)
  end
end

#write_file(filename, contents) ⇒ Object



168
169
170
171
172
173
# File 'lib/chef/knife/setup.rb', line 168

def write_file(filename, contents)
  FileUtils.mkdir_p(File.dirname(filename))
  File.open(filename, 'w') do |file|
    file.write(contents)
  end
end