Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Tobias Luetke http://blog.leetsoft.com

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Deploy script for rails applications under version control

   1  #!/usr/bin/env ruby
   2  
   3  require 'yaml'
   4  require 'rubygems'
   5  require_gem 'net-ssh'
   6  
   7  $stdout.sync = true
   8  
   9  def help
  10    puts <<-USAGE
  11  This tool allows you to conviniently deploy your projects on production
  12  or staging servers.
  13  
  14    Useage:
  15      deploy <target>
  16      Please specify configuration in ~/.deployrc
  17      
  18    Example:  
  19      
  20      example ~/.deployrc:   
  21      
  22        hieraki:
  23          server: linux
  24          get: svn export svn://myserver/hieraki/trunk
  25          directory: /var/www/applications/hieraki
  26          test: RAILS_ENV=production rake
  27  
  28          after_test:
  29            - chmod 777 log/
  30            - chmod 666 log/*
  31  
  32          after_commit:
  33            - sudo /etc/init.d/apache reload    
  34  
  35    Note: 
  36    
  37      server, get and directory are required
  38      
  39      available hooks are
  40        - after_connect
  41        - after_deploy
  42        - after_test
  43        - after_commit
  44          
  45    USAGE
  46    exit
  47  end
  48  
  49  def do_exec(cmd)
  50    print "\n\n  #{cmd}\n    "
  51    
  52    stderr = ""
  53    $session.process.open( cmd ) do |cmd|
  54      cmd.on_exit do |p, status|
  55        if status == 0
  56          print "[done]\n" 
  57        else
  58          puts "    #{stderr}"        
  59          print "[failed]\n" 
  60          exit
  61        end
  62      end
  63      
  64      cmd.on_stderr do |p, data|
  65        stderr << data.gsub(/\n/, "\n    ")
  66      end    
  67      cmd.on_stdout do |p, data|
  68        STDOUT << data.gsub(/\n/, "\n    ")
  69      end                
  70    end  
  71  end
  72  
  73  # Batch jobs
  74  
  75  def deploy
  76    puts      "\ndeploying"  
  77  
  78    print     "  creating dirs"
  79    do_exec   "mkdir -p #{$config['directory']}" 
  80  
  81    print     "  get"
  82    do_exec   "#{$config['get']} #{$config['deploy']}"
  83  end
  84  
  85  def test
  86    puts      "\ntesting"  
  87    do_exec   "cd #{$config['deploy']}; #{$config['test']}"
  88  end
  89  
  90  def commit
  91    puts      "\ncomitting"
  92    print     "  creating new link"
  93    do_exec   "ln -nfs #{$config['deploy']}/ #{$config['target']}"
  94  end
  95  
  96  def run_tasks(tasks)
  97    $config[tasks].to_a.each do |task|    
  98      do_exec "cd #{$config['deploy']}; #{task}" 
  99    end
 100  end
 101  
 102  
 103  app_name = ARGV[0] || help
 104  $config = YAML::load(File.open(File.expand_path("~/.deployrc")))[app_name]
 105  
 106  # compute deploy directory 
 107  $config['deploy'] = $config['directory'] + "/#{Time.now.strftime("%Y%m%d-%H%M")}"
 108  # compute target directory 
 109  $config['target'] = $config['directory'] + "/latest"
 110  
 111  puts "deploying #{app_name} to #{$config["server"]}"
 112  
 113  $session = Net::SSH.start( $config["server"] )
 114  
 115  run_tasks("after_connect")  
 116  deploy
 117  run_tasks("after_deploy")
 118  test
 119  run_tasks("after_test")
 120  commit
 121  run_tasks("after_commit")
 122  puts ""
 123  puts "success!"
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS