Skip to content

Instantly share code, notes, and snippets.

@bxt
Created October 31, 2012 21:10

Revisions

  1. bxt revised this gist Oct 31, 2012. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -35,6 +35,7 @@ Additional useful commands are:
    cap deploy

    The files go here:

    Gemfile
    Capfile
    config/deploy.rb
  2. bxt revised this gist Oct 31, 2012. 2 changed files with 20 additions and 0 deletions.
    12 changes: 12 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -11,6 +11,7 @@ Mostly based on:
    Additional useful commands are:

    capify .
    cp config/database.yml config/database.example.yml
    chmod +x config/unicorn_init.sh
    mkdir .ssh
    cat .ssh/id_dsa.pub | ssh bernhard@capivm -p 2222 "cat >> .ssh/authorized_keys"
    @@ -33,3 +34,14 @@ Additional useful commands are:
    cap deploy:cold
    cap deploy

    The files go here:
    Gemfile
    Capfile
    config/deploy.rb
    config/nginx.conf
    config/unicorn.rb
    config/unicorn_init.sh
    ~/apps/depot/shared/config/database.yml

    The `unicorn_init.sh` will be copied into `/etc/init.d/`.

    8 changes: 8 additions & 0 deletions database.yml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    production:
    adapter: postgresql
    encoding: unicode
    database: depot
    pool: 5
    host: localhost
    username: railzor
    password: railzor-pw
  3. bxt revised this gist Oct 31, 2012. 1 changed file with 35 additions and 0 deletions.
    35 changes: 35 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    Rails deployment files
    ======================

    Here are some useful files for making a rails app ready to delpoy via capistrano on a ubuntu/unicorn/nginx/postgresql stack.

    Mostly based on:
    * http://railscasts.com/episodes/293-nginx-unicorn
    * http://railscasts.com/episodes/335-deploying-to-a-vps
    * http://railscasts.com/episodes/133-capistrano-tasks-revised

    Additional useful commands are:

    capify .
    chmod +x config/unicorn_init.sh
    mkdir .ssh
    cat .ssh/id_dsa.pub | ssh bernhard@capivm -p 2222 "cat >> .ssh/authorized_keys"
    apt-get install vim htop
    apt-get install curl git-core python-software-properties
    add-apt-repository ppa:chris-lea/node.js
    apt-get install nodejs npm
    mkdir -p gitsrv/depot.git && cd gitsrv/depot.git && git --bare init
    git remote add origin ssh://bernhard@capivm/~/gitsrv/depot.git
    apt-get install postgresql libpq-dev
    apt-get install libsqlite3-dev
    su postgres
    \curl -L https://get.rvm.io | bash -s stable --rails
    gem install activerecord-postgresql-adapter
    rails console production
    rm /etc/nginx/sites-enabled/default
    vim shared/config/database.yml
    bundle install
    cap deploy:setup
    cap deploy:cold
    cap deploy

  4. bxt revised this gist Oct 31, 2012. 5 changed files with 231 additions and 0 deletions.
    48 changes: 48 additions & 0 deletions Gemfile
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    source 'https://rubygems.org'

    gem 'rails', '3.2.8'

    # Bundle edge Rails instead:
    # gem 'rails', :git => 'git://github.com/rails/rails.git'

    gem 'sqlite3'

    group :production do
    #gem 'therubyracer-heroku', '0.8.1.pre3' # you will need this too
    gem 'pg'
    end

    # Gems used only for assets and not required
    # in production environments by default.
    group :assets do
    gem 'sass-rails', '~> 3.2.3'
    gem 'coffee-rails', '~> 3.2.1'

    # See https://github.com/sstephenson/execjs#readme for more supported runtimes
    # gem 'therubyracer', :platforms => :ruby

    gem 'uglifier', '>= 1.0.3'
    end

    gem 'jquery-rails'

    # To use ActiveModel has_secure_password
    gem 'bcrypt-ruby', '~> 3.0.0'

    # To use Jbuilder templates for JSON
    # gem 'jbuilder'

    # Use unicorn as the app server
    gem 'unicorn'

    # Deploy with Capistrano
    gem 'capistrano'

    # Add rvm support to capistrano
    gem 'rvm-capistrano'

    # To use debugger
    # gem 'debugger'


    gem 'will_paginate', '~> 3.0'
    72 changes: 72 additions & 0 deletions deploy.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,72 @@
    require "rvm/capistrano"
    require "bundler/capistrano"

    set :application, "depot"
    set :repository, "ssh://bernhard@capivm/~/gitsrv/depot.git"

    server "capivm", :web, :app, :db, primary: true
    set :user, "bernhard"

    set :deploy_to, "/home/#{user}/apps/#{application}"
    set :deploy_via, :remote_cache
    set :use_sudo, false

    set :scm, :git # You can set :scm explicitly or Capistrano will make an intelligent guess based on known version control directory names
    # Or: `accurev`, `bzr`, `cvs`, `darcs`, `git`, `mercurial`, `perforce`, `subversion` or `none`
    set :branch, "master"

    #role :web, "your web-server here" # Your HTTP server, Apache/etc
    #role :app, "your app-server here" # This may be the same as your `Web` server
    #role :db, "your primary db-server here", :primary => true # This is where Rails migrations will run
    #role :db, "your slave db-server here"

    # if you want to clean up old releases on each deploy uncomment this:
    # after "deploy:restart", "deploy:cleanup"

    # if you're still using the script/reaper helper you will need
    # these http://github.com/rails/irs_process_scripts

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

    default_run_options[:pty] = true
    ssh_options[:forward_agent] = true

    namespace :deploy do
    %w[start stop restart].each do |command|
    desc "#{command} unicorn server"
    task command, roles: :app, except: {no_release: true} do
    run "/etc/init.d/unicorn_#{application} #{command}"
    end
    end

    task :setup_config, roles: :app do
    sudo "ln -nfs #{current_path}/config/nginx.conf /etc/nginx/sites-enabled/#{application}"
    sudo "ln -nfs #{current_path}/config/unicorn_init.sh /etc/init.d/unicorn_#{application}"
    run "mkdir -p #{shared_path}/config"
    put File.read("config/database.example.yml"), "#{shared_path}/config/database.yml"
    puts "Now edit the config files in #{shared_path}."
    end
    after "deploy:setup", "deploy:setup_config"

    task :symlink_config, roles: :app do
    run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
    end
    after "deploy:finalize_update", "deploy:symlink_config"

    desc "Make sure local git is in sync with remote."
    task :check_revision, roles: :web do
    unless `git rev-parse HEAD` == `git rev-parse origin/master`
    puts "WARNING: HEAD is not the same as origin/master"
    puts "Run `git push` to sync changes."
    exit
    end
    end
    before "deploy", "deploy:check_revision"
    end
    27 changes: 27 additions & 0 deletions nginx.conf
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    upstream unicorn {
    server unix:/tmp/unicorn.blog.sock fail_timeout=0;
    }

    server {
    listen 80 default deferred;
    # server_name example.com;
    root /home/bernhard/apps/depot/current/public;

    location ^~ /assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
    }

    try_files $uri/index.html $uri @unicorn;
    location @unicorn {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://unicorn;
    }

    error_page 500 502 503 504 /500.html;
    client_max_body_size 4G;
    keepalive_timeout 10;
    }
    9 changes: 9 additions & 0 deletions unicorn.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    root = "/home/bernhard/apps/depot/current"
    working_directory root
    pid "#{root}/tmp/pids/unicorn.pid"
    stderr_path "#{root}/log/unicorn.log"
    stdout_path "#{root}/log/unicorn.log"

    listen "/tmp/unicorn.blog.sock"
    worker_processes 2
    timeout 30
    75 changes: 75 additions & 0 deletions unicorn_init.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,75 @@
    #!/bin/sh
    set -e

    # Feel free to change any of the following variables for your app:
    TIMEOUT=${TIMEOUT-60}
    APP_ROOT=/home/bernhard/apps/depot/current
    PID=$APP_ROOT/tmp/pids/unicorn.pid
    CMD="cd $APP_ROOT; bundle exec unicorn -D -c $APP_ROOT/config/unicorn.rb -E production"
    AS_USER=bernhard
    set -u

    OLD_PIN="$PID.oldbin"

    sig () {
    test -s "$PID" && kill -$1 `cat $PID`
    }

    oldsig () {
    test -s $OLD_PIN && kill -$1 `cat $OLD_PIN`
    }

    run () {
    if [ "$(id -un)" = "$AS_USER" ]; then
    eval $1
    else
    su -c "$1" - $AS_USER
    fi
    }

    case "$1" in
    start)
    sig 0 && echo >&2 "Already running" && exit 0
    run "$CMD"
    ;;
    stop)
    sig QUIT && exit 0
    echo >&2 "Not running"
    ;;
    force-stop)
    sig TERM && exit 0
    echo >&2 "Not running"
    ;;
    restart|reload)
    sig HUP && echo reloaded OK && exit 0
    echo >&2 "Couldn't reload, starting '$CMD' instead"
    run "$CMD"
    ;;
    upgrade)
    if sig USR2 && sleep 2 && sig 0 && oldsig QUIT
    then
    n=$TIMEOUT
    while test -s $OLD_PIN && test $n -ge 0
    do
    printf '.' && sleep 1 && n=$(( $n - 1 ))
    done
    echo

    if test $n -lt 0 && test -s $OLD_PIN
    then
    echo >&2 "$OLD_PIN still exists after $TIMEOUT seconds"
    exit 1
    fi
    exit 0
    fi
    echo >&2 "Couldn't upgrade, starting '$CMD' instead"
    run "$CMD"
    ;;
    reopen-logs)
    sig USR1
    ;;
    *)
    echo >&2 "Usage: $0 <start|stop|restart|upgrade|force-stop|reopen-logs>"
    exit 1
    ;;
    esac
  5. bxt created this gist Oct 31, 2012.
    8 changes: 8 additions & 0 deletions Capfile
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    load 'deploy'

    # Uncomment if you are using Rails' asset pipeline
    load 'deploy/assets'

    Dir['vendor/gems/*/recipes/*.rb','vendor/plugins/*/recipes/*.rb'].each { |plugin| load(plugin) }

    load 'config/deploy' # remove this line to skip loading any of the default tasks