Skip to content

Deploying Adhearsion

Ben Langfeld edited this page Apr 11, 2012 · 8 revisions

DEPRECATION NOTICE: This is old documentation relevant to Adhearsion 1.x and will soon be removed. See the main documentation for up-to-date info.

Below follows a brief tutorial in deploying Adhearsion applications using Capistrano. At some point in the future, this will become a part of Adhearsion's app generators.

Prerequisites

  1. A user on the server specifically for your app
  2. Ruby installed via RVM for that user
  3. An rvm gemset for your application
  4. The following gems installed in that gemset: Adhearsion, Capistrano, i18n, god
  5. Capistrano, railsless-deploy and rvm installed on your development machine

Setup steps

  1. Copy the below Capfile to yourahnapp/Capfile.
  2. Replace "ahntestapp" with your application name, "example.com" with your server's domain/IP and configure your repository settings.
  3. Copy the below adhearsion.god script to yourahnapp/adhearsion.god, tweak file paths, gemsets and app name to your liking
  4. Commit your ahn app to a git repo if you havn't already, and push to your repo URL.
  5. Setup god to run on startup via init.d or similar, using the path to adhearsion.god
  6. Run cap deploy:setup from your development machine to setup your application directory structure on the remote server.
  7. Run cap deploy from your development machine to deploy to your server and restart adhearsion.

Capfile

$:.unshift(File.expand_path('./lib', ENV['rvm_path']))
require 'rubygems'
require 'railsless-deploy'
require 'rvm/capistrano'

default_run_options[:pty] = true

set :application, "ahntestapp"     # Change this to the name of your app
set :deploy_to, "/srv/adhearsion/#{application}"
set :user, "#{application}"
set :use_sudo, false
role :app, "example.com"           # Change this to where your app should run

set :repository, "git://github.com/...." # Change this to where your app lives
set :scm, :git
set :deploy_via, :export # :remote_cache
set :branch, "master"

set :rvm_ruby_string, "ruby-1.8.7@#{application}" # or jruby
set :rvm_type, :user

namespace :deploy do
  after "deploy:update", "deploy:restart"

  task :finalize_update do
    run "rm -rf #{release_path}/log && ln -s #{deploy_to}/shared/log #{release_path}/log"
  end

  task :restart, :roles => :app do
    run "god restart ahntestapp-adhearsion"
  end

  after "deploy:setup", "deploy:setup_shared_dirs"

  task :setup_shared_dirs, :roles => :app do
    run "mkdir #{deploy_to}/shared/pids"
    run "mkdir #{deploy_to}/shared/log"
  end
end

adhearsion.god

# run with: god -c /path/to/adhearsion.god -D

APP_PATH = File.dirname(__FILE__)
PID_FILE = "#{APP_PATH}/../../../shared/adhearsion/pids/adhearsion.pid"

def ahnctl_command(action = 'start')
  "rvm_path=$HOME/.rvm/ $HOME/.rvm/bin/rvm-shell 'ruby-1.9.2@ahntestapp' -c 'bundle exec ahnctl #{action} #{APP_PATH} --pid-file=#{PID_FILE}'"
end

God.watch do |w|
  w.name = "ahntestapp-adhearsion"
  w.group = "ahntestapp"

  w.interval = 30.seconds
  w.start_grace = 20.seconds
  w.restart_grace = 20.seconds

  w.dir = APP_PATH

  w.start = ahnctl_command 'start'
  w.stop = ahnctl_command 'stop'
  w.restart = ahnctl_command 'restart'

  w.pid_file = PID_FILE
  w.behavior :clean_pid_file

  w.start_if do |start|
    start.condition(:process_running) do |c|
      c.interval = 5.seconds
      c.running = false
    end
  end

  w.restart_if do |restart|
    restart.condition(:memory_usage) do |c|
      c.above = 150.megabytes
      c.times = [3, 5] # 3 out of 5 intervals
    end

    restart.condition(:cpu_usage) do |c|
      c.above = 50.percent
      c.times = 5
    end
  end

  w.lifecycle do |on|
    on.condition(:flapping) do |c|
      c.to_state = [:start, :restart]
      c.times = 5
      c.within = 5.minute
      c.transition = :unmonitored
      c.retry_in = 10.minutes
      c.retry_times = 5
      c.retry_within = 2.hours
    end
  end
end