Permalink
Browse files

Moving chef-server into lib, reworking specs, adding stories

  • Loading branch information...
adamhjk committed Jun 16, 2008
1 parent 14ca7fd commit ebc1b392fe7e8f0fbabc305c299b4d365d2b4d9b
Showing with 2,366 additions and 18 deletions.
  1. +11 −0 config/chef-server.rb
  2. +2 −1 examples/config/cookbooks/fakefile/recipes/default.rb
  3. +5 −1 lib/chef.rb
  4. +3 −3 lib/chef/client.rb
  5. +1 −0 lib/chef/config.rb
  6. +1 −1 lib/chef/cookbook.rb
  7. +12 −12 lib/chef/couchdb.rb
  8. +69 −0 lib/chef_server/controllers/application.rb
  9. +20 −0 lib/chef_server/controllers/exceptions.rb
  10. +74 −0 lib/chef_server/controllers/nodes.rb
  11. +99 −0 lib/chef_server/controllers/openid_consumer.rb
  12. +74 −0 lib/chef_server/controllers/openid_register.rb
  13. +218 −0 lib/chef_server/controllers/openid_server.rb
  14. +17 −0 lib/chef_server/helpers/global_helpers.rb
  15. +19 −0 lib/chef_server/helpers/nodes_helper.rb
  16. +9 −0 lib/chef_server/helpers/openid_server_helpers.rb
  17. +165 −0 lib/chef_server/init.rb
  18. +434 −0 lib/chef_server/log/chef-server.log
  19. BIN lib/chef_server/public/images/merb.jpg
  20. +153 −0 lib/chef_server/public/stylesheets/master.css
  21. +1 −0 lib/chef_server/views/exceptions/bad_request.json.erb
  22. +216 −0 lib/chef_server/views/exceptions/internal_server_error.html.erb
  23. +63 −0 lib/chef_server/views/exceptions/not_acceptable.html.erb
  24. +47 −0 lib/chef_server/views/exceptions/not_found.html.erb
  25. +22 −0 lib/chef_server/views/layout/application.html.haml
  26. +13 −0 lib/chef_server/views/nodes/_action.html.haml
  27. +11 −0 lib/chef_server/views/nodes/_node.html.haml
  28. +22 −0 lib/chef_server/views/nodes/_resource.html.haml
  29. +5 −0 lib/chef_server/views/nodes/compile.html.haml
  30. +9 −0 lib/chef_server/views/nodes/index.html.haml
  31. +1 −0 lib/chef_server/views/nodes/show.html.haml
  32. +25 −0 lib/chef_server/views/openid_consumer/index.html.haml
  33. +4 −0 lib/chef_server/views/openid_consumer/start.html.haml
  34. +6 −0 lib/chef_server/views/openid_login/index.html.haml
  35. +15 −0 lib/chef_server/views/openid_register/index.html.haml
  36. +5 −0 lib/chef_server/views/openid_register/show.html.haml
  37. +27 −0 lib/chef_server/views/openid_server/decide.html.haml
  38. +9 −0 log/chef-server.log
  39. +7 −0 log/merb_test.log
  40. +4 −0 spec/chef_server/controllers/log/merb_test.log
  41. +214 −0 spec/chef_server/controllers/nodes_spec.rb
  42. +29 −0 spec/chef_server/controllers/openid_consumer_spec.rb
  43. +93 −0 spec/chef_server/controllers/openid_register_spec.rb
  44. +4 −0 spec/chef_server/log/merb_test.log
  45. +4 −0 spec/chef_server/spec.opts
  46. +16 −0 spec/chef_server/spec_helper.rb
  47. +1 −0 spec/unit/compile_spec.rb
  48. +26 −0 stories/chef-client
  49. +48 −0 stories/chef-client.rb
  50. +33 −0 stories/story_helper.rb
View
@@ -0,0 +1,11 @@
+#
+# Example Chef Server Config
+
+cookbook_path File.join(File.dirname(__FILE__), "..", "examples", "config", "cookbooks")
+node_path File.join(File.dirname(__FILE__), "..", "examples", "config", "nodes")
+file_store_path File.join(File.dirname(__FILE__), "..", "examples", "store")
+log_level :debug
+
+openid_providers [ "localhost:4001", "openid.hjksolutions.com" ]
+
+Chef::Log::Formatter.show_time = false
@@ -16,4 +16,5 @@
mode 0644
action :create
end
-end
+end
+
View
@@ -19,7 +19,11 @@
require 'rubygems'
-Dir[File.join(File.dirname(__FILE__), 'chef/**/*.rb')].sort.each { |lib| require lib unless lib =~ /server/ }
+Dir[
+ File.join(
+ File.dirname(__FILE__),
+ 'chef/**/*.rb'
+ )].sort.each { |lib| require lib unless lib =~ /server/ }
class Chef
VERSION = '0.0.1'
View
@@ -26,7 +26,7 @@
class Chef
class Client
- attr_accessor :node, :registration
+ attr_accessor :node, :registration, :safe_name
def initialize()
@node = nil
@@ -43,8 +43,8 @@ def run
converge
end
- def build_node
- node_name = Facter["fqdn"].value ? Facter["fqdn"].value : Facter["hostname"].value
+ def build_node(node_name=nil)
+ node_name ||= Facter["fqdn"].value ? Facter["fqdn"].value : Facter["hostname"].value
@safe_name = node_name.gsub(/\./, '_')
begin
@node = @rest.get_rest("nodes/#{@safe_name}")
View
@@ -49,6 +49,7 @@ class Config
:couchdb_url => "http://localhost:5984",
:registration_url => "http://localhost:4000",
:openid_url => "http://localhost:4001",
+ :couchdb_database => "chef",
}
class << self
View
@@ -92,7 +92,7 @@ def recipes
def load_recipe(name, node, collection=nil, definitions=nil, cookbook_loader=nil)
cookbook_name = @name
recipe_name = nil
- nmatch = name.match(/^(.+)::(.+)$/)
+ nmatch = name.match(/^(.+?)::(.+)$/)
recipe_name = nmatch ? nmatch[2] : name
unless @recipe_names.has_key?(recipe_name)
View
@@ -13,16 +13,16 @@ def initialize(url=nil)
def create_db
@database_list = @rest.get_rest("_all_dbs")
- unless @database_list.detect { |db| db == "chef" }
- response = @rest.put_rest("chef", Hash.new)
+ unless @database_list.detect { |db| db == Chef::Config[:couchdb_database] }
+ response = @rest.put_rest(Chef::Config[:couchdb_database], Hash.new)
end
- "chef"
+ Chef::Config[:couchdb_database]
end
def create_design_document(name, data)
to_update = true
begin
- old_doc = @rest.get_rest("chef/_design%2F#{name}")
+ old_doc = @rest.get_rest("#{Chef::Config[:couchdb_database]}/_design%2F#{name}")
if data["version"] != old_doc["version"]
data["_rev"] = old_doc["_rev"]
Chef::Log.debug("Updating #{name} views")
@@ -33,7 +33,7 @@ def create_design_document(name, data)
Chef::Log.debug("Creating #{name} views for the first time")
end
if to_update
- @rest.put_rest("chef/_design%2F#{name}", data)
+ @rest.put_rest("#{Chef::Config[:couchdb_database]}/_design%2F#{name}", data)
end
true
end
@@ -49,7 +49,7 @@ def store(obj_type, name, object)
:object => { :respond_to => :to_json },
}
)
- @rest.put_rest("chef/#{obj_type}_#{safe_name(name)}", object)
+ @rest.put_rest("#{Chef::Config[:couchdb_database]}/#{obj_type}_#{safe_name(name)}", object)
end
def load(obj_type, name)
@@ -63,7 +63,7 @@ def load(obj_type, name)
:name => { :kind_of => String },
}
)
- @rest.get_rest("chef/#{obj_type}_#{safe_name(name)}")
+ @rest.get_rest("#{Chef::Config[:couchdb_database]}/#{obj_type}_#{safe_name(name)}")
end
def delete(obj_type, name, rev=nil)
@@ -78,14 +78,14 @@ def delete(obj_type, name, rev=nil)
}
)
unless rev
- last_obj = @rest.get_rest("chef/#{obj_type}_#{safe_name(name)}")
+ last_obj = @rest.get_rest("#{Chef::Config[:couchdb_database]}/#{obj_type}_#{safe_name(name)}")
if last_obj.respond_to?(:couchdb_rev)
rev = last_obj.couchdb_rev
else
rev = last_obj['_rev']
end
end
- @rest.delete_rest("chef/#{obj_type}_#{safe_name(name)}?rev=#{rev}")
+ @rest.delete_rest("#{Chef::Config[:couchdb_database]}/#{obj_type}_#{safe_name(name)}?rev=#{rev}")
end
def list(view, inflate=false)
@@ -98,9 +98,9 @@ def list(view, inflate=false)
}
)
if inflate
- @rest.get_rest("chef/_view/#{view}/all")
+ @rest.get_rest("#{Chef::Config[:couchdb_database]}/_view/#{view}/all")
else
- @rest.get_rest("chef/_view/#{view}/all_id")
+ @rest.get_rest("#{Chef::Config[:couchdb_database]}/_view/#{view}/all_id")
end
end
@@ -116,7 +116,7 @@ def has_key?(obj_type, name)
}
)
begin
- @rest.get_rest("chef/#{obj_type}_#{safe_name(name)}")
+ @rest.get_rest("#{Chef::Config[:couchdb_database]}/#{obj_type}_#{safe_name(name)}")
true
rescue
false
@@ -0,0 +1,69 @@
+class Application < Merb::Controller
+
+ def fix_up_node_id
+ if params.has_key?(:id)
+ params[:id].gsub!(/_/, '.')
+ end
+ end
+
+ def escape_node_id
+ if params.has_key?(:id)
+ params[:id].gsub(/_/, '.')
+ end
+ end
+
+ def login_required
+ if session[:openid]
+ return session[:openid]
+ else
+ self.store_location
+ throw(:halt, :access_denied)
+ end
+ end
+
+ def authorized_node
+ if session[:level] == :admin
+ Chef::Log.debug("Authorized as Administrator")
+ true
+ elsif session[:level] == :node
+ Chef::Log.debug("Authorized as node")
+ if session[:node_name] == params[:id].gsub(/\./, '_')
+ true
+ else
+ raise(
+ Unauthorized,
+ "You are not the correct node for this action: #{session[:node_name]} instead of #{params[:id]}"
+ )
+ end
+ else
+ Chef::Log.debug("Unauthorized")
+ raise Unauthorized, "You are not allowed to take this action."
+ end
+ end
+
+ # Store the URI of the current request in the session.
+ #
+ # We can return to this location by calling #redirect_back_or_default.
+ def store_location
+ session[:return_to] = request.uri
+ end
+
+ # Redirect to the URI stored by the most recent store_location call or
+ # to the passed default.
+ def redirect_back_or_default(default)
+ loc = session[:return_to] || default
+ session[:return_to] = nil
+ redirect loc
+ end
+
+ def access_denied
+ case content_type
+ when :html
+ store_location
+ redirect url(:openid_consumer)
+ else
+ raise Unauthorized, "You must authenticate first!"
+ end
+ end
+
+end
@@ -0,0 +1,20 @@
+class Exceptions < Application
+
+ provides :html, :json
+
+ # handle NotFound exceptions (404)
+ def not_found
+ display params
+ end
+
+ # handle NotAcceptable exceptions (406)
+ def not_acceptable
+ display params
+ end
+
+ # handle BadRequest exceptions (400)
+ def bad_request
+ display params
+ end
+
+end
@@ -0,0 +1,74 @@
+class Nodes < Application
+
+ provides :html, :json
+
+ before :fix_up_node_id
+ before :login_required, :only => [ :create, :update, :destroy ]
+ before :authorized_node, :only => [ :update, :destroy ]
+
+ def index
+ @node_list = Chef::Node.list
+ display @node_list
+ end
+
+ def show
+ begin
+ @node = Chef::Node.load(params[:id])
+ rescue Net::HTTPServerException => e
+ raise NotFound, "Cannot load node #{params[:id]}"
+ end
+ display @node
+ end
+
+ def create
+ @node = params.has_key?("inflated_object") ? params["inflated_object"] : nil
+ if @node
+ @status = 202
+ @node.save
+ display @node
+ else
+ raise BadRequest, "You must provide a Node to create"
+ end
+ end
+
+ def update
+ @node = params.has_key?("inflated_object") ? params["inflated_object"] : nil
+ if @node
+ @status = 202
+ @node.save
+ display @node
+ else
+ raise NotFound, "You must provide a Node to update"
+ end
+ end
+
+ def destroy
+ begin
+ @node = Chef::Node.load(params[:id])
+ rescue RuntimeError => e
+ raise BadRequest, "Node #{params[:id]} does not exist to destroy!"
+ end
+ @status = 202
+ @node.destroy
+ if content_type == :html
+ redirect url(:nodes)
+ else
+ display @node
+ end
+ end
+
+ def compile
+ # Grab a Chef::Compile object
+ compile = Chef::Compile.new()
+ compile.load_node(params[:id])
+ compile.node.save
+ compile.load_definitions
+ compile.load_recipes
+ @output = {
+ :node => compile.node,
+ :collection => compile.collection,
+ }
+ display @output
+ end
+
+end
Oops, something went wrong.

0 comments on commit ebc1b39

Please sign in to comment.