Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Chef resource is now fully generic

  • Loading branch information...
commit 508fa50fba229a7bad91f4e6be0f4f17378270b6 1 parent b6ca6c4
Nicolas Szalay authored
11 clients/generic_chefresource.rb
... ... @@ -0,0 +1,11 @@
  1 +require 'mcollective'
  2 +include MCollective::RPC
  3 +
  4 +mc = rpcclient("chefresource")
  5 +mc.progress = false
  6 +
  7 +r = [ { "action" => "restart" }, { "supports" => {:status => true } } ]
  8 +
  9 +mc.handle(:resourcename => "cron", :resourcetype => "service", :resourceactions => r).each do |resp|
  10 + puts resp[:sender] + " => " + resp[:status].inspect
  11 +end
15 plugins/agents/chef-resource.rb
@@ -15,7 +15,6 @@ class Chefresource<RPC::Agent
15 15 action "handle" do
16 16 validate :resourcetype, String
17 17 validate :resourcename, String
18   - validate :resourceaction, String
19 18
20 19 require 'chef'
21 20 require 'chef/client'
@@ -31,11 +30,15 @@ class Chefresource<RPC::Agent
31 30
32 31 run_context = Chef::RunContext.new(client.node, Chef::CookbookCollection.new(Chef::CookbookLoader.new))
33 32 recipe = Chef::Recipe.new("adhoc", "default", run_context)
34   - resource = recipe.send(request[:resourcetype].to_sym, request[:resourcename])
35   - resource.send("action",request[:resourceaction])
36   - # add generic handling of more arguments
37   -
38   - Log.instance.debug("Doing '#{request[:resourceaction]}' for resource #{request[:resourcetype]} '#{request[:resourcename]}'")
  33 + # create the resource
  34 + resource = recipe.send(request[:resourcetype].to_sym, request[:resourcename])
  35 + # insert action, attribute, whatever supported by your resource type
  36 + request[:resourceactions].each { |action|
  37 + action.each_pair { |k,v|
  38 + resource.send(k,v)
  39 + }
  40 + }
  41 + Log.instance.debug("Converging for resource #{request[:resourcetype]} '#{request[:resourcename]}'")
39 42 status=Chef::Runner.new(run_context).converge
40 43
41 44 reply["status"] = status

0 comments on commit 508fa50

Please sign in to comment.
Something went wrong with that request. Please try again.