我可以拆分我的 Capistrano capfile 吗?

发布于 2024-12-03 18:56:54 字数 489 浏览 0 评论 0原文

我有一个 capfile,它的顶部定义了一个角色,下面有一堆任务。它工作得很好,但我希望能够轻松(并以编程方式)更新角色列表中的计算机。我知道我可以就地完成它,但为了安全起见,我希望能够将我的 capfile 分成(本质上)两个文件:主机和任务

当前(一般):

role :machines,
"machine1",
"machine2"

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

我希望能够类似以下内容(忽略“语法”):

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

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

有没有办法分解 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
  # stuff
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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泅人 2024-12-10 18:56:54

由于 Capfile 只是 Ruby,因此您可以使用 Ruby 代码来做您想做的事情。例如,如果您的hosts.cap 文件如下所示:

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

您将使用以下 Ruby 代码将其放入数组中:

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

并正确将其交给 role 调用,请使用 splat (*) 运算符:

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

尽管我建议将其分成两部分,因为这样更清楚:

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

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
冷月断魂刀 2024-12-10 18:56:54

普通的 Capfile 用于随 load 'config/deploy' 行一起发布 - 也许您可以利用它来加载多个文件。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文