# Sign up for AWS credentials at http://aws.amazon.com/, and learn how to
# upload your own public ssh key at http://alestic.com/2010/10/ec2-ssh-keys.
# Use environment variables in this file if you share it with others.

require 'breeze'

Breeze::CONFIGURATION = {

  # Access credentials are needed for all tasks.
  :cloud_service => {
    :provider => 'AWS',
    :aws_access_key_id => 'YOUR-ACCESS-KEY-ID',
    :aws_secret_access_key => 'YOUR-SECTET-ACCESS-KEY'
  },

  # ssh_command and ssh_user are required in order to create a server image or deploy
  # an application. Use "ssh -i /path/to/key" if not using your default ssh key.
  :ssh => {:ssh_command => "ssh -o 'UserKnownHostsFile /dev/null'", :ssh_user => 'ubuntu'},

  # :rollback_window specifies the number of minutes to keep old instances running after new ones
  # have been deployed. Rollback is no longer possible when the old instances have been destroyed.
  :rollback_window => 60,

  # Default server options are needed when launching new servers.
  :default_server_options => {
    :image_id           => 'YOUR-PRIVATE-AMI-OR-A-PUBLIC-ONE',
    :key_name           => 'THE-NAME-OF-YOUR-KEYPAIR', # http://alestic.com/2010/10/ec2-ssh-keys
    :flavor_id          => 't1.micro', # t1.micro m1.small c1.medium ...
    :availability_zone  => 'us-east-1a', # us-west-1a eu-west-1a ap-southeast-1a
    :user_data_file     => nil
  },

  # Override default_server_options when building your own private server image:
  :create_image_options => {
    :image_id => 'ami-ccf405a5', # a base AMI from http://alestic.com
    :root_device_size => 15 # in GB
  },

  # machine images owned by this account are included in describe:images (provided that you have access to them)
  :image_owner => 'YOUR-ACCOUNT-ID-WITHOUT-DASHES', # canonical: '099720109477'

  # db configuration is required in order to use Amazon RDS
  :db_region => 'us-east-1', # us-west-1 eu-west-1 ap-southeast-1
  :default_db_options => {
    :engine => 'mysql',
    :engine_version => '5.5',
    :auto_minor_version_upgrade => true,
    :allocated_storage => 5, # 5 - 1024 GB
    :availability_zone => 'us-east-1a',
    :backup_retention_period => 1, # 0 - 8 days
    :preferred_backup_window => '05:30-06:00', # daily, times in UTC
    :preferred_maintenance_window => 'sun:06:00-sun:06:30', # weekly
    :master_username => 'admin',
    :password => 'admin'
  },

  # you can add your own keys and access this hash as CONFIGURATION in the erb templates
  :admin_email => 'YOUR-EMAIL',
  :app_path => '/srv/YOUR-APP'
}.freeze

# Define Breeze::CONFIGURATION before requiring the tasks.
# This allows us to pick up default options that can be viewed
# with the "thor help" command.
require 'breeze/tasks'

# prepare_private_image is called from server:image:create. Modify this and/or the scripts
# that are uploaded in order to create your own private machine image.
class Breeze::Server::Image
  private
  def prepare_private_image(ip_address)
    wait_until_host_is_available(ip_address)
    puts("Uploading scripts...")
    upload('config/breeze/scripts/*.sh', :host => ip_address, :remote_path => './')
    remote('chmod 600 credentials.sh; chmod 700 deploy.sh; bash install.sh', :host => ip_address)
  end
end

# deploy_command is called from app:start and app:deploy.
class Breeze::App
  private
  def deploy_command(servers, public_server_name, db_server_name, branch)
    servers.each do |server|
      wait_until_host_is_available(ip(server))
      remote("/home/ubuntu/deploy.sh #{public_server_name} #{db_endpoint(db_server_name)} #{branch}", :host => ip(server))
    end
  end
end

# Define staging:start etc. below. The constants are also needed by
# inherited tasks: deploy, rollback, enable, disable and ssh.

class Staging < Breeze::App

  PUBLIC_SERVER_NAME = 'staging.example.com'
  DB_SERVER_NAME = 'staging-db'
  BRANCH = 'master'

  desc 'start', 'Start web server and db for staging'
  def start
    thor("app:start #{PUBLIC_SERVER_NAME} #{DB_SERVER_NAME} --db-to-clone=#{Production::DB_SERVER_NAME} --deploy-branch=#{BRANCH}")
  end

  desc 'stop', 'Stop staging and destroy server and db'
  def stop
    thor("app:stop #{PUBLIC_SERVER_NAME} --force")
  end

end

class Production < Breeze::App

  PUBLIC_SERVER_NAME = 'www.example.com'
  DB_SERVER_NAME = 'production-db'
  BRANCH = 'stable'

  desc 'start', 'Start web server and db for production'
  def start
    thor("app:start #{PUBLIC_SERVER_NAME} #{DB_SERVER_NAME} db_name --dns-ttl=300 --deploy-branch=#{BRANCH}")
  end

end
