开发者

Can I split up my Capistrano capfile?

I've got a capfile that has a role defined at the top of it, with a bunch of tasks below. It works great, but I want to be able to easily (and programatically) update the machines in the roles list. I know I could do it in place, but to be safe, I'd like to be able to split out my capfile into (essentially) two files: hosts and tasks

Currently (generically):

role :machines,
"machine1",
"machine2"

desc "This is task 1"
task :task1 do
  # stuf开发者_运维技巧f
end

I'd like to be able to have something like the following (ignore the "syntax"):

role :machines ==> {Get this information from 'hosts.cap' or something}

desc "This is task 1"
task :task1 do
  # stuff
end

Is there a way to break the capfile up? Or would I need to dive into source to do that?


Since a Capfile is just Ruby, you can use Ruby code to do what you want. For example, if your hosts.cap file looked like this:

db-master.example.com
db-slave1.example.com

you would get it in an array with this Ruby code:

File.read('hosts.cap').strip.split

and to give it to the role call properly, use the splat (*) operator:

role :db_hosts, *File.read('hosts.cap').strip.split

although I would recommend to put it in two parts, because it is clearer:

machines = File.read('hosts.cap').strip.split
role :db_hosts, *machines


The vanilla Capfile used to ship with the line load 'config/deploy' - maybe you can leverage that to load multiple files.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