为什么我必须在部署后使用Capistrano

时间:2015-05-20 08:06:24

标签: ruby-on-rails ruby nginx passenger

Ubuntu服务器,生产模式。使用ruby(2.0)和Bundler安装RVM。 Capfile:

# Load DSL and Setup Up Stages
require 'capistrano/setup'

# Includes default deployment tasks
require 'capistrano/deploy'

# Includes tasks from other gems included in your Gemfile
#
# For documentation on these, see for example:
#
#   https://github.com/capistrano/rvm
#   https://github.com/capistrano/rbenv
#   https://github.com/capistrano/chruby
#   https://github.com/capistrano/bundler
#   https://github.com/capistrano/rails
#
require 'capistrano/rvm'
# require 'capistrano/rbenv'
# require 'capistrano/chruby'
require 'capistrano/bundler'
require 'capistrano/rails/assets'
require 'capistrano/rails/migrations'

# Loads custom tasks from `lib/capistrano/tasks' if you have any defined.
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }

配置/部署/生产:

# Simple Role Syntax
# ==================
# Supports bulk-adding hosts to roles, the primary server in each group
# is considered to be the first unless any hosts have the primary
# property set.  Don't declare `role :all`, it's a meta role.

# Extended Server Syntax
# ======================
# This can be used to drop a more detailed server definition into the
# server list. The second argument is a, or duck-types, Hash and is
# used to set extended properties on the server.

set :stage, :production
set :branch, 'master'
set :deploy_to, 'xxxx'
set :rails_env, 'production'
set :deploy_via, :copy

server 'xxxx', user: 'xxxx', roles: %w{app web db}

# Custom SSH Options
# ==================
# You may pass any option but keep in mind that net/ssh understands a
# limited set of options, consult[net/ssh documentation](http://net-ssh.github.io/net-ssh/classes/Net/SSH.html#method-c-start).
#
# Global options
# --------------
#  set :ssh_options, {
#    keys: %w(/home/rlisowski/.ssh/id_rsa),
#    forward_agent: false,
#    auth_methods: %w(password)
#  }
#
# And/or per server (overrides global)
# ------------------------------------
# server 'example.com',
#   user: 'user_name',
#   roles: %w{web app},
#   ssh_options: {
#     user: 'user_name', # overrides user setting above
#     keys: %w(/home/user_name/.ssh/id_rsa),
#     forward_agent: false,
#     auth_methods: %w(publickey password)
#     # password: 'please use keys'
#   }

deploy.rb:

# config valid only for Capistrano 3.1
lock '3.2.1'

set :application, 'voting_app'
set :scm,         :git
set :repo_url,    'git@bitbucket.org:xxxx/xxxx_app.git'

# Default branch is :master
# ask :branch, proc { `git rev-parse --abbrev-ref HEAD`.chomp }.call

# Default deploy_to directory is /var/www/my_app
# set :deploy_to, '/var/www/my_app'

# Default value for :scm is :git
# set :scm, :git

# Default value for :format is :pretty
# set :format, :pretty

# Default value for :log_level is :debug
# set :log_level, :debug

# Default value for :pty is false
# set :pty, true

# Default value for :linked_files is []
# set :linked_files, %w{config/database.yml}

# Default value for linked_dirs is []
# set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}

# Default value for default_env is {}
# set :default_env, { path: "/opt/ruby/bin:$PATH" }

# Default value for keep_releases is 5
# set :keep_releases, 5

namespace :deploy do

  desc 'Restart application'
  task :restart do
    on roles(:app), in: :sequence, wait: 5 do
      # Your restart mechanism here, for example:
      execute :mkdir, '-p', "#{ release_path }/tmp"
      execute :touch, release_path.join('tmp/restart.txt')
    end
  end

  after :publishing, :restart

  after :restart, :clear_cache do
    on roles(:web), in: :groups, limit: 3, wait: 10 do
      # Here we can do anything such as:
      # within release_path do
      #   execute :rake, 'cache:clear'
      # end
    end
  end

end

的Gemfile:

source 'https://rubygems.org'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.1.6'
# Use SLIM for HTML
gem 'slim'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.3'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
gem 'therubyracer',  platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0',          group: :doc

# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring',        group: :development

# Bootstrap
gem 'bootstrap-sass', '~> 3.3.4'

# Pagination
gem 'will_paginate-bootstrap'

# Authentication
gem 'clearance'
gem 'omniauth'
gem 'omniauth-facebook'
gem 'omniauth-vkontakte'

# Database
gem 'pg'

# Elasticsearch
gem 'elasticsearch-model'
gem 'elasticsearch-rails'

# Async tasks
gem 'sidekiq'

# Production deploy
group :development do
  gem 'capistrano',  '~> 3.2.0'
  gem 'capistrano-rails'
  gem 'capistrano-bundler'
  gem 'capistrano-rvm'
end

# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use unicorn as the app server
# gem 'unicorn'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

# Use debugger
# gem 'debugger', group: [:development, :test]

部署后capistrano运行迁移,但不执行'bundle install',因为我看到错误:

It looks like Bundler could not find a gem. Maybe you didn't install all the gems that this application needs. To install your gems, please run:

bundle install

-------- The exception is as follows: -------

Could not find rake-10.4.2 in any of the sources (Bundler::GemNotFound)

这是什么意思?我做错了什么?提前致谢!

2 个答案:

答案 0 :(得分:0)

您需要指定rvm ruby​​版本&您deploy.rb中的宝石集如下:

set :rvm_ruby_version, '2.0.xx@mygemset'

答案 1 :(得分:0)

ssh进入您的服务器并输入which bundler

它将返回如下内容:(您的ubuntu用户和ruby版本将不同)

/home/ubuntu_user/.rvm/gems/ruby-1.9.3-p327@global/bin/bundle

复制此行并在本地 - config / deploy / production:

set :bundle_cmd, /home/ubuntu_user/.rvm/gems/ruby-1.9.3-p327@global/bin/bundle

在同一档案中添加以下内容:

depend :remote, :gem, "bundler", ">=1.1.5"

(同时粘贴下面写的行并将“user”更改为您的ubuntu用户和ruby版本到您正在使用的版本)

set :default_environment, { 'BUNDLE_PATH' => '/home/user/.rvm/gems/ruby-1.9.3-p327@global/bin/', 'GEM_HOME' => '/home/user/.rvm/gems/ruby-1.9.3-p327', 'GEM_PATH'=>'/home/user/.rvm/gems/ruby-1.9.3-p327:/home/user/.rvm/gems/ruby-1.9.3-p327@global', 'PATH'=>'/home/user/.rvm/gems/ruby-1.9.3-p327/bin:/home/user/.rvm/gems/ruby-1.9.3-p327@global/bin:/home/user/.rvm/rubies/ruby-1.9.3-p327/bin:/home/user/.rvm/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games' }