Skip to content

Instantly share code, notes, and snippets.

@jelder
Created June 26, 2012 21:00
Show Gist options
  • Save jelder/2998972 to your computer and use it in GitHub Desktop.
Save jelder/2998972 to your computer and use it in GitHub Desktop.
drivethru - continuous deployment for chef
#!/usr/bin/env ruby
# drivethru - continuous deployment for chef
# June 2012 Jacob Elder <[email protected]>
require 'listen'
require 'net/ssh'
require 'smart_colored/extend'
node = ARGV[2]
abort("usage: knife exec drivethru.knife NODE") unless node
cookbook_path = Chef::Config[:cookbook_path].first
def with_duration(what)
start = Time.now
yield
puts "#{what} complete after #{(Time.now - start).to_i.to_s.bold} seconds.\n"
end
Net::SSH.start(node, ENV["USER"]) do |ssh|
callback = Proc.new do |modified, added, removed|
paths = [modified,added,removed].flatten
cookbooks = paths.map do |path|
path.split('/').first
end
puts "Change#{cookbooks.length == 1 ? '' : 's'} detected in #{cookbooks.map{|c|c.bold}.join(', ')}; pushing to #{node.bold}"
with_duration "Cookbook upload" do
Chef::Knife::CookbookUpload.new(cookbooks).run
end
with_duration "chef-client run on #{node.bold}" do
ssh.exec "sudo chef-client" do |ch, stream, data|
if stream == :stderr
puts "#{node.bold}: #{data.red}"
else
puts "#{node.bold}: #{data}"
end
end
ssh.loop
end
end
puts "Listening for changes in #{cookbook_path.bold}"
listener = Listen.to(
cookbook_path,
:filter => /\.e?rb$/,
:relative_paths => true
)
listener.change(&callback).start
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment