Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 58 lines (48 sloc) 1.537 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
# Author: Manuel Hutter <manuel.hutter@gmail.com>
# Rake Tasks for automatic deployment of chef recipes etc...

require 'net/ssh'

desc 'upload files and run chef-solo'
task :deploy, [:hostname] => [:upload, :run]

desc 'upload all the cookbooks etc to the hostname'
task :upload, [:hostname] do |t, args|
  say "Uploading current directory..."
  cmd = %Q{rsync -rv --delete #{File.expand_path(__FILE__+"/..")}/ root@#{args.hostname}:/var/chef --exclude='.git*' --exclude=cache --exclude=backup}
  say cmd
  system cmd
end

desc 'run the chef-solo command on the hostname'
task :run, [:hostname] do |t, args|
  Net::SSH.start(args.hostname, 'root') do |ssh|
    run_cmd ssh, "chef-solo -c /var/chef/solo.rb"
  end
end

# administrative tasks
desc 'installs the prequisites'
task :prepare, [:hostname] do |t, args|

  Net::SSH.start(args.hostname, "root") do |ssh|
    run_cmd ssh, "apt-get install -y curl"
    run_cmd ssh, "curl -L https://raw.github.com/gist/2349875/chef_solo_bootstrap.sh | bash"
  end

end

# Helper Stuff
def say(text)
  puts "[#{Time.new}] :: #{text}"
end

def run_cmd(ssh, cmd)
  say "[SSH>] #{cmd}"
  ssh.open_channel do |channel|
    channel.exec(cmd) do |ch, success|
      abort "[ERROR] error executing '#{cmd}'!" unless success
      # callbacks
      channel.on_data do |c, data|
        STDOUT.print data
      end
      channel.on_extended_data do |c, type, data|
        STDERR.print data
      end
      channel.on_close { say "[SSH] Channel closed!" }

      channel.wait
    end
  end
  ssh.loop
end
Something went wrong with that request. Please try again.