-
Notifications
You must be signed in to change notification settings - Fork 5
Description
Allow specifying Fezzik env vars outside of destination blocks
A pattern we've commonly seen is to create a big hash of env options, declare it outside of a Fezzik.destination block, and then call Fezzik.env on each of the key value pairs in that hash inside of a Fezzik.destination block:
options = {
db_host: "localhost",
unicorn_workers: 1,
...
}
Fezzik.destination :vagrant do
set :domain, "..."
options.each { |key, value| Fezzik.env key, value }
...
end
Fezzik.destination :staging do
set :domain, "..."
options.each { |key, value| Fezzik.env key, value }
...
end
What we really want to do is just declare these env vars using Fez and let each host override some of them:
Fez.env :db_host, "localhost"
Fez.env :unicorn_workers, 1
...
Fezzik.destination :vagrant do
set :domain, "..."
Fezzik.env :unicorn_workers, 2
end
This syntax is shorter (you don't need an "options.each" block in each destination) but more importantly, it's DRY built directly into Fezzik. As we've seen, people are lazy when defining configuration and by default copy and paste things, even when they could use Ruby to make it more DRY. When the tool itself promotes good behavior, it's more likely to be used.
If you try to use Fezzik.env outside of a destination block, Fez throws an exception:
Please specify the server domain via the :domain variable
/Users/philc/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/rake-remote_task-2.0.6/lib/rake/remote_task.rb:311:in `block in mandatory'
/Users/philc/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/rake-remote_task-2.0.6/lib/rake/remote_task.rb:268:in `call'
/Users/philc/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/rake-remote_task-2.0.6/lib/rake/remote_task.rb:268:in `block in fetch'
/Users/philc/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/rake-remote_task-2.0.6/lib/rake/remote_task.rb:321:in `block in protect_env'
<internal:prelude>:10:in `synchronize'
/Users/philc/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/rake-remote_task-2.0.6/lib/rake/remote_task.rb:320:in `protect_env'
/Users/philc/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/rake-remote_task-2.0.6/lib/rake/remote_task.rb:266:in `fetch'
/Users/philc/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/rake-remote_task-2.0.6/lib/rake/remote_task.rb:429:in `block in set'
/Users/philc/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/fezzik-0.6.2/lib/fezzik/environment.rb:4:in `env'