# = Rudy -- Skeleton configuration
# 
# Rudy automatically looks for configuration files in the following 
# locations (in this order):
#
#     ./.rudy/config
#     ~/.rudy/config
#
#     ~/.rudy/*.rb
#     ./Rudyfile
#     ./machines.rb, ./routines.rb, ./commands.rb
#     ./config/rudy/*.rb
#     ./.rudy/*.rb
#     /etc/rudy/*.rb
#
# When multuple files are found, the configuration is NOT OVERRIDDEN,
# it's ADDED or APPENDED depending on context. This means you can split 
# configuration across many files as you please. 
#
# There are five sections: accounts, defaults, machines, commands and routines.
# 
# By convention, accounts go in ./.rudy/config or ~/.rudy/config 
# machines, commands, routines, and defaults configuration go in ./Rudyfile or  
# into separate files in ./.rudy or ./config/rudy (machines.rb, commands.rb, ...)
#

# ---------------------------------------------------------  MACHINES  --------
# The machines block describes the 'physical' characteristics of your machines. 
machines do
  
  region :'us-east-1' do
    ami 'ami-e348af8a'               # Alestic Debian 5.0, 32-bit (US)
  end
  region :'eu-west-1' do
    ami 'ami-6ecde51a'               # Alestic Debian 5.0, 32-bit (EU)
  end
  
  hostname :rudy                     # One of: :default, :rudy, 'your-name'
  
  # We've defined an environment called 'stage' with one role: 'app'. 
  # The configuration inside the env block is available to all its 
  # roles. The configuration inside the role blocks is available only
  # to machines in that specific role. 
  env :stage, :prod do
    user :root                       # User to connect as
    size 'm1.small'                  # EC2 machine type for all machines
                                     # in the 'stage' environment

    role :app do                     # stage-app is the default                                     
    end                              # machine group. 
    
    role :db do  
      positions 1                    # 2 machines in stage-app
      #addresses '11.22.33.44'       # Define an elastic IP to reuse
      
      disks do                       # Define EBS volumes 
        path '/rudy/disk1' do        # The paths can be anything but
          size 2                     # they must be unique. 
          device '/dev/sdr'          # Devices must be unique too.
        end
      end
                    
    end                              
    
    role :balancer do                # You can define as many roles
    end                              # as you like. These are just 
                                     # a couple examples.
                                     
    users do                         # Specify existing private keys per user
      #rudy do                       
      #  keypair '/path/2/private-key'
      #end
    end
    
    role :ubuntu do
      ami 'ami-1a837773'
      root 'ubuntu'
      user 'ubuntu'
    end
    
  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 :gem_install, '/usr/bin/gem', 'install', :V, '--no-rdoc', '--no-ri'
  allow :apt_get, 'apt-get', :y, :q
  allow :rubycode do
    puts "Some ruby code running in #{self}"
  end
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 :stage, :prod do               # We'll define routines for the stage-db
    role :db do                      # and prod-db machine groups.
      
      user :root                     # The default remote user
      
      startup do                     # $ rudy startup
        adduser :rudy                # Create a user called 'rudy'
                                     #
        disks do                     # Define EBS volume routines
          create '/rudy/disk1'       # Create an EBS volume, attach it, give
        end                          # it a filesystem, and mount it.
                                     #
        remote :root do              # Run remote SSH commands after startup
          mkdir :p, 'great'          # $ mkdir -p great
          touch 'great/scott'        # $ touch great/scott
          ls :l, :a                  # $ ls -l -a *
        end                          
      end                            
                                     
      shutdown do                    # $ rudy shutdown
        remote :root do              # Run remote SSH commands before shutdown
          uptime               
        end                               
        disks do                     
          destroy '/rudy/disk1'      # Unmount and destroy the EBS volume
        end                          
      end                            
      
      reboot do                      # $ rudy reboot
        before_remote do             # Run any pre-reboot tasks like stopping
          uptime                     # web servers and databases. 
        end                          #
        remote do                    # Run any startup tasks like starting
          uname                      # processes or initializing the filesystem
        end
        disks do
          mount "/rudy/disk1"
        end
      end
      
    end                              
  end                                
  
  # Define global routines available to all machine groups
  # This routine will update a basic Debian machine and
  # install essential libraries. 
  # See http://github.com/rudy/arcade
  sysupdate do                       # $ rudy sysupdate
    adduser :rudy
    remote :root do                  
      apt_get 'update'               # Update debian / ubuntu 
      apt_get 'install', 'build-essential', 'sqlite3', 'libsqlite3-dev'
      apt_get 'install', 'apache2-prefork-dev', 'libapr1-dev', 'rubygems'
      gem_install 'rudy'
    end
  end
  
  anything do                        # $ rudy anything
    before :uptime                   # Specify a dependency
    local do                         # This is Ruby, so any valid syntax
      ls :l                          # can be used in the definitions. 
    end                              # See: SysInfo gem for more info.
  end
  
  uptime do                          # $ rudy uptime
    local { uptime }                 # Short block syntax
  end
  
end


# ----------------------------------------------------------- DEFAULTS --------
# These values are used as defaults for their respective global settings. They
# can be overridden by the command-line global options.  
#
defaults do
  zone :'us-east-1d'
  environment :stage
  role :app
  color true                         # Terminal colors? true/false
  #root 'rootuser'                   # The "root" account (on Ubuntu, set to "ubuntu")
  #user 'someuser'                   # The default remote user
  #localhost 'hostname'              # A local hostname instead of localhost
  #auto true                         # Skip interactive confirmation?
  #keydir 'path/2/keys/'             # The path to store SSH keys
end

