Guest User

Untitled

a guest
Jun 20th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2. #
  3. # This script runs puppet on your local machine but executes commands
  4. # (package management) on a remote host
  5. # Usage: puppet-package-over-ssh.rb HOST PACKAGE ACTION
  6.  
  7. require "rubygems"
  8. require "puppet"
  9.  
  10. $host = ARGV.shift
  11. name = ARGV.shift
  12. action = ARGV.shift
  13.  
  14. # Monkeypatch so our executed commands get sent over ssh.
  15. module Puppet::Util
  16. alias original_execute execute
  17.  
  18. def execute(command, *args)
  19. # Shell escapings
  20. newcommand = command.collect { |arg| arg.to_s.gsub(/["$\\]/) { "\\#{$&}" } } \
  21. .collect { |arg| "\"#{arg}\"" }
  22.  
  23. # ssh to our host, run sudo.
  24. newcommand = ["ssh", $host, "sudo", *newcommand]
  25. original_execute(newcommand, *args)
  26. end
  27. end
  28.  
  29. begin
  30. package = Puppet::Type.type(:package).new(:name => name)
  31. provider = package.provider
  32. # Should be 'install' or 'uninstall'
  33. provider.send action
  34.  
  35. puts("#{package} #{action} status: #{provider.properties.inspect}")
  36. rescue Exception => e
  37. puts("Could not #{action} package #{package}: #{e}")
  38. end
Add Comment
Please, Sign In to add comment