Header

  1. View current page

    aproxacs

Profile_img_60x60_01
112

[Rails] Capistrano 2.0 을 사용해서 deployment 자동화하기

개요

Capistrano 를 통해 rails application의 배포를 자동화 할 수 있다. 이 문서는 mongrel_cluster와 capistrano 2.0 을 사용해 자동화하는 방법에 대해 설명한다.

mongrel과 apache + proxy_balancer를 사용해서 debian에 application을 설치하는 것은 application deployment on debian 를 참조.

이 문서는 mongrel을 사용한다고 가정하고 있지만, mongrel을 사용하기 보다는 passenger를 사용하기를 추천한다.(설정이 훨씬 간단하다.)

 

설치

 gem을 이용해 설치를 하자

  1. gem install capistrano

 

가정들

  • subversion이 서버 어딘가에 설치되어 있어야 한다.
  • subversion에 local과 서비스를 하려고 하는 server에서 접속할 수 있어야 한다.
  • mongrel과 mongrel_cluster를 사용한다.
  • server는 ssh을 지원한다.

 

Mongrel 설정

mogrel_cluster 설정을 하자. config/mongrel_cluster.rb가 없다면 다음을 실행하자.

  1. mongrel_rails cluster::configure -e production -p 3001 -a 127.0.0.1 -N 2 -c /myapp_path/current

 

생성된 config/mongrel_cluster.rb는 다음과 같다.

  1. cwd: /home/myapp_path/current
    log_file: log/mongrel.log
    port: "3001"
    environment: production
    address: 127.0.0.1
    pid_file: tmp/pids/mongrel.pid
    servers: 2

 

Capistrano 설정

다음을 실행해서 Capfile과 config/deploy.rb 를 생성하자.

  1. capify .

 

아래 명령을 이용해 사용할 수 있는 명령들을 확인할 수 있다.

  1. cap -T

 

config/deploy.rb 를 수정해서 환경을 만들자

  1. require 'mongrel_cluster/recipes'
    set :application, "myapp"
    set :repository, "http://svn.myhost.com/svn/#{application}/trunk"
    # We only have one host
    role :web, "myapp.com"
    role :app, "myapp.com"
    role :db, "myapp.com", :primary => true
    # Don't forget to change this
    set :deploy_to, "/home/#{application}"

    set :use_sudo, false

  2. set :scm_password, "1111"
  3. set :scm_username, "svn_user"

    set :mongrel_conf, "#{current_path}/config/mongrel_cluster.yml"

 

mongrel_cluster는 capistrano의 start/stop/restart task를 mongrel_cluster/recipes.rb에 재정의해 놓았다. require 'mongrel_cluster/recipes' 를 함으로써 start/stop/restart task에 대해서 mongrel_cluster를 이용하게 된다.

 

:application => application 이름

:repository => svn repository 이름

:deploy_to => server에 application을 설치하고자 하는 directory,

default 값은 /u/apps/#{application}

:use_sudo => server 명령을 sudo를 사용해서 실행하고 싶지 않은 경우 false로 정하자.

:scm_user => subversion의 user 이름

:scm_password => subversion 의 password

 

각 task에는 수행하고자 하는 server를 :roles를 이용해서 지정해줄 수 있는데 rails recipes 에서는 app, web, db 를 사용한다.

  • role :app => rails application이 배포될 서버 이름들.
  • role :web => web 서비스를 대표하는 서버(apache를 사용하는 경우 apache가 있는 서버정도..)
    deploy:web:disable, deploy:web:enable 에 사용됨.
  • role :db => database 관련 작업을 할 서버의 이름. ( migrate를 수행할 서버의 이름 )
    deploy:migrate 에 사용됨.

서버 이름을 다음과 같은 형식으로 사용할 수 있다.

  1. user@host:port

예를 들어 서버 이름이 chang@myapp.com 이라면, capistrano는 task를 수행하기 위해 myapp.com에 chang이라는 계정으로 접속을 시도한다.

 

Capistrano 실행

다음을 실행해 초기화를 하자.

  1. cap deploy:setup

capistrano는 server에 ssh로 login을 해서 아래 directory들을 생성할 것이다.

  1. #{deploy_to}/

  2. #{deploy_to}/releases

  3. #{deploy_to}/shared

  4. #{deploy_to}/shared/log

  5. #{deploy_to}/shared/system

  6. #{deploy_to}/shared/pidsca

 

다음을 실행해 필요한 것들이 잘 생성되었는지 확인하자.

  1. cap deploy:check

 

첫번째 release를 해보자.

  1. cap deploy:cold

release 과정은 deploy_to/releases/20070801144124 에 svn으로 부터 checkout을 받는다.

deploy_to/current 가 2007080114412를 가리키도록 만든다.

rails migration을 수행한다.

server를 시작 한다.

 

두번째 release 부터는 다음을 사용하자.

  1. cap deploy

 

