简体   繁体   中英

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 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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM