Tuesday Dec 11, 2007

Add Custom Rake Tasks to NetBeans 6 Ruby IDE

As part of a demo, I wanted to deploy a Ruby on Rails application into a Rails Virtual Appliance using Capistrano. A Rails Virtual Appliance is a Virtual Machine that contains a complete stack of software from the Operating System up to Ruby on Rails itself. If you are using NetBeans 6 as your IDE, you can create a file such as $PROJECT/lib/tasks/demo.rake containing something like the following:

# Rake task to deploy to a production server for virtual appliance demo
# 2007-12-10eeg

# File that contains IP address of virtual appliance. If this file cannot
# be read, then the user will be prompted for the information.
TARGET_IP_FILE = '/ApplianceShare/ipfile.txt'

namespace :demo do
  desc 'Print the target IP address'
  task :print_target_ip do
    puts "Target host IP: #{get_target_host}"
  end

  desc 'Show the coolstack page'
  task :show_coolstack do
    sh "open http://#{get_target_host}/"
  end

  desc 'Show the main depot web application page'
  task :show_app do
    sh "open http://#{get_target_host}:8000/store/"
  end

  desc 'Deploy to production'
  task :deploy do
    target_host = get_target_host
    sh "cap -S target=#{target_host} deploy:setup"
    sh "cap -S target=#{target_host} deploy:cold"
  end

  desc 'Deploy and run the application'
  task :run => [:deploy, :show_app]
end

def get_target_host
  if File.readable?(TARGET_IP_FILE)
    f = File.new(TARGET_IP_FILE)
    line = f.readline
    host_ip = line.chomp
  else
    print "Target host: "
    line = $stdin.gets
    host_ip = line.chomp
  end
  host_ip
end

### Local Variables:
### mode: ruby
### End:

Note that the rake task uses sh to call Capistrano via the cap command to deploy the app into the production machine which in this case is a Virtual Appliance. Then, within NetBeans, you can invoke the context menu on the project node in the Projects Window and select "Run Rake Task" and navigate to the task you want to invoke. See this blog entry or wiki page for more information on NetBeans and Rake.

About

edwingo

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today