开发者

What is the best way to store project specific config info in ruby Rake tasks?

I have rake tasks for getting the production database from the remote server, etc. It's always the same tasks but the server info changes per project. I have the code开发者_JAVA技巧 here: https://gist.github.com/868423 In the last task, I'm getting a @local_db_dir_path = nil error.

I don't think want to use shell environment variables because I don't want to set them up each time I use rake or open a new shell.


Stick the settings in a YAML file, and read it like this:

require 'yaml'
config = YAML.load("config.yaml") # or wherever

$remote_host = config['remote_host']
$ssh_username = config['ssh_username']
# and so on

Or you can just read one big config hash:

$config = YAML.load("config.yaml")

Note that I'm using globals here, not instance variables, so there's no chance of being surprised by variable scope.

config.yaml would then look like this:

--- 
remote_host: some.host.name
ssh_username: myusername
other_setting: foo
whatever: bar

I tend to keep a config.yaml.sample checked in with the main body of the code which has example but non-working settings for everything which I can copy across to the non-versioned config.yaml. Some people like to keep their config.yaml checked in to a live branch on the server itself, so that it's versioned, but I've never bothered with that.


you should be using capistrano for this, you could use mulitsage or just separate host setting to a task, example capistrano would look like this:

task :development do
  server "development.host"
end

task :backup do
  run "cd #{current_path}; rake db:dump"
  download "remote_path", "local_path"
end

and call it like this:

cap development backup
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