From 51b02f9f2cbee48de8fe3036b8d2ad007f25d3dc Mon Sep 17 00:00:00 2001 From: Hawk Newton Date: Thu, 8 Sep 2011 17:20:41 -0400 Subject: [PATCH 1/2] Added ability to define environment for runall and runonce for puppetd plugin --- agent/puppetd/agent/puppetd.rb | 4 ++++ agent/puppetd/application/puppetd.rb | 17 +++++++++++++++-- agent/puppetd/sbin/mc-puppetd | 22 +++++++++++++++++++--- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/agent/puppetd/agent/puppetd.rb b/agent/puppetd/agent/puppetd.rb index 2d5cbce..119b8ba 100644 --- a/agent/puppetd/agent/puppetd.rb +++ b/agent/puppetd/agent/puppetd.rb @@ -96,6 +96,10 @@ def runonce end end + if request[:env] + cmd << "--environment" << request[:env] + end + cmd = cmd.join(" ") if respond_to?(:run) diff --git a/agent/puppetd/application/puppetd.rb b/agent/puppetd/application/puppetd.rb index e114cb2..062e86b 100644 --- a/agent/puppetd/application/puppetd.rb +++ b/agent/puppetd/application/puppetd.rb @@ -7,6 +7,10 @@ class MCollective::Application::Puppetd ["--force", "-f"], :type => :bool + option :env, + :description => "Environment to pass to puppet when invoking runonce or runall", + :arguments => ["--environment", "-e [ENV]"] + def post_option_parser(configuration) if ARGV.length >= 1 configuration[:command] = ARGV.shift @@ -20,6 +24,15 @@ def post_option_parser(configuration) end end + def get_opts + opts = {:forcerun => configuration[:command] == "runall" ? true : configuration[:force] } + if configuration[:env] + opts[:env] = configuration[:env] + log("Passing explicit environment #{opts[:env]}") + end + return opts + end + # Prints a log statement with a time def log(msg) puts("#{Time.now}> #{msg}") @@ -69,7 +82,7 @@ def main hosts.each do |host| running = waitfor(configuration[:concurrency], mc) log("Running #{host}, concurrency is #{running}") - result = mc.custom_request("runonce", {:forcerun => true}, host, {"identity" => host}) + result = mc.custom_request("runonce", get_opts, host, {"identity" => host}) if result.is_a?(Array) log("#{host} schedule status: #{result[0][:statusmsg]}") @@ -85,7 +98,7 @@ def main end when "runonce" - printrpc mc.runonce(:forcerun => configuration[:force]) + printrpc mc.runonce(get_opts) when "status" mc.send(configuration[:command]).each do |node| diff --git a/agent/puppetd/sbin/mc-puppetd b/agent/puppetd/sbin/mc-puppetd index 67d0709..49ddc55 100755 --- a/agent/puppetd/sbin/mc-puppetd +++ b/agent/puppetd/sbin/mc-puppetd @@ -20,6 +20,10 @@ options = rpcoptions do |parser, options| parser.on("--force", "-f", "Force the puppet run to happen immediately without splay") do @force = true end + + parser.on("--environment", "-e ENVIRONMENT", "Environment option to invoke runonce or runall") do |v| + @env = v + end end puppetd = rpcclient("puppetd", :options => options) @@ -62,6 +66,18 @@ def waitfor(concurrency, client) end end +def get_opts(command) + #Always force for runall + opts = {:forcerun => command == "runall" ? true : @force} + + if !@env.nil? + log("Passing explicit environment #{@env}") + opts[:env] = @env + end + + return opts +end + if command == "status" puppetd.send(command).each do |node| node[:statuscode] == 0 ? msg = node[:data][:output] : msg = node[:statusmsg] @@ -96,6 +112,7 @@ elsif command == "count" elsif command == "runall" if ARGV.length == 1 concurrency = ARGV.shift.to_i + opts = get_opts(command) if concurrency > 0 log("Running all machines with a concurrency of #{concurrency}") @@ -116,7 +133,7 @@ elsif command == "runall" log("Running #{host}, concurrency is #{running}") - result = puppetd.custom_request("runonce", {:forcerun => true}, host, {"identity" => host}) + result = puppetd.custom_request("runonce", opts, host, {"identity" => host}) if result.is_a?(Array) log("#{host} schedule status: #{result[0][:statusmsg]}") @@ -140,10 +157,9 @@ elsif command == "summary" printrpcstats elsif command == "runonce" - printrpc puppetd.runonce(:forcerun => @force) + printrpc puppetd.runonce(get_opts(command)) printrpcstats - else printrpc puppetd.send(command) From d7d2d7aab7b2395012299f9064cb477680490943 Mon Sep 17 00:00:00 2001 From: Hawk Newton Date: Fri, 9 Sep 2011 05:05:06 -0400 Subject: [PATCH 2/2] SimpleRPC ddl for puppetd runonce action --- agent/puppetd/agent/puppetd.ddl | 20 +++++++++++++------- agent/puppetd/application/puppetd.rb | 6 ++++++ 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/agent/puppetd/agent/puppetd.ddl b/agent/puppetd/agent/puppetd.ddl index a17cdca..fcd46fd 100644 --- a/agent/puppetd/agent/puppetd.ddl +++ b/agent/puppetd/agent/puppetd.ddl @@ -38,14 +38,20 @@ action "disable", :description => "Disables the Puppetd" do end action "runonce", :description => "Initiates a single Puppet run" do - #input :forcerun, - # :prompt => "Force puppet run", - # :description => "Should the puppet run happen immediately", - # :type => :string, - # :validation => '^.+$', - # :optional => true, - # :maxlength => 5 + input :forcerun, + :prompt => "Force puppet run", + :description => "Should the puppet run happen immediately", + :type => :boolean, + :optional => true + input :env, + :prompt => "Environment", + :description => "Environment agent should use, if any", + :type => :string, + :validation => '^[^\d][a-zA-Z_\d]+$', + :optional => true, + :maxlength => 100 + output :output, :description => "Output from puppetd", :display_as => "Output" diff --git a/agent/puppetd/application/puppetd.rb b/agent/puppetd/application/puppetd.rb index 062e86b..3e81bdb 100644 --- a/agent/puppetd/application/puppetd.rb +++ b/agent/puppetd/application/puppetd.rb @@ -19,6 +19,11 @@ def post_option_parser(configuration) unless configuration[:command].match(/^(enable|disable|runonce|runall|status|summary|count)$/) raise "Command has to be enable, disable, runonce, runonce, runall, status, summary or count" end + + #I would think the :bool type above would take care of this + unless configuration[:force] + configuration[:force] = false + end else raise "Please specify a command" end @@ -140,3 +145,4 @@ def main printrpcstats end end +# vi:tabstop=4:expandtab:ai:filetype=ruby