PDA

View Full Version : Rails Capistrano Recipe for Hostmonster shared hosting



jrguitar21
05-23-2008, 03:56 PM
I FINALLY got a capistrano recipe working for deploying to HostMonster.com.

Hoping that anyone else might just find this useful I put forth the following Capfile for my hostmonster projects on shared hosting environment (which uses FastCGI and production mode by default).

For this deployment recipe to function correclty you must have a standard hostmonster environment setup including a ~/rails/ folder and a ~/www symlink to ~/public_html... more info on this can easily be found in the getting started with rails and hostmonster in their knowledge base.

you can follow the instructions here (http://freakemonk.blogspot.com/2008/01/howto-install-rails-20-on-hostmonster.html) to get the basic idea of whats going on with my capistrano tasks. I chose to take a little different route instead of editing files by hand, just run a few lines of shell commands and leave your server setup with some custom configured files (htaccess and dispatch.*).

Disclaimer == this script is not the end all solution to your project, if your doing funky things with htaccess file, then your better off just checking in the custom hostmonster configuration requirements into your repository and excluding the .htaccess file copy that happens in the :host_configuration task.

1) setup ssh (explained in the above link)
2) setup subversion... find the forum entry on how to set this up.
3) create the HM_CONFIG directory for which the capistrano tasks will use to update / overwrite config files that may be in your repository

Run the following commands on the hostmonster server via ssh:


## Create the config directory (and the rails directory if it doesnt exist)
mkdir -p ~/rails/HM_CONFIG

## create temporary rails application
cd ~/rails
rails temp

## copy the needed config files to HM_CONFIG folder
cd HM_CONFIG
cp ../temp/public/.htaccess .
cp ../temp/public/public/dispatch.* .

## remove temporary rails app
rm -rf ../temp



4) Now configure your application deployment policy locally:

Note, my hostmonster account is located at /home2/my_hostmonster_username your account may be different, you have to check this in the control panel or via ssh executing

cd ~; pwd

==> RAILS_ROOT/config/deploy.rb

# RAILS_ROOT/config/deploy.rb
# server config
set :user, 'your_hostmonster_username'
set :domain, "hostXXX.hostmonster.com"
set :project, 'rails_app' #project name in repository, pulls from trunk by default
set :application, 'myapp' #how the folder will appear in public_html (must setup subdomain as well in cPanel)
set :applicationdir, "/home2/#{user}/rails/#{application}"

# version control config
# NOTE: hostmonster supports http port 80,
# svn port 3690 doesnt work,
# and svn+ssh is not tested...
set :repository, "http://svn.my-repo-domain.com/#{project}/trunk"
set :scm_username, 'capistrano_user'
set :scm_password, 'capistrano_password'
set :scm_command, "/home2/#{user}/usr/bin/svn"
set :local_scm_command, :default


# deployment config
set :deploy_to, applicationdir
set :deploy_via, :export

# production database config
set :db_type, "mysql"

# roles (servers)
role :app, domain
role :web, domain
role :db, domain, :primary => true

# additional config options
default_run_options[:pty] = true
ssh_options[:keys] = %w(/Path/to/id_rsa) # If you are using ssh_keys
set :chmod755, %w(app config db lib public vendor script tmp public/dispatch.cgi public/dispatch.fcgi public/dispatch.rb)
set :use_sudo, false


5) Configure Capistrano 2 custom tasks for deployment to Hostmonster....

==> RAILS_ROOT/Capfile

load 'deploy' if respond_to?(:namespace) # cap2 differentiator
Dir['vendor/plugins/*/recipes/*.rb'].each { |plugin| load(plugin) }
load 'config/deploy'

# ========================
# For FCGI Apps
# ========================
namespace :deploy do

# The default deploy:start task in Cap2 requires script/spin
# (which doesnt exist in our projects by default),
# you can override :start here which simply links up the
# application to a sub-folder of your ~/public_html/ (~/www/)
#
# Note:
# Hostmonster.com does not require script/spin or
# script/server to run the FastCGI
#
task :start, :roles => :app do
link_app
host_configuration
set_perms #creating symbolic link to public screws up perms
end

task :restart, :roles => :app do
start
end

end


