Feature #2116 » use_mco_instead_of_puppetrun_v2.patch
lib/proxy/puppet.rb 2013-05-09 16:47:26.000000000 -0400 | ||
---|---|---|
# search for puppet for users using puppet 2.6+
|
||
puppetrun = which("puppetrun", default_path) || which("puppet", default_path)
|
||
sudo = which("sudo", "/usr/bin")
|
||
mco = which("mco", default_path)
|
||
unless puppetrun and sudo
|
||
logger.warn "sudo or puppetrun binary was not found - aborting"
|
||
return false
|
||
end
|
||
puppet_cmd = [puppetrun]
|
||
puppet_cmd += ["kick"] unless puppetrun.include?('puppetrun')
|
||
# If MCollective client is installed, see if it has a Puppet plugin
|
||
# The 'puppet' plugin is a newer replacement for 'puppetd', so
|
||
# prefer the 'puppet' plugin.
|
||
#
|
||
mco_puppet_plugin = nil
|
||
if mco
|
||
`#{sudo} #{mco} completion --list-agents`.each do |agent|
|
||
case agent.chomp!
|
||
when 'puppet'
|
||
mco_puppet_plugin = 'puppet'
|
||
break
|
||
when 'puppetd'
|
||
mco_puppet_plugin = 'puppetd'
|
||
end
|
||
end
|
||
unless mco_puppet_plugin
|
||
logger.warn 'mco found, but no available Puppet plugin'
|
||
end
|
||
end
|
||
# Add a --host argument for each client where a run was requested.
|
||
hosts.map { |h| puppet_cmd += ["--host", escape_for_shell(h)] }
|
||
# Use MCollective if possible, otherwise use puppetrun
|
||
if mco and mco_puppet_plugin
|
||
mco_command = [sudo, mco, mco_puppet_plugin, 'runonce']
|
||
hosts.map { |h| mco_command.push '-I', h }
|
||
# Returns a boolean with whether or not the command executed successfully.
|
||
Open3.popen3(*puppet_cmd) do |stdin, stdout, stderr|
|
||
stdrout = stdout.read
|
||
if stdrout =~ /finished with exit code 0/
|
||
logger.debug 'command = ' + mco_command.join(' ')
|
||
system(*mco_command)
|
||
if $? == 0
|
||
return true
|
||
else
|
||
logger.warn "The attempted puppetrun failed: \n#{stderr.read}\n#{stdrout}"
|
||
else
|
||
logger.warn "command returned exit code #{$?}: " + command
|
||
return false
|
||
end
|
||
else
|
||
logger.error 'hello'
|
||
puppet_cmd = [puppetrun]
|
||
puppet_cmd += ["kick"] unless puppetrun.include?('puppetrun')
|
||
|
||
# Add a --host argument for each client where a run was requested.
|
||
hosts.map { |h| puppet_cmd += ["--host", h] }
|
||
|
||
# Returns a boolean with whether or not the command executed successfully.
|
||
Open3.popen3(*puppet_cmd) do |stdin, stdout, stderr|
|
||
stdrout = stdout.read
|
||
if stdrout =~ /finished with exit code 0/
|
||
return true
|
||
else
|
||
logger.warn "The attempted puppetrun failed: \n#{stderr.read}\n#{stdrout}"
|
||
return false
|
||
end
|
||
end
|
||
end
|
||
end
|
||
end
|