Skip to content

Instantly share code, notes, and snippets.

@jordansissel
Created Oct 29, 2010
Embed
What would you like to do?
puppetless puppet (package management of remote hosts)
Usage: puppet-package-over-ssh.rb HOST PACKAGE ACTION
% ruby puppet-package-over-ssh.rb snack sl uninstall
Package[sl] uninstall status: {:status=>"not-installed", :error=>"ok", :desired=>"unknown", :ensure=>:purged, :name=>"sl", :provider=>:apt}
% ruby puppet-package-over-ssh.rb snack sl install
Package[sl] install status: {:status=>"installed", :error=>"ok", :desired=>"install", :ensure=>"3.03-16", :name=>"sl", :provider=>:apt}
% ruby puppet-package-over-ssh.rb snack fancypants install
Could not install package Package[fancypants]: Execution of 'ssh snack sudo "/usr/bin/apt-get" "-q" "-y" "-o" "DPkg::Options::=--force-confold" "install" "fancypants"' returned 100: Reading package lists...
Building dependency tree...
Reading state information...
E: Couldn't find package fancypants
#!/usr/bin/env ruby
#
# This script runs puppet on your local machine but executes commands
# (package management) on a remote host
# Usage: puppet-package-over-ssh.rb HOST PACKAGE ACTION
require "rubygems"
require "puppet"
$host = ARGV.shift
name = ARGV.shift
action = ARGV.shift
# 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)
provider = package.provider
# Should be 'install' or 'uninstall'
provider.send action
puts("#{package} #{action} status: #{provider.properties.inspect}")
rescue Exception => e
puts("Could not #{action} package #{package}: #{e}")
end
Oct 29 11:08:05 snack sudo: jls : TTY=unknown ; PWD=/home/jls ; USER=root ; COMMAND=/usr/bin/rpm --version
Oct 29 11:08:05 snack sudo: jls : TTY=unknown ; PWD=/home/jls ; USER=root ; COMMAND=/usr/bin/rpm -ql rpm
Oct 29 11:08:06 snack sudo: jls : TTY=unknown ; PWD=/home/jls ; USER=root ; COMMAND=/usr/bin/rpm --version
Oct 29 11:08:06 snack sudo: jls : TTY=unknown ; PWD=/home/jls ; USER=root ; COMMAND=/usr/bin/rpm -ql rpm
Oct 29 11:08:07 snack sudo: jls : TTY=unknown ; PWD=/home/jls ; USER=root ; COMMAND=/usr/bin/apt-get -y -q remove sl
Oct 29 11:08:12 snack sudo: jls : TTY=unknown ; PWD=/home/jls ; USER=root ; COMMAND=/usr/bin/dpkg-query -W --showformat ${Status} ${Package} ${Version}\n sl
Oct 29 11:08:20 snack sudo: jls : TTY=unknown ; PWD=/home/jls ; USER=root ; COMMAND=/usr/bin/rpm --version
Oct 29 11:08:20 snack sudo: jls : TTY=unknown ; PWD=/home/jls ; USER=root ; COMMAND=/usr/bin/rpm -ql rpm
Oct 29 11:08:21 snack sudo: jls : TTY=unknown ; PWD=/home/jls ; USER=root ; COMMAND=/usr/bin/rpm --version
Oct 29 11:08:21 snack sudo: jls : TTY=unknown ; PWD=/home/jls ; USER=root ; COMMAND=/usr/bin/rpm -ql rpm
Oct 29 11:08:22 snack sudo: jls : TTY=unknown ; PWD=/home/jls ; USER=root ; COMMAND=/usr/bin/apt-get -q -y -o DPkg::Options::=--force-confold install sl
Oct 29 11:08:29 snack sudo: jls : TTY=unknown ; PWD=/home/jls ; USER=root ; COMMAND=/usr/bin/dpkg-query -W --showformat ${Status} ${Package} ${Version}\n sl
Oct 29 11:08:40 snack sudo: jls : TTY=unknown ; PWD=/home/jls ; USER=root ; COMMAND=/usr/bin/rpm --version
Oct 29 11:08:41 snack sudo: jls : TTY=unknown ; PWD=/home/jls ; USER=root ; COMMAND=/usr/bin/rpm -ql rpm
Oct 29 11:08:41 snack sudo: jls : TTY=unknown ; PWD=/home/jls ; USER=root ; COMMAND=/usr/bin/rpm --version
Oct 29 11:08:42 snack sudo: jls : TTY=unknown ; PWD=/home/jls ; USER=root ; COMMAND=/usr/bin/rpm -ql rpm
Oct 29 11:08:43 snack sudo: jls : TTY=unknown ; PWD=/home/jls ; USER=root ; COMMAND=/usr/bin/apt-get -q -y -o DPkg::Options::=--force-confold install fancypants
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment