Skip to content

Instantly share code, notes, and snippets.

@stympy
Created May 25, 2011 13:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stympy/990989 to your computer and use it in GitHub Desktop.
Save stympy/990989 to your computer and use it in GitHub Desktop.
A chef runner
#!/usr/bin/env ruby
require 'optparse'
require 'rubygems'
require 'net/ssh'
require 'yaml'
options = { :json => 'node.json', :config => 'chef-config.rb', :server => nil, :user => 'root' }
if config = YAML.load_file(File.join(File.dirname(__FILE__), 'server.yml'))
options.merge!(config)
end
opts = OptionParser.new do |opts|
opts.on('-c', '--config [chef-config.rb]') {|config| options[:config] = config }
opts.on('-j', '--json [node.json]') {|json| options[:json] = json }
opts.on('-s', '--server name') {|server| options[:server] = server }
opts.on('-u', '--user [root]') {|user| options[:user] = user }
opts.parse!
end
if (missing = options.select {|k, v| v.nil? }).any?
puts "Please supply the following options: #{missing.flatten.compact.join(', ')}"
exit
end
puts "Uploading cookbooks..."
puts `rsync -crve "ssh -o StrictHostKeyChecking=no" #{File.dirname(__FILE__)}/ #{options[:user]}@#{options[:server]}:`
puts "Running chef..."
Net::SSH.start(options[:server], options[:user]) do |ssh|
channel = ssh.open_channel do |ch|
ch.exec "chef-solo -c #{options[:config]} -j #{options[:json]}" do |ch, success|
raise "could not execute command" unless success
ch.on_data do |c, data|
STDOUT.print data
end
ch.on_extended_data do |c, type, data|
STDERR.print data
end
ch.on_close { puts "done!" }
end
end
channel.wait
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment