0

我在一个阶段将 Capistrano v3 中的配置实施到多台服务器时遇到问题。或者我想要一些不可能的东西。

我在 deploy.rb 中创建了一个测试任务,如下所示:

namespace :test do
  task :run do
    on roles(:all) do
      print "\nenvcode:\n"
      p fetch(:envcode)
      print "\nglobal_var:\n"
      p fetch(:global_var)
      print "\nssh_options:\n"
      p fetch(:ssh_options)
      print "\nmain_domain_name:\n"
      p fetch(:main_domain_name)
    end
  end
end

这是一个在同一阶段定义为模拟服务器的配置:

role :app, %w{ip1 ip2}
role :web, %w{ip1 ip2}
role :db,  %w{ip1 ip2}

set :main_domain_name, "#{fetch(:envcode)}.mytestdomain.com"
set :global_var, 'foobar'

set :ssh_options, -> do
  { 
    user: 'userip0'
  }
end

server 'ip1',
  roles: %w{web db app},
  ssh_options: { user: 'userip1' },
  user: 'userip1',
  envcode: "envip1",
  dbname: 'userip1',
  dbuser: 'userip1',
  dbpass: 'password',
  dbhost: 'localhost'

server 'ip2',
  roles: %w{web db app},
  ssh_options: { user: 'userip2' },
  user: 'userip2',
  envcode: "envip2",
  dbname: 'userip2',
  dbuser: 'userip2',
  dbpass: 'password',
  dbhost: 'localhost'

当我运行 cap staging test:run 我得到这个输出意味着 capistrano 完全忽略了我的服务器覆盖:

envcode:
nil

global_var:
"foobar"

ssh_options:
{:user=>"userip0"}
envcode:
nil

main_domain_name:
".mytestdomain.com"

global_var:
"foobar"

ssh_options:
{:user=>"userip0"}

main_domain_name:
".mytestdomain.com"

我希望得到:

envcode:
envip1

global_var:
"foobar"

ssh_options:
{:user=>"userip1"}
envcode:
envip2

main_domain_name:
"envip1.mytestdomain.com"

global_var:
"foobar"

ssh_options:
{:user=>"userip2"}

main_domain_name:
"envip2.mytestdomain.com"

我做错了什么,还是我对这些服务器配置阵列的理解有误?

4

1 回答 1

0

envcode是服务器的属性,不能只使用fetch方法来获取值。

namespace :test do
  task :run do
    on roles(:all) do |server|
      puts "envcode: #{server.properties.fetch(:envcode)}\n"
      puts "global_var: #{fetch(:global_var)}\n"
      puts "ssh_options: #{server.properties.fetch(:ssh_options)}\n"
      puts "main_domain_name: #{server.properties.fetch(:envcode)}.mytestdomain.com\n"
    end
  end
end
于 2015-02-05T08:14:03.657 回答