Skip to content

Commit

Permalink
Huge amount of work, covering openid, clients, and all sorts of serve…
Browse files Browse the repository at this point in the history
…r stuff
  • Loading branch information
adamhjk committed Jun 10, 2008
1 parent d373915 commit af84319
Show file tree
Hide file tree
Showing 45 changed files with 9,425 additions and 684 deletions.
5 changes: 4 additions & 1 deletion History.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@
* Birthday!

Sun Apr 27 20:30:43 PDT 2008
* Compiled first full recipes, and actually passed them to a provider. :)
* Compiled first full recipes, and actually passed them to a provider. :)

Mon Jun 9 01:57:58 PDT 2008
* Compiled first full recipes over a network, with registration and authorization.
67 changes: 67 additions & 0 deletions bin/chef-client
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/usr/bin/ruby
#
# ./chef-client - Build a meal with chef
#
# Author:: Adam Jacob (<adam@hjksolutions.com>)
# Copyright:: Copyright (c) 2008 HJK Solutions, LLC
# License:: GNU General Public License version 2 or later
#
# This program and entire repository is free software; you can
# redistribute it and/or modify it under the terms of the GNU
# General Public License as published by the Free Software
# Foundation; either version 2 of the License, or any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#

$: << File.join(File.dirname(__FILE__), "..", "lib")

require 'optparse'
require 'chef'
require 'rubygems'
require 'facter'

config = {
:config_file => "/etc/chef/config.rb",
:log_level => :info,
:noop => false
}
opts = OptionParser.new do |opts|
opts.banner = "Usage: #{$0} [-d DIR|-r FILE] (options)"
opts.on("-c CONFIG", "--config CONFIG", "The Chef Config file to use") do |c|
config[:config_file] = c
end
opts.on("-n", "--noop", "Print what you would do, but don't actually do it.") do
config[:noop] = true
end
opts.on_tail("-l LEVEL", "--loglevel LEVEL", "Set the log level (debug, info, warn, error, fatal)") do |l|
config[:log_level] = l.to_sym
end
opts.on_tail("-h", "--help", "Show this message") do
puts opts
exit
end
end
opts.parse!(ARGV)

unless File.exists?(config[:config_file]) && File.readable?(config[:config_file])
puts "I cannot find or read the config file: #{config[:config_file]}"
puts opts
exit
end

# Load our config file
Chef::Config.from_file(config[:config_file])
if config[:log_level]
Chef::Log.level(config[:log_level].to_sym)
end

c = Chef::Client.new
c.run
7 changes: 4 additions & 3 deletions examples/config/chef-solo.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#
# Example Chef Solo Config

cookbook_path File.join(File.dirname(__FILE__), "cookbooks")
node_path File.join(File.dirname(__FILE__), "nodes")
cookbook_path File.join(File.dirname(__FILE__), "cookbooks")
node_path File.join(File.dirname(__FILE__), "nodes")
search_index_path File.join(File.dirname(__FILE__), "..", "search_index")
log_level :info
log_level :info
file_store_path "/tmp/chef"

Chef::Log::Formatter.show_time = false
8 changes: 4 additions & 4 deletions examples/config/cookbooks/fakefile/recipes/default.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
file "/tmp/foo" do
owner "adam"
mode 0644
action :create
owner "adam"
mode 0644
action :create
notifies :delete, resources(:file => "/tmp/glen"), :delayed
end

link "/tmp/foo" do
link_type :symbolic
link_type :symbolic
target_file "/tmp/xmen"
end

Expand Down
2 changes: 1 addition & 1 deletion examples/config/nodes/latte.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
##
# Nodes should have a unique name
##
name "latte"
name "latte.local"

##
# Nodes can set arbitrary arguments
Expand Down
123 changes: 123 additions & 0 deletions lib/chef/client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
#
# Author:: Adam Jacob (<adam@hjksolutions.com>)
# Copyright:: Copyright (c) 2008 HJK Solutions, LLC
# License:: GNU General Public License version 2 or later
#
# This program and entire repository is free software; you can
# redistribute it and/or modify it under the terms of the GNU
# General Public License as published by the Free Software
# Foundation; either version 2 of the License, or any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#

require File.join(File.dirname(__FILE__), "mixin", "params_validate")

require 'rubygems'
require 'facter'

class Chef
class Client

attr_accessor :node, :registration

def initialize()
@node = nil
@safe_name = nil
@registration = nil
@rest = Chef::REST.new(Chef::Config[:registration_url])
end

def run
build_node
register
authenticate
save_node
converge
end

def build_node
node_name = Facter["fqdn"].value ? Facter["fqdn"].value : Facter["hostname"].value
@safe_name = node_name.gsub(/\./, '_')
begin
@node = @rest.get_rest("nodes/#{@safe_name}")
rescue Net::HTTPServerException => e
unless e.message =~ /^404/
raise e
end
end
unless @node
@node ||= Chef::Node.new
@node.name(node_name)
end
Facter.each do |field, value|
@node[field] = value
end
@node
end

def register
@registration = nil
begin
@registration = @rest.get_rest("registrations/#{@safe_name}")
rescue Net::HTTPServerException => e
unless e.message =~ /^404/
raise e
end
end

if @registration
reg = Chef::FileStore.load("registration", @safe_name)
@secret = reg["secret"]
else
create_registration
end
end

def create_registration
@secret = random_password(40)
Chef::FileStore.store("registration", @safe_name, { "secret" => @secret })
@rest.post_rest("registrations", { :id => @safe_name, :password => @secret })
end

def authenticate
response = @rest.post_rest('openid/consumer/start', {
"openid_identifier" => "#{Chef::Config[:openid_url]}/openid/server/node/#{@safe_name}",
"submit" => "Verify"
})
@rest.post_rest(
"#{Chef::Config[:openid_url]}#{response["action"]}",
{ "password" => @secret }
)
end

def save_node
@rest.put_rest("nodes/#{@safe_name}", @node)
end

def converge
results = @rest.get_rest("nodes/#{@safe_name}/compile")
results["collection"].resources.each do |r|
r.collection = results["collection"]
end
cr = Chef::Runner.new(results["node"], results["collection"])
cr.converge
end

protected
def random_password(len)
chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
newpass = ""
1.upto(len) { |i| newpass << chars[rand(chars.size-1)] }
newpass
end

end
end
7 changes: 5 additions & 2 deletions lib/chef/compile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@ def initialize()
end

def load_node(name)
Chef::Log.debug("Loading Chef Node #{name}")
@node = Chef::Node.find(name)
Chef::Log.debug("Loading Chef Node #{name} from CouchDB")
@node = Chef::Node.load(name)
Chef::Log.debug("Loading Recipe for Chef Node #{name}")
@node.find_file(name)
@node
end

def load_definitions()
Expand Down
12 changes: 9 additions & 3 deletions lib/chef/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,16 @@ class Config
@configuration = {
:cookbook_path => [ "/etc/chef/site-cookbook", "/etc/chef/cookbook" ],
:node_path => "/etc/chef/node",
:file_store_path => "/var/lib/chef/store",
:search_index_path => "/var/lib/chef/search_index",
:file_store_path => "/var/chef/store",
:search_index_path => "/var/chef/search_index",
:log_level => :info,
:log_location => STDOUT
:log_location => STDOUT,
:openid_providers => nil,
:ssl_verify_mode => :verify_none,
:rest_timeout => 60,
:couchdb_url => "http://localhost:5984",
:registration_url => "http://localhost:4000",
:openid_url => "http://localhost:4001",
}

class << self
Expand Down
Loading

0 comments on commit af84319

Please sign in to comment.