desc "Link up the application. This creates/updates a standard symbolic link
from the application location to a sub-folder of your ~/public_html (a.k.a, ~/www)"
task :link_app do
run "rm -rf ~/www/#{application} && ln -s #{current_path}/public ~/www/#{application}"
end


desc "Prefer the webhost configuration files. Calling this task replaces
public/.htaccess and the public/dispatch.* files over those from svn from
a test application I've setup by hand on the hostmonster.com following the
directions here."
task :host_configuration do
#I've tried this same step a thousand different
#ways and this is the only one that worked :/
files = %w(.htaccess dispatch.rb dispatch.cgi dispatch.fcgi)
run "cd ~/www/#{application}; " +
(files.collect { |file| "rm #{file} && cp ~/rails/HM_CONFIG/#{file} ."}.join(" && "))
end


desc "Set the proper permissions for directories and files. Specify a
list of folders and files with `set :chmod755` in config/deploy.rb"
task :set_perms, :roles => [:app, :db, :web] do
run(chmod755.collect { |i| "chmod 755 #{current_path}/#{i}" }.join(" && "))
end


desc "Restarting after deployment"
#this might not be necessary on hostmonster....
task :after_deploy, :roles => [:app, :db, :web] do
run "sed 's/# ENV\\[/ENV\\[/g' #{deploy_to}/current/config/environment.rb > #{deploy_to}/current/config/environment.temp"
run "mv #{deploy_to}/current/config/environment.temp #{deploy_to}/current/config/environment.rb"
end


desc "Returns last lines of log file. Usage: cap log [-s lines=100] [-s rails_env=production]"
task :log do
lines = variables[:lines] || 100
rails_env = variables[:rails_env] || 'production'
run "tail -n #{lines} #{shared_path}/log/#{rails_env}.log" do |ch, stream, out|
puts out
end
end

# DATABASE only
after 'deploy:update_code', 'db:symlink'
namespace :db do
desc "Make symlink for database yaml using the #{db_type} implementation."
# this is a task for my custom svn setup... you probably
# wont need if you have a database.yml file checked into svn
task :symlink do
run "ln -nfs #{release_path}/config/#{db_type}.database.yml #{release_path}/config/database.yml"
end
end


6) Final step: DEPLOY!


yourlocalmachine$ cd projects/rails_app
yourlocalmachine$ cap deploy:setup
yourlocalmachine$ cap deploy:cold

jrguitar21
05-26-2008, 03:34 PM
After you've made the HM_CONFIG/.htaccess file you'll do well to follow the instructions here for changing it to support FastCGI (http://freakemonk.blogspot.com/2008/01/howto-install-rails-20-on-hostmonster.html).

tzeoneco
05-01-2010, 05:46 PM
Hi JRGuitar!

I just tried your Recipe and got "cannot create directory" errors during deploy:setup.

I could create directories when logged in via Putty terminal, but for some reason capistrano couldn't even with the same login id/pw.

I've even temporarily made the /rails permission 777, but still capistrano couldn't create directories under it.

I'm not using SSH keys; just logging in with id saved in the deploy.rb file and typing in my pw when prompted by capistrano.

Any idea how I can solve this permissions problem?

Thanks much in advance!

RoR & HM newbie,
Tze

tzeoneco
05-01-2010, 07:45 PM
Hi JRGuitar!

Never mind. I just fixed my permissions problem.

Set SSH keys on my host server, put the path to the server keys in deploy.rb. Now the permissions errors are all gone during deploy:setup and deploy:check, and I can see via the host's c-panel file manager that the folders have been created on the server.

Thanks anyway, and sorry if my question wasted any of your time!

-Tze

Ariem
09-01-2010, 05:25 AM
Hi,

is any of you using HM as source control as well?
Or, by other words, running capistrano successfully using svn+ssh://... ?
Before the openssh update i didn't have any problems with it, but after that I can't seem to make it work.
The problem is while running svn from within the server itself (in HM).
I can access it from outside, but capistrano checks-out the source code from withing the server (after getting the latest revision number from outside of it) and at that point I get the usual "svnserve: command not found" error message, or the output message from svnserve (i dont have it here to write it down) when using keys to bypass login.
The same goes when trying to checkout directly on the server using the svn+ssh protocol, but works fine if I use the file:/// protocol... but again, capistrano accesses the SVN server from outside and from inside, and using the file:/// protocol doesnt work from outside..

Any ideas?

Thanks