Task execution based on :primary flag

671 views
Skip to first unread message

jmadtech

unread,
Dec 4, 2009, 9:59:27 AM12/4/09
to Capistrano
Hello all,

I am currently trying to write a set of task commands that run based
on whether or not a particular server within the role attached to the
task is the primary server.

So for example, I have the following:

task :push_my_configs, :on_error => :continue do
run "my command 1", :roles => :app, :only => { :primary => true }
run "my command 2", :roles => :app, :except => { :primary =>
true }
end

This works perfectly if I have the following:

role :app, "mydomain1.com", :primary => true
role :app, "mydomain2.com"

it executes command 1 on mydomain1.com and command 2 on mydomain2.com
exactly as I intend. However, if I have a situation where I only have
1 server, I get an error on one or the other command (depending on
whether or not I have the single server denoted as the primary).

'configs:push_my_configs' is only run for servers matching {}, but no
servers matched.

I'd be ok with the error if I could get it to proceed, but it won't.
It hits that and exits the deploy script.

How do I handle this situation?

Thanks in advance!

Donovan Bray

unread,
Dec 4, 2009, 9:30:08 PM12/4/09
to capis...@googlegroups.com
You could set :primary => false on any non primary servers and change
your run mycommand2 to :only => { :primary => false }

Keep in mind :primary must be set to false not omitted from the :role
specification.

If your use case supports it you may be able to:

> run "my command 2", :roles => :app
> run "my command 1", :roles => :app, :once => true

As long as c1 will overwrite the state of c2 and it's not critical
which server c1 is ran on; just that it's ran once.
> --
> * You received this message because you are subscribed to the Google
> Groups "Capistrano" group.
> * To post to this group, send email to capis...@googlegroups.com
> * To unsubscribe from this group, send email to capistrano+...@googlegroups.com
> For more options, visit this group at http://groups.google.com/group/capistrano?hl=en

jmadtech

unread,
Dec 7, 2009, 10:00:56 AM12/7/09
to Capistrano
the :only => { :primary => true } and :except => { :primary => true }
work as intended... but ONLY if I have at least 1 server that matches
each scenario.

Not every environment that I deploy to is guaranteed to have multiple
servers... some have 1, some have 2 and some have more than 2. I'm
trying to have 1 deploy script that works for all my scenarios (using
multi-stage)... but for the single server setups, the original run
commands fail when there are no servers beyond the primary.

Thoughts?

On Dec 4, 9:30 pm, Donovan Bray <donno...@gmail.com> wrote:
> You could set :primary => false on any non primary servers and change  
> your run mycommand2 to :only => { :primary => false }
>
> Keep in mind :primary must be set to false not omitted from the :role  
> specification.
>
> If your use case supports it you may be able to:
>
> > run "my command 2", :roles => :app
> > run "my command 1", :roles => :app, :once => true
>
> As long as c1 will overwrite the state of c2 and it's not critical  
> which server c1 is ran on; just that it's ran once.
>

Donovan Bray

unread,
Dec 7, 2009, 3:34:07 PM12/7/09
to capis...@googlegroups.com
I think I understand now, this is what I use to override capistrano's
behavior to allow tasks with empty servers to be skipped. (Which I
think should be default behavior), Just stick the file in with your
recipes and let it be required like normal.

http://gist.github.com/251078

Lee Hambley

unread,
Dec 7, 2009, 4:38:35 PM12/7/09
to capis...@googlegroups.com
Donoval,

You didn't feel like submitting a patch?

-- Lee Hambley

Twitter: @leehambley | @capistranorb

jmadtech

unread,
Dec 7, 2009, 8:01:44 PM12/7/09
to Capistrano
Donovan,

You're a life saver! That solved the problem perfectly.

Thanks again!

Donovan Bray

unread,
Dec 8, 2009, 11:44:44 AM12/8/09
to capis...@googlegroups.com
That's a good point Lee, at the time I was in a rush and I just
scratched my own itch.

I'll review that monkey patch and wrap the functionality with a
variable so that you can enable it with something like:

set :allow_empty_roles, true

in your deploy.rb

Do you prefer pull requests or patches?

Lee Hambley

unread,
Dec 8, 2009, 12:27:55 PM12/8/09
to capis...@googlegroups.com
Either/or 0 lets take the dicussion to the lighthouse app though -- I think something like another option to task would be nice and clean:

task :whatever, :raise_on_empty_roles => false do
 # whatever
end

-- Lee Hambley
Reply all
Reply to author
Forward
0 new messages