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/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..3e81bdb 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 @@ -15,11 +19,25 @@ 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 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 +87,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 +103,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| @@ -127,3 +145,4 @@ def main printrpcstats end end +# vi:tabstop=4:expandtab:ai:filetype=ruby 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)