Vinova tuyển lập trình viên Mobile & Web ở Hà Nội, lương $300-1000

Article: Cài đặt project từ xa với Capistrano 1647

ngocdaothanh.myopenid.com 172
Updated over 3 years ago

Project đều phải qua nhiều công đoạn, thường là: (1) xác định nhu cầu, (2a) thiết kế sơ bộ, (2b) thiết kế chi tiết, (3) viết chương trình, (4) kiểm tra, (5a) cài đặt, (5b) bảo trì (thiết kế sơ bộ dùng khi bàn bạc với khách hàng, thường là sơ đồ màn hình và cách chương trình hoạt động, còn thiết kế chi tiết dùng giữa lập trình viên với nhau). Thường quá trình không diễn ra thẳng ruột ngựa từ (1) đến (5), mà phải quay vòng. Ví dụ thường gặp nhất là khách hàng cứ vài ngày lại muốn xem project tiến hành đến đâu để có thể bàn bạc góp ý. Lúc này ta có chu (chu = xoay vòng) trình: (3) -> (4) -> (5a) -> (3) -> (4) -> (5a) -> ...

Như vậy, thường ta phải cài đặt rất nhiều lần. Với web project thì quả cực hình vì mỗi lần đều phải: SSH vào server, cập nhật mã nguồn, cập nhật cơ sở dữ liệu, khởi động server v.v. mỗi lần mất cả mươi phút. Bài viết này giới thiệu công cụ Capistrano giúp tự động hóa qui trình trên, rút ngắn thời gian mỗi lần cài đặt xuống còn vài giây.

Capistrano là gì?

Capistrano đã rất nổi tiếng, hãy đọc Wikipedia, thông tin ở trang nhà, và tự google thêm. Nó hoạt động tương tự các công cụ tự động hóa như make, ant, maven. Điểm khác biệt là thay vì tác động lên máy của bạn, nó tác động lên server ở xa.