migration의 수행은

  1. cap deploy:migrate RAILS_ENV=production

서버의 시작은

  1. cap cap deploy:start

서버의 정지는

  1. cap deploy:stop

서버의 재시작은

  1. cap deploy:restart

 

에러들

다음 에러가 발생한다면

  1. connection failed for: pichu (Net::SSH::AuthenticationFailed: chang)

 password가 잘못 입력되었거나 chang 이라는 계정이 server에 없는 것이다. chang이란 현재 local의 계정이다. capistrano는 이 계정 이름으로 server와 svn에 접속을 시도한다.

 

만약 다음과 같은 에러가 발생한다면

  1. C:\webroot\pichu>cap deploy:cold
      * executing `deploy:cold'
      * executing `deploy:update'
     ** transaction: start
      * executing `deploy:update_code'
    *** [deploy:update_code] rolling back
      * executing "rm -rf /home/pichu/releases/20070801144124; true"
        servers: ["pichu"]
    Password:
        [pichu] executing command
        command finished

이것은 local이 window인 경우 svn 이 설치되지 않았기 때문에 발생하는 것이니

http://subversion.tigris.org/servlets/ProjectDocumentList?folderID=91 에서 subversion binary version을 다운 받아서 설치하자. ( 설치후 cmd shell을 다시 실행시키는 것도 잊지 말자 )

 

아래와 같은 상태에서 멈춰있는 경우가 있는데,

  1. C:\webroot\pichu>cap deploy:cold
      * executing `deploy:cold'
      * executing `deploy:update'
     ** transaction: start
      * executing `deploy:update_code'
      * executing "svn checkout -q  -r61 svn://1.1.1.1/pichu/trunk/pichu /home/pichu/releases/20070801145441 && (echo 61 > /home/pichu/releases/20070801145441/REVISION)"
        servers: ["pichu"]
    Password:
        [pichu] executing command
     ** [out] 인증 영역(realm): <svn://1.1.1.1:3690> Pichu Project
     ** [out] 'chang'의 암호:

server 에 chang id로 접속해서 "svn checkout -q  -r61 svn://1.1.1.1/pichu/trunk/pichu /home/pichu/releases/20070801145441" 를 한번 실행해주면 멈추지 않는다. (좀더 멋지게 해결할수 있는 방법은 없을까..?)

 

rake가 실패하는 경우

  1. command "cd /home/pichu/releases/20070801152127; rake RAILS_ENV=production  db:migrate" failed on pchu

db의 pichu 계정에 지정한 host에서 접근을 허용하지 않는다는 것 같은데... 일단 host를 localhost로 변경해서 넘어가기로 하고..

 

아래의 설명은 mongrel_cluster-1.0.2 와 capistrano 2.0.0을 사용할 때 발생하는 문제이다.  mongrel_cluster-1.0.5에서는 해결되었다.

spin을 이용해 deploy:start가 안된다면서 에러가 나는 경우

  1. command "sudo -u app sh -c 'cd /home/pichu/current && nohup script/spin'" failed on pichu

capistrano의 기본 depoly:start는 spin을 사용해서 application 을 시작한다. 그러나 우리는 mongrel_cluster를 사용해서 server를 수행하고자 한다. spin에 대한 어떤 설정도 해주지 않았기 때문에 불평을 하고 있는 것이다. 에러를 발생하지 않도록 하는 방법은 에러가 발생하는 capistrano의 task를 재정의 해 주는 것이다. mongrel_cluster gem의 살펴보면 mongrel_cluster-1.0.2\lib\mongrel_cluster에 recipes.rb가 있다.이 파일은 restart와 spinner task를 재정의하고 있는데 caspirano2.0과 namespace가 달라서 동작하지 않는다. namespace를 추가해 주자. (아마도 capistrano2가 namespace를 추가했는데 mongrel_cluster 는 이것을 아직 반영하지 못한 듯하다...)

  1. namespace :deploy do
      desc <<-DESC
      Restart the Mongrel processes on the app server by calling restart_mongrel_cluster.
      DESC
      task :restart, :roles => :app do
        restart_mongrel_cluster
      end
     
      desc <<-DESC
      Start the Mongrel processes on the app server by calling start_mongrel_cluster.
      DESC
      task :start, :roles => :app do
        start_mongrel_cluster
      end
    end

 

capistrano 에 자신의 task를 추가

config/deploy.rb에 자신만의 task를 추가할 수 있다.

다음은 svn update를 수행한 후에 asset_packager를 수행시키는 task이다.

  1. namespace :deploy do
      task :after_update_code, :roles => [:web] do
        run <<-EOF
          cd #{release_path} && rake RAILS_ENV=production asset:packager:build_all && env EVENT=1
        EOF
      end
    end

 

SSH key를 사용해서 로그인 자동화하기

참조

 

Tags

History

Last edited on 05/07/2008 19:15 by 창

Comments (0)

You must log in to leave a comment. Please sign in.