s

Posts Tagged with "capistrano"

Deploy Sinatra Application with Capistrano

May 25, 2010  -  Comments

Step 0: Vendor the sinatra gem (optional).

  mkdir vendor
  cd vendor
  gem unpack sinatra
  mv sinatra-* sinatra

Step 1. Create a config.ru Rack file in your project's root directory.

  require 'rubygems'
  require 'vendor/sinatra/lib/sinatra.rb'

  set :public, File.expand_path(File.dirname(__FILE__) + '/public/index.html') # Include your public folder
  set :views, File.expand_path(File.dirname(__FILE__) + '/views/index.html')  # Include the views
  
  set :environment, :production

  disable :run, :reload

  require 'app_file' # replace this with your sinatra app file
  run Sinatra::Application

Step 2. Create a Capistrano deploy file.

This file should live at config/deploy.rb.

  set :domain, "yourdomain.com"
  set :application, "app_name"
  set :deploy_to, "/var/www/apps/#{domain}"

  set :user, "your_deploy_user"
  set :use_sudo, false

  set :scm, :git
  set :repository,  "git@github.com:you/application.git"
  set :branch, 'master'
  set :git_shallow_clone, 1

  role :web, domain
  role :app, domain
  role :db,  domain, :primary => true

  set :deploy_via, :remote_cache

  namespace :deploy do
    task :start do ; end
    task :stop do ; end
    # Assumes you are using Passenger
    task :restart, :roles => :app, :except => { :no_release => true } do
      run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"
    end

    task :finalize_update, :except => { :no_release => true } do
      run "chmod -R g+w #{latest_release}" if fetch(:group_writable, true)

      # mkdir -p is making sure that the directories are there for some SCM's that don't save empty folders
      run <<-CMD
        rm -rf #{latest_release}/log &&
        mkdir -p #{latest_release}/public &&
        mkdir -p #{latest_release}/tmp &&
        ln -s #{shared_path}/log #{latest_release}/log
      CMD

      if fetch(:normalize_asset_timestamps, true)
        stamp = Time.now.utc.strftime("%Y%m%d%H%M.%S")
        asset_paths = %w(images css).map { |p| "#{latest_release}/public/#{p}" }.join(" ")
        run "find #{asset_paths} -exec touch -t #{stamp} {} ';'; true", :env => { "TZ" => "UTC" }
      end
    end
  end

Step 3: Deploy the app.

  cap deploy:setup
  cap deploy

Step 4: Get a beer, you're done!

Tagged: capistranosinatra

Deploying a Merb application with the RailsMachine gem

Feb 07, 2009  -  Comments

I recently launched a monthly bill/task tracking application I've been working on in my spare time. I used Merb so I could get some experience with the framework.

Plexus was kind enough to donate some server space on a RailsMachine server. Luckily, they recently added Passenger support to their awesome RailsMachine gem, so all I needed was to add a Rack config file to run my app on Passenger.

require 'rubygems'
require 'merb-core'
 
Merb::Config.setup(:merb_root => File.expand_path(File.dirname(__FILE__)),
                   :environment => ENV['RACK_ENV'])
Merb.environment = Merb::Config[:environment]
Merb.root = Merb::Config[:merb_root]
Merb::BootLoader.run
 
run Merb::Rack::Application.new

After that, I only needed to update the Capistrano deploy file to work with Merb and Passenger.

require 'railsmachine/recipes'
 
# The name of your application. Used for directory and file names associated with
# the application.
set :application, "listode"
 
# Target directory for the application on the web and app servers.
set :deploy_to, "/var/www/apps/#{application}"
 
# Primary domain name of your application. Used as a default for all server roles.
set :domain, "listode.com"
 
# Login user for ssh.
set :user, "deploy"
set :runner, user
set :admin_runner, user
 
# Rails environment. Used by application setup tasks and migrate tasks.
set :rails_env, "production"
 
# Automatically symlink these directories from curent/public to shared/public.
set :app_symlinks, %w{graphs}
 
set :deploy_via, :remote_cache
 
# =============================================================================
# ROLES
# =============================================================================
# Modify these values to execute tasks on a different server.
role :web, domain
role :app, domain
role :db, domain, :primary => true
role :scm, domain
 
 
# =============================================================================
# APPLICATION SERVER OPTIONS
# =============================================================================
set :app_server, :passenger # :mongrel or :passenger
 
# =============================================================================
# SCM OPTIONS
# =============================================================================
set :scm, :git # :subversion or :git
set :repository, "git@github.com:travisr/#{application}.git"
 
# =============================================================================
# CUSTOM CONFIGURATION
# =============================================================================
# action to symlink database file
namespace :deploy do
  desc "Symlink database config file."
  task :symlink_db do
    run "ln -nfs #{shared_path}/system/database.yml #{release_path}/config/database.yml"
  end
end
 
# Overwrite the default deploy.migrate as it calls:
# rake RAILS_ENV=production db:migrate
desc "Use datamapper to call autoupgrade instead of db:migrate."
deploy.task :migrate do
 run "cd #{release_path}; rake db:autoupgrade MERB_ENV=production"
end

after 'deploy:update_code', 'deploy:symlink_db'

The custom section at the bottom sets up a symlink to my databases.yml file since I don't keep that in my git repo. I also have to override the migration action to use DataMapper's db:autoupgrade.

Tagged: merbpassengercapistranorails machinetutorial