Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capistrano: How to Include common settings in multiple project deploy.rb files

this is probably a newbie ruby question. I have several libraries and apps that I need to deploy to several different hosts. All of the apps and libs will share some common settings for those hosts-- e.g. host name, database server/user/pass, etc.

My goal is to do something like:

cap host1 stage deploy
cap host2 stage deploy
cap host1 prod deploy
# ...

My question is how do you include these common settings in all of your deploy.rb files? More specifically, I want to create a an rb file that I can include that has some common settings and several host specific task definitions:

set :use_sudo, false
# set some other options

task :host1 do
    role :app, "host1.example.com"
    role :web, "host1.example.com"
    role :db,  "host1.example.com", :primary => true

    set :rodb_host, "dbhost"
    set :rodb_user, "user"
    set :rodb_pass, "pass"
    set :rodb_name, "db"
 end

 task :host2 do
     #...
 end

deploy.task :carsala do
    transaction do
        setup
        update_code
        symlink
    end
end

And then "include" this file in all of my deploy.rb files where I define stage, prod, etc and overwrite any "common" configuration parameters as necessary. Any suggestions would be appreciated. I've tried a few different things, but I get errors from cap for all of them.

Edit: I've tried

require 'my_module'

But I get errors complaining about an undefined task object.

like image 439
Damon Snyder Avatar asked Dec 23 '22 14:12

Damon Snyder


1 Answers

I just experimented with it a little more and what I discovered is that you have to:

load 'config/my_module'

I can put all of my common definitions here and just load it into my deploy.rb.

It appears from the docs that load loads and executes the file. Alternatively, require attempts to load the library specified. I'm not totally sure about real difference, but it appears that there is some separation between the current app symbol space and the library require'd (hence the errors about the undefined task object) that isn't a problem when you do a load.

like image 197
Damon Snyder Avatar answered Dec 28 '22 10:12

Damon Snyder