Hello all, I have a quick question on the Capistrano roles and
switching them out quickly and on the fly. I'm using Cap to do site-
based cloning and storing the site configs in a {path_to_Capfile}/
settings/{name_of_config}.rb. Basically, I load the config file in by
first setting a local ENV variable with the path to the config file
and then with code like:
if FileTest.exist?( ENV['TARGET'] )
instance_eval(File.open(ENV['TARGET']).read)
ENV['TARGET'] = ENV['TARGET'].gsub(/.*settings\/(.*)\.rb/,'\1')
else
raise "The configuration file you specified does not exist. Please
check to make sure it is there."
end
That's not a problem. I'm also able to switch between TARGETs by
doing something like this:
task :switch_target do
set :current_target, new_target
roles.clear
if FileTest.exist?( current_target )
instance_eval(File.open(current_target).read)
ENV['TARGET'] = current_target.gsub(/.*settings\/(.*)\.rb/,'\1')
else
raise "The configuration file you specified does not exist.
Please check to make sure it is there."
end
pp("Now switching TARGET to #{current_target}")
end
You'll note the roles.clear line, which clears out the target
server(s) and then loads in the new ones from the config. If you
don't do a roles.clear, it keeps the old servers in the roles struct
through the session, and so tries to clone them on all servers
(including the original server that was being cloned).
So as a test, I created the following task to switch targets multiple
times just to make sure it was all working. You'll see that I create
the var 'old_target' to preserve the original, pass in the new one,
and switch between them on the fly. I'm also printing out the roles
struct in between each switch so I can see it clearing and recreating
it on demand:
task :test_switch_to_restart do
set :old_target, "settings/#{ENV['TARGET']}.rb"
pp(roles)
set :new_target, new_target
switch_target
pp(roles)
set :new_target, old_target
switch_target
pp(roles)
restart
end
That all works nicely! However, recently I created a large macro-task
that contains a number of smaller tasks within it and somehow broke
this process. For some reason the 'restart' task continues to be run
on a server that I thought I had cleared from the roles struct! Just
to be sure it was cleared from the roles, I printed it out just before
the restart task, and indeed it appears to be out of the struct and
yet Capistrano tries to run the restart task on it, as below (names of
servers and config files sanitized):
* executing task switch_target
"Now switching TARGET to settings/site_02.rb"
{:drb=>
[#<struct Capistrano::Configuration::Role
host="pr02.server.dmn.int",
options={}>],
:app=>
[#<struct Capistrano::Configuration::Role
host="pr02.server.dmn.int",
options={}>],
:db=>
[#<struct Capistrano::Configuration::Role
host="pr02.server.dmn.int",
options={:primary=>true}>],
:web=>
[#<struct Capistrano::Configuration::Role
host="pr02.server.dmn.int",
options={}>]}
* executing task restart
* executing task stop
* executing "sudo /etc/init.d/site_02 stop"
servers: ["pr00.server.dmn.int"]
[pr00.server.dmn.int] executing command
** [out :: pr00.server.dmn.int] sudo: /etc/init.d/site_02: command
not found
command finished
command "sudo /etc/init.d/site_02 stop" failed on pr00.server.dmn.int
You can see that it sets the new target appropriately and empties the
roles struct then adds its server in. Right after that I call the
'restart' task and it tries to execute it on pr00.server.dmn.int
*instead* of pr02.server.dmn.int, even though pr02.server.dmn.int is
the only server listed in the struct. The restart task itself is
fairly simple and looks like this:
task :restart, :roles => [:app] do
stop
start
end
The server pr02.server.dmn.int is shown to have populated the :app
portion of the struct, so why would the restart task attempt to run on
pr00.server.dmn.int instead? If anyone has any ideas about how I
could further troubleshoot this or get to the bottom of this
mysterious server that I thought was cleared from my roles, it would
be greatly appreciated!
Thanks,
-ctrvl
--
* You received this message because you are subscribed to the Google Groups
"Capistrano" group.
* To post to this group, send email to [email protected]
* To unsubscribe from this group, send email to
[email protected] For more options, visit this group at
http://groups.google.com/group/capistrano?hl=en
To unsubscribe from this group, send email to
capistrano+unsubscribegooglegroups.com or reply to this email with the words
"REMOVE ME" as the subject.