2016-03-09 63 views
0

我需要爲守護程序和capistrano爲我的rails應用程序部署mailman。我遇到了這個article,只是跟着他寫的步驟。如何重寫此Capistrano 2代碼以使用Capistrano 3語法? (Rails)

但是,當我試圖部署它,我的本地加工給了我這個錯誤:

The deploy has failed with an error: undefined local variable or method `mailman'. 

我知道爲什麼這個失敗是因爲我使用的是最新版本Capistrano的(3.4.0版本),而該代碼是舊版本的它(可能是版本2+)

下面是代碼:

# Mailman configuration 
namespace :mailman do 
    desc "Mailman::Start" 
    task :start, :roles => [:app] do 
     run "cd #{current_path};RAILS_ENV=#{rails_env} script/mailman_daemon.rb start" 
    end 

    desc "Mailman::Stop" 
    task :stop, :roles => [:app] do 
     run "cd #{current_path};RAILS_ENV=#{rails_env} script/mailman_daemon.rb stop" 
    end 

    desc "Mailman::Restart" 
    task :restart, :roles => [:app] do 
     mailman.stop mailman.start 
    end 
end 
before "deploy:cleanup", "mailman:restart" 

所以,我可以知道我怎麼可以把上面的代碼遵循最新的五個capistrano的rsion?

謝謝!

############################## UPDATE ############## ##########

下面是我的deploy.rb文件:

server '178.62.16.69', port: 22, roles: [:web, :app, :db], primary: true 

set :repo_url,  '[email protected]:ryzalyusoff/xxxx.git' 
set :application,  'xxxx' 
set :user,   'deploy' 
set :puma_threads, [4, 16] 
set :puma_workers, 0 

set :pty,    false 
set :use_sudo,  false 
set :stage,   :production 
set :deploy_via,  :remote_cache 
set :deploy_to,  "/home/#{fetch(:user)}/apps/#{fetch(:application)}" 
set :puma_bind,  "unix://#{shared_path}/tmp/sockets/#{fetch(:application)}-puma.sock" 
set :puma_state,  "#{shared_path}/tmp/pids/puma.state" 
set :puma_pid,  "#{shared_path}/tmp/pids/puma.pid" 
set :puma_access_log, "#{release_path}/log/puma.error.log" 
set :puma_error_log, "#{release_path}/log/puma.access.log" 
set :ssh_options,  { forward_agent: true, user: fetch(:user), keys: %w(~/.ssh/id_rsa.pub) } 
set :puma_preload_app, true 
set :puma_worker_timeout, nil 
set :puma_init_active_record, true # Change to false when not using ActiveRecord 


set :whenever_identifier, ->{ "myapp_#{fetch(:stage)}" } 

namespace :puma do 
    desc 'Create Directories for Puma Pids and Socket' 
    task :make_dirs do 
    on roles(:app) do 
     execute "mkdir #{shared_path}/tmp/sockets -p" 
     execute "mkdir #{shared_path}/tmp/pids -p" 
    end 
    end 

    before :start, :make_dirs 
end 

namespace :deploy do 
    desc "Make sure local git is in sync with remote." 
    task :check_revision do 
    on roles(:app) 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 
    end 

    desc 'Initial Deploy' 
    task :initial do 
    on roles(:app) do 
     before 'deploy:restart', 'puma:start' 
     invoke 'deploy' 
    end 
    end 

    desc 'Restart application' 
    task :restart do 
    on roles(:app), in: :sequence, wait: 5 do 
     invoke 'puma:restart' 
    end 
    end 

    before :starting,  :check_revision 
    after :finishing, :compile_assets 
    after :finishing, :cleanup 
    after :finishing, :restart 

end 


# Mailman configuration 
namespace :mailman do 
    desc "Mailman::Start" 
    task :start do 
    on roles(:app) do 
     within release_path do 
     with default_env: fetch(:default_env) do 
      execute "script/mailman_daemon.rb", "start" 
     end 
     end 
    end 
    end 

    desc "Mailman::Stop" 
    task :stop do 
    on roles(:app) do 
     within release_path do 
     with default_env: fetch(:default_env) do 
      execute "script/mailman_daemon.rb", "stop" 
     end 
     end 
    end 
    end 

    desc "Mailman::Restart" 
    task :restart do 
    on roles(:app) do 
     invoke("mailman:stop") 
     invoke("mailman:start") 
    end 
    end 
end 

before "deploy:cleanup", "mailman:restart" 

回答

2

Capistrano的3語法有了很大的變化,試試這個:

# Mailman configuration 
namespace :mailman do 
    desc "Mailman::Start" 
    task :start do 
    on roles(:app) do 
     within release_path do 
     with rails_env: fetch(:rails_env) do 
      execute "script/mailman_daemon.rb", "start" 
     end 
     end 
    end 
    end 

    desc "Mailman::Stop" 
    task :stop do 
    on roles(:app) do 
     within release_path do 
     with rails_env: fetch(:rails_env) do 
      execute "script/mailman_daemon.rb", "stop" 
     end 
     end 
    end 
    end 

    desc "Mailman::Restart" 
    task :restart do 
    on roles(:app) do 
     invoke("mailman:stop") 
     invoke("mailman:start") 
    end 
    end 
end 

before "deploy:cleanup", "mailman:restart" 

Capistrano upgrade guide爲更多的選擇。

+0

嘿謝謝你的答案!但是,當我嘗試它時,我得到了這個錯誤:'不知道如何構建任務'mailman:restart'' –

+0

我發現了一個錯字,請嘗試編輯後的版本。 – BoraMa

+0

嘿,現在我得到了這個錯誤:'script/mailman_daemon.rb stderr:/ usr/bin/env:ruby:沒有這樣的文件或目錄' –