2011-04-14 84 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