Skip to content

Instantly share code, notes, and snippets.

@mrflip
Created May 18, 2010
Embed
What would you like to do?
# Author:: Adam Jacob <adam@opscode.com>
# Author:: Joshua Timberman <joshua@opscode.com>
#
# Copyright 2009-2010, Opscode, Inc
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Chef Client Config File: /etc/chef/client.rb
# Automatically grabs configuration from ohai ec2 metadata.
#
# Also note: sets the node's name to the EC2 instance ID, recommended as
# hostnames, fqdn's and IPs can be recycled.
require 'ohai'
require 'json'
o = Ohai::System.new
o.all_plugins
chef_config = JSON.parse(o[:ec2][:userdata])
if chef_config.kind_of?(Array)
chef_config = chef_config[o[:ec2][:ami_launch_index]]
end
log_level :info
log_location STDOUT
node_name o[:ec2][:instance_id]
chef_server_url chef_config["chef_server"]
CHEF_CONFIG_FILE = "/etc/chef/client-config.json"
unless File.exists?("/etc/chef/client.pem")
File.open("/etc/chef/validation.pem", "w", 0600) do |f|
f.print(chef_config["validation_key"])
end
end
# Adopt chef config settings from the attributes key
if chef_config.has_key?("attributes") and (not File.exists?(CHEF_CONFIG_FILE))
File.open(CHEF_CONFIG_FILE, "w", 0600) do |f|
f.print(JSON.pretty_generate(chef_config))
end
end
begin
json_attribs CHEF_CONFIG_FILE if File.exists?(CHEF_CONFIG_FILE)
rescue Exception => e ; warn "Couldn't load initial attributes: #{e}" ; end
validation_key "/etc/chef/validation.pem"
validation_client_name chef_config["validation_client_name"]
Mixlib::Log::Formatter.show_time = true
{
"validation_key": "-----BEGIN RSA PRIVATE KEY-----\nsomelongreallyawesomekey\n-----END RSA PRIVATE KEY-----\n",
"attributes": {
"run_list": [
"role[base]"
]
},
"chef_server": "http://chef.example.com:4000",
"validation_client_name": "chef-validator"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment