A chef runner
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
#!/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