Permalink
Browse files

basic generic chef resource agent

  • Loading branch information...
1 parent 8951569 commit 0468fdc98bff460382d10c92752c78241f770401 @rottenbytes committed Mar 9, 2011
Showing with 52 additions and 0 deletions.
  1. +52 −0 plugins/agents/chef-resource.rb
@@ -0,0 +1,52 @@
+module MCollective
+ module Agent
+ # An agent that uses Opscode to manage services
+ # Made from the puppet version
+ # Original credit goes to R.I. Pienaar
+ class Chefresource<RPC::Agent
+ metadata :name => "SimpleRPC Chef Resource Agent",
+ :description => "Generic resource management",
+ :author => "Nicolas Szalay <nico@rottenbytes.info>",
+ :license => "BSD",
+ :version => "1.0",
+ :url => "https://github.com/rottenbytes/mcollective",
+ :timeout => 60
+
+ # Does the actual work with the chef provider and sets appropriate reply options
+ action "handle" do
+ validate :resourcetype, String
+ validate :resourcename, String
+ validate :resourceaction, String
+
+ require 'chef'
+ require 'chef/client'
+ require 'chef/run_context'
+
+ begin
+ Chef::Config[:solo] = true
+ Chef::Config[:log_level] = :debug
+ Chef::Log.level(:debug)
+ client = Chef::Client.new
+ client.run_ohai
+ client.build_node
+
+ run_context = Chef::RunContext.new(client.node, Chef::CookbookCollection.new(Chef::CookbookLoader.new))
+ recipe = Chef::Recipe.new("adhoc", "default", run_context)
+ resource = recipe.send(request[:resourcetype].to_sym, request[:resourcename])
+ resource.send("action",request[:resourceaction])
+ # add generic handling of more arguments
+
+ Log.instance.debug("Doing '#{request[:resourceaction]}' for resource #{request[:resourcetype]} '#{request[:resourcename]}'")
+ status=Chef::Runner.new(run_context).converge
+
+ reply["status"] = status
+ rescue Exception => e
+ reply.fail "#{e}"
+ end
+ end
+
+ end
+ end
+end
+
+# vi:tabstop=4:expandtab:ai:filetype=ruby

0 comments on commit 0468fdc

Please sign in to comment.