<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: deploy code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 16 May 2008 16:28:23 GMT</pubDate>
    <description>DZone Snippets: deploy code</description>
    <item>
      <title>execute a task serially on different hosts, not in parallel</title>
      <link>http://snippets.dzone.com/posts/show/3837</link>
      <description>// This code will let you define a task, using the same conventions as Capistrano's "task", that executes serially on the hosts defined by its role rather than in parallel.  This is useful if you are deploying to multiple live servers behind a load balancer, the deployment process takes down the server, and you want only one server down at a given time.&lt;br /&gt;&lt;br /&gt;This efinitely uses magic.  we're not sure if this will work in future versions of Capistrano, but changing it should be relatively simple.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def serial_task(name, options = {}, &amp;block)&lt;br /&gt;  servers = self.roles[options[:roles]].collect {|server| server.host}&lt;br /&gt;  task_syms = []&lt;br /&gt;  servers.each do |hostname|&lt;br /&gt;    role_sym = "_serial_task_#{name.to_s}_#{hostname}".to_sym&lt;br /&gt;    task_sym = "_do_task_#{name.to_s}_#{hostname}".to_sym&lt;br /&gt;    task_syms &lt;&lt; task_sym&lt;br /&gt;    role role_sym, hostname&lt;br /&gt;    task task_sym, :roles =&gt; role_sym, &amp;block&lt;br /&gt;  end&lt;br /&gt;  task name do&lt;br /&gt;    task_syms.each do |t|&lt;br /&gt;      self.send t&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Here is an example.  Note the syntax is just the same as Capistrano's "task".&lt;br /&gt;&lt;code&gt;&lt;br /&gt;serial_task :pwd, :roles =&gt; web do&lt;br /&gt;  run "pwd"&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 18 Apr 2007 19:37:06 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3837</guid>
      <author>stephen.judkins (stephen judkins)</author>
    </item>
    <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>
