I agree with lee that this would be better as a rake task.
I've got some other lessons for you
You correctly said you would target the :app role but your snippet targets the
:web role, :web. Is generally your front end lb; and your code shouldn't be on
a lb.
> File.join("#{fixtures_directory}", "*.json")
Is not the same as
> File.join("#{fixtures_directory}","**","*.json)
the /**/ will match any file spec to the right arbitrarily deep. Ie a recursive
match.
It's not necessary to quote a single variable inside a string to pass to a
method
> File.join("#{fixtures_directory}","**","*.json) ==
> File.join(fixtures_directory,"**","*.json)
File.join in this context is actually dangerous; it's job is to normalize the
filespec separator on the ruby system it is running on; if this string you are
building is to be executed on the system running this ruby process then fine.
But if say you were on windows where the filespec separator is a backslash but
you were sending the string to be executed on a Linux box the separator would
be wrong.
> jsonfiles = File.join("#{fixtures_directory}", "*.json")
> puts("#{jsonfiles}")
The above does nothing in your script; it's never used and it's output will not
match what you actually glob.
As lee mentioned before unless it's in a run/sudo/capture the methods are all
running in the context of your local machine.
You have the following set:
> set :mongo_import, "mongoimport -d #{database_name} -c #{file}"
'file' in this case will always be nil because it doesn't have a value when
ruby executes that set; caps deferred execution won't help you on this case
either because it will only use the value the first time the variable is
invoked.
If you want a list of remote files you'll need to build the list remotely,
something like the following could work
> set :mongo_import, "mongoimport -d #{database_name} -c $(find
> #{fixtures_directory} -name *.json)"
Also
> set :fixtures_directory, "#{current_release}/fixtures/#{stage}"
You should almost always use latest_release instead of current_release because
current_release is only right during a deploy; latest_release will work both
during a deploy and if the task is called outside of the deploy cycle.
Last but not least this method will work fine while you only have one app
server; the moment you have more than one it will run on each server in
parallel you should protect it by using :once
you can remove all the dir settings and surrounding glob since you are building
the list remotely
> run "#{mongo_import}", :once => true
On Feb 24, 2012, at 6:06 AM, romain Simiand <[email protected]> wrote:
> Hi there,
>
> thanks for the info.
> So if I get you well, all I need to do is make
>
> File.join("#{fixtures_directory}", "*.json"
>
> run on #{current_release} server.
>
> Could I do that by simply applying the task to the web server, using :
>
> task :dbcleanup do, :roles => :app
>
> like in the example below ?
> Or am I missing something crucial here ?
>
> Also, here's an example of what I'm trying to do precisely
>
> namespace :database do
> desc "Load 'em fixtures"
> task :dbcleanup, :roles => :web do
> set :database_name, "dbname"
> set :fixtures_directory, "#{current_release}/fixtures/#{stage}"
> set :mongo_import, "mongoimport -d #{database_name} -c #{file}"
>
> jsonfiles = File.join("#{fixtures_directory}", "*.json")
> puts("#{jsonfiles}")
>
> files = File.join("#{fixtures_directory}","**","*.json")
> Dir.glob(files).each do |file|
> run "#{mongo_import}"
> end
>
> logger.debug "Ended"
> end
> end
>
>
> Am I clearer ?
> --
> * 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
--
* 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