Capistrano viết bằng Ruby nên project viết bằng Ruby đều dùng, nhưng hiển nhiên project Java này nọ vẫn dùng được vô tư, miễn project có nhu cầu cài đặt từ xa thì đều dùng Capistrano được ngốc ạ{#emotions_dlg.tongue_out}.

Cài đặt Rails project để chạy với Mongrel

Để hiểu phần này, cần biết Rails và đọc qua ít nhất Getting Started.

Nếu make có makefile, ant có build.xml, maven có pom.xml, thì Capistrano có Capfile, là tập hợp các task. cho phép override và thêm before/after hook cho task. Ta override 3 task deploy:start, deploy:stop, deploy:restart như sau:

# If you use SSH key on Mac OS, run the following before running cap xxx:
# eval `ssh-agent`
# ssh-add

set :application, 'myapplication'
set :repository, 'http://path/to/mysubversion/repository/trunk'
set :host, 'myserver.com'
set :user, 'me'
set :deploy_to, "/home/#{user}/#{application}"

set :deploy_via, :export # Use export rather than checkout
set :runner, nil
set :mongrel_conf, "#{deploy_to}/current/config/mongrel_cluster.yml"

#-------------------------------------------------------------------------------

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

namespace :deploy do
namespace :mongrel do
[:stop, :start, :restart].each do |t|
desc "#{t.to_s.capitalize} the mongrel appserver"
task t, :roles => :app do
run "mongrel_rails cluster::#{t.to_s} -C #{mongrel_conf}"
end
end
end

desc "Custom restart task for mongrel cluster"
task :restart, :roles => :app, :except => {:no_release => true} do
deploy.mongrel.restart
end

desc "Custom start task for mongrel cluster"
task :start, :roles => :app do
deploy.mongrel.start
end

desc "Custom stop task for mongrel cluster"
task :stop, :roles => :app do
deploy.mongrel.stop
end
end

Lần cài đặt đầu chạy cap deploy:setup rồi cap deploy:cold là xong. Các lần sau chỉ cần chạy cap deploy. Tập tin mongrel_cluster.yml cần có dòng cwd (change working directory) như sau:

---
cwd: /absolute/path/to/myrails/app/current
log_file: log/mongrel.log
port: "3001"
environment: production
address: 127.0.0.1
pid_file: tmp/pids/mongrel.pid
servers: 5

Cấu hình riêng cho từng server

Nếu project cần cài lên nhiều server, thì vì thường mỗi server có cấu hình khác nhau, ta cần có phần cấu hình riêng cho từng server. Project Rails thường có 3 tập tin cấu hình cần điều chỉnh khi cài đặt:

  • config/database.yml
  • config/mongrel_cluster.yml
  • config/initializers/config.rb

Ta giải quyết bằng cách cấu hình thủ công khi cài đặt lần đầu như sau:

  • Khi viết chương trình, tạo database.yml.sample, mongrel_cluster.yml.sample, config.rb.sample có nội dug chung chung không ứng với server cụ thể.
  • Thêm đoạn sau vào deploy.rb, vào trong namespace deploy:
      task :after_symlink, :roles => :app do
    run "ln -s #{deploy_to}/shared/config/database.yml #{deploy_to}/current/config/database.yml"
    run "ln -s #{deploy_to}/shared/config/mongrel_cluster.yml #{deploy_to}/current/config/mongrel_cluster.yml"
    run "ln -s #{deploy_to}/shared/config/initializers/config.rb #{deploy_to}/current/config/initializers/config.rb"
    end
  • Lần cài đặt đầu: (1) chạy cap deploy:setup (2) SSH vào server, tạo thư mục config trong thư mục shared, copy các tập tin sample vào đó rồi sửa cho phù hợp (3) chạy cap deploy:cold.

Công cụ phụ trợ

Quản lí bằng giao diện web: Webistrano

Comments

ndgiang84.myopenid.com 2
over 3 years ago

em deploy mãi mà không đc

"cap setup" đã ok

"cap deploy" thì bị thế này:

  * executing task deploy
  * executing task update
 ** transaction: start
  * executing task update_code
  * querying latest revision...
*** [update_code] transaction: rollback
  * [update_code] rolling back
  * executing "rm -rf /xxxxxxx"
    servers: ["xx.xx.xx.xx"]
Password: 

    [xx.xx.xx.xx] executing command
    command finished
c:/ruby/lib/ruby/gems/1.8/gems/capistrano-1.4.1/lib/capistrano/scm/subversion.rb:87:in ``': No such file or directory - svn log --no-auth-cache   -q --limit 1 svn+ssh://xx.xx.xx.xx/.... (Errno::ENOENT)
        from c:/ruby/lib/ruby/gems/1.8/gems/capistrano-1.4.1/lib/capistrano/scm/subversion.rb:87:in `svn_log'
        from c:/ruby/lib/ruby/gems/1.8/gems/capistrano-1.4.1/lib/capistrano/scm/subversion.rb:23:in `latest_revision'
        from c:/ruby/lib/ruby/gems/1.8/gems/capistrano-1.4.1/lib/capistrano/configuration.rb:62:in `initialize'
        from c:/ruby/lib/ruby/gems/1.8/gems/capistrano-1.4.1/lib/capistrano/configuration.rb:89:in `call'
        from c:/ruby/lib/ruby/gems/1.8/gems/capistrano-1.4.1/lib/capistrano/configuration.rb:89:in `[]'
        from c:/ruby/lib/ruby/gems/1.8/gems/capistrano-1.4.1/lib/capistrano/configuration.rb:236:in `method_missing'
        from c:/ruby/lib/ruby/gems/1.8/gems/capistrano-1.4.1/lib/capistrano/scm/subversion.rb:63:in `checkout'
        from c:/ruby/lib/ruby/gems/1.8/gems/capistrano-1.4.1/lib/capistrano/recipes/standard.rb:80:in `load'
         ... 15 levels...
        from c:/ruby/lib/ruby/gems/1.8/gems/capistrano-1.4.1/lib/capistrano/cli.rb:12:in `execute!'
        from c:/ruby/lib/ruby/gems/1.8/gems/capistrano-1.4.1/bin/cap:11
        from c:/ruby/bin/cap:19:in `load'
        from c:/ruby/bin/cap:19

em đang xài capistrano 1.4.1, ruby 1.8.6.25. hic

anh xem giùm em với.

ngocdaothanh.myopenid.com 172
over 3 years ago

Ý tưởng của Capistrano là sau khi gõ lệnh cap trên máy mình, các lệnh trong deploy.rb sẽ được thực thi trên máy ở xa. Do đó, máy ở xa phải hoàn hảo, có đầy đủ những thứ như Subversion, Ruby v.v. Theo thông báo lỗi, có vẻ từ máy ở xa không update được source code bằng Subversion (chưa cài, sai user, sai password v.v.).

ndgiang84.myopenid.com 2
over 3 years ago

....e đã check trên deployment machine có đầy đủ Ruby, Rails, Capistrano đúng y version máy local.

ngoài ra ssh lên máy đó rồi gõ trực tiếp lệnh "svn log --no-auth-cache   -q --limit 1 svn+ssh://xx.xx.xx.xx/...." thì không bị lỗi như phía trên

về user/password, trên máy định deploy, trong .ssh/config đã có thông tin Host, User, password thì sẽ prompt khi cần thiết.

em đã thử ssh từ máy định deploy tới máy SVN với user/pass đó, thành công !

giúp em với !

ngocdaothanh.myopenid.com 172
over 3 years ago

Máy local thay vì dùng Windows, thử dùng Linux xem. Nếu vẫn không được, có thể deploy.rb viết sai chỗ nào đó.

You must login to be able to comment

Uploaded files

No file uploaded yet

You must login to be able to upload

Nhà tài trợ:

Mọi người đều tự do viết bài, sửa bài của người khác, và bình luận ở trang web này. Bạn muốn chủ động tạo bài mới để chia sẻ kinh nghiệm với mọi người? Xin click link ở dưới.

Create new content