将capistrano v2 deploy.rb转换为capistrano v3 deploy.rb

时间:2013-12-02 23:37:38

标签: capistrano config file-conversion capistrano3

尝试将这段代码赋予capistrano版本3无济于事。一切都变了。任何人都可以帮我正确转换它?

旧版本2 deploy.rb:

#require 'new_relic/recipes'
require 'bundler/capistrano'
require './config/boot'
require 'whenever/capistrano'
require 'rvm/capistrano'
require 'bundler/capistrano'
require 'puma/capistrano'


set :application, "books"
set :whenever_command, "bundle exec whenever"
set :rvm_type, :system
set :rvm_ruby_string, ENV['GEM_HOME'].gsub(/.*\//,'')
set :scm, :git
set :repository, "/srv/books.git"

set :bundle_flags, "--deployment --binstubs"
set (:bundle_cmd) { "#{release_path}/bin/bundle" }

set :branch, "master"
set :migrate_target, :current
set :ssh_options, {:forward_agent => true}
set :rails_env, "production"
set :deploy_to, "/srv/books"
set :normalize_asset_timestamps, false

set :keep_releases, 5
after "deploy:update", "deploy:cleanup"
#, "deploy:eye:stop", "deploy:eye:start"


set :user, "root"
set :group, ""
set :use_sudo, false
default_run_options[:pty] = true

#set :port, 5984
#ssh_options[:port] = 5984
set :port, 22
ssh_options[:port] = 22

role :web,  "x"
role :app,  "x"
role :db,   "x", :primary => true

set(:latest_release) { fetch(:current_path) }
set(:release_path) { fetch(:current_path) }
set(:current_release) { fetch(:current_path) }

set(:current_revision) { capture("cd #{current_path}; git rev-parse --short HEAD").strip }
set(:latest_revision) { capture("cd #{current_path}; git rev-parse --short HEAD").strip }
set(:previous_revision) { capture("cd #{current_path}; git rev-parse --short HEAD@{1}").strip }

default_environment["RAILS_ENV"] = 'production'

#before :deploy, 'pgsql:backup'#, "deploy:rvm:trust_rvmrc"
load 'config/recipes/asset_pipeline.cap'
load 'config/recipes/database.cap'
load 'config/recipes/misc'
#load 'config/recipes/performance'
load 'config/recipes/rvm'
load 'config/recipes/web.cap'
load 'config/recipes/eye.cap'

#new relic
#after "deploy", "newrelic:notice_deployment"
#after "deploy:update", "newrelic:notice_deployment"
#after "deploy:migrations", "newrelic:notice_deployment"
#after "deploy:cold", "newrelic:notice_deployment"

namespace :deploy do

  desc "Deploy your application"
  task :default do
    cleanlog
    update
    migrate
    #sitemap
    restart_nginx
  end

  desc "Setup your git-based deployment app."
  task :setup, :except => {:no_release => true} do
    dirs = [deploy_to, shared_path]
    dirs += shared_children.map { |d| File.join(shared_path, d) }
    run "#{try_sudo} mkdir -p #{dirs.join(' ')} && #{try_sudo} chmod g+w #{dirs.join(' ')}"
    run "git clone #{repository} #{current_path}"
  end

  task :cold do
    update
    migrate
  end

  desc "Update the deployed code."
  task :update_code, :except => {:no_release => true} do
    run "cd #{current_path}; git fetch origin; git reset --hard #{branch}"
    finalize_update
  end

  desc "Update the database (overwritten to avoid symlink)"
  task :migrations do
    transaction do
      update_code
    end
    migrate
    restart
  end

  task :finalize_update, :except => {:no_release => true} do
    run "chmod -R g+w #{latest_release}" if fetch(:group_writable, true)

    # mkdir -p is making sure that the directories are there for some SCM's that don't
    # save empty folders
    # ln -sf #{shared_path}/database.yml #{latest_release}/config/database.yml
    run <<-CMD
      rm -rf #{latest_release}/log #{latest_release}/public/system #{latest_release}/tmp/pids &&
      mkdir -p #{latest_release}/public &&
      mkdir -p #{latest_release}/tmp &&
      ln -s #{shared_path}/log #{latest_release}/log &&
      ln -s #{shared_path}/system #{latest_release}/public/system &&
      ln -s #{shared_path}/pids #{latest_release}/tmp/pids

    CMD

    if fetch(:normalize_asset_timestamps, true)
      stamp = Time.now.utc.strftime("%Y%m%d%H%M.%S")
      asset_paths = fetch(:public_children, %w(images stylesheets javascripts)).map { |p| "#{latest_release}/public/#{p}" }.join(" ")
      run "find #{asset_paths} -exec touch -t #{stamp} {} ';'; true", :env => {"TZ" => "UTC"}
    end
  end

  desc "Zero-downtime restart of Unicorn"
  task :restart, :except => {:no_release => true} do
    run "#{try_sudo} touch #{File.join(current_path, 'tmp', 'restart.txt')}"
    run "kill -s USR2 `cat /srv/books/shared/tmp/pids/unicorn.pid`"
    deploy.eye.stop
    deploy.eye.start
  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

  namespace :rollback do
    desc "Moves the repo back to the previous version of HEAD"
    task :repo, :except => {:no_release => true} do
      set :branch, "HEAD@{1}"
      deploy.default
    end

    desc "Rewrite reflog so HEAD@{1} will continue to point to at the next previous release."
    task :cleanup, :except => {:no_release => true} do
      run "cd #{current_path}; git reflog delete --rewrite HEAD@{1}; git reflog delete --rewrite HEAD@{1}"
    end

    desc "Rolls back to the previously deployed version."
    task :default do
      rollback.repo
      rollback.cleanup
    end
  end
end

def run_rake(cmd)
  run "cd #{current_path}; #{rake} #{cmd}"
end

新版本V3 deploy.rb:

set :application, 'books'
set :repo_url, 'root@prod:srv/books.git'

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

# set :deploy_to, '/var/www/my_app'
# set :scm, :git

# set :format, :pretty
# set :log_level, :debug
# set :pty, true

# set :linked_files, %w{config/database.yml}
# set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}

# set :default_env, { path: "/opt/ruby/bin:$PATH" }
# 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 :touch, release_path.join('tmp/restart.txt')
    end
  end

  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

  after :finishing, 'deploy:cleanup'


  ## CUSTOM NEEDS UPDATE
  desc "Clearing the production log"
  task :cleanlog do
    run "cd #{current_path}; rake log:clear"
  end

  desc "Refresh the sitemap."
  task :sitemap do
    run "cd #{current_path}; rake sitemap:refresh"
  end

  task :update do
    transaction do
      update_code
    end
  end

end

1 个答案:

答案 0 :(得分:1)

在每个需要添加角色的任务中,如下所示:

  task :update do    
     on roles(:web) do
       transaction do
         update_code
       end
     end
  end