2011-04-14 64 views
1

Capistrano似乎沒有適當地處理角色 - 至少我怎麼理解他們。我不能讓下面的簡單Capfile按預期工作:capistrano角色和主機問題/錯誤?

role :test1, "[email protected]" 
role :test2, "[email protected]" 


task :full_test, :roles => [:test1,:test2] do 
    log_test1 
    log_test2 
end 

task :log_test1, :roles => :test1 do 
    logger.info "TEST1 !!!" 
    run "echo `hostname`" 
end 

task :log_test2, :roles => :test2 do 
    logger.info "TEST2 !!!" 
    run "echo `hostname`" 
end 

當我嘗試用角色的角色限制=執行:爲test1,在log_test2是未聲明爲同一臺主機上仍會執行作爲角色的一部分:test2!卡皮斯特拉諾的預期行爲是?如果預期的話,有什麼辦法可以防止這種情況發生?

ROLES=test1 cap full_test 
    * executing `full_test' 
    * executing `log_test1' 
** TEST1 !!! 
    * executing "echo `hostname`" 
    servers: ["beta-app-01"] 
    [[email protected]] executing command 
** [out :: [email protected]] ec2-*****.compute-1.amazonaws.com 
    command finished in 350ms 
    * executing `log_test2' <<<< shouldn't that be filtered ? because of :roles => :test2 ? 
** TEST2 !!! 
    * executing "echo `hostname`" 
    servers: ["beta-app-01"] 
    [[email protected]] executing command 
** [out :: [email protected]] ec2-*****.compute-1.amazonaws.com 
    command finished in 410ms 

由於提前,相關條目(http://stackoverflow.com/questions/754015/creating-a-capistrano-task-that-performs-different-tasks-based-on-role)我可以發現似乎並沒有涵蓋這個問題......

回答

1

我從來不明白爲什麼卡皮斯特拉諾這樣做,但卡米斯特拉諾的原始維護者賈米斯巴克說,it has to be the ways it is

爲了解決這個問題,只需創建一個xtask方法以及與此更換所有任務電話:

# Tasks are executed even on servers which do not have the right role 
# see http://www.mail-archive.com/[email protected]/msg01312.html 
def xtask(name, options={}, &block) 
    task(name, options) do 
    if find_servers_for_task(current_task).empty? 
     logger.info '... NOT on this role!' 
    else 
     block.call 
    end 
    end 
end 

此外,不要在隨機位置定義這個方法,就像在從您的Capfile開始。它必須在加載路徑中存在的文件中定義(誰知道爲什麼)。例如,寫在一個新的助手/ roles.rb文件xtask方法,並添加到您的Capfile

# Add the current directory to the load path, it's mandatory 
# to load the helpers (especially the +xtask+ method) 
$:.unshift File.expand_path(File.dirname(__FILE__)) 
require 'helpers/roles' 

如果你不這樣做,一個帽-T將顯示使用xtask定義的所有任務不會有任何名稱空間,因此如果它們在不同名稱空間中具有相同名稱,它們將相互覆蓋。

+0

謝謝!那就是訣竅... – zuzur 2011-04-27 05:28:03