You can extend the default behavior of Capistrano by different ways. Here I am mentioning the ways that I used to extend the default Capistrano behavior.
Writing Custom Task
We used Capistrano for the first time in a project [www.scrumpad.com] to deploy our application. We used Rails 1.2.5 and the default support from Capistrano was fine for us. But in next project when we were trying to use Capistrano to deploy our Rails application we felt the necessity of extending the default behavior of Capistrano.
In the second case we had different servers (6 servers) to deploy the application in development (2 servers), testing (2 servers) and production (2 servers) environment. We were planning to use the same capfile for deployment in all the servers. There were different configurations for different types of environments and we needed something that will do the configuration based on the user command.
Capistrano has nice support to extend its default behavior to write custom tasks. For our purpose we made three tasks (development, testing, production) and each task set the environment of each environment.
desc "this task sets the development environment"
task :development do
role :app, "my.dev1.server.ip"
role :app, "my.dev2.server.ip"
set :environment, "development"
end
Now as we want to set the environment before any standard Capistrano task start running we run the capfile in this way:
cap [environment] [standard_capistrano_task]
i.e. cap development setup
Using the Event Framework
In Capistrano 1.x we can extend a default task my creating new tasks whose names are prefixed with 'before_' and 'after_'. For example depending on the environment we need to change the value of 'deploy_to' in our application. In that case we can write another task 'before_deploy':
desc "this task set the value for deploy_to based on the value of 'environment'"
task :before_deploy do
if environment == 'development'
set :deploy_to = "deploy_folder_path_in_development_server"
elsif environment == 'testing'
set :deploy_to = "deploy_folder_path_in_testing_server"
elsif environment == 'production'
set :deploy_to = "deploy_folder_path_in_production_server"
end
end
So this task will be called before the calling of 'deploy' task. Similarly if you want to do some extra work after 'deploy' task is called in that case you could write a new task 'after_deploy'
Overriding default task
As I have already said, I was using Capistrano for Rails application. Later for a Ruby application I need to use Capistrano to deploy the application in server and here I found a problem. In Rails application there is a folder 'Public' and the default 'update_code' task makes a link to that folder. But as I was trying with Ruby application where there is no 'Public' folder I was getting an error. To solve the problem I override the standard implementation by creating the same task with custom implementation. Here is the code:
desc <<-DESC
Update all servers with the latest release of the source code. All this does is do a checkout (as defined by the selected scm module). This task is modified for Ruby application
DESC
task :update_code, :except => { :no_release => true } do
on_rollback { delete release_path, :recursive => true }
source.checkout(self)
set_permissions
run <<-CMD rm -rf #{release_path}/log #{release_path}/public/system &&
ln -nfs #{shared_path}/log #{release_path}/log
CMD
@releases = nil
end
In future I will post more on how to extend the default Capistrano tasks with other ways.
Tuesday, November 18, 2008
Thursday, November 6, 2008
Connecting with Oracle from you ruby/rails applications on an "Intel Mac OS X" using DBI with OCI8
Prerequisite:
- Mac OS X (10.5.5)
- ruby --version => 1.8.6 (2008-03-03 patchlevel 114) [universal-darwin9.0]
NOTE: Here the machine where i am working is 'SBKislamzD1' and the user name is 'islamz'. In your case just replace this user name with your user name
Installing DBI
The easiest way of installing a gem is using gem command [gem install dbi] But here I am going to use OCI8-1.0.3. With this version of OCI8 library not all the version of DBI works. In my case DBI version 0.2.0 worked. You can't install this version (0.2.0) of DBI using gem install. You need to downloaded the tar file and unzip it.
Lets say the unzip folder is /Users/islamz/Downloads/dbi-0.2.2
Now open a terminal/console and goto /Users/islamz/Downloads/dbi-0.2.2 and run following commands:
> ruby setup.rb config --with=dbi
> ruby setup.rb setup
Now you should run the following command with 'sudo'
> sudo ruby setup.rb install
If you have any other version of DBI gem you might need to uninstall that gem
Installing Oracle Instant Client
At first from http://www.oracle.com/technology/software/tech/oci/instantclient/htdocs/intel_macsoft.html download following packages:
Now unzip all of packages in /Library/Oracle/instantclient_10_2. After unzipping you should have followings in this folder:
Its now time to add some environment variables. For this purpose open /etc/profile in edit mode and add the environment variables as stated below:
Now you need to create symlinks. To do this follow these steps:
To compile ruby-oci8 follow the steps as stated below:
You can easily test by following these steps:
If there is no error then you installation is SUCCESSFUL
SIDE NOTES
1) How to check which version of DBI is supported:
- goto /Library/Ruby/Site/1.8/DBD/OCI8/
- open the file OCI8.rb
- check the value of USED_DBD_VERSION. [In my case it was "0.2"]
2) Problems with NetBeans:
If you are using NetBeans for developing your project in that case you might face some problems:
a) You might get some permission related problems. So run Netbeans with sudo
b) When running your application if it throws an error that 'libclntsh.dylib.10.1' is not found in '/scratch/plebld/208/rdbms/lib/' then put the file in the specified path
c) When running your application you are getting this error:
OCIError (Error while trying to retrieve text for error ORA-24812):
lob.c:194:in oci8lib.so
/Library/Ruby/Site/1.8/oci8.rb:1005:in `write'
It means when you are running NetBeans as sudo the environment variables are missing. Follow these steps:
- Mac OS X (10.5.5)
- ruby --version => 1.8.6 (2008-03-03 patchlevel 114) [universal-darwin9.0]
NOTE: Here the machine where i am working is 'SBKislamzD1' and the user name is 'islamz'. In your case just replace this user name with your user name
Installing DBI
The easiest way of installing a gem is using gem command [gem install dbi] But here I am going to use OCI8-1.0.3. With this version of OCI8 library not all the version of DBI works. In my case DBI version 0.2.0 worked. You can't install this version (0.2.0) of DBI using gem install. You need to downloaded the tar file and unzip it.
Lets say the unzip folder is /Users/islamz/Downloads/dbi-0.2.2
Now open a terminal/console and goto /Users/islamz/Downloads/dbi-0.2.2 and run following commands:
> ruby setup.rb config --with=dbi
> ruby setup.rb setup
Now you should run the following command with 'sudo'
> sudo ruby setup.rb install
If you have any other version of DBI gem you might need to uninstall that gem
Installing Oracle Instant Client
At first from http://www.oracle.com/technology/software/tech/oci/instantclient/htdocs/intel_macsoft.html download following packages:
- - Instant Client Package - Basic (10.2.0.4)
- - Instant Client Package - SQL*Plus (10.2.0.4)
- - Instant Client Package - SDK (10.2.0.4)
Now unzip all of packages in /Library/Oracle/instantclient_10_2. After unzipping you should have followings in this folder:
- - BASIC_README
- - classes12.jar
- - genezi
- - glogin.sql
- - libclntsh.dylib.10.1
- - libnnz10.dylib
- - libocci.dylib.10.1
- - libociei.dylib
- - libocijdbc10.dylib
- - libocijdbc10.jnilib
- - libsqlplus.dylib
- - libsqlplusic.dylib
- - ojdbc14.jar
- - sdk
- - sqlplus
- - SQLPLUS_README
Its now time to add some environment variables. For this purpose open /etc/profile in edit mode and add the environment variables as stated below:
- Open the file in edit mode using: sudo vi /etc/profile
- Add following in this file:
ORACLE_HOME=/Library/Oracle/instantclient_10_2
export TNS_ADMIN=$ORACLE_HOME
export LD_LIBRARY_PATH=$ORACLE_HOME
export DYLD_LIBRARY_PATH=$ORACLE_HOME
export PATH=$PATH:$ORACLE_HOME - Save the file and logoff then login
- To test whether the environment variables are set or not type 'env' in terminal(console)
Now you need to create symlinks. To do this follow these steps:
- open a terminal/console and goto /Library/Oracle/instantclient_10_2
- sudo ln -s libclntsh.dylib.10.1 libclntsh.dylib
- sudo ln -s libocci.dylib.10.1 libocci.dylib
- in /Library/Oracle/instantclient_10_2 folder you will find two new symlink
To compile ruby-oci8 follow the steps as stated below:
- download the ruby-oci8-1.0.3.tar If you are using Safari then it will be downloaded in /Users/islamz/Downloads/
- unzip it to ruby-oci8-1.0.3 (/Users/islamz/Downloads/ruby-oci8-1.0.3)
- run terminal/console and goto /Users/islamz/Downloads/ruby-oci8-1.0.3
- export SQLPATH=$ORACLE_HOME
- export RC_ARCHS=i386
- ruby setup.rb config
- make
- sudo make install [Don't forget to run this command as 'sudo']
You can easily test by following these steps:
- run terminal/console
- irb
- require 'oci8' #it should return 'true'
If there is no error then you installation is SUCCESSFUL
SIDE NOTES
1) How to check which version of DBI is supported:
- goto /Library/Ruby/Site/1.8/DBD/OCI8/
- open the file OCI8.rb
- check the value of USED_DBD_VERSION. [In my case it was "0.2"]
2) Problems with NetBeans:
If you are using NetBeans for developing your project in that case you might face some problems:
a) You might get some permission related problems. So run Netbeans with sudo
b) When running your application if it throws an error that 'libclntsh.dylib.10.1' is not found in '/scratch/plebld/208/rdbms/lib/' then put the file in the specified path
c) When running your application you are getting this error:
OCIError (Error while trying to retrieve text for error ORA-24812):
lob.c:194:in oci8lib.so
/Library/Ruby/Site/1.8/oci8.rb:1005:in `write'
It means when you are running NetBeans as sudo the environment variables are missing. Follow these steps:
- SBKislamzD1:~ islamz# sudo su
- sh-3.2# ORACLE_HOME=/Library/Oracle/instantclient_10_2
- sh-3.2# export ORACLE_HOME=$ORACLE_HOME
- sh-3.2# export TNS_ADMIN=$ORACLE_HOME
- sh-3.2# export LD_LIBRARY_PATH=$ORACLE_HOME
- sh-3.2# export DYLD_LIBRARY_PATH=$ORACLE_HOME
- sh-3.2# export NLS_LANG=AMERICAN_AMERICA.AL32UTF8
- sh-3.2# /Applications/NetBeans/NetBeans\ 6.1.app/Contents/MacOS/netbeans
Subscribe to:
Posts (Atom)