0

我使用在 DigitalOcean 液滴上运行的 Capistrano 2.15 和 Nginx。

有时,特别是当我安装新的 gem 或运行迁移时,我需要硬重置我的 droplet 以使我的更改生效。

这不适用,如果我只是在某处更改代码,运行时

cap deploy:restart 

很好。

另外,当我登录到我的机器并运行时

service nginx restart 

它无助于捕捉新的迁移。

部署.rb

require "rvm/capistrano"

set :rvm_ruby_string, 'default'
set :rvm_type, :user

require "bundler/capistrano"

server "xx.xx.xx.xx", :web, :app, :db, primary: true

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

set :scm, "git"
set :repository, "git@bitbucket.org:xxx/#{application}.git"
set :branch, "master"

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

after "deploy", "deploy:cleanup" # keep only the last 5 releases

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
  desc "reload the database with seed data"
  task :seed do
    run "cd #{current_path}; bundle exec rake db:seed RAILS_ENV=#{rails_env}"
  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"

  desc "tail production log files" 
  task :tail_logs, :roles => :app do
    trap("INT") { puts 'Interupted'; exit 0; }
    run "tail -f #{shared_path}/log/unicorn.log" do |channel, stream, data|
      puts  # for an extra line break before the host name
      puts "#{channel[:host]}: #{data}" 
      break if stream == :err
    end
  end

end


namespace :assets do

  task :symlink, roles: :web do
    run ("rm -rf #{latest_release}/public/assets &&
          mkdir -p #{latest_release}/public &&
          mkdir -p #{shared_path}/assets &&
          ln -s #{shared_path}/assets #{latest_release}/public/assets")
  end
end

namespace :uploads do

    desc <<-EOD
      Creates the upload folders unless they exist
      and sets the proper upload permissions.
    EOD
    task :setup, :except => { :no_release => true } do
      dirs = [File.join(shared_path,'uploads' )]

      run "#{try_sudo} mkdir -p #{dirs.join(' ')} && #{try_sudo} chmod g+w #{dirs.join(' ')}"
    end 

    desc <<-EOD
      [internal] Creates the symlink to uploads shared folder
      for the most recently deployed version.
    EOD
    task :symlink, :except => { :no_release => true } do
      run "rm -rf #{release_path}/public/uploads"
      run "ln -nfs #{shared_path}/uploads #{release_path}/public/uploads"
    end

    desc <<-EOD
      [internal] Computes uploads directory paths
      and registers them in Capistrano environment.
    EOD
    task :register_dirs do
      set :uploads_dirs,    %w(public/uploads)
      set :shared_children, fetch(:shared_children) + fetch(:uploads_dirs)
    end

    after       "deploy:finalize_update", "uploads:symlink"
    on :start,  "uploads:register_dirs"

  end

这是 unicorn_init:

http://pastebin.com/cm6NCupK

4

1 回答 1

1

部署时使用 cap:deploy?

你用独角兽还是乘客?

请在此处粘贴您的代码 deploy.rb unicorn.rb(如果您使用 unicorn)和 unicorn_init.sh(或您从中创建符号链接的 sh 文件)

您使用 before_fork after_fork 的最后一个问题?(对于零停机部署?)

于 2014-05-11T10:03:17.983 回答