How to deploy Rails 3 project using "Rake"? - ruby-on-rails

How to deploy Rails project on live server using Rake task?
For other projects I used Capistrano deployment.But for this project I wish to use rake...if anybody guide me please ... What gem I will need to install or what is the procedure I should follow?

You already answered your question yourself:
Either you use capistrano (the recommended way) - or you write your own custom rake Tasks that do what you want.
Writing Rake tasks is nothing complicated, you simply define tasks that depend on each other for each step of your deployment and then run them.
Remember: Rake tasks are just plain Ruby and you therefore can use any Gem that suits your needs.
Only if you get a bit more detailed on what tasks you want to do during your deployment I can start recommending Gems or what Tasks you may need to write.
Article by Martin Fowler on Rake: http://martinfowler.com/articles/rake.html
Generally a Rake file looks pretty much like this:
task :default => [:test]
task :test do
# You can write regular ruby here and do anything you want
puts "Foo"
end
task :dependant => [:test] do
# This task will automatically make sure task test is run before running.
puts "Hello World"
end

Linux or windows? which is the os you are using?
you can follow this refeerence
http://guides.rubyonrails.org/command_line.html
http://www.tutorialspoint.com/ruby-on-rails/rails-and-rake.htm

Just guessing a bit.
You probably would need:
A command line options parser
A way to interact through ssh
Some linux command execution
Optionally a way to interact with git

Related

Rails 4: stop `rake` from running all rake tasks

A developer had authority to drop a DB but not re-create it. While working on a rake tasks, he accidentally ran the entire rake suite, which included destroying the development DB but without the proper authority to re-create and populate it.
How can I ensure this doesn't happen again? Is there someway in the Rails app to override running rake so that it does NOT execute a bunch of unspecified tasks?
The developer was looking for a list of tasks and figured that running rake would provide that listing, similarly to how running rails by itself puts out instructions.
I know there's a binstub for rake, but I really do not know what happens if I mess with things in there.
Are there any good solutions to a situation like this?
Set the default task? IIRC, outside of a namespace block:
task :default => "something_that_doesnt_destroy_the_world"
Taking a note from Dave's answer and another SO question (couldn't find link again), here is how you can override the default rake tasks in Rails 4.
# lib/tasks/default.rake (name is not important)
namespace :override do
task :default do
puts "This is now the default rake task executed via 'rake'"
end
end
# Remove default task and switch to above (still in same file)
task(:default).clear.enhance ["override:default"]
At the terminal:
$ rake
/lib/tasks/default.rake: this is now the default 'rake' task
If there is a "cleaner" or more "conventional" Rails way, anyone's welcome to shout it out. This is the "cleanest" solution I could find.

Rails 3 - Trigger a task in the background in frequent intervals

I need to schedule a task that needs to run on a particular interval.
What are the gems / plugins available in rails 3 for achieving the same? If we have a list, how should we choose one among them?
Whenever is by far the most popular gem for using cron to do this.
https://github.com/javan/whenever
Not sure a gem is needed at all for this, cron was built for exactly this purpose, why not leverage it?
This seems to be a good solution:
Rails3, Running rake task from cron
The gist: convert the job into a rake task, which you can run with an environment variable.

Expose Rails App Environment to Ruby Script

Out of pure curiosity, I am wondering if it's possible (no doubt it is) to 'hook into' a Rails Application's environment. So for example, say I want to create a cron script (I don't) that operates some sort of maintenance on a Rails app, and I want to write it in Ruby and using all of the nice code that I already have, for example, User.find etc.
Is this possible, and if so, how?
I'm just curious, as I feel I would eventually want to do this for some reason or other.
I'm currently on Rails 3 with Ruby 1.9.1, in case it matters.
This is certainly possible. Here is a good writeup on how to do that: How to run a rake task from cron
Take a look at the Rails::Railtie class. If you need to run code code when you start up your app, this is a way to do it. Here's a very simple example.
From the beginning of Rails there is ./script/runner, designed exactly for such kind of problems.
In Rails 3 you call it as: ./script/rails runner "puts User.find(:all).map(&:inspect)"
Try ./script/runner --help or ./script/rails runner --help
As the argument to the runner you provide a filename or just a code.
It's often more useful than preparing a Rake task, because you can execute just one-time actions:
ssh prod#example.com "cd rails/app && ./script/runner -e production 'puts User.count'"
You could either use script/rails runner as suggested by Arsen7 or you could write your own script in which you load the app environment in the beginning:
require 'config/environment'
is actually everything you need.
To have your script working in a cron job, make sure that it is executable (chmod u+x) and that it starts with a correct shebang line (#!/usr/bin/env ruby or whatever is appropriate for your situation).
yeah just require these file at top of your script file
require 'config/boot.rb'
require 'config/application.rb'
Rails.application.require_environment!
Now you'll have access to your models

What steps are followed after you say "rake install"?

I'd like to install a plugin but I'm afraid it's going to install a lot of unnecessary stuff. I'd like to look at whatever file rake takes it installation instructions from and remove anything unnecessary.
I believe this is the Rakefile. But I'm not sure what happens when rake looks at the rakefile - does it execute the entire rakefile or only parts of the rakefile that are designated as being relevant to this "install" procedure?
a rake file is a collection of tasks, when you call rake with an argument (in this case install) that's the task that get's executed. (It's similar to ant if you come from Java)
So, no, rake does not execute the whole rakefile when you call "rake + task" but only the task chosen. Note that tasks can have dependencies (eg the "test" task may depend on other previous tasks, like creating some folders and stuff for the tests to run).
Lastly, the rake user guide as pointed by other users is useful, but I recommend a more enjoyable read here -> Ruby on Rails Rake tutorial.
Rake is set up much like Make in that a Rakefile consists of targets and dependencies. This is different from a regular ruby script in that Rake starts at the target you ask for and recursively executes its dependencies before executing the target itself.
So, install might look like this:
task :install => :stage do
# stuff to do
end
Here, your target is the install task, and it depends on some other task called stage.
To execute install, Rake must first execute the dependencies of stage (if it has any), then stage, then finally it executes install. So no, you don't execute the whole file, just enough of it to safely execute the target you asked for.
Rake also supports file targets:
file 'foo.html' => 'bar.xml' do |t|
# Build foo.html from bar.xml, however that is done
end
If you know Make, this looks familiar. Rake first checks whether bar.xml depends on anything, and if so, it executes that. Then, if bar.xml is newer than foo.html, then Rake executes this file task. If foo.html is newer, then Rake assumes that it has aleady been built and skips it.
For more, the Rake User Guide is a good place to start if you want to learn what Rake does.
Why would a plugin install something "unnecessary"?
Assuming your fears are justified, though, could you not install the plugin, do your investigation and then, if not satisfied, revert to the pre-installed version using your source control system? If you're not using source control, this might be the perfect excuse to start...

RoR environment in Ruby standalone script

I want to run a standalone ruby script in which I need my RoR environment to be used. Specifically, I need my models extending ActionMailer and ActiveRecord. I also need to read the database configuration from my database.yml.
How do I go about it?
The easiest way is to change the shebang of your script from :
#!/usr/bin/ruby
to
#!/path/to/your/rails/script/runner
Et voilĂ , your script will be run with the full rails environment loaded. You can also run your script as ./my_script -e production to have it run with the production database.
Check out this thread:
How do I run Ruby tasks that use my Rails models?
Essentially it boils down to:
require "#{ENV['RAILS_ROOT']}/config/environment.rb"
Have fun!
I think the best way to do this is to make it a rake task.
# lib/tasks/mystuff.rake
desc 'do my stuff'
task :my_stuff => [:environment] do
# do my stuff
end
The [:environment] stanza loads the rails environment.

Resources