<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: rails code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 29 Aug 2008 07:16:37 GMT</pubDate>
    <description>DZone Snippets: rails code</description>
    <item>
      <title>Deploy script for rails applications under version control</title>
      <link>http://snippets.dzone.com/posts/show/42</link>
      <description>&lt;code&gt;#!/usr/bin/env ruby&lt;br /&gt;&lt;br /&gt;require 'yaml'&lt;br /&gt;require 'rubygems'&lt;br /&gt;require_gem 'net-ssh'&lt;br /&gt;&lt;br /&gt;$stdout.sync = true&lt;br /&gt;&lt;br /&gt;def help&lt;br /&gt;  puts &lt;&lt;-USAGE&lt;br /&gt;This tool allows you to conviniently deploy your projects on production&lt;br /&gt;or staging servers.&lt;br /&gt;&lt;br /&gt;  Useage:&lt;br /&gt;    deploy &lt;target&gt;&lt;br /&gt;    Please specify configuration in ~/.deployrc&lt;br /&gt;    &lt;br /&gt;  Example:  &lt;br /&gt;    &lt;br /&gt;    example ~/.deployrc:   &lt;br /&gt;    &lt;br /&gt;      hieraki:&lt;br /&gt;        server: linux&lt;br /&gt;        get: svn export svn://myserver/hieraki/trunk&lt;br /&gt;        directory: /var/www/applications/hieraki&lt;br /&gt;        test: RAILS_ENV=production rake&lt;br /&gt;&lt;br /&gt;        after_test:&lt;br /&gt;          - chmod 777 log/&lt;br /&gt;          - chmod 666 log/*&lt;br /&gt;&lt;br /&gt;        after_commit:&lt;br /&gt;          - sudo /etc/init.d/apache reload    &lt;br /&gt;&lt;br /&gt;  Note: &lt;br /&gt;  &lt;br /&gt;    server, get and directory are required&lt;br /&gt;    &lt;br /&gt;    available hooks are&lt;br /&gt;      - after_connect&lt;br /&gt;      - after_deploy&lt;br /&gt;      - after_test&lt;br /&gt;      - after_commit&lt;br /&gt;        &lt;br /&gt;  USAGE&lt;br /&gt;  exit&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;def do_exec(cmd)&lt;br /&gt;  print "\n\n  #{cmd}\n    "&lt;br /&gt;  &lt;br /&gt;  stderr = ""&lt;br /&gt;  $session.process.open( cmd ) do |cmd|&lt;br /&gt;    cmd.on_exit do |p, status|&lt;br /&gt;      if status == 0&lt;br /&gt;        print "[done]\n" &lt;br /&gt;      else&lt;br /&gt;        puts "    #{stderr}"        &lt;br /&gt;        print "[failed]\n" &lt;br /&gt;        exit&lt;br /&gt;      end&lt;br /&gt;    end&lt;br /&gt;    &lt;br /&gt;    cmd.on_stderr do |p, data|&lt;br /&gt;      stderr &lt;&lt; data.gsub(/\n/, "\n    ")&lt;br /&gt;    end    &lt;br /&gt;    cmd.on_stdout do |p, data|&lt;br /&gt;      STDOUT &lt;&lt; data.gsub(/\n/, "\n    ")&lt;br /&gt;    end                &lt;br /&gt;  end  &lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;# Batch jobs&lt;br /&gt;&lt;br /&gt;def deploy&lt;br /&gt;  puts      "\ndeploying"  &lt;br /&gt;&lt;br /&gt;  print     "  creating dirs"&lt;br /&gt;  do_exec   "mkdir -p #{$config['directory']}" &lt;br /&gt;&lt;br /&gt;  print     "  get"&lt;br /&gt;  do_exec   "#{$config['get']} #{$config['deploy']}"&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;def test&lt;br /&gt;  puts      "\ntesting"  &lt;br /&gt;  do_exec   "cd #{$config['deploy']}; #{$config['test']}"&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;def commit&lt;br /&gt;  puts      "\ncomitting"&lt;br /&gt;  print     "  creating new link"&lt;br /&gt;  do_exec   "ln -nfs #{$config['deploy']}/ #{$config['target']}"&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;def run_tasks(tasks)&lt;br /&gt;  $config[tasks].to_a.each do |task|    &lt;br /&gt;    do_exec "cd #{$config['deploy']}; #{task}" &lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;app_name = ARGV[0] || help&lt;br /&gt;$config = YAML::load(File.open(File.expand_path("~/.deployrc")))[app_name]&lt;br /&gt;&lt;br /&gt;# compute deploy directory &lt;br /&gt;$config['deploy'] = $config['directory'] + "/#{Time.now.strftime("%Y%m%d-%H%M")}"&lt;br /&gt;# compute target directory &lt;br /&gt;$config['target'] = $config['directory'] + "/latest"&lt;br /&gt;&lt;br /&gt;puts "deploying #{app_name} to #{$config["server"]}"&lt;br /&gt;&lt;br /&gt;$session = Net::SSH.start( $config["server"] )&lt;br /&gt;&lt;br /&gt;run_tasks("after_connect")  &lt;br /&gt;deploy&lt;br /&gt;run_tasks("after_deploy")&lt;br /&gt;test&lt;br /&gt;run_tasks("after_test")&lt;br /&gt;commit&lt;br /&gt;run_tasks("after_commit")&lt;br /&gt;puts ""&lt;br /&gt;puts "success!"&lt;/code&gt;</description>
      <pubDate>Thu, 07 Apr 2005 09:42:10 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/42</guid>
      <author>tobi (Tobias Luetke)</author>
    </item>
  </channel>
</rss>
