How to use Vlad the Deployer with git, nginx, mongrel, mongrel_cluster and Rails
This is a draft…
Installing Vlad the Deployer
1 gem install vlad
Configuring Vlad the Deployer
Add this to the end of RakeFile:
1 begin 2 require 'rubygems' 3 require 'vlad' 4 Vlad.load :scm => :git 5 rescue LoadError => e 6 puts "Unable to load Vlad #{e}." 7 end
Note that we’re telling Vlad to use git. This snippet- gives you a quick introduction on how to use git with Rails.
Creating the deployment recipe
If you’re uncertain what these variables mean, have a look at the docs. This folder is also worth a look, and don’t forget to take a peek at the vlad source code.
1 # 2 # General configuration 3 # 4 set :ssh_flags, '-p 666' 5 set :application, 'xxx.com' 6 set :domain, '127.0.01' 7 set :deploy_to, '/var/www/xxx.com' 8 set :repository, '/var/lib/git/repositories/xxx.com/.git/' 9 10 11 # 12 # Mongrel configuration 13 # 14 set :mongrel_clean, true 15 set :mongrel_command, 'sudo mongrel_rails' 16 set :mongrel_group, 'www-data' 17 set :mongrel_port, 9000 18 set :mongrel_servers, 3 19 20 #set :mongrel_address, '127.0.0.1' 21 #set(:mongrel_conf) { '#{shared_path}/mongrel_cluster.conf' } 22 #set :mongrel_config_script, nil 23 #set :mongrel_environment, 'production' 24 #set :mongrel_log_file, nil 25 #set :mongrel_pid_file, nil 26 #set :mongrel_prefix, nil 27 #set :mongrel_user, 'mongrel' 28 29 # 30 # Customize Vlad to our needs 31 # 32 namespace :vlad do 33 # 34 # Add an after_update hook 35 # 36 remote_task :update do 37 Rake::Task['vlad:after_update'].invoke 38 end 39 40 # 41 # The after_update hook, which is run after vlad:update 42 # 43 remote_task :after_update do 44 # Link to shared resources, if you have them in .gitignore 45 # run "ln -s #{deploy_to}/shared/system/database.yml #{deploy_to}/current/config/database.yml" 46 end 47 48 # 49 # Deploys a new version of your application 50 # 51 remote_task :deploy => [:update, :migrate, :start_app] 52 end
Setup the server
1 $ rake vlad:setup
This will create the necessary folders and mongrel_cluster configuration file.
Deploy the application
Now deploy the application with vlad:deploy, which is a custom rake task that we added to the deployment recipe:
1 $ rake vlad:deploy
Copying your SSH public key to the remote server
Vlad uses ssh for executing commands on the remotely, and rsync for copying the build to your server, which means you’ll quickly grow tired of typing your password each time a command is run.
This problem is solved by copying your public SSH keys to the remote server, this snippet- explains how to do exactly that.
Jump start a Rails project with Rails Edge, Capistrano, Mongrel and Mercurial
1 # Create a Rails project 2 rails project -d sqlite3 3 cd project 4 # Delete index file 5 rm public/index.html 6 # Use Rails edge. Use rake rails:freeze:edge TAG=rel_1-2-3 to get a specific version. 7 rake rails:freeze:edge 8 # Add Capistrano configuration file 9 capify . 10 # Add Mongrel cluster configuration file 11 sudo mongrel_rails cluster::configure -e production \ 12 --user mongrel --group mongrel \ 13 -c /var/www/project-xxx/current \ 14 -a 127.0.0.1 \ 15 -p 8000 \ 16 -N 3 17 # Create a Mercurial repository 18 hg init 19 # Add project to repository 20 hg commit -A --message "Project started" 21 # Push changes to a remote repository 22 hg push ssh://user@ip:port//var/mercurial/xxx
Cloning is done with hg clone:
1 hg clone ssh://user@ip:port//var/mercurial/xxx
Capistrano 2 task for backing up your MySQL production database before each deployment
This Capistrano task connects to your production database and dumps the contents to a file. The file is compressed and put in a directory specified with set :backup_dir, ”#{deploy_to}/backups”. This is a slight modification of http://pastie.caboo.se/42574. All credit to court3nay.
1 task :backup, :roles => :db, :only => { :primary => true } do 2 filename = "#{backup_dir}/#{application}.dump.#{Time.now.to_f}.sql.bz2" 3 text = capture "cat #{deploy_to}/current/config/database.yml" 4 yaml = YAML::load(text) 5 6 on_rollback { run "rm #{filename}" } 7 run "mysqldump -u #{yaml['production']['username']} -p #{yaml['production']['database']} | bzip2 -c > #{filename}" do |ch, stream, out| 8 ch.send_data "#{yaml['production']['password']}\n" if out =~ /^Enter password:/ 9 end 10 end
To automatically backup your data before you deploy a new version add this to config/deploy.rb:
1 task :before_deploy do 2 backup 3 end
To restore the backup run the following command:
1 mysql database_name -uroot < filename.sql