Skip to content

Instantly share code, notes, and snippets.

@jamtur01
Created April 17, 2012 20:39
Show Gist options
  • Save jamtur01/2408847 to your computer and use it in GitHub Desktop.
Save jamtur01/2408847 to your computer and use it in GitHub Desktop.
Updated PuppetRAL agent
metadata :name => "Agent for Puppet RAL interaction",
:description => "Agent to inspect and act on the RAL",
:author => "R.I.Pienaar, Max Martin",
:license => "ASL2",
:version => "0.2",
:url => "http://mcollective-plugins.googlecode.com/",
:timeout => 180
action "create", :description => "Add a resource to the RAL" do
display :always
input :type,
:prompt => "Resource type",
:description => "Type of resource to add",
:type => :string,
:validation => '.',
:optional => false,
:maxlength => 90
input :title,
:prompt => "Resource name",
:description => "Name of resource to add",
:type => :string,
:validation => '.',
:optional => false,
:maxlength => 90
output :result,
:description => "Result of the action",
:display_as => "Result"
end
action "find", :description => "Get the value of a resource" do
display :always
input :type,
:prompt => "Resource type",
:description => "Type of resource to check",
:type => :string,
:validation => '.',
:optional => false,
:maxlength => 90
input :name,
:prompt => "Resource name",
:description => "Name of resource to check",
:type => :string,
:validation => '.',
:optional => true,
:maxlength => 90
output :result,
:description => "Value of the inspected resource",
:display_as => "Result"
end
action "search", :description => "Get the value of all resources of a certain type" do
display :always
input :type,
:prompt => "Resource type",
:description => "Type of resource to check",
:type => :string,
:validation => '.',
:optional => false,
:maxlength => 90
output :result,
:description => "Value of the inspected resources",
:display_as => "Result"
end
action "do", :description => "Foo" do
input :type,
:prompt => "The puppet type",
:description => "Type to create",
:type => :string,
:validation => '^.+$',
:optional => false,
:maxlength => 20
input :name,
:prompt => "Resource namevar",
:description => "The namevar for the resource",
:type => :string,
:validation => '^.+$',
:optional => false,
:maxlength => 80
output :result,
:description => "Result",
:display_as => "Result"
end
require 'puppet'
module MCollective
module Agent
# A SimpleRPC agent that uses the Puppet RAL to perform any action
# that Puppet supports.
#
# To use this you can make requests like:
#
# mc-rpc puppetral create type=user name=foo comment="Foo User"
#
# This will add a user foo with the correct comment:
#
# mc-rpc puppetral create type=user name=foo comment="Foo User" ensure=absent
#
# This will remove the user.
#
# You can call any Puppet type that makes sense, you need to supply all the
# needed properties that the type require etc.
class Puppetral<RPC::Agent
metadata :name => "puppetral",
:description => "Agent to inspect and act on the RAL",
:author => "R.I.Pienaar <rip@devco.net>, Max Martin <max@puppetlabs.com>",
:license => "ASL2",
:version => "0.2",
:url => "https://github.com/puppetlabs/mcollective-plugins",
:timeout => 180
action "do" do
validate :type, String
validate :name, String
params = request.data.clone
type = request[:type]
params.delete :type
params.delete :process_results
pup = Puppet::Type.type(type).new(params)
catalog = Puppet::Resource::Catalog.new
catalog.add_resource pup
catalog.apply
reply[:result] = "OK"
end
action "create" do
inputs = request.data.clone
resource = Puppet::Resource.new(inputs[:type], inputs[:title], :parameters => inputs[:parameters])
result = Puppet::Resource.indirection.save(resource)
if result[:ensure] == :absent
reply[:output] = "Resource was not created"
else
reply[:output] = "Resource was created"
end
end
action "find" do
type = request[:type]
name = request[:name]
typeobj = Puppet::Type.type(type) or raise "Could not find type #{type}"
if typeobj
result = Puppet::Resource.indirection.find([type, name].join('/')).to_pson_data_hash
result.each { |k,v| reply[k] = v }
end
end
action "search" do
type = request[:type]
name = request[:name]
typeobj = Puppet::Type.type(type) or raise "Could not find type #{type}"
if typeobj
result = Puppet::Resource.indirection.search(type, {}).map {|r| r.to_pson_data_hash}
result.each {|r| reply[r["title"]] = r}
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment