# = Rudy configuration
#

# ----------------------------------------------------------- DEFAULTS --------
# These values are used as defaults for their respective global settings. All
# non-boolean values are expected to be Symbols. 
#
defaults do
  zone :'eu-west-1b'
  environment :rye
  color true                        # Terminal colors? true/false
  yes false                         # Auto-confirm? true/false
end

# ---------------------------------------------------------  MACHINES  --------
# The machines block describes the 'physical' characteristics of your machines. 
machines do
  
  zone :'us-east-1b' do
    ami 'ami-e348af8a'               # Alestic Debian 5.0, 32-bit (US)
    bucket 'rudy-ami-us'
  end
  zone :'eu-west-1b' do
    ami 'ami-1dbd9569'               # rudy-ami-eu/debian-5.0-32-ruby-r1
    bucket 'rudy-ami-eu'
  end
  
  hostname :rudy                     # One of: :default, :rudy, 'your-name'

  env :rye do
    user :root                       # User to connect as
    size 'm1.small'                  # EC2 machine type for all machines
  end  
  
end


# ----------------------------------------------------------- COMMANDS --------
# The commands block defines shell commands that can be used in routines. The
# ones defined here are added to the default list defined by Rye::Cmd (Rudy 
# executes all SSH commands via Rye). 
#
# Usage: 
#
# allow COMMAND-NAME
# allow COMMAND-NAME, '/path/2/COMMAND'
# allow COMMAND-NAME, '/path/2/COMMAND', 'default argument', 'another arg'
#
commands do
  allow :apt_get, "apt-get", :y, :q
  allow :gem18_install, "/usr/bin/gem1.8", "install", :n, '/usr/bin', :y, :V, "--no-rdoc", "--no-ri"
  allow :gem18_sources, "/usr/bin/gem1.8", "sources"
  allow :gem19_install, "/usr/local/bin/gem", "install", :n, '/usr/bin', :y, :V, "--no-rdoc", "--no-ri"
  allow :gem19_sources, "/usr/local/bin/gem", "sources"
  allow :update_rubygems
  allow :ruby18, "/usr/bin/ruby1.8"
  allow :ruby19, "/usr/local/bin/ruby"
  allow :ssh_keygen, 'ssh-keygen'
  allow :rm
end

# ----------------------------------------------------------- ROUTINES --------
# The routines block describes the repeatable processes for each machine group.
# To run a routine, specify its name on the command-line: rudy startup
routines do
  
  env :rye do
    startup do
      after :installdeps, :authorize
      after :runtest
    end
  end
  
  runtest do
    remote :root do 
      ruby18 :r, 'rubygems', 'rye/bin/try' 
      ruby19 'rye/bin/try'
    end
  end
  
  authorize do
    remote :root do
      rm :f, '/root/.ssh/id_rsa'
      ssh_keygen :q, :f, '/root/.ssh/id_rsa', :N, ''
      rye 'authorize_local'
    end
  end
  
  installdeps do
    local do
      rake 'package'
    end
    remote :root do
      gem18_install "rye", "delano-rye"
      gem19_install "rye", 'delano-rye'
      disable_safe_mode
      rm :r, :f, 'rye*'
      file_upload 'pkg/rye-*gz'
      tar :z, :x, :f, 'rye-*gz'
      rm 'rye-*gz'
      mv 'rye-*', 'rye'
      cd 'rye'
    end
  end
  
  # NOTE: sysupdate only needs to be run for the bare instances. 
  sysupdate do
    remote :root do                  
      apt_get "update"               
      apt_get "install", "build-essential", "git-core"
      apt_get "install", "libssl-dev", "libreadline5-dev", "zlib1g-dev"
      apt_get "install", "ruby1.8-dev", "rubygems"
      gem18_install 'rubygems-update' # for 1.8
      update_rubygems                 # for 1.8
      gem18_sources :a, "http://gems.github.com"
    end
    after :install_ruby19
  end
  
  install_ruby19 do
    remote :root do
      wget :q, 'ftp://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.1-p129.tar.gz'
      tar :z, :x, :f, 'ruby-1.9.1-p129.tar.gz'
      cd 'ruby-1.9.1-p129'
      configure
      make
      make 'install'
      apt_get "install", "rubygems1.9"
      gem19_sources :a, "http://gems.github.com"
    end
  end
  
end


