Created
October 29, 2010 17:41
-
-
Save jordansissel/653983 to your computer and use it in GitHub Desktop.
Hack puppet to do stuff over ssh.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
snack(~) % ruby puppet-package-over-ssh.rb snack apache2.2-common latest | |
Package[apache2.2-common] ensure => latest status: {:status=>"installed", :provider=>:apt, :error=>"ok", :desired=>"install", :name=>"apache2.2-common", :ensure=>"2.2.14-5ubuntu8.3"} | |
snack(~) % ruby puppet-package-over-ssh.rb snack sl latest | |
Package[sl] ensure => latest status: {:status=>"installed", :provider=>:apt, :error=>"ok", :desired=>"install", :name=>"sl", :ensure=>"3.03-16"} | |
# There's a bug where 'latest' seems to be ignored. If you try to use 'absent' it still installs anyway. I'm not familiar with puppet internals so this bug should be easy to fix. | |
snack(~) % ruby puppet-package-over-ssh.rb snack sl absent | |
Package[sl] ensure => absent status: {:status=>"installed", :provider=>:apt, :error=>"ok", :desired=>"install", :name=>"sl", :ensure=>"3.03-16"} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "rubygems" | |
require "puppet" | |
$host = ARGV.shift | |
name = ARGV.shift | |
ensurevalue = ARGV.shift | |
ARGV.length > 0 ? hasstatus = true : hasstatus = false | |
# Monkeypatch so our executed commands get sent over ssh. | |
module Puppet::Util | |
alias original_execute execute | |
def execute(command, *args) | |
# Shell escapings | |
newcommand = command.collect { |arg| arg.to_s.gsub(/["$\\]/) { "\\#{$&}" } } \ | |
.collect { |arg| "\"#{arg}\"" } | |
# ssh to our host, run sudo. | |
newcommand = ["ssh", $host, "sudo", *newcommand] | |
original_execute(newcommand, *args) | |
end | |
end | |
begin | |
package = Puppet::Type.type(:package).new(:name => name, :ensure => ensurevalue) | |
provider = package.provider | |
provider.update | |
puts("#{package} ensure => #{ensurevalue} status: #{provider.properties.inspect}") | |
rescue Exception => e | |
puts("Could not ensure => #{ensurevalue} package #{package}: #{e}") | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# apache2.2-common => latest | |
Oct 29 10:54:41 snack sudo: jls : TTY=unknown ; PWD=/home/jls ; USER=root ; COMMAND=/usr/bin/rpm --version | |
Oct 29 10:54:41 snack sudo: jls : TTY=unknown ; PWD=/home/jls ; USER=root ; COMMAND=/usr/bin/rpm -ql rpm | |
Oct 29 10:54:42 snack sudo: jls : TTY=unknown ; PWD=/home/jls ; USER=root ; COMMAND=/usr/bin/rpm --version | |
Oct 29 10:54:43 snack sudo: jls : TTY=unknown ; PWD=/home/jls ; USER=root ; COMMAND=/usr/bin/rpm -ql rpm | |
Oct 29 10:54:44 snack sudo: jls : TTY=unknown ; PWD=/home/jls ; USER=root ; COMMAND=/usr/bin/dpkg-query -W --showformat ${Status} ${Package} ${Version}\n apache2.2-common |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment